Skip to content

Commit bfb80c2

Browse files
authored
Merge pull request #10645 from dhalbert/sd-card-usb-presentation-fixes
Sd card usb presentation fixes
2 parents 9fd7df7 + edd4532 commit bfb80c2

File tree

15 files changed

+67
-22
lines changed

15 files changed

+67
-22
lines changed

docs/workflows.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ A few boards have SD card automounting. (This is based on the ``DEFAULT_SD`` set
4444
``mpconfigboard.h``.) The card is writable from CircuitPython by default and read-only to the host.
4545
`storage.remount()` can be used to remount the drive to the host as read-write.
4646

47+
On most other boards, except for ``atmel-samd`` boards, an SD card mounted in user code
48+
at ``/sd`` will become visible after a few seconds on the attached host computer, as an
49+
additional drive besides CIRCUITPY and (if present) CPSAVES. It will present with the volume
50+
label on the SD card. Depending on the host operating system settings, the drive may or may not be
51+
auto-mounted on the host. Host writes to drives mounted by user code will not trigger a reload.
52+
4753
### CDC serial
4854
CircuitPython exposes one CDC USB interface for CircuitPython serial. This is a standard serial
4955
USB interface.

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static void stop_mp(void) {
214214
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
215215

216216
// Unmount all heap allocated vfs mounts.
217-
while (gc_nbytes(vfs) > 0) {
217+
while (gc_ptr_on_heap(vfs)) {
218218
vfs = vfs->next;
219219
}
220220
MP_STATE_VM(vfs_mount_table) = vfs;

ports/atmel-samd/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
// Definitions that control circuitpy_mpconfig.h:
1313

14+
// On SAMD, presenting the SD card as a second LUN causes USB disconnect. This needs to be fixed eventually.
15+
#define CIRCUITPY_SDCARD_USB (0)
16+
1417
////////////////////////////////////////////////////////////////////////////////////////////////////
1518

1619
#ifdef SAMD21

ports/cxd56/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#define MICROPY_PY_SYS_PLATFORM "CXD56"
1010

11+
// SD card socket on board is configured for sdioio, which is not supported for automatic USB presentation.
12+
#define CIRCUITPY_SDCARD_USB (0)
13+
1114
// 64kiB stack
1215
#define CIRCUITPY_DEFAULT_STACK_SIZE (0x10000)
1316

ports/espressif/common-hal/_bleio/Characteristic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self) {
120120
return;
121121
}
122122
if (self->current_value != NULL) {
123-
if (gc_nbytes(self->current_value) > 0) {
123+
if (gc_ptr_on_heap(self->current_value)) {
124124
m_free(self->current_value);
125125
} else {
126126
port_free(self->current_value);

ports/nordic/common-hal/_bleio/Characteristic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
8080
// to allocate.
8181
self->initial_value_len = initial_value_bufinfo->len;
8282
if (gc_alloc_possible()) {
83-
if (gc_nbytes(initial_value_bufinfo->buf) > 0) {
83+
if (gc_ptr_on_heap(initial_value_bufinfo->buf)) {
8484
uint8_t *initial_value = m_malloc_without_collect(self->initial_value_len);
8585
memcpy(initial_value, initial_value_bufinfo->buf, self->initial_value_len);
8686
self->initial_value = initial_value;

ports/nordic/common-hal/_bleio/Service.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self,
131131
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&user_desc_md.read_perm);
132132
// If the description is on the Python heap, then have the SD copy it. If not, assume it's
133133
// static and will live for longer than the SD.
134-
user_desc_md.vloc = gc_nbytes(user_description) > 0 ? BLE_GATTS_VLOC_STACK : BLE_GATTS_VLOC_USER;
134+
user_desc_md.vloc = gc_ptr_on_heap(user_description) ? BLE_GATTS_VLOC_STACK : BLE_GATTS_VLOC_USER;
135135
char_md.p_user_desc_md = &user_desc_md;
136136
char_md.p_char_user_desc = (const uint8_t *)user_description;
137137
char_md.char_user_desc_max_size = strlen(user_description);

ports/nordic/common-hal/busio/UART.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void uart_reset(void) {
122122

123123
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
124124
// Don't never reset objects on the heap.
125-
if (gc_alloc_possible() && gc_nbytes(self) > 0) {
125+
if (gc_alloc_possible() && gc_ptr_on_heap(self)) {
126126
return;
127127
}
128128
for (size_t i = 0; i < MP_ARRAY_SIZE(nrfx_uartes); i++) {
@@ -346,7 +346,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
346346
RUN_BACKGROUND_TASKS;
347347
}
348348

349-
if (!nrfx_is_in_ram(data) && gc_alloc_possible() && gc_nbytes(tx_buf) > 0) {
349+
if (!nrfx_is_in_ram(data) && gc_alloc_possible() && gc_ptr_on_heap(tx_buf)) {
350350
gc_free(tx_buf);
351351
}
352352

py/circuitpy_mpconfig.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,18 @@ void background_callback_run_all(void);
514514

515515
// USB settings
516516

517+
#ifndef CIRCUITPY_SDCARD_USB
518+
#if CIRCUITPY_USB_DEVICE
519+
#define CIRCUITPY_SDCARD_USB (CIRCUITPY_SDCARDIO && CIRCUITPY_USB_MSC)
520+
#else
521+
#define CIRCUITPY_SDCARD_USB (0)
522+
#endif
523+
#endif
524+
525+
#if CIRCUITPY_SDCARD_USB && !(CIRCUITPY_SDCARDIO)
526+
#error CIRCUITPY_SDCARD_USB requires CIRCUITPY_SDCARDIO
527+
#endif
528+
517529
// Debug level for TinyUSB. Only outputs over debug UART so it doesn't cause
518530
// additional USB logging.
519531
#ifndef CIRCUITPY_DEBUG_TINYUSB

py/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ bool gc_is_locked(void) {
446446
}
447447

448448
// CIRCUITPY-CHANGE: additional function
449-
bool gc_ptr_on_heap(void *ptr) {
449+
bool gc_ptr_on_heap(const void *ptr) {
450450
for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) {
451451
if (ptr >= (void *)area->gc_pool_start // must be above start of pool
452452
&& ptr < (void *)area->gc_pool_end) { // must be below end of pool

0 commit comments

Comments
 (0)