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.
This commit is contained in:
Damien George 2014-02-23 00:31:11 +00:00
parent ee462489f1
commit 790eed6f93
5 changed files with 59 additions and 7 deletions

View File

@ -29,7 +29,7 @@ CFLAGS += -Iboards/$(BOARD)
#Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -g
CFLAGS += -g -DPENDSV_DEBUG
COPT = -O0
else
COPT += -Os -DNDEBUG

View File

@ -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 {

View File

@ -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;

View File

@ -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]);
}
*/
}

View File

@ -245,6 +245,24 @@ void pyexec_repl(void) {
stdout_tx_str("Micro Python build <git hash> 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);