Merge pull request #8094 from furbrain/fix_i2c_hangs

Fix for #8093
This commit is contained in:
Dan Halbert 2023-06-23 13:12:24 -04:00 committed by GitHub
commit ffebb5af8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 22 deletions

View File

@ -114,6 +114,7 @@ SRC_NRFX = $(addprefix nrfx/,\
drivers/src/nrfx_spim.c \
drivers/src/nrfx_timer.c \
drivers/src/nrfx_twim.c \
drivers/src/nrfx_twi_twim.c \
drivers/src/nrfx_uarte.c \
drivers/src/nrfx_gpiote.c \
drivers/src/nrfx_rtc.c \

View File

@ -79,20 +79,59 @@ void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
}
}
static bool _bus_is_sane(uint32_t scl_pin, uint32_t sda_pin) {
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
nrf_gpio_cfg_input(scl_pin, NRF_GPIO_PIN_PULLDOWN);
nrf_gpio_cfg_input(sda_pin, NRF_GPIO_PIN_PULLDOWN);
common_hal_mcu_delay_us(10);
nrf_gpio_cfg_input(scl_pin, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(sda_pin, NRF_GPIO_PIN_NOPULL);
// We must pull up within 3us to achieve 400khz.
common_hal_mcu_delay_us(3);
if (!nrf_gpio_pin_read(sda_pin) || !nrf_gpio_pin_read(scl_pin)) {
return false;
} else {
return true;
}
#else
return true;
#endif
}
static nrfx_err_t _safe_twim_enable(busio_i2c_obj_t *self) {
// check to see if bus is in sensible state before enabling twim
nrfx_err_t recover_result;
if (!_bus_is_sane(self->scl_pin_number, self->sda_pin_number)) {
// bus not in a sane state - try to recover
recover_result = nrfx_twim_bus_recover(self->scl_pin_number, self->sda_pin_number);
if (NRFX_SUCCESS != recover_result) {
// return error message if unable to recover the bus
return recover_result;
}
}
nrfx_twim_enable(&self->twim_peripheral->twim);
return NRFX_SUCCESS;
}
static uint8_t twi_error_to_mp(const nrfx_err_t err) {
switch (err) {
case NRFX_ERROR_DRV_TWI_ERR_ANACK:
return MP_ENODEV;
case NRFX_ERROR_BUSY:
return MP_EBUSY;
case NRFX_SUCCESS:
return 0;
case NRFX_ERROR_DRV_TWI_ERR_DNACK:
case NRFX_ERROR_INVALID_ADDR:
return MP_EIO;
case NRFX_ERROR_INTERNAL:
default:
break;
return MP_EIO;
}
return 0;
}
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
@ -114,25 +153,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
mp_raise_ValueError(translate("All I2C peripherals are in use"));
}
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_PULLDOWN);
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_PULLDOWN);
common_hal_mcu_delay_us(10);
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_NOPULL);
// We must pull up within 3us to achieve 400khz.
common_hal_mcu_delay_us(3);
if (!nrf_gpio_pin_read(sda->number) || !nrf_gpio_pin_read(scl->number)) {
// check bus is in a sane state
if (!_bus_is_sane(scl->number,sda->number)) {
reset_pin_number(sda->number);
reset_pin_number(scl->number);
mp_raise_RuntimeError(translate("No pull up found on SDA or SCL; check your wiring"));
}
#endif
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(scl->number, sda->number);
@ -188,7 +214,9 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
NRF_TWIM_Type *reg = self->twim_peripheral->twim.p_twim;
bool found = true;
nrfx_twim_enable(&self->twim_peripheral->twim);
if (NRFX_SUCCESS != _safe_twim_enable(self)) {
return false;
}
nrf_twim_address_set(reg, addr);
nrf_twim_tx_buffer_set(reg, NULL, 0);
@ -246,7 +274,10 @@ STATIC uint8_t _common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
nrfx_err_t err = NRFX_SUCCESS;
nrfx_twim_enable(&self->twim_peripheral->twim);
err = _safe_twim_enable(self);
if (NRFX_SUCCESS != err) {
return twi_error_to_mp(err);
}
// break into MAX_XFER_LEN transaction
while (len) {
@ -278,7 +309,10 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t
nrfx_err_t err = NRFX_SUCCESS;
nrfx_twim_enable(&self->twim_peripheral->twim);
err = _safe_twim_enable(self);
if (NRFX_SUCCESS != err) {
return twi_error_to_mp(err);
}
// break into MAX_XFER_LEN transaction
while (len) {