Skip to content

Commit c6d539a

Browse files
committed
atmel-samd: Fix sporadic "syntax errors"
The GC was deleting memory that was in use because its scan of the stack missed the very top. Switching to _estack fixes this by relying on the location from the linker. Fixes adafruit#124
1 parent 5ad4261 commit c6d539a

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

atmel-samd/main.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ void init_flash_fs(void) {
115115
f_chdrive("/flash");
116116
}
117117

118-
static char *stack_top;
119118
static char heap[16384];
120119

121120
void reset_mp(void) {
@@ -519,12 +518,6 @@ int main(void) {
519518
// initialise the cpu and peripherals
520519
samd21_init();
521520

522-
int stack_dummy;
523-
// Store the location of stack_dummy as an approximation for the top of the
524-
// stack so the GC can account for objects that may be referenced by the
525-
// stack between here and where gc_collect is called.
526-
stack_top = (char*)&stack_dummy;
527-
528521
// Stack limit should be less than real stack size, so we have a chance
529522
// to recover from limit hit. (Limit is measured in bytes.)
530523
mp_stack_ctrl_init();
@@ -581,7 +574,7 @@ void gc_collect(void) {
581574
gc_collect_start();
582575
// This naively collects all object references from an approximate stack
583576
// range.
584-
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
577+
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
585578
gc_collect_end();
586579
}
587580

py/gc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@
104104

105105
#ifdef LOG_HEAP_ACTIVITY
106106
volatile uint32_t change_me;
107-
107+
#pragma GCC push_options
108+
#pragma GCC optimize ("O0")
108109
void __attribute__ ((noinline)) gc_log_change(uint32_t start_block, uint32_t length) {
109110
change_me += start_block;
110111
change_me += length; // Break on this line.
111112
}
113+
#pragma GCC pop_options
112114
#endif
113115

114116
// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool

0 commit comments

Comments
 (0)