8000 stm32/i2c: Add support for I2C4 on H7 MCUs. · micropython/micropython@5aec051 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5aec051

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/i2c: Add support for I2C4 on H7 MCUs.
The current code assumes all I2Cs are on the same peripheral bus, which is not true for I2C4 and the same goes for the clock enable code. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent cac666f commit 5aec051

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

ports/stm32/i2c.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,17 @@ int i2c_write(i2c_t *i2c, const uint8_t *src, size_t len, size_t next_len) {
285285

286286
STATIC uint16_t i2c_timeout_ms[MICROPY_HW_MAX_I2C];
287287

288+
static uint32_t i2c_get_id(i2c_t *i2c) {
289+
#if defined(STM32H7)
290+
if (i2c == I2C4) {
291+
return 3;
292+
}
293+
#endif
294+
return ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
295+
}
296+
288297
int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t freq, uint16_t timeout_ms) {
289-
uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
298+
uint32_t i2c_id = i2c_get_id(i2c);
290299

291300
// Init pins
292301
if (!mp_hal_pin_config_alt(scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, AF_FN_I2C, i2c_id + 1)) {
@@ -300,9 +309,22 @@ int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t fr
300309
i2c_timeout_ms[i2c_id] = timeout_ms;
301310

302311
// Enable I2C peripheral clock
303-
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN << i2c_id;
304-
volatile uint32_t tmp = RCC->APB1ENR; // delay after RCC clock enable
312+
volatile uint32_t tmp;
305313
(void)tmp;
314+
switch (i2c_id) {
315+
case 0:
316+
case 1:
317+
case 2:
318+
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN << i2c_id;
319+
tmp = RCC->APB1ENR; // delay after RCC clock enable
320+
break;
321+
#if defined(STM32H7)
322+
case 3:
323+
RCC->APB4ENR |= RCC_APB4ENR_I2C4EN;
324+
tmp = RCC->APB4ENR; // delay after RCC clock enable
325+
break;
326+
#endif
327+
}
306328

307329
// Initialise I2C peripheral
308330
i2c->CR1 = 0;
@@ -340,7 +362,8 @@ int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t fr
340362
}
341363

342364
STATIC int i2c_wait_cr2_clear(i2c_t *i2c, uint32_t mask) {
343-
uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
365+
uint32_t i2c_id = i2c_get_id(i2c);
366+
344367
uint32_t t0 = HAL_GetTick();
345368
while (i2c->CR2 & mask) {
346369
if (HAL_GetTick() - t0 >= i2c_timeout_ms[i2c_id]) {
@@ -352,7 +375,8 @@ STATIC int i2c_wait_cr2_clear(i2c_t *i2c, uint32_t mask) {
352375
}
353376

354377
STATIC int i2c_wait_isr_set(i2c_t *i2c, uint32_t mask) {
355-
uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
378+
uint32_t i2c_id = i2c_get_id(i2c);
379+
356380
uint32_t t0 = HAL_GetTick();
357381
while (!(i2c->ISR & mask)) {
358382
if (HAL_GetTick() - t0 >= i2c_timeout_ms[i2c_id]) {

0 commit comments

Comments
 (0)
0