Skip to content

Commit a753490

Browse files
committed
Refactor out test for bus sanity state
1 parent 028a819 commit a753490

File tree

1 file changed

+22
-23
lines changed
  • ports/nrf/common-hal/busio

1 file changed

+22
-23
lines changed

ports/nrf/common-hal/busio/I2C.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,33 @@ void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
7979
}
8080
}
8181

82-
static nrfx_err_t _safe_twim_enable(busio_i2c_obj_t *self) {
83-
// check to see if bus is in sensible state before enabling twim
84-
nrfx_err_t recover_result;
85-
nrf_gpio_cfg_input(self->scl_pin_number, NRF_GPIO_PIN_PULLDOWN);
86-
nrf_gpio_cfg_input(self->sda_pin_number, NRF_GPIO_PIN_PULLDOWN);
82+
static bool _bus_is_sane(uint32_t scl_pin, uint32_t sda_pin) {
83+
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
84+
nrf_gpio_cfg_input(scl_pin, NRF_GPIO_PIN_PULLDOWN);
85+
nrf_gpio_cfg_input(sda_pin, NRF_GPIO_PIN_PULLDOWN);
8786

8887
common_hal_mcu_delay_us(10);
8988

90-
nrf_gpio_cfg_input(self->scl_pin_number, NRF_GPIO_PIN_NOPULL);
91-
nrf_gpio_cfg_input(self->sda_pin_number, NRF_GPIO_PIN_NOPULL);
89+
nrf_gpio_cfg_input(scl_pin, NRF_GPIO_PIN_NOPULL);
90+
nrf_gpio_cfg_input(sda_pin, NRF_GPIO_PIN_NOPULL);
9291

9392
// We must pull up within 3us to achieve 400khz.
9493
common_hal_mcu_delay_us(3);
94+
if (!nrf_gpio_pin_read(sda_pin) || !nrf_gpio_pin_read(scl_pin)) {
95+
return false;
96+
} else {
97+
return true;
98+
}
99+
#else
100+
return true;
101+
#endif
102+
}
103+
104+
static nrfx_err_t _safe_twim_enable(busio_i2c_obj_t *self) {
105+
// check to see if bus is in sensible state before enabling twim
106+
nrfx_err_t recover_result;
95107

96-
if (!nrf_gpio_pin_read(self->sda_pin_number) || !nrf_gpio_pin_read(self->scl_pin_number)) {
108+
if (!_bus_is_sane(self->scl_pin_number, self->sda_pin_number)) {
97109
// bus not in a sane state - try to recover
98110
recover_result = nrfx_twim_bus_recover(self->scl_pin_number, self->sda_pin_number);
99111
if (NRFX_SUCCESS != recover_result) {
@@ -141,25 +153,12 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
141153
mp_raise_ValueError(translate("All I2C peripherals are in use"));
142154
}
143155

144-
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
145-
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
146-
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_PULLDOWN);
147-
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_PULLDOWN);
148-
149-
common_hal_mcu_delay_us(10);
150-
151-
nrf_gpio_cfg_input(scl->number, NRF_GPIO_PIN_NOPULL);
152-
nrf_gpio_cfg_input(sda->number, NRF_GPIO_PIN_NOPULL);
153-
154-
// We must pull up within 3us to achieve 400khz.
155-
common_hal_mcu_delay_us(3);
156-
157-
if (!nrf_gpio_pin_read(sda->number) || !nrf_gpio_pin_read(scl->number)) {
156+
// check bus is in a sane state
157+
if (!_bus_is_sane(scl->number,sda->number)) {
158158
reset_pin_number(sda->number);
159159
reset_pin_number(scl->number);
160160
mp_raise_RuntimeError(translate("No pull up found on SDA or SCL; check your wiring"));
161161
}
162-
#endif
163162

164163
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(scl->number, sda->number);
165164

0 commit comments

Comments
 (0)