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:
- 80:66393a7b209d
- Parent:
- 76:aeb1df146756
- Child:
- 118:b44c45162f28
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Thu Jan 30 09:45:05 2014 +0000 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Thu Jan 30 12:15:05 2014 +0000 @@ -42,12 +42,12 @@ #define LONG_TIMEOUT ((int)0x8000) static const PinMap PinMap_I2C_SDA[] = { - {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1 + {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, {NC, NC, 0} }; static const PinMap PinMap_I2C_SCL[] = { - {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1 + {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, {NC, NC, 0} }; @@ -71,10 +71,10 @@ } // Configure I2C pins + pinmap_pinout(scl, PinMap_I2C_SCL); + pin_mode(scl, OpenDrain); pinmap_pinout(sda, PinMap_I2C_SDA); - pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); - pin_mode(scl, OpenDrain); // Reset to clear pending flags if any i2c_reset(obj); @@ -88,6 +88,8 @@ I2C_InitTypeDef I2C_InitStructure; if ((hz != 0) && (hz <= 400000)) { + I2C_DeInit(i2c); + // I2C configuration I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; @@ -95,8 +97,9 @@ I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = hz; + I2C_Init(i2c, &I2C_InitStructure); + I2C_Cmd(i2c, ENABLE); - I2C_Init(i2c, &I2C_InitStructure); } } @@ -113,9 +116,10 @@ timeout = FLAG_TIMEOUT; //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) { while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { - if ((timeout--) == 0) { - return 1; - } + timeout--; + if (timeout == 0) { + return 1; + } } return 0; @@ -141,7 +145,8 @@ // Wait until the bus is not busy anymore timeout = LONG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { - if ((timeout--) == 0) { + timeout--; + if (timeout == 0) { return 0; } } @@ -155,9 +160,10 @@ // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) { - if ((timeout--) == 0) { - return 0; - } + timeout--; + if (timeout == 0) { + return 0; + } } // Read all bytes except last one @@ -188,7 +194,8 @@ // Wait until the bus is not busy anymore timeout = LONG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { - if ((timeout--) == 0) { + timeout--; + if (timeout == 0) { return 0; } } @@ -202,9 +209,10 @@ // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { - if ((timeout--) == 0) { - return 0; - } + timeout--; + if (timeout == 0) { + return 0; + } } for (count = 0; count < length; count++) { @@ -238,9 +246,10 @@ // Wait until the byte is received timeout = FLAG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) { - if ((timeout--) == 0) { - return 0; - } + timeout--; + if (timeout == 0) { + return 0; + } } data = I2C_ReceiveData(i2c); @@ -259,7 +268,8 @@ //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) { while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { - if ((timeout--) == 0) { + timeout--; + if (timeout == 0) { return 0; } }