Skip to content

Commit 2185e80

Browse files
committed
STM32F4 I2C driver: Add explicit pinmap support
1 parent e0fdee9 commit 2185e80

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ struct i2c_s {
9696
int hz;
9797
PinName sda;
9898
PinName scl;
99+
int sda_func;
100+
int scl_func;
99101
IRQn_Type event_i2cIRQ;
100102
IRQn_Type error_i2cIRQ;
101103
uint8_t XferOperation;
@@ -165,4 +167,3 @@ struct qspi_s {
165167
#endif
166168

167169
#endif
168-

targets/TARGET_STM/i2c_api.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
8282
#define FLAG_TIMEOUT ((int)0x1000)
8383

8484
/* Declare i2c_init_internal to be used in this file */
85-
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl);
85+
void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap);
8686

8787
/* GENERIC INIT and HELPERS FUNCTIONS */
8888

@@ -271,24 +271,20 @@ void i2c_sw_reset(i2c_t *obj)
271271
handle->Instance->CR1 |= I2C_CR1_PE;
272272
}
273273

274-
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
275-
{
276-
memset(obj, 0, sizeof(*obj));
277-
i2c_init_internal(obj, sda, scl);
278-
}
279-
280-
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
274+
void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
281275
{
282276
struct i2c_s *obj_s = I2C_S(obj);
283277

284278
// Determine the I2C to use
285-
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
286-
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
287-
obj_s->sda = sda;
288-
obj_s->scl = scl;
289-
290-
obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
291-
MBED_ASSERT(obj_s->i2c != (I2CName)NC);
279+
if (pinmap != NULL) {
280+
obj_s->sda = pinmap->sda_pin;
281+
obj_s->scl = pinmap->scl_pin;
282+
obj_s->sda_func = pinmap->sda_function;
283+
obj_s->scl_func = pinmap->scl_function;
284+
285+
obj_s->i2c = (I2CName)pinmap->peripheral;
286+
MBED_ASSERT(obj_s->i2c != (I2CName)NC);
287+
}
292288

293289
#if defined I2C1_BASE
294290
// Enable I2C1 clock and pinout if not done
@@ -338,10 +334,10 @@ void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
338334
#endif
339335

340336
// Configure I2C pins
341-
pinmap_pinout(sda, PinMap_I2C_SDA);
342-
pinmap_pinout(scl, PinMap_I2C_SCL);
343-
pin_mode(sda, OpenDrainNoPull);
344-
pin_mode(scl, OpenDrainNoPull);
337+
pin_function(obj_s->sda, obj_s->sda_func);
338+
pin_function(obj_s->scl, obj_s->scl_func);
339+
pin_mode(obj_s->sda, OpenDrainNoPull);
340+
pin_mode(obj_s->scl, OpenDrainNoPull);
345341

346342
// I2C configuration
347343
// Default hz value used for timeout computation
@@ -368,6 +364,28 @@ void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
368364
#endif
369365
}
370366

367+
void i2c_init_direct(i2c_t *obj, const i2c_pinmap_t *pinmap)
368+
{
369+
memset(obj, 0, sizeof(*obj));
370+
i2c_init_internal(obj, pinmap);
371+
}
372+
373+
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
374+
{
375+
uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
376+
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
377+
378+
int peripheral = (int)pinmap_merge(i2c_sda, i2c_scl);
379+
380+
int sda_function = (int)pinmap_find_function(sda, PinMap_I2C_SDA);
381+
int scl_function = (int)pinmap_find_function(scl, PinMap_I2C_SCL);
382+
383+
const i2c_pinmap_t explicit_i2c_pinmap = {peripheral, sda, sda_function, scl, scl_function};
384+
385+
i2c_init_direct(obj, &explicit_i2c_pinmap);
386+
}
387+
388+
371389
void i2c_frequency(i2c_t *obj, int hz)
372390
{
373391
int timeout;
@@ -488,7 +506,7 @@ void i2c_reset(i2c_t *obj)
488506
/* As recommended in i2c_api.h, mainly send stop */
489507
i2c_stop(obj);
490508
/* then re-init */
491-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
509+
i2c_init_internal(obj, NULL);
492510
}
493511

494512
/*
@@ -542,7 +560,7 @@ int i2c_stop(i2c_t *obj)
542560
* re-init HAL state
543561
*/
544562
if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) {
545-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
563+
i2c_init_internal(obj, NULL);
546564
}
547565

548566
return 0;
@@ -618,7 +636,7 @@ int i2c_stop(i2c_t *obj)
618636
#if DEVICE_I2CSLAVE
619637
if (obj_s->slave) {
620638
/* re-init slave when stop is requested */
621-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
639+
i2c_init_internal(obj, NULL);
622640
return 0;
623641
}
624642
#endif
@@ -659,7 +677,7 @@ int i2c_stop(i2c_t *obj)
659677
/* In case of mixed usage of the APIs (unitary + SYNC)
660678
* re-init HAL state */
661679
if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) {
662-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
680+
i2c_init_internal(obj, NULL);
663681
}
664682

665683
return 0;
@@ -837,7 +855,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
837855
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
838856
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
839857
/* re-init IP to try and get back in a working state */
840-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
858+
i2c_init_internal(obj, NULL);
841859
} else {
842860
count = length;
843861
}
@@ -904,7 +922,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
904922
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
905923
DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
906924
/* re-init IP to try and get back in a working state */
907-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
925+
i2c_init_internal(obj, NULL);
908926
} else {
909927
count = length;
910928
}
@@ -974,7 +992,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
974992
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
975993

976994
/* re-init IP to try and get back in a working state */
977-
i2c_init_internal(obj, obj_s->sda, obj_s->scl);
995+
i2c_init_internal(obj, NULL);
978996

979997
#if DEVICE_I2CSLAVE
980998
/* restore slave address */

0 commit comments

Comments
 (0)