mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c
- Revision:
- 174:8bb9f3a33240
- Parent:
- 129:0182c99221bc
- Child:
- 190:bde2479eef9e
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Mon Apr 28 18:15:06 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Tue Apr 29 11:15:07 2014 +0100 @@ -36,8 +36,8 @@ #include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -55,19 +55,19 @@ {NC, NC, 0} }; -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); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - + if (obj->i2c == (I2CName)NC) { error("I2C pin mapping failed"); } // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); } if (obj->i2c == I2C_2) { @@ -79,12 +79,12 @@ pin_mode(scl, OpenDrain); pinmap_pinout(sda, PinMap_I2C_SDA); pin_mode(sda, OpenDrain); - + // Reset to clear pending flags if any i2c_reset(obj); - + // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { @@ -97,7 +97,7 @@ /* Warning: To use the I2C at 400 kHz (in fast mode), the PCLK1 frequency (I2C peripheral input clock) must be a multiple of 10 MHz. With the actual clock configuration, the max frequency is measured at 296 kHz */ - + // I2C configuration I2C_DeInit(i2c); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; @@ -113,12 +113,12 @@ inline int i2c_start(i2c_t *obj) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; - + I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag - + // Generate the START condition - I2C_GenerateSTART(i2c, ENABLE); - + I2C_GenerateSTART(i2c, ENABLE); + // Wait the START condition has been correctly sent timeout = FLAG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { @@ -127,7 +127,7 @@ return 1; } } - + return 0; } @@ -135,10 +135,10 @@ I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; volatile int temp; - + if (I2C_GetFlagStatus(i2c, I2C_FLAG_MSL) == RESET) { timeout = LONG_TIMEOUT; - // wait for STOP + // wait for STOP while (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == RESET) { timeout--; if (timeout == 0) { @@ -147,11 +147,10 @@ } temp = i2c->SR1; I2C_Cmd(i2c, ENABLE); - } - else { + } else { I2C_GenerateSTOP(i2c, ENABLE); } - + return 0; } @@ -160,13 +159,13 @@ int timeout; int count; int value; - + if (length == 0) return 0; i2c_start(obj); // Send slave address for read - I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); + I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); // Wait address is acknowledged timeout = FLAG_TIMEOUT; @@ -176,13 +175,13 @@ return 0; } } - + // Read all bytes except last one for (count = 0; count < (length - 1); count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } - + // If not repeated start, send stop. // Warning: must be done BEFORE the data is read. if (stop) { @@ -192,7 +191,7 @@ // Read the last byte value = i2c_byte_read(obj, 1); data[count] = (char)value; - + return length; } @@ -205,7 +204,7 @@ // Send slave address for write I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter); - + // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { @@ -234,7 +233,7 @@ I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); uint8_t data; int timeout; - + if (last) { // Don't acknowledge the last byte I2C_AcknowledgeConfig(i2c, DISABLE); @@ -253,7 +252,7 @@ } data = I2C_ReceiveData(i2c); - + return (int)data; } @@ -266,24 +265,24 @@ // 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; } } - + return 1; } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } if (obj->i2c == I2C_2) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); - RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); } } @@ -292,7 +291,7 @@ 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; - + // Get the old register value tmpreg = i2c->OAR1; // Reset address bits @@ -314,28 +313,28 @@ #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) int i2c_slave_receive(i2c_t *obj) { - return(0); + return (0); } int i2c_slave_read(i2c_t *obj, char *data, int length) { int count = 0; - + // Read all bytes for (count = 0; count < length; count++) { data[count] = i2c_byte_read(obj, 0); } - + return count; } int i2c_slave_write(i2c_t *obj, const char *data, int length) { int count = 0; - + // Write all bytes for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } - + return count; }