diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c index 7d5e3ed1e9..6af3541b06 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c @@ -79,12 +79,12 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) { if ((obj->i2c == I2C_2)&& !i2c2_inited) { i2c2_inited = 1; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); - // Configure I2C pins - pinmap_pinout(scl, PinMap_I2C_SCL); - pin_mode(scl, OpenDrain); - pinmap_pinout(sda, PinMap_I2C_SDA); - pin_mode(sda, OpenDrain); - } + // Configure I2C pins + pinmap_pinout(scl, PinMap_I2C_SCL); + pin_mode(scl, OpenDrain); + pinmap_pinout(sda, PinMap_I2C_SDA); + pin_mode(sda, OpenDrain); + } // Reset to clear pending flags if any i2c_reset(obj); @@ -94,17 +94,17 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) { } void i2c_frequency(i2c_t *obj, int hz) { - int timeout; - - I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); + int timeout; + + I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; if ((hz != 0) && (hz <= 400000)) { - // wait before init timeout = LONG_TIMEOUT; - while((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)); - + while ((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)) { + } + I2C_DeInit(i2c); // I2C configuration @@ -122,7 +122,7 @@ void i2c_frequency(i2c_t *obj, int hz) { inline int i2c_start(i2c_t *obj) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - int timeout; + int timeout; I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag @@ -151,9 +151,9 @@ inline int i2c_stop(i2c_t *obj) { int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - int timeout; - int count; - int value; + int timeout; + int count; + int value; i2c_start(obj); @@ -171,7 +171,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { // Read all bytes except last one for (count = 0; count < (length - 1); count++) { - value = i2c_byte_read(obj, 0); + value = i2c_byte_read(obj, 0); data[count] = (char)value; } @@ -182,7 +182,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { } // Read the last byte - value = i2c_byte_read(obj, 1); + value = i2c_byte_read(obj, 1); data[count] = (char)value; return length; @@ -190,8 +190,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - int timeout; - int count; + int timeout; + int count; i2c_start(obj); @@ -224,8 +224,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { int i2c_byte_read(i2c_t *obj, int last) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - uint8_t data; - int timeout; + uint8_t data; + int timeout; if (last) { // Don't acknowledge the last byte @@ -251,14 +251,14 @@ int i2c_byte_read(i2c_t *obj, int last) { int i2c_byte_write(i2c_t *obj, int data) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - int timeout; + int timeout; I2C_SendData(i2c, (uint8_t)data); // Wait until the byte is transmitted timeout = FLAG_TIMEOUT; while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && - (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { + (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { timeout--; if (timeout == 0) { return 0; @@ -270,12 +270,13 @@ int i2c_byte_write(i2c_t *obj, int data) { void i2c_reset(i2c_t *obj) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - int timeout; - + int timeout; + // wait before reset timeout = LONG_TIMEOUT; - while((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)); - + while ((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)) { + } + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); @@ -290,7 +291,7 @@ void i2c_reset(i2c_t *obj) { void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - uint16_t tmpreg; + uint16_t tmpreg; // Get the old register value tmpreg = i2c->OAR1; @@ -313,8 +314,8 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) { #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) int i2c_slave_receive(i2c_t *obj) { - int retValue = NoData; - uint32_t event; + int retValue = NoData; + uint32_t event; I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); event = I2C_GetLastEvent(i2c); @@ -376,7 +377,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) { return count; } - #endif // DEVICE_I2CSLAVE #endif // DEVICE_I2C