From 790eed6f93dc12df40715ad1062b5ce01badcbd9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 23 Feb 2014 00:31:11 +0000 Subject: [PATCH] stm: Make pendsv_nlr_jump work when debugging is enabled. Stack layout is different when -g used, so need to handle the pendsv jump differently. Addresses Issue #315. --- stm/Makefile | 2 +- stm/led.c | 4 ++-- stm/main.c | 2 +- stm/pendsv.c | 40 +++++++++++++++++++++++++++++++++++++--- stm/pyexec.c | 18 ++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/stm/Makefile b/stm/Makefile index 161777c46b..991645b6c9 100644 --- a/stm/Makefile +++ b/stm/Makefile @@ -29,7 +29,7 @@ CFLAGS += -Iboards/$(BOARD) #Debugging/Optimization ifeq ($(DEBUG), 1) -CFLAGS += -g +CFLAGS += -g -DPENDSV_DEBUG COPT = -O0 else COPT += -Os -DNDEBUG diff --git a/stm/led.c b/stm/led.c index 683e82bc66..0e02715f46 100644 --- a/stm/led.c +++ b/stm/led.c @@ -103,8 +103,8 @@ void led_toggle(pyb_led_t led) { return; } - // XXX this assumes LED is driven by a low MCU output (true for PYBv3, false for PYBv4) - if (!(port->ODR & pin)) { + // XXX this assumes LED is driven by a high MCU output (false for PYBv3, true for PYBv4) + if (port->ODR & pin) { // turn LED off PYB_LED_OFF(port, pin); } else { diff --git a/stm/main.c b/stm/main.c index 4fed5850a6..2757e7665d 100644 --- a/stm/main.c +++ b/stm/main.c @@ -355,7 +355,7 @@ int main(void) { storage_init(); // uncomment these 2 lines if you want REPL on USART_6 (or another usart) as well as on USB VCP - //pyb_usart_global_debug = PYB_USART_3; + //pyb_usart_global_debug = PYB_USART_YA; //usart_init(pyb_usart_global_debug, 115200); int first_soft_reset = true; diff --git a/stm/pendsv.c b/stm/pendsv.c index e8e5b840c6..216bb4db09 100644 --- a/stm/pendsv.c +++ b/stm/pendsv.c @@ -22,15 +22,51 @@ void pendsv_nlr_jump(mp_obj_t o) { SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; } +// since we play tricks with the stack, the compiler must not generate a +// prelude for this function +void pendsv_isr_handler(void) __attribute__((naked)); + void pendsv_isr_handler(void) { // re-jig the stack so that when we return from this interrupt handler // it returns instead to nlr_jump with argument pendsv_object + // note that stack has a different layout if DEBUG is enabled + // + // on entry to this (naked) function, stack has the following layout: + // + // stack layout with DEBUG disabled: + // sp[6]: pc + // sp[5]: ? + // sp[4]: ? + // sp[3]: ? + // sp[2]: ? + // sp[1]: ? + // sp[0]: r0 + // + // stack layout with DEBUG enabled: + // sp[8]: pc + // sp[7]: lr + // sp[6]: ? + // sp[5]: ? + // sp[4]: ? + // sp[3]: ? + // sp[2]: r0 + // sp[1]: 0xfffffff9 + // sp[0]: ? + __asm volatile ( "ldr r0, pendsv_object_ptr\n" "ldr r0, [r0]\n" +#if defined(PENDSV_DEBUG) + "str r0, [sp, #8]\n" +#else "str r0, [sp, #0]\n" +#endif "ldr r0, nlr_jump_ptr\n" +#if defined(PENDSV_DEBUG) + "str r0, [sp, #32]\n" +#else "str r0, [sp, #24]\n" +#endif "bx lr\n" ".align 2\n" "pendsv_object_ptr: .word pendsv_object\n" @@ -38,12 +74,10 @@ void pendsv_isr_handler(void) { ); /* - sp[0] = r0 = exception to raise - sp[6] = pc = nlr_jump uint32_t x[2] = {0x424242, 0xdeaddead}; printf("PendSV: %p\n", x); for (uint32_t *p = (uint32_t*)(((uint32_t)x - 15) & 0xfffffff0), i = 64; i > 0; p += 4, i -= 4) { - printf(" %p: %08x %08x %08x %08x\n", p, p[0], p[1], p[2], p[3]); + printf(" %p: %08x %08x %08x %08x\n", p, (uint)p[0], (uint)p[1], (uint)p[2], (uint)p[3]); } */ } diff --git a/stm/pyexec.c b/stm/pyexec.c index 16a399ce07..207f2234f8 100644 --- a/stm/pyexec.c +++ b/stm/pyexec.c @@ -245,6 +245,24 @@ void pyexec_repl(void) { stdout_tx_str("Micro Python build on 25/1/2014; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n"); stdout_tx_str("Type \"help()\" for more information.\r\n"); + // to test ctrl-C + /* + { + uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef}; + for (;;) { + nlr_buf_t nlr; + printf("pyexec_repl: %p\n", x); + usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); + if (nlr_push(&nlr) == 0) { + for (;;) { + } + } else { + printf("break\n"); + } + } + } + */ + vstr_t line; vstr_init(&line, 32);