@@ -136,13 +136,19 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
136136 self -> scl_pin = NULL ;
137137}
138138
139- bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
139+ static esp_err_t i2c_zero_length_write (busio_i2c_obj_t * self , uint8_t addr , TickType_t timeout ) {
140+ // i2c_master_write_to_device() won't do zero-length writes, so we do it by hand.
140141 i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
141142 i2c_master_start (cmd );
142143 i2c_master_write_byte (cmd , addr << 1 , true);
143144 i2c_master_stop (cmd );
144- esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , 10 );
145+ esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , timeout );
145146 i2c_cmd_link_delete (cmd );
147+ return result ;
148+ }
149+
150+ bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
151+ esp_err_t result = i2c_zero_length_write (self , addr , 1 );
146152 return result == ESP_OK ;
147153}
148154
@@ -163,45 +169,36 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
163169 self -> has_lock = false;
164170}
165171
166- uint8_t common_hal_busio_i2c_write (busio_i2c_obj_t * self , uint16_t addr ,
167- const uint8_t * data , size_t len , bool transmit_stop_bit ) {
168- i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
169- i2c_master_start (cmd );
170- i2c_master_write_byte (cmd , addr << 1 , true);
171- i2c_master_write (cmd , (uint8_t * )data , len , true);
172- if (transmit_stop_bit ) {
173- i2c_master_stop (cmd );
172+ static uint8_t convert_esp_err (esp_err_t result ) {
173+ switch (result ) {
174+ case ESP_OK :
175+ return 0 ;
176+ case ESP_FAIL :
177+ return MP_ENODEV ;
178+ case ESP_ERR_TIMEOUT :
179+ return MP_ETIMEDOUT ;
180+ default :
181+ return MP_EIO ;
174182 }
175- esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , 100 /* wait in ticks */ );
176- i2c_cmd_link_delete (cmd );
183+ }
177184
178- if (result == ESP_OK ) {
179- return 0 ;
180- } else if (result == ESP_FAIL ) {
181- return MP_ENODEV ;
182- }
183- return MP_EIO ;
185+ uint8_t common_hal_busio_i2c_write (busio_i2c_obj_t * self , uint16_t addr , const uint8_t * data , size_t len ) {
186+ return convert_esp_err (len == 0
187+ ? i2c_zero_length_write (self , addr , 100 )
188+ : i2c_master_write_to_device (self -> i2c_num , (uint8_t )addr , data , len , 100 /* wait in ticks */ )
189+ );
184190}
185191
186- uint8_t common_hal_busio_i2c_read (busio_i2c_obj_t * self , uint16_t addr ,
187- uint8_t * data , size_t len ) {
188- i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
189- i2c_master_start (cmd );
190- i2c_master_write_byte (cmd , addr << 1 | 1 , true); // | 1 to indicate read
191- if (len > 1 ) {
192- i2c_master_read (cmd , data , len - 1 , 0 );
193- }
194- i2c_master_read_byte (cmd , data + len - 1 , 1 );
195- i2c_master_stop (cmd );
196- esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , 100 /* wait in ticks */ );
197- i2c_cmd_link_delete (cmd );
192+ uint8_t common_hal_busio_i2c_read (busio_i2c_obj_t * self , uint16_t addr , uint8_t * data , size_t len ) {
193+ return convert_esp_err (
194+ i2c_master_read_from_device (self -> i2c_num , (uint8_t )addr , data , len , 100 /* wait in ticks */ ));
195+ }
198196
199- if (result == ESP_OK ) {
200- return 0 ;
201- } else if (result == ESP_FAIL ) {
202- return MP_ENODEV ;
203- }
204- return MP_EIO ;
197+ uint8_t common_hal_busio_i2c_write_read (busio_i2c_obj_t * self , uint16_t addr ,
198+ uint8_t * out_data , size_t out_len , uint8_t * in_data , size_t in_len ) {
199+ return convert_esp_err (
200+ i2c_master_write_read_device (self -> i2c_num , (uint8_t )addr ,
201+ out_data , out_len , in_data , in_len , 100 /* wait in ticks */ ));
205202}
206203
207204void common_hal_busio_i2c_never_reset (busio_i2c_obj_t * self ) {
0 commit comments