mirror of https://github.com/ARMmbed/mbed-os.git
[NUCLEO_L152RE] Correction on I2C gpio configuration + code cleanup
parent
740f801bb5
commit
6f06f6ecd2
|
@ -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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
}
|
||||
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pin_mode(sda, OpenDrain);
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
@ -88,6 +88,8 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
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 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
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 @@ inline int i2c_start(i2c_t *obj) {
|
|||
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 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
// 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 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
// 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 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
// 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 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
// 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 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
// 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 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
//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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue