@@ -79,59 +79,20 @@ void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
79
79
}
80
80
}
81
81
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 );
86
-
87
- common_hal_mcu_delay_us (10 );
88
-
89
- nrf_gpio_cfg_input (scl_pin , NRF_GPIO_PIN_NOPULL );
90
- nrf_gpio_cfg_input (sda_pin , NRF_GPIO_PIN_NOPULL );
91
-
92
- // We must pull up within 3us to achieve 400khz.
93
- 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 ;
107
-
108
- if (!_bus_is_sane (self -> scl_pin_number , self -> sda_pin_number )) {
109
- // bus not in a sane state - try to recover
110
- recover_result = nrfx_twim_bus_recover (self -> scl_pin_number , self -> sda_pin_number );
111
- if (NRFX_SUCCESS != recover_result ) {
112
- // return error message if unable to recover the bus
113
- return recover_result ;
114
- }
115
- }
116
-
117
- nrfx_twim_enable (& self -> twim_peripheral -> twim );
118
- return NRFX_SUCCESS ;
119
- }
120
-
121
82
static uint8_t twi_error_to_mp (const nrfx_err_t err ) {
122
83
switch (err ) {
123
84
case NRFX_ERROR_DRV_TWI_ERR_ANACK :
124
85
return MP_ENODEV ;
125
86
case NRFX_ERROR_BUSY :
126
87
return MP_EBUSY ;
127
- case NRFX_SUCCESS :
128
- return 0 ;
129
88
case NRFX_ERROR_DRV_TWI_ERR_DNACK :
130
89
case NRFX_ERROR_INVALID_ADDR :
131
- case NRFX_ERROR_INTERNAL :
132
- default :
133
90
return MP_EIO ;
91
+ default :
92
+ break ;
134
93
}
94
+
95
+ return 0 ;
135
96
}
136
97
137
98
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 ) {
@@ -153,12 +114,25 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
153
114
mp_raise_ValueError (translate ("All I2C peripherals are in use" ));
154
115
}
155
116
156
- // check bus is in a sane state
157
- if (!_bus_is_sane (scl -> number ,sda -> number )) {
117
+ #if CIRCUITPY_REQUIRE_I2C_PULLUPS
118
+ // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
119
+ nrf_gpio_cfg_input (scl -> number , NRF_GPIO_PIN_PULLDOWN );
120
+ nrf_gpio_cfg_input (sda -> number , NRF_GPIO_PIN_PULLDOWN );
121
+
122
+ common_hal_mcu_delay_us (10 );
123
+
124
+ nrf_gpio_cfg_input (scl -> number , NRF_GPIO_PIN_NOPULL );
125
+ nrf_gpio_cfg_input (sda -> number , NRF_GPIO_PIN_NOPULL );
126
+
127
+ // We must pull up within 3us to achieve 400khz.
128
+ common_hal_mcu_delay_us (3 );
129
+
130
+ if (!nrf_gpio_pin_read (sda -> number ) || !nrf_gpio_pin_read (scl -> number )) {
158
131
reset_pin_number (sda -> number );
159
132
reset_pin_number (scl -> number );
160
133
mp_raise_RuntimeError (translate ("No pull up found on SDA or SCL; check your wiring" ));
161
134
}
135
+ #endif
162
136
163
137
nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG (scl -> number , sda -> number );
164
138
@@ -214,9 +188,7 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
214
188
NRF_TWIM_Type * reg = self -> twim_peripheral -> twim .p_twim ;
215
189
bool found = true;
216
190
217
- if (NRFX_SUCCESS != _safe_twim_enable (self )) {
218
- return false;
219
- }
191
+ nrfx_twim_enable (& self -> twim_peripheral -> twim );
220
192
221
193
nrf_twim_address_set (reg , addr );
222
194
nrf_twim_tx_buffer_set (reg , NULL , 0 );
@@ -274,10 +246,7 @@ STATIC uint8_t _common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
274
246
275
247
nrfx_err_t err = NRFX_SUCCESS ;
276
248
277
- err = _safe_twim_enable (self );
278
- if (NRFX_SUCCESS != err ) {
279
- return twi_error_to_mp (err );
280
- }
249
+ nrfx_twim_enable (& self -> twim_peripheral -> twim );
281
250
282
251
// break into MAX_XFER_LEN transaction
283
252
while (len ) {
@@ -309,10 +278,7 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t
309
278
310
279
nrfx_err_t err = NRFX_SUCCESS ;
311
280
312
- err = _safe_twim_enable (self );
313
- if (NRFX_SUCCESS != err ) {
314
- return twi_error_to_mp (err );
315
- }
281
+ nrfx_twim_enable (& self -> twim_peripheral -> twim );
316
282
317
283
// break into MAX_XFER_LEN transaction
318
284
while (len ) {
0 commit comments