Skip to content

Commit 6f06f6e

Browse files
committed
[NUCLEO_L152RE] Correction on I2C gpio configuration + code cleanup
1 parent 740f801 commit 6f06f6e

File tree

1 file changed

+30
-20
lines changed
  • libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE

1 file changed

+30
-20
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
#define LONG_TIMEOUT ((int)0x8000)
4343

4444
static const PinMap PinMap_I2C_SDA[] = {
45-
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
45+
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)},
4646
{NC, NC, 0}
4747
};
4848

4949
static const PinMap PinMap_I2C_SCL[] = {
50-
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
50+
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)},
5151
{NC, NC, 0}
5252
};
5353

@@ -71,10 +71,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
7171
}
7272

7373
// Configure I2C pins
74-
pinmap_pinout(sda, PinMap_I2C_SDA);
7574
pinmap_pinout(scl, PinMap_I2C_SCL);
76-
pin_mode(sda, OpenDrain);
7775
pin_mode(scl, OpenDrain);
76+
pinmap_pinout(sda, PinMap_I2C_SDA);
77+
pin_mode(sda, OpenDrain);
7878

7979
// Reset to clear pending flags if any
8080
i2c_reset(obj);
@@ -88,15 +88,18 @@ void i2c_frequency(i2c_t *obj, int hz) {
8888
I2C_InitTypeDef I2C_InitStructure;
8989

9090
if ((hz != 0) && (hz <= 400000)) {
91+
I2C_DeInit(i2c);
92+
9193
// I2C configuration
9294
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
9395
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
9496
I2C_InitStructure.I2C_OwnAddress1 = 0;
9597
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
9698
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
9799
I2C_InitStructure.I2C_ClockSpeed = hz;
100+
I2C_Init(i2c, &I2C_InitStructure);
101+
98102
I2C_Cmd(i2c, ENABLE);
99-
I2C_Init(i2c, &I2C_InitStructure);
100103
}
101104
}
102105

@@ -113,9 +116,10 @@ inline int i2c_start(i2c_t *obj) {
113116
timeout = FLAG_TIMEOUT;
114117
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
115118
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
116-
if ((timeout--) == 0) {
117-
return 1;
118-
}
119+
timeout--;
120+
if (timeout == 0) {
121+
return 1;
122+
}
119123
}
120124

121125
return 0;
@@ -141,7 +145,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
141145
// Wait until the bus is not busy anymore
142146
timeout = LONG_TIMEOUT;
143147
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
144-
if ((timeout--) == 0) {
148+
timeout--;
149+
if (timeout == 0) {
145150
return 0;
146151
}
147152
}
@@ -155,9 +160,10 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
155160
// Wait address is acknowledged
156161
timeout = FLAG_TIMEOUT;
157162
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
158-
if ((timeout--) == 0) {
159-
return 0;
160-
}
163+
timeout--;
164+
if (timeout == 0) {
165+
return 0;
166+
}
161167
}
162168

163169
// 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) {
188194
// Wait until the bus is not busy anymore
189195
timeout = LONG_TIMEOUT;
190196
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
191-
if ((timeout--) == 0) {
197+
timeout--;
198+
if (timeout == 0) {
192199
return 0;
193200
}
194201
}
@@ -202,9 +209,10 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
202209
// Wait address is acknowledged
203210
timeout = FLAG_TIMEOUT;
204211
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
205-
if ((timeout--) == 0) {
206-
return 0;
207-
}
212+
timeout--;
213+
if (timeout == 0) {
214+
return 0;
215+
}
208216
}
209217

210218
for (count = 0; count < length; count++) {
@@ -238,9 +246,10 @@ int i2c_byte_read(i2c_t *obj, int last) {
238246
// Wait until the byte is received
239247
timeout = FLAG_TIMEOUT;
240248
while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
241-
if ((timeout--) == 0) {
242-
return 0;
243-
}
249+
timeout--;
250+
if (timeout == 0) {
251+
return 0;
252+
}
244253
}
245254

246255
data = I2C_ReceiveData(i2c);
@@ -259,7 +268,8 @@ int i2c_byte_write(i2c_t *obj, int data) {
259268
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
260269
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
261270
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
262-
if ((timeout--) == 0) {
271+
timeout--;
272+
if (timeout == 0) {
263273
return 0;
264274
}
265275
}

0 commit comments

Comments
 (0)