mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c
- Revision:
- 306:1f0269907d8b
- Parent:
- 247:135e3186a638
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c Fri Aug 29 17:15:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c Fri Aug 29 20:45:07 2014 +0100 @@ -55,6 +55,9 @@ {NC, NC, 0} }; +int i2c1_inited = 0; +int i2c2_inited = 0; + void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); @@ -63,20 +66,28 @@ obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj->i2c != (I2CName)NC); - // Enable I2C clock - if (obj->i2c == I2C_1) { + // Enable I2C1 clock and pinout if not done + if ((obj->i2c == I2C_1)&& !i2c1_inited) { + i2c1_inited = 1; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); - } - if (obj->i2c == I2C_2) { - 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); + // Enable I2C2 clock and pinout if not done + 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); + } // Reset to clear pending flags if any i2c_reset(obj); @@ -90,6 +101,11 @@ I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; + int timeout; + + // wait before init + timeout = LONG_TIMEOUT; + while((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)); // Disable the Fast Mode Plus capability RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock @@ -260,6 +276,13 @@ } void i2c_reset(i2c_t *obj) { + I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); + int timeout; + + // wait before reset + timeout = LONG_TIMEOUT; + 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);