Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ static void start_mp(safe_mode_t safe_mode) {
_heap = _allocate_memory(safe_mode, "CIRCUITPY_HEAP_START_SIZE", CIRCUITPY_HEAP_START_SIZE, &heap_size);
gc_init(_heap, _heap + heap_size);
#endif
background_callback_heap_ready();
mp_init();
mp_obj_list_init((mp_obj_list_t *)mp_sys_path, 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C, W25Q16JVxQ"
LONGINT_IMPL = MPZ

CIRCUITPY_AUDIOBUSIO_PDMIN = 0
CIRCUITPY_CODEOP = 0
CIRCUITPY_ERRNO = 0
CIRCUITPY_RAINBOWIO = 0
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "W25Q32FV"
LONGINT_IMPL = MPZ

CIRCUITPY_AUDIOBUSIO_PDMIN = 0
CIRCUITPY_CODEOP = 0
CIRCUITPY_ERRNO = 0
CIRCUITPY_RAINBOWIO = 0
4 changes: 4 additions & 0 deletions ports/atmel-samd/common-hal/audiobusio/PDMIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: MIT

#if CIRCUITPY_AUDIOBUSIO_PDMIN

#include <stdint.h>
#include <string.h>
#include <math.h>
Expand Down Expand Up @@ -492,3 +494,5 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t *se

return values_output;
}

#endif // CIRCUITPY_AUDIOBUSIO_PDMIN
2 changes: 1 addition & 1 deletion ports/atmel-samd/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ static void evsyshandler_common(void) {
audio_dma_evsys_handler();
#endif

#if CIRCUITPY_AUDIOBUSIO
#if CIRCUITPY_AUDIOBUSIO_PDMIN
pdmin_evsys_handler();
#endif
}
Expand Down
5 changes: 4 additions & 1 deletion py/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,10 @@ bool gc_is_locked(void) {
}

// CIRCUITPY-CHANGE: additional function
bool gc_ptr_on_heap(const void *ptr) {
// In ITCM (RAM, on chips that have it) because background_callback_add_core() can call
// this from an interrupt that fires during a flash write. Code in flash can't run during
// a flash write.
bool PLACE_IN_ITCM(gc_ptr_on_heap)(const void *ptr) {
for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) {
if (ptr >= (void *)area->gc_pool_start // must be above start of pool
&& ptr < (void *)area->gc_pool_end) { // must be below end of pool
Expand Down
15 changes: 14 additions & 1 deletion supervisor/background_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,22 @@ void background_callback_run_all(void);
* interrupts are disabled. */
bool background_callback_pending(void);

/* During soft reset, remove all pending callbacks and clear the critical section flag */
/* During soft reset, remove pending callbacks that are on the GC heap or whose data is on
* the GC heap, and cancel any background_callback_prevent() calls.
*
* Until background_callback_heap_ready() is called, background_callback_add() also drops
* any new callback that is on the heap or whose data is on the heap. Hardware owned by a
* heap object can keep interrupting until the object's finaliser stops it. A callback
* queued during teardown would still be pending after the heap is freed.
*/
void background_callback_reset(void);

/* Let background_callback_add() queue callbacks on the GC heap again, after
* background_callback_reset() stopped it. Call once gc_init() has set up the new heap.
* Never fails.
*/
void background_callback_heap_ready(void);

/* Sometimes background callbacks must be blocked. Use these functions to
* bracket the section of code where this is the case. These calls nest, and
* begins must be balanced with ends.
Expand Down
20 changes: 20 additions & 0 deletions supervisor/shared/background_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

static volatile background_callback_t *volatile callback_head, *volatile callback_tail;

// True from background_callback_reset() until background_callback_heap_ready(), while
// the VM's heap is torn down. While it is set, background_callback_add_core() drops any
// callback that is on the heap or whose data is on the heap.
static bool reject_heap_callbacks;

#ifndef CALLBACK_CRITICAL_BEGIN
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
#endif
Expand All @@ -32,6 +37,14 @@ void PLACE_IN_ITCM(background_callback_add_core)(background_callback_t * cb) {
CALLBACK_CRITICAL_END;
return;
}
// During heap teardown, hardware owned by a heap object (such as an I2SOut) keeps
// interrupting until the object's finaliser stops it. A callback queued during teardown
// would still be pending after the heap is freed. Drop any callback that is on the heap
// or whose data is on the heap.
if (reject_heap_callbacks && (gc_ptr_on_heap(cb) || gc_ptr_on_heap(cb->data))) {
CALLBACK_CRITICAL_END;
return;
}
cb->next = 0;
cb->prev = (background_callback_t *)callback_tail;
if (callback_tail) {
Expand Down Expand Up @@ -142,6 +155,13 @@ void background_callback_reset(void) {
callback_head = new_head;
callback_tail = new_tail;
background_prevention_count = 0;
reject_heap_callbacks = true;
CALLBACK_CRITICAL_END;
}

void background_callback_heap_ready(void) {
CALLBACK_CRITICAL_BEGIN;
reject_heap_callbacks = false;
CALLBACK_CRITICAL_END;
}

Expand Down
Loading