@@ -139,9 +139,6 @@ static void qspibus_send_color_bytes(
139139 qspibus_send_command_bytes (self , command , NULL , 0 );
140140 return ;
141141 }
142- if (self -> dma_buffer_size == 0 ) {
143- mp_raise_OSError_msg (MP_ERROR_TEXT ("Could not allocate DMA capable buffer" ));
144- }
145142
146143 // RAMWR must transition to RAMWRC for continued payload chunks.
147144 uint8_t chunk_command = command ;
@@ -290,8 +287,7 @@ void common_hal_qspibus_qspibus_construct(
290287 }
291288
292289 if (!qspibus_allocate_dma_buffers (self )) {
293- vSemaphoreDelete (self -> transfer_done_sem );
294- self -> transfer_done_sem = NULL ;
290+ common_hal_qspibus_qspibus_deinit (self );
295291 mp_raise_msg (& mp_type_MemoryError , MP_ERROR_TEXT ("Could not allocate DMA capable buffer" ));
296292 }
297293
@@ -307,12 +303,13 @@ void common_hal_qspibus_qspibus_construct(
307303
308304 esp_err_t err = spi_bus_initialize (self -> host_id , & bus_config , SPI_DMA_CH_AUTO );
309305 if (err != ESP_OK ) {
310- qspibus_release_dma_buffers (self );
311- vSemaphoreDelete (self -> transfer_done_sem );
312- self -> transfer_done_sem = NULL ;
306+ common_hal_qspibus_qspibus_deinit (self );
313307 mp_raise_ValueError_varg (MP_ERROR_TEXT ("%q in use" ), MP_QSTR_SPI );
314308 }
315309
310+ // Mark bus as initialized so deinit knows to call spi_bus_free().
311+ self -> bus_initialized = true;
312+
316313 const esp_lcd_panel_io_spi_config_t io_config = {
317314 .cs_gpio_num = self -> cs_pin ,
318315 .dc_gpio_num = -1 ,
@@ -330,10 +327,7 @@ void common_hal_qspibus_qspibus_construct(
330327
331328 err = esp_lcd_new_panel_io_spi ((esp_lcd_spi_bus_handle_t )self -> host_id , & io_config , & self -> io_handle );
332329 if (err != ESP_OK ) {
333- spi_bus_free (self -> host_id );
334- qspibus_release_dma_buffers (self );
335- vSemaphoreDelete (self -> transfer_done_sem );
336- self -> transfer_done_sem = NULL ;
330+ common_hal_qspibus_qspibus_deinit (self );
337331 mp_raise_OSError_msg_varg (MP_ERROR_TEXT ("%q failure: %d" ), MP_QSTR_QSPI , (int )err );
338332 }
339333
@@ -358,26 +352,22 @@ void common_hal_qspibus_qspibus_construct(
358352 gpio_set_level ((gpio_num_t )self -> reset_pin , 1 );
359353 vTaskDelay (pdMS_TO_TICKS (120 ));
360354 }
361-
362- self -> bus_initialized = true;
363355}
364356
365357void common_hal_qspibus_qspibus_deinit (qspibus_qspibus_obj_t * self ) {
366- if (!self -> bus_initialized ) {
367- qspibus_release_dma_buffers (self );
368- return ;
369- }
358+ if (self -> bus_initialized ) {
359+ qspibus_panel_sleep_best_effort (self );
360+ self -> in_transaction = false;
370361
371- qspibus_panel_sleep_best_effort (self );
372- self -> in_transaction = false;
362+ if (self -> io_handle != NULL ) {
363+ esp_lcd_panel_io_del (self -> io_handle );
364+ self -> io_handle = NULL ;
365+ }
373366
374- if (self -> io_handle != NULL ) {
375- esp_lcd_panel_io_del (self -> io_handle );
376- self -> io_handle = NULL ;
367+ spi_bus_free (self -> host_id );
368+ self -> bus_initialized = false;
377369 }
378370
379- spi_bus_free (self -> host_id );
380-
381371 if (self -> transfer_done_sem != NULL ) {
382372 // Set NULL before delete so late ISR callbacks (if any) see NULL and skip.
383373 SemaphoreHandle_t sem = self -> transfer_done_sem ;
0 commit comments