Skip to content

Commit 1754f94

Browse files
committed
stm32/i2c: Fix I2C timings.
1 parent 07200fb commit 1754f94

1 file changed

Lines changed: 6 additions & 48 deletions

File tree

ports/stm32/pyb_i2c.c

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ const pyb_i2c_obj_t pyb_i2c_obj[] = {
197197

198198
// I2C TIMINGs obtained from the STHAL examples.
199199
#define MICROPY_HW_I2C_BAUDRATE_TIMING { \
200-
{PYB_I2C_SPEED_STANDARD, 0x40604E73}, \
201-
{PYB_I2C_SPEED_FULL, 0x00901954}, \
202-
{PYB_I2C_SPEED_FAST, 0x10810915}, \
203-
}
200+
{PYB_I2C_SPEED_STANDARD, 0xE0701F28}, \
201+
{PYB_I2C_SPEED_FULL, 0x40900C22}, \
202+
{PYB_I2C_SPEED_FAST, 0x4030040B}, \
203+
}
204204
#define MICROPY_HW_I2C_BAUDRATE_DEFAULT (PYB_I2C_SPEED_FULL)
205205
#define MICROPY_HW_I2C_BAUDRATE_MAX (PYB_I2C_SPEED_FAST)
206206

@@ -449,7 +449,7 @@ void pyb_i2c_deinit_all(void) {
449449

450450
static void i2c_reset_after_error(I2C_HandleTypeDef *i2c) {
451451
// wait for bus-busy flag to be cleared, with a timeout
452-
for (int timeout = 50; timeout > 0; --timeout) {
452+
for (int timeout = 10; timeout > 0; --timeout) {
453453
if (!__HAL_I2C_GET_FLAG(i2c, I2C_FLAG_BUSY)) {
454454
// stop bit was generated and bus is back to normal
455455
return;
@@ -489,50 +489,8 @@ void i2c_ev_irq_handler(mp_uint_t i2c_id) {
489489
return;
490490
}
491491

492-
#if defined(STM32F4)
493-
494-
if (hi2c->Instance->SR1 & I2C_FLAG_SB) {
495-
if (hi2c->State == HAL_I2C_STATE_BUSY_TX) {
496-
hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
497-
} else {
498-
hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
499-
}
500-
hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
501-
} else if (hi2c->Instance->SR1 & I2C_FLAG_ADDR) {
502-
__IO uint32_t tmp_sr2;
503-
if (hi2c->State == HAL_I2C_STATE_BUSY_RX) {
504-
if (hi2c->XferCount == 1U) {
505-
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
506-
} else {
507-
if (hi2c->XferCount == 2U) {
508-
hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
509-
hi2c->Instance->CR1 |= I2C_CR1_POS;
510-
}
511-
hi2c->Instance->CR2 |= I2C_CR2_LAST;
512-
}
513-
}
514-
tmp_sr2 = hi2c->Instance->SR2;
515-
UNUSED(tmp_sr2);
516-
} else if (hi2c->Instance->SR1 & I2C_FLAG_BTF && hi2c->State == HAL_I2C_STATE_BUSY_TX) {
517-
if (hi2c->XferCount != 0U) {
518-
hi2c->Instance->DR = *hi2c->pBuffPtr++;
519-
hi2c->XferCount--;
520-
} else {
521-
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
522-
if (hi2c->XferOptions != I2C_FIRST_FRAME) {
523-
hi2c->Instance->CR1 |= I2C_CR1_STOP;
524-
}
525-
hi2c->Mode = HAL_I2C_MODE_NONE;
526-
hi2c->State = HAL_I2C_STATE_READY;
527-
}
528-
}
529-
530-
#else
531-
532-
// if not an F4 MCU, use the HAL's IRQ handler
492+
// Use the HAL's IRQ handler
533493
HAL_I2C_EV_IRQHandler(hi2c);
534-
535-
#endif
536494
}
537495

538496
void i2c_er_irq_handler(mp_uint_t i2c_id) {

0 commit comments

Comments
 (0)