Skip to content

Commit d1255a5

Browse files
authored
Merge pull request adafruit#10887 from dhalbert/fix-sdcard-spi-share
USB SDCard handling improvements and related changes
2 parents 8ad3a2f + c6b713a commit d1255a5

File tree

21 files changed

+220
-115
lines changed

21 files changed

+220
-115
lines changed

ports/espressif/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include "freertos/task.h"
1414

1515
void port_background_tick(void) {
16-
// Zero delay in case FreeRTOS wants to switch to something else.
17-
vTaskDelay(0);
16+
// Yield with zero delay in case FreeRTOS wants to switch to something else.
17+
port_task_yield();
1818
}
1919

2020
void port_background_task(void) {

ports/espressif/common-hal/busio/SPI.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
7272
mp_raise_ValueError(MP_ERROR_TEXT("All SPI peripherals are in use"));
7373
}
7474

75-
self->mutex = xSemaphoreCreateMutex();
76-
if (self->mutex == NULL) {
77-
mp_raise_RuntimeError(MP_ERROR_TEXT("Unable to create lock"));
78-
}
79-
8075
esp_err_t result = spi_bus_initialize(self->host_id, &bus_config, SPI_DMA_CH_AUTO);
8176
if (result == ESP_ERR_NO_MEM) {
8277
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("ESP-IDF memory allocation failed"));
@@ -162,11 +157,17 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
162157
return true;
163158
}
164159

165-
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
160+
// Wait as long as needed for the lock. This is used by SD card access from USB.
161+
// Overrides the default busy-wait implementation in shared-bindings/busio/SPI.c
162+
bool common_hal_busio_spi_wait_for_lock(busio_spi_obj_t *self, uint32_t timeout_ms) {
166163
if (common_hal_busio_spi_deinited(self)) {
167164
return false;
168165
}
169-
return xSemaphoreTake(self->mutex, 1) == pdTRUE;
166+
return xSemaphoreTake(self->mutex, pdMS_TO_TICKS(timeout_ms)) == pdTRUE;
167+
}
168+
169+
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
170+
return common_hal_busio_spi_wait_for_lock(self, 0);
170171
}
171172

172173
bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {

ports/espressif/supervisor/port.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ safe_mode_t port_init(void) {
251251
#define pin_GPIOn(n) pin_GPIO##n
252252
#define pin_GPIOn_EXPAND(x) pin_GPIOn(x)
253253

254-
#ifdef CONFIG_CONSOLE_UART_TX_GPIO
255-
common_hal_never_reset_pin(&pin_GPIOn_EXPAND(CONFIG_CONSOLE_UART_TX_GPIO));
254+
#ifdef CONFIG_ESP_CONSOLE_UART_TX_GPIO
255+
common_hal_never_reset_pin(&pin_GPIOn_EXPAND(CONFIG_ESP_CONSOLE_UART_TX_GPIO));
256256
#endif
257257

258-
#ifdef CONFIG_CONSOLE_UART_RX_GPIO
259-
common_hal_never_reset_pin(&pin_GPIOn_EXPAND(CONFIG_CONSOLE_UART_RX_GPIO));
258+
#ifdef CONFIG_ESP_CONSOLE_UART_RX_GPIO
259+
common_hal_never_reset_pin(&pin_GPIOn_EXPAND(CONFIG_ESP_CONSOLE_UART_RX_GPIO));
260260
#endif
261261

262262
#ifndef ENABLE_JTAG
@@ -404,8 +404,8 @@ void reset_port(void) {
404404
watchdog_reset();
405405
#endif
406406

407-
// Yield so the idle task can run and do any IDF cleanup needed.
408-
port_yield();
407+
// Yield so the idle task, at priority 0, can run and do any IDF cleanup needed.
408+
port_task_sleep_ms(4);
409409
}
410410

411411
void reset_to_bootloader(void) {
@@ -483,8 +483,13 @@ void port_wake_main_task_from_isr(void) {
483483
}
484484
}
485485

486-
void port_yield(void) {
487-
vTaskDelay(4);
486+
// Yield to other tasks at the same priority.
487+
void port_task_yield(void) {
488+
vTaskDelay(0);
489+
}
490+
491+
void port_task_sleep_ms(uint32_t msecs) {
492+
vTaskDelay(pdMS_TO_TICKS(msecs));
488493
}
489494

490495
void sleep_timer_cb(void *arg) {

ports/espressif/supervisor/usb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static void usb_device_task(void *param) {
5656
tud_task();
5757
tud_cdc_write_flush();
5858
}
59-
vTaskDelay(1);
59+
// Yield with zero delay to switch to any other tasks at same priority.
60+
port_task_yield();
6061
}
6162
}
6263
#endif // CIRCUITPY_USB_DEVICE
@@ -112,7 +113,7 @@ void init_usb_hardware(void) {
112113
"usbd",
113114
USBD_STACK_SIZE,
114115
NULL,
115-
5,
116+
1,
116117
usb_device_stack,
117118
&usb_device_taskdef,
118119
xPortGetCoreID());

ports/raspberrypi/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ __attribute__((used)) void __not_in_flash_func(isr_hardfault)(void) {
600600
}
601601
}
602602

603-
void port_yield(void) {
603+
void port_task_yield(void) {
604604
#if CIRCUITPY_CYW43
605605
cyw43_arch_poll();
606606
#endif

ports/renode/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ __attribute__((used)) void HardFault_Handler(void) {
210210
}
211211
}
212212

213-
void port_yield(void) {
213+
void port_task_yield(void) {
214214
}
215215

216216
void port_boot_info(void) {

ports/zephyr-cp/supervisor/port.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ void port_wake_main_task_from_isr(void) {
170170
k_event_set(&main_needed, 1);
171171
}
172172

173-
void port_yield(void) {
173+
void port_task_yield(void) {
174174
k_yield();
175175
}
176176

177+
void port_task_sleep_ms(uint32_t msecs) {
178+
k_msleep(msecs);
179+
}
180+
177181
void port_boot_info(void) {
178182
}
179183

py/circuitpy_defns.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ endif
201201
ifeq ($(CIRCUITPY_DISPLAYIO),1)
202202
SRC_PATTERNS += displayio/%
203203
endif
204+
ifeq ($(CIRCUITPY_DOTCLOCKFRAMEBUFFER),1)
205+
SRC_PATTERNS += dotclockframebuffer/%
206+
endif
204207
ifeq ($(CIRCUITPY_DUALBANK),1)
205208
SRC_PATTERNS += dualbank/%
206209
endif
@@ -228,9 +231,6 @@ endif
228231
ifeq ($(CIRCUITPY_FOURWIRE),1)
229232
SRC_PATTERNS += fourwire/%
230233
endif
231-
ifeq ($(CIRCUITPY_QSPIBUS),1)
232-
SRC_PATTERNS += qspibus/%
233-
endif
234234
ifeq ($(CIRCUITPY_FRAMEBUFFERIO),1)
235235
SRC_PATTERNS += framebufferio/%
236236
endif
@@ -357,8 +357,8 @@ endif
357357
ifeq ($(CIRCUITPY_RGBMATRIX),1)
358358
SRC_PATTERNS += rgbmatrix/%
359359
endif
360-
ifeq ($(CIRCUITPY_DOTCLOCKFRAMEBUFFER),1)
361-
SRC_PATTERNS += dotclockframebuffer/%
360+
ifeq ($(CIRCUITPY_QSPIBUS),1)
361+
SRC_PATTERNS += qspibus/%
362362
endif
363363
ifeq ($(CIRCUITPY_RP2PIO),1)
364364
SRC_PATTERNS += rp2pio/%

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,12 @@ typedef double mp_float_t;
22052205
#define MP_INLINE inline MP_NO_INSTRUMENT
22062206
#endif
22072207

2208+
// CIRCUITPY-CHANGE
2209+
// Modifier for functions whose return value should not be ignored
2210+
#ifndef MP_WARN_UNUSED_RESULT
2211+
#define MP_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
2212+
#endif
2213+
22082214
// Modifier for functions which should be never inlined
22092215
#ifndef MP_NOINLINE
22102216
#define MP_NOINLINE __attribute__((noinline))

shared-bindings/busdisplay/BusDisplay.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
//| :param int native_frames_per_second: Number of display refreshes per second that occur with the given init_sequence.
123123
//| :param bool backlight_on_high: If True, pulling the backlight pin high turns the backlight on.
124124
//| :param bool SH1107_addressing: Special quirk for SH1107, use upper/lower column set and page set
125-
//| :param int set_vertical_scroll: This parameter is accepted but ignored for backwards compatibility. It will be removed in a future release.
126125
//| :param int backlight_pwm_frequency: The frequency to use to drive the PWM for backlight brightness control. Default is 50000.
127126
//| """
128127
//| ...
@@ -133,7 +132,7 @@ static mp_obj_t busdisplay_busdisplay_make_new(const mp_obj_type_t *type, size_t
133132
ARG_rotation, ARG_color_depth, ARG_grayscale, ARG_pixels_in_byte_share_row,
134133
ARG_bytes_per_cell, ARG_reverse_pixels_in_byte, ARG_reverse_bytes_in_word,
135134
ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command,
136-
ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness_command,
135+
ARG_backlight_pin, ARG_brightness_command,
137136
ARG_brightness, ARG_single_byte_bounds, ARG_data_as_commands,
138137
ARG_auto_refresh, ARG_native_frames_per_second, ARG_backlight_on_high,
139138
ARG_SH1107_addressing, ARG_backlight_pwm_frequency };
@@ -154,7 +153,6 @@ static mp_obj_t busdisplay_busdisplay_make_new(const mp_obj_type_t *type, size_t
154153
{ MP_QSTR_set_column_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2a} },
155154
{ MP_QSTR_set_row_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2b} },
156155
{ MP_QSTR_write_ram_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2c} },
157-
{ MP_QSTR_set_vertical_scroll, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x0} },
158156
{ MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
159157
{ MP_QSTR_brightness_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_BRIGHTNESS_COMMAND} },
160158
{ MP_QSTR_brightness, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },

0 commit comments

Comments
 (0)