Skip to content

Commit

Permalink
Fix FreeRTOS weirdness on Funhouse
Browse files Browse the repository at this point in the history
Funhouse has DotStars attached to SPI that exercises this bug.
Most boards are NeoPixel and avoid this.

Fixes #9486
  • Loading branch information
tannewt committed Aug 21, 2024
1 parent 5e4cecd commit b284815
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ports/espressif/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {

// Wait for any other users of this to finish.
while (!common_hal_busio_spi_try_lock(self)) {
RUN_BACKGROUND_TASKS;
}

// Mark us as deinit early in case we are used in an interrupt.
Expand All @@ -139,7 +140,10 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
spi_bus_remove_device(spi_handle[self->host_id]);
spi_bus_free(self->host_id);

// Release the mutex before we delete it. Otherwise FreeRTOS gets unhappy.
xSemaphoreGive(self->mutex);
vSemaphoreDelete(self->mutex);
self->mutex = NULL;

common_hal_reset_pin(self->MOSI);
common_hal_reset_pin(self->MISO);
Expand All @@ -166,7 +170,7 @@ bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
}

bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
return xSemaphoreGetMutexHolder(self->mutex) == xTaskGetCurrentTaskHandle();
return self->mutex != NULL && xSemaphoreGetMutexHolder(self->mutex) == xTaskGetCurrentTaskHandle();
}

void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
Expand Down

0 comments on commit b284815

Please sign in to comment.