@@ -180,43 +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- mp_uint_t length ;
183+ static uint8_t * get_buffer_data (mp_obj_t transmit_data , mp_uint_t * length )
184+ {
185185 uint8_t * data_out = NULL ;
186186 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 {
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 {
196196 mp_raise_ValueError ("Expected a bytestring like object" );
197- return mp_const_none ;
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 ;
197+ }
198+ return data_out ;
199+ }
200+
201+ static mp_obj_t driver_ice40_transaction (mp_obj_t transmit_data ) {
202+ mp_uint_t length ;
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+ }
211+
212+ esp_err_t res = ice40_transaction (& ice40 , data_out , length , data_in , length );
213+
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 );
221+ heap_caps_free (data_in );
222+ return data_in_obj ;
223+ }
224+ return mp_const_none ;
220225}
221226
222227static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_transaction_obj , driver_ice40_transaction ) ;
@@ -246,61 +251,31 @@ static MP_DEFINE_CONST_FUN_OBJ_1(ice40_receive_obj, driver_ice40_receive);
246251
247252static mp_obj_t driver_ice40_send (mp_obj_t transmit_data ) {
248253 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 {
260- mp_raise_ValueError ("Expected a bytestring like object" );
261- return mp_const_none ;
262- }
263-
264- if (data_out != NULL ) {
265- esp_err_t res = ice40_send (& ice40 , data_out , length );
254+ uint8_t * data_out = get_buffer_data (transmit_data , & length );
266255
267- if (res != ESP_OK ) {
268- mp_raise_ValueError ("Failed to execute transaction" );
269- return mp_const_none ;
270- }
271- }
256+ if (data_out != NULL ) {
257+ esp_err_t res = ice40_send (& ice40 , data_out , length );
272258
259+ if (res != ESP_OK ) {
260+ mp_raise_ValueError ("Failed to execute transaction" );
261+ }
262+ }
273263 return mp_const_none ;
274264}
275265
276266static MP_DEFINE_CONST_FUN_OBJ_1 (ice40_send_obj , driver_ice40_send ) ;
277267
278268static mp_obj_t driver_ice40_send_turbo (mp_obj_t transmit_data ) {
279269 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 {
291- mp_raise_ValueError ("Expected a bytestring like object" );
292- return mp_const_none ;
293- }
270+ uint8_t * data_out = get_buffer_data (transmit_data , & length );
294271
295- if (data_out != NULL ) {
296- esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
297-
298- if (res != ESP_OK ) {
299- mp_raise_ValueError ("Failed to execute transaction" );
300- return mp_const_none ;
301- }
302- }
272+ if (data_out != NULL ) {
273+ esp_err_t res = ice40_send_turbo (& ice40 , data_out , length );
303274
275+ if (res != ESP_OK ) {
276+ mp_raise_ValueError ("Failed to execute transaction" );
277+ }
278+ }
304279 return mp_const_none ;
305280}
306281
0 commit comments