9 uint8_t __wait_SR1_bit(I2C_TypeDef* i2c_inst,uint16_t mask)
11 volatile uint32_t safety_counter;
29 inline uint8_t __generate_START(I2C_TypeDef* i2c_inst)
31 i2c_inst->CR1 |= I2C_CR1_START;
33 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_SB))
43 inline uint8_t __read_single_byte(I2C_TypeDef* i2c_inst,uint8_t i2c_addr,uint8_t* byte_out)
45 i2c_inst->DR = (i2c_addr<<1) | 1;
47 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_ADDR))
53 i2c_inst->CR1 &= ~I2C_CR1_ACK;
55 if (!(i2c_inst->SR2 & I2C_SR2_MSL))
61 i2c_inst->CR1 |= I2C_CR1_STOP;
63 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_RXNE))
68 (*byte_out) = i2c_inst->DR;
73 inline uint8_t __read_two_bytes(I2C_TypeDef* i2c_inst,uint8_t i2c_addr,uint8_t* bytes_out)
75 i2c_inst->DR = (i2c_addr<<1) | 1;
77 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_ADDR))
82 i2c_inst->CR1 &= ~I2C_CR1_ACK;
83 i2c_inst->CR1 |= I2C_CR1_POS;
85 if (!(i2c_inst->SR2 & I2C_SR2_MSL))
90 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_BTF))
95 i2c_inst->CR1 |= I2C_CR1_STOP;
97 bytes_out[0] = i2c_inst->DR;
98 bytes_out[1] = i2c_inst->DR;
100 i2c_inst->CR1 &= ~I2C_CR1_POS;
105 inline uint8_t __read_multiple_bytes(I2C_TypeDef* i2c_inst,uint8_t i2c_addr,uint8_t* bytes_out,uint16_t read_length)
114 i2c_inst->DR = (i2c_addr<<1) | 1;
116 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_ADDR))
121 if (!(i2c_inst->SR2 & I2C_SR2_MSL))
126 i2c_inst->CR1 |= I2C_CR1_ACK;
128 for (k=0; k<read_length; k++)
130 if (k<(read_length-3))
132 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_RXNE))
137 bytes_out[k] = i2c_inst->DR;
141 if (k==(read_length-3))
143 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_BTF))
147 i2c_inst->CR1 &= ~I2C_CR1_ACK;
148 bytes_out[k] = i2c_inst->DR;
151 if (k==(read_length-2))
153 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_BTF))
157 i2c_inst->CR1 |= I2C_CR1_STOP;
158 bytes_out[k] = i2c_inst->DR;
161 if (k==(read_length-1))
163 bytes_out[k] = i2c_inst->DR;
171 i2c_error_t
i2c_read_with_pointer(I2C_TypeDef* i2c_inst,uint8_t i2c_addr,uint8_t pointer_byte,uint8_t read_length,uint8_t* read_buffer)
173 volatile uint32_t safety_counter;
177 return I2C_ERROR_READ_FAIL;
180 if (read_buffer==NULL)
182 return I2C_ERROR_READ_FAIL;
186 if (!__generate_START(i2c_inst))
188 return I2C_ERROR_START_FAIL;
192 i2c_inst->DR = i2c_addr<<1;
194 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_ADDR))
196 return I2C_ERROR_ADDR_FAIL;
199 if (!(i2c_inst->SR2 & I2C_SR2_MSL))
201 return I2C_ERROR_ADDR_FAIL;
204 i2c_inst->DR = pointer_byte;
206 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_TXE))
209 return I2C_ERROR_READ_FAIL;
213 if (!__generate_START(i2c_inst))
215 return I2C_ERROR_START_FAIL;
220 if (!__read_single_byte(i2c_inst,i2c_addr,read_buffer))
222 return I2C_ERROR_READ_FAIL;
228 if (!__read_two_bytes(i2c_inst,i2c_addr,read_buffer))
230 return I2C_ERROR_READ_FAIL;
236 if (!__read_multiple_bytes(i2c_inst,i2c_addr,read_buffer,read_length))
238 return I2C_ERROR_READ_FAIL;
248 return I2C_ERROR_NONE;
251 i2c_error_t
i2c_write_with_pointer(I2C_TypeDef* i2c_inst,uint8_t i2c_addr,uint8_t pointer_byte,uint16_t write_length,uint8_t* write_buffer)
254 volatile uint32_t safety_counter;
256 if (!__generate_START(i2c_inst))
258 return I2C_ERROR_START_FAIL;
261 i2c_inst->DR = i2c_addr<<1;
263 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_ADDR))
265 return I2C_ERROR_ADDR_FAIL;
268 if (!(i2c_inst->SR2 & I2C_SR2_MSL))
270 return I2C_ERROR_ADDR_FAIL;
273 i2c_inst->DR = pointer_byte;
275 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_TXE))
277 return I2C_ERROR_WRITE_FAIL;
280 if (write_buffer!=NULL)
282 for (k=0; k<write_length; k++)
284 i2c_inst->DR = write_buffer[k];
286 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_TXE))
288 return I2C_ERROR_WRITE_FAIL;
293 if (!__wait_SR1_bit(i2c_inst,I2C_SR1_BTF))
295 return I2C_ERROR_WRITE_FAIL;
298 i2c_inst->CR1 |= I2C_CR1_STOP;
307 return I2C_ERROR_NONE;
310 void init_i2c(I2C_TypeDef* i2c_inst,uint32_t sys_clk)
312 i2c_inst->CR2 = sys_clk / 1000000UL;
313 i2c_inst->CCR = sys_clk / 200000UL;
314 i2c_inst->TRISE = (sys_clk / 1000000UL) + 1;
315 i2c_inst->CR1 = I2C_CR1_PE;