@@ -180,31 +180,48 @@ static mp_obj_t driver_ice40_disable() {
180180
181181static MP_DEFINE_CONST_FUN_OBJ_0 (ice40_disable_obj , driver_ice40_disable ) ;
182182
183- static mp_obj_t driver_ice40_transaction (mp_obj_t transmit_data ) {
184- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
183+ static uint8_t * get_buffer_data (mp_obj_t transmit_data , mp_uint_t * length )
184+ {
185+ uint8_t * data_out = NULL ;
186+ if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
187+ data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , length );
188+ } else if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytearray )) {
189+ mp_buffer_info_t bufinfo ;
190+
191+ if (mp_get_buffer (transmit_data , & bufinfo , MP_BUFFER_READ )) {
192+ data_out = (uint8_t * ) bufinfo .buf ;
193+ * length = (mp_uint_t ) bufinfo .len ;
194+ }
195+ } else {
185196 mp_raise_ValueError ("Expected a bytestring like object" );
186- return mp_const_none ;
187197 }
198+ return data_out ;
199+ }
188200
201+ static mp_obj_t driver_ice40_transaction (mp_obj_t transmit_data ) {
189202 mp_uint_t length ;
190- uint8_t * data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
191- uint8_t * data_in = heap_caps_malloc (length + 4 , MALLOC_CAP_DMA );
192- if (data_in == NULL ) {
193- mp_raise_ValueError ("Out of memory" );
194- return mp_const_none ;
195- }
203+ uint8_t * data_out = get_buffer_data (transmit_data , & length );
204+
205+ if (data_out != NULL ) {
206+ uint8_t * data_in = heap_caps_malloc (length + 4 , MALLOC_CAP_DMA );
207+ if (data_in == NULL ) {
208+ mp_raise_ValueError ("Out of memory" );
209+ return mp_const_none ;
210+ }
196211
197- esp_err_t res = ice40_transaction (& ice40 , data_out , length , data_in , length );
212+ esp_err_t res = ice40_transaction (& ice40 , data_out , length , data_in , length );
198213
199- if (res != ESP_OK ) {
214+ if (res != ESP_OK ) {
215+ heap_caps_free (data_in );
216+ mp_raise_ValueError ("Failed to execute transaction" );
217+ return mp_const_none ;
218+ }
219+
220+ mp_obj_t data_in_obj = mp_obj_new_bytes (data_in , length );
200221 heap_caps_free (data_in );
201- mp_raise_ValueError ("Failed to execute transaction" );
202- return mp_const_none ;
222+ return data_in_obj ;
203223 }
204-
205- mp_obj_t data_in_obj = mp_obj_new_bytes (data_in , length );
206- heap_caps_free (data_in );
207- return data_in_obj ;
224+ return mp_const_none ;
208225}
209226
210227static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_transaction_obj , driver_ice40_transaction ) ;
@@ -233,42 +250,32 @@ static mp_obj_t driver_ice40_receive(mp_obj_t length_obj) {
233250static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_receive_obj , driver_ice40_receive ) ;
234251
235252static mp_obj_t driver_ice40_send (mp_obj_t transmit_data ) {
236- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
237- mp_raise_ValueError ("Expected a bytestring like object" );
238- return mp_const_none ;
239- }
240-
241253 mp_uint_t length ;
242- uint8_t * data_out = ( uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
254+ uint8_t * data_out = get_buffer_data (transmit_data , & length );
243255
244- esp_err_t res = ice40_send (& ice40 , data_out , length );
256+ if (data_out != NULL ) {
257+ esp_err_t res = ice40_send (& ice40 , data_out , length );
245258
246- if (res != ESP_OK ) {
247- mp_raise_ValueError ("Failed to execute transaction" );
248- return mp_const_none ;
259+ if (res != ESP_OK ) {
260+ mp_raise_ValueError ("Failed to execute transaction" );
261+ }
249262 }
250-
251263 return mp_const_none ;
252264}
253265
254266static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_send_obj , driver_ice40_send ) ;
255267
256268static mp_obj_t driver_ice40_send_turbo (mp_obj_t transmit_data ) {
257- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
258- mp_raise_ValueError ("Expected a bytestring like object" );
259- return mp_const_none ;
260- }
261-
262269 mp_uint_t length ;
263- uint8_t * data_out = ( uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
270+ uint8_t * data_out = get_buffer_data (transmit_data , & length );
264271
265- esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
272+ if (data_out != NULL ) {
273+ esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
266274
267- if (res != ESP_OK ) {
268- mp_raise_ValueError ("Failed to execute transaction" );
269- return mp_const_none ;
275+ if (res != ESP_OK ) {
276+ mp_raise_ValueError ("Failed to execute transaction" );
277+ }
270278 }
271-
272279 return mp_const_none ;
273280}
274281
0 commit comments