24 static void i2c1_setup(
void);
25 static mcu_error i2c1_write(uint8_t byte);
26 static uint8_t i2c1_read(
void);
27 static uint8_t i2c1_repeatRead(
void);
30 static mcu_error i2c1_address(uint8_t address, uint8_t operation);
36 static void i2c2_setup(
void);
37 static mcu_error i2c2_write(uint8_t byte);
38 static uint8_t i2c2_read(
void);
39 static uint8_t i2c2_repeatRead(
void);
42 static mcu_error i2c2_address(uint8_t address, uint8_t operation);
77 void i2c1_setup(
void){
84 rcc_periph_clock_enable(RCC_AFIO);
85 rcc_periph_clock_enable(RCC_I2C1);
88 i2c_peripheral_disable(I2C1);
92 I2C_CR2(I2C1) |= (rcc_apb2_frequency/1000000);
95 i2c_set_fast_mode(I2C1);
99 i2c_set_ccr(I2C1,(uint8_t)(900/(10000000000.0f/(
float)rcc_apb2_frequency)));
102 I2C_CCR(I2C1) |= I2C_CCR_DUTY;
106 i2c_set_trise(I2C1, (uint8_t)(300/(1000000000.0f/(
float)rcc_apb2_frequency)));
112 i2c_peripheral_enable(I2C1);
115 void i2c2_setup(
void){
122 rcc_periph_clock_enable(RCC_AFIO);
123 rcc_periph_clock_enable(RCC_I2C2);
126 i2c_peripheral_disable(I2C2);
129 I2C_CR2(I2C2) |= (rcc_apb2_frequency/1000000);
132 i2c_set_fast_mode(I2C2);
136 i2c_set_ccr(I2C2,(uint8_t)(900/(10000000000.0f/(
float)rcc_apb2_frequency)));
139 I2C_CCR(I2C2) |= I2C_CCR_DUTY;
143 i2c_set_trise(I2C2, (uint8_t)(300/(1000000000.0f/(
float)rcc_apb2_frequency)));
149 i2c_peripheral_enable(I2C2);
186 uint32_t timeoutCounter = 0;
188 i2c_send_data(I2C1, byte);
189 while (!(I2C_SR1(I2C1) & (I2C_SR1_BTF | I2C_SR1_TxE))){
206 uint32_t timeoutCounter = 0;
208 i2c_send_data(I2C2, byte);
209 while (!(I2C_SR1(I2C2) & (I2C_SR1_BTF | I2C_SR1_TxE))){
256 uint8_t i2c1_read(
void){
258 uint32_t timeoutCounter = 0;
260 I2C_CR1(I2C1) &= ~I2C_CR1_ACK;
263 while (!(I2C_SR1(I2C1) & I2C_SR1_RxNE)){
279 uint8_t i2c2_read(
void){
281 uint32_t timeoutCounter = 0;
283 I2C_CR1(I2C2) &= ~I2C_CR1_ACK;
286 while (!(I2C_SR1(I2C2) & I2C_SR1_RxNE)){
311 byte = i2c1_repeatRead();
317 byte = i2c2_repeatRead();
332 uint8_t i2c1_repeatRead(
void){
334 uint32_t timeoutCounter = 0;
336 I2C_CR1(I2C1) |= I2C_CR1_ACK;
339 while (!(I2C_SR1(I2C1) & I2C_SR1_RxNE)){
355 uint8_t i2c2_repeatRead(
void){
357 uint32_t timeoutCounter = 0;
359 I2C_CR1(I2C2) |= I2C_CR1_ACK;
362 while (!(I2C_SR1(I2C2) & I2C_SR1_RxNE)){
411 uint32_t timeoutCounter = 0;
414 i2c_send_start(I2C1);
416 while (!((I2C_SR1(I2C1) & I2C_SR1_SB)& (I2C_SR2(I2C1) & (I2C_SR2_MSL | I2C_SR2_BUSY)))){
434 uint32_t timeoutCounter = 0;
437 i2c_send_start(I2C2);
439 while (!((I2C_SR1(I2C2) & I2C_SR1_SB)& (I2C_SR2(I2C2) & (I2C_SR2_MSL | I2C_SR2_BUSY)))){
510 i2c1_address(address, operation);
516 i2c2_address(address, operation);
531 mcu_error i2c1_address(uint8_t address, uint8_t operation){
533 uint32_t timeoutCounter = 0;
535 i2c_send_7bit_address(I2C1, address, operation);
537 while (!(I2C_SR1(I2C1) & I2C_SR1_ADDR)){
551 address = I2C_SR2(I2C1);
557 mcu_error i2c2_address(uint8_t address, uint8_t operation){
559 uint32_t timeoutCounter = 0;
561 i2c_send_7bit_address(I2C2, address, operation);
563 while (!(I2C_SR1(I2C2) & I2C_SR1_ADDR)){
577 address = I2C_SR2(I2C2);
#define I2C_MY_ADDRESS
The address of this device.
mcu_error I2C_address(i2c_periph peripheral, uint8_t address, uint8_t operation)
Send a seven bit address on the bus.
Header file for stm32f103cb I2C.
ERROR I2C: Port doesn't exist.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
mcu_error I2C_setup(i2c_periph peripheral)
Initialize I2C data structure and setup port.
#define I2C_TIMEOUT
The number of loop cycles to wait when polling peripheral registers.
mcu_error I2C_stop(i2c_periph peripheral)
Generate a stop condition on the bus.
ERROR I2C: Timeout during get()
mcu_error I2C_read(i2c_periph peripheral)
Read one byte from the bus without acknowledge generation.
ERROR I2C: Timeout during stop()
ERROR I2C: Timeout during start()
ERROR I2C: Timeout during put()
i2c_periph
I2C ports available on the MCU.
mcu_error pinSetup(gpio_mode mode, gpio_port port, gpio_pin pin)
Setup a GPIO pin for a given function.
mcu_error I2C_start(i2c_periph peripheral)
Generate a start condition on the bus.
uint8_t I2C_repeatRead(i2c_periph peripheral)
Read one byte from the bus with acknowledge generation.
mcu_error I2C_write(i2c_periph peripheral, uint8_t byte)
Send one byte on the bus.
mcu_error
Error enumerators for the Debug peripheral.