Skip to content

Commit 076b096

Browse files
committed
STM: I2C: Confifure pins in OpenDrainNoPull by default (no pullup)
As reported by MBED user Fran6Jack: I2C bus are usually 5V tolerant on all STM32 processor. If an external device on the I2C bus requires 5V operation, we usually acheive it by using 5V external pull-ups on the bus. Since signaling uses open-drain output on I2C for both signal SCL and SDA any 5V tolerant MCU will work on a 5V I2C bus. Having pull-up activated on a 5V externally pull bus, cause the pin to clamp on the STM32 die diode and could damage the IC (There is a note in STM32 datasheet specifying this issue). It is understood by all the community that I2C bus should always be externally pulled by physical resistor. I2C initialization should then be ALWAYS OpenDrainNoPull by default. Up to now, this I2C driver was setting pull up by default as it helps basic testing, like 1 master and 1 slave, conencted with 2 wires without any external pull ups. This will not work anymore after this commit and applications tests or examples needs to be modified to explicitely configure pull ups ... But it is safer to follow reference manual guidelines.
1 parent 5bddd88 commit 076b096

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
274274
obj_s->index = 0;
275275
__HAL_RCC_I2C1_CLK_ENABLE();
276276
// Configure I2C pins
277-
pinmap_pinout(sda, PinMap_I2C_SDA);
278-
pinmap_pinout(scl, PinMap_I2C_SCL);
279-
pin_mode(sda, OpenDrainPullUp);
280-
pin_mode(scl, OpenDrainPullUp);
281277
obj_s->event_i2cIRQ = I2C1_EV_IRQn;
282278
obj_s->error_i2cIRQ = I2C1_ER_IRQn;
283279
}
@@ -287,11 +283,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
287283
if (obj_s->i2c == I2C_2) {
288284
obj_s->index = 1;
289285
__HAL_RCC_I2C2_CLK_ENABLE();
290-
// Configure I2C pins
291-
pinmap_pinout(sda, PinMap_I2C_SDA);
292-
pinmap_pinout(scl, PinMap_I2C_SCL);
293-
pin_mode(sda, OpenDrainPullUp);
294-
pin_mode(scl, OpenDrainPullUp);
295286
obj_s->event_i2cIRQ = I2C2_EV_IRQn;
296287
obj_s->error_i2cIRQ = I2C2_ER_IRQn;
297288
}
@@ -301,11 +292,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
301292
if (obj_s->i2c == I2C_3) {
302293
obj_s->index = 2;
303294
__HAL_RCC_I2C3_CLK_ENABLE();
304-
// Configure I2C pins
305-
pinmap_pinout(sda, PinMap_I2C_SDA);
306-
pinmap_pinout(scl, PinMap_I2C_SCL);
307-
pin_mode(sda, OpenDrainPullUp);
308-
pin_mode(scl, OpenDrainPullUp);
309295
obj_s->event_i2cIRQ = I2C3_EV_IRQn;
310296
obj_s->error_i2cIRQ = I2C3_ER_IRQn;
311297
}
@@ -315,11 +301,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
315301
if (obj_s->i2c == I2C_4) {
316302
obj_s->index = 3;
317303
__HAL_RCC_I2C4_CLK_ENABLE();
318-
// Configure I2C pins
319-
pinmap_pinout(sda, PinMap_I2C_SDA);
320-
pinmap_pinout(scl, PinMap_I2C_SCL);
321-
pin_mode(sda, OpenDrainPullUp);
322-
pin_mode(scl, OpenDrainPullUp);
323304
obj_s->event_i2cIRQ = I2C4_EV_IRQn;
324305
obj_s->error_i2cIRQ = I2C4_ER_IRQn;
325306
}
@@ -329,16 +310,17 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
329310
if (obj_s->i2c == FMPI2C_1) {
330311
obj_s->index = 4;
331312
__HAL_RCC_FMPI2C1_CLK_ENABLE();
332-
// Configure I2C pins
333-
pinmap_pinout(sda, PinMap_I2C_SDA);
334-
pinmap_pinout(scl, PinMap_I2C_SCL);
335-
pin_mode(sda, OpenDrainPullUp);
336-
pin_mode(scl, OpenDrainPullUp);
337313
obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
338314
obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
339315
}
340316
#endif
341317

318+
// Configure I2C pins
319+
pinmap_pinout(sda, PinMap_I2C_SDA);
320+
pinmap_pinout(scl, PinMap_I2C_SCL);
321+
pin_mode(sda, OpenDrainNoPull);
322+
pin_mode(scl, OpenDrainNoPull);
323+
342324
// I2C configuration
343325
// Default hz value used for timeout computation
344326
if(!obj_s->hz)

0 commit comments

Comments
 (0)