Skip to content

Commit eaaebf3

Browse files
committed
stmhal: Initialise stack pointer correctly.
Stack is full descending and must be 8-byte aligned. It must start off pointing to just above the last byte of RAM. Previously, stack started pointed to last byte of RAM (eg 0x2001ffff) and so was not 8-byte aligned. This caused a bug in combination with alloca. This patch also updates some debug printing code. Addresses issue adafruit#872 (among many other undiscovered issues).
1 parent 2c180f7 commit eaaebf3

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

py/builtinimport.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
167167

168168
mp_obj_t mp_builtin___import__(mp_uint_t n_args, mp_obj_t *args) {
169169
#if DEBUG_PRINT
170-
printf("__import__:\n");
170+
DEBUG_printf("__import__:\n");
171171
for (int i = 0; i < n_args; i++) {
172-
printf(" ");
172+
DEBUG_printf(" ");
173173
mp_obj_print(args[i], PRINT_REPR);
174-
printf("\n");
174+
DEBUG_printf("\n");
175175
}
176176
#endif
177177

@@ -199,9 +199,9 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, mp_obj_t *args) {
199199
mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
200200
assert(this_name_q != MP_OBJ_NULL);
201201
#if DEBUG_PRINT
202-
printf("Current module: ");
202+
DEBUG_printf("Current module: ");
203203
mp_obj_print(this_name_q, PRINT_REPR);
204-
printf("\n");
204+
DEBUG_printf("\n");
205205
#endif
206206

207207
mp_uint_t this_name_l;

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ STATIC void dump_args(const mp_obj_t *a, int sz) {
183183
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
184184
MP_STACK_CHECK();
185185

186-
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
186+
DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
187187
DEBUG_printf("Input pos args: ");
188188
dump_args(args, n_args);
189189
DEBUG_printf("Input kw args: ");

py/runtime.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void mp_delete_global(qstr qstr) {
190190
}
191191

192192
mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
193-
DEBUG_OP_printf("unary %d %p\n", op, arg);
193+
DEBUG_OP_printf("unary " UINT_FMT " %p\n", op, arg);
194194

195195
if (MP_OBJ_IS_SMALL_INT(arg)) {
196196
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
@@ -226,7 +226,7 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
226226
}
227227

228228
mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
229-
DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
229+
DEBUG_OP_printf("binary " UINT_FMT " %p %p\n", op, lhs, rhs);
230230

231231
// TODO correctly distinguish inplace operators for mutable objects
232232
// lookup logic that CPython uses for +=:
@@ -523,7 +523,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args, mp_uint_t n_kw
523523
// TODO improve this: fun object can specify its type and we parse here the arguments,
524524
// passing to the function arrays of fixed and keyword arguments
525525

526-
DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
526+
DEBUG_OP_printf("calling function %p(n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", fun_in, n_args, n_kw, args);
527527

528528
// get the type
529529
mp_obj_type_t *type = mp_obj_get_type(fun_in);
@@ -539,7 +539,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args, mp_uint_t n_kw
539539
// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
540540
// if n_args==0 and n_kw==0 then there are only fun and self/NULL
541541
mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
542-
DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p)\n", args[0], args[1], n_args, n_kw, args);
542+
DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", args[0], args[1], n_args, n_kw, args);
543543
int adjust = (args[1] == NULL) ? 0 : 1;
544544
return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
545545
}
@@ -732,7 +732,7 @@ void mp_unpack_sequence(mp_obj_t seq_in, mp_uint_t num, mp_obj_t *items) {
732732
void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) {
733733
mp_uint_t num_left = num_in & 0xff;
734734
mp_uint_t num_right = (num_in >> 8) & 0xff;
735-
DEBUG_OP_printf("unpack ex %d %d\n", num_left, num_right);
735+
DEBUG_OP_printf("unpack ex " UINT_FMT " " UINT_FMT "\n", num_left, num_right);
736736
mp_uint_t seq_len;
737737
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
738738
mp_obj_t *seq_items;

stmhal/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ static const char fresh_readme_txt[] =
174174
int main(void) {
175175
// TODO disable JTAG
176176

177-
// Stack limit should be less than real stack size, so we
178-
// had chance to recover from limit hit.
179-
mp_stack_set_limit(&_ram_end - &_heap_end - 512);
177+
// Stack limit should be less than real stack size, so we have a chance
178+
// to recover from limit hit. (Limit is measured in bytes.)
179+
mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024);
180180

181181
/* STM32F4xx HAL library initialization:
182182
- Configure the Flash prefetch, instruction and Data caches

stmhal/stm32f405.ld

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ MEMORY
1616
_minimum_stack_size = 2K;
1717
_minimum_heap_size = 16K;
1818

19-
/* top end of the stack */
20-
21-
/*_stack_end = ORIGIN(RAM) + LENGTH(RAM);*/
22-
_estack = ORIGIN(RAM) + LENGTH(RAM) - 1;
19+
/* Define tho top end of the stack. The stack is full descending so begins just
20+
above last byte of RAM. Note that EABI requires the stack to be 8-byte
21+
aligned for a call. */
22+
_estack = ORIGIN(RAM) + LENGTH(RAM);
2323

2424
/* RAM extents for the garbage collector */
2525
_ram_end = ORIGIN(RAM) + LENGTH(RAM);

0 commit comments

Comments
 (0)