Modified version of the mbed library for use with the Nucleo boards.
Dependents: EEPROMWrite Full-Project
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_STM32F0/i2c_api.c
- Revision:
- 630:825f75ca301e
- Parent:
- 469:fc4922e0c183
--- a/targets/hal/TARGET_STM/TARGET_STM32F0/i2c_api.c Mon Sep 28 10:30:09 2015 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F0/i2c_api.c Mon Sep 28 10:45:10 2015 +0100 @@ -45,10 +45,11 @@ I2C_HandleTypeDef I2cHandle; int i2c1_inited = 0; +#if defined(I2C2_BASE) int i2c2_inited = 0; +#endif -void i2c_init(i2c_t *obj, PinName sda, PinName scl) -{ +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); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); @@ -68,6 +69,7 @@ pin_mode(scl, OpenDrain); } +#if defined(I2C2_BASE) // Enable I2C2 clock and pinout if not done if ((obj->i2c == I2C_2) && !i2c2_inited) { i2c2_inited = 1; @@ -78,6 +80,7 @@ pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } +#endif // Reset to clear pending flags if any i2c_reset(obj); @@ -86,8 +89,7 @@ i2c_frequency(obj, 100000); // 100 kHz per default } -void i2c_frequency(i2c_t *obj, int hz) -{ +void i2c_frequency(i2c_t *obj, int hz) { MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -122,8 +124,7 @@ HAL_I2C_Init(&I2cHandle); } -inline int i2c_start(i2c_t *obj) -{ +inline int i2c_start(i2c_t *obj) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -146,8 +147,7 @@ return 0; } -inline int i2c_stop(i2c_t *obj) -{ +inline int i2c_stop(i2c_t *obj) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); // Generate the STOP condition @@ -156,8 +156,7 @@ return 0; } -int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) -{ +int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -202,8 +201,7 @@ return length; } -int i2c_write(i2c_t *obj, int address, const 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); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -245,8 +243,7 @@ return count; } -int i2c_byte_read(i2c_t *obj, int last) -{ +int i2c_byte_read(i2c_t *obj, int last) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -261,8 +258,7 @@ return (int)i2c->RXDR; } -int i2c_byte_write(i2c_t *obj, int data) -{ +int i2c_byte_write(i2c_t *obj, int data) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; @@ -279,8 +275,7 @@ return 1; } -void i2c_reset(i2c_t *obj) -{ +void i2c_reset(i2c_t *obj) { int timeout; // Wait before reset @@ -291,16 +286,17 @@ __I2C1_FORCE_RESET(); __I2C1_RELEASE_RESET(); } +#if defined(I2C2_BASE) if (obj->i2c == I2C_2) { __I2C2_FORCE_RESET(); __I2C2_RELEASE_RESET(); } +#endif } #if DEVICE_I2CSLAVE -void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) -{ +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 = 0; @@ -318,8 +314,7 @@ i2c->OAR1 |= I2C_OAR1_OA1EN; } -void i2c_slave_mode(i2c_t *obj, int enable_slave) -{ +void i2c_slave_mode(i2c_t *obj, int enable_slave) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); uint16_t tmpreg; @@ -343,8 +338,7 @@ #define WriteGeneral 2 // the master is writing to all slave #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) -int i2c_slave_receive(i2c_t *obj) -{ +int i2c_slave_receive(i2c_t *obj) { I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int retValue = NoData; @@ -362,8 +356,7 @@ return (retValue); } -int i2c_slave_read(i2c_t *obj, char *data, int length) -{ +int i2c_slave_read(i2c_t *obj, char *data, int length) { char size = 0; while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); @@ -371,8 +364,7 @@ return size; } -int i2c_slave_write(i2c_t *obj, const char *data, int length) -{ +int i2c_slave_write(i2c_t *obj, const char *data, int length) { char size = 0; I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);