@@ -181,30 +181,42 @@ static mp_obj_t driver_ice40_disable() {
181181static MP_DEFINE_CONST_FUN_OBJ_0 (ice40_disable_obj , driver_ice40_disable ) ;
182182
183183static mp_obj_t driver_ice40_transaction (mp_obj_t transmit_data ) {
184- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
185- mp_raise_ValueError ("Expected a bytestring like object" );
186- return mp_const_none ;
187- }
188-
189184 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- }
196-
197- esp_err_t res = ice40_transaction ( & ice40 , data_out , length , data_in , length ) ;
198-
199- if ( res != ESP_OK ) {
200- heap_caps_free ( data_in );
201- mp_raise_ValueError ("Failed to execute transaction " );
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 {
196+ mp_raise_ValueError ("Expected a bytestring like object " );
202197 return mp_const_none ;
203- }
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 ;
198+ }
199+
200+ if (data_out != NULL ) {
201+ uint8_t * data_in = heap_caps_malloc (length + 4 , MALLOC_CAP_DMA );
202+ if (data_in == NULL ) {
203+ mp_raise_ValueError ("Out of memory" );
204+ return mp_const_none ;
205+ }
206+
207+ esp_err_t res = ice40_transaction (& ice40 , data_out , length , data_in , length );
208+
209+ if (res != ESP_OK ) {
210+ heap_caps_free (data_in );
211+ mp_raise_ValueError ("Failed to execute transaction" );
212+ return mp_const_none ;
213+ }
214+
215+ mp_obj_t data_in_obj = mp_obj_new_bytes (data_in , length );
216+ heap_caps_free (data_in );
217+ return data_in_obj ;
218+ }
219+ return mp_const_none ;
208220}
209221
210222static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_transaction_obj , driver_ice40_transaction ) ;
@@ -233,41 +245,61 @@ static mp_obj_t driver_ice40_receive(mp_obj_t length_obj) {
233245static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_receive_obj , driver_ice40_receive ) ;
234246
235247static mp_obj_t driver_ice40_send (mp_obj_t transmit_data ) {
236- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
248+ mp_uint_t length ;
249+ uint8_t * data_out = NULL ;
250+ if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
251+ data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
252+ } else if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytearray )) {
253+ mp_buffer_info_t bufinfo ;
254+
255+ if (mp_get_buffer (transmit_data , & bufinfo , MP_BUFFER_READ )) {
256+ data_out = (uint8_t * ) bufinfo .buf ;
257+ length = (mp_uint_t ) bufinfo .len ;
258+ }
259+ } else {
237260 mp_raise_ValueError ("Expected a bytestring like object" );
238261 return mp_const_none ;
239- }
240-
241- mp_uint_t length ;
242- uint8_t * data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
262+ }
243263
244- esp_err_t res = ice40_send (& ice40 , data_out , length );
264+ if (data_out != NULL ) {
265+ esp_err_t res = ice40_send (& ice40 , data_out , length );
245266
246- if (res != ESP_OK ) {
247- mp_raise_ValueError ("Failed to execute transaction" );
248- return mp_const_none ;
249- }
267+ if (res != ESP_OK ) {
268+ mp_raise_ValueError ("Failed to execute transaction" );
269+ return mp_const_none ;
270+ }
271+ }
250272
251273 return mp_const_none ;
252274}
253275
254276static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_send_obj , driver_ice40_send ) ;
255277
256278static mp_obj_t driver_ice40_send_turbo (mp_obj_t transmit_data ) {
257- if (!MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
279+ mp_uint_t length ;
280+ uint8_t * data_out = NULL ;
281+ if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytes )) {
282+ data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
283+ } else if (MP_OBJ_IS_TYPE (transmit_data , & mp_type_bytearray )) {
284+ mp_buffer_info_t bufinfo ;
285+
286+ if (mp_get_buffer (transmit_data , & bufinfo , MP_BUFFER_READ )) {
287+ data_out = (uint8_t * ) bufinfo .buf ;
288+ length = (mp_uint_t ) bufinfo .len ;
289+ }
290+ } else {
258291 mp_raise_ValueError ("Expected a bytestring like object" );
259292 return mp_const_none ;
260- }
261-
262- mp_uint_t length ;
263- uint8_t * data_out = (uint8_t * ) mp_obj_str_get_data (transmit_data , & length );
293+ }
264294
265- esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
295+ if (data_out != NULL ) {
296+ esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
266297
267- if (res != ESP_OK ) {
268- mp_raise_ValueError ("Failed to execute transaction" );
269- return mp_const_none ;
270- }
298+ if (res != ESP_OK ) {
299+ mp_raise_ValueError ("Failed to execute transaction" );
300+ return mp_const_none ;
301+ }
302+ }
271303
272304 return mp_const_none ;
273305}
0 commit comments