forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stm: Add support for ctrl-C to interrupt running Python.
Using PendSV interrupt at lowest priority, code can now raise an exception during an interrupt by calling pendsv_nlr_jump. The exception will be raised when all interrupts are finished. This is used to trap ctrl-C from the USB VCP to break out of running Python code.
- Loading branch information
Showing
8 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ SRC_C = \ | |
string0.c \ | ||
malloc0.c \ | ||
systick.c \ | ||
pendsv.c \ | ||
gccollect.c \ | ||
lexerfatfs.c \ | ||
led.c \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <stdlib.h> | ||
#include <stm32f4xx.h> | ||
|
||
#include "misc.h" | ||
#include "mpconfig.h" | ||
#include "qstr.h" | ||
#include "obj.h" | ||
#include "pendsv.h" | ||
|
||
static mp_obj_t pendsv_object = MP_OBJ_NULL; | ||
|
||
void pendsv_init(void) { | ||
// set PendSV interrupt at lowest priority | ||
NVIC_SetPriority(PendSV_IRQn, 0xff); | ||
} | ||
|
||
// call this function to raise a pending exception during an interrupt | ||
// it will wait until all interrupts are finished then raise the given | ||
// exception object using nlr_jump in the context of the top-level thread | ||
void pendsv_nlr_jump(mp_obj_t o) { | ||
pendsv_object = o; | ||
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; | ||
} | ||
|
||
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 | ||
__asm volatile ( | ||
"ldr r0, pendsv_object_ptr\n" | ||
"ldr r0, [r0]\n" | ||
"str r0, [sp, #0]\n" | ||
"ldr r0, nlr_jump_ptr\n" | ||
"str r0, [sp, #24]\n" | ||
"bx lr\n" | ||
".align 2\n" | ||
"pendsv_object_ptr: .word pendsv_object\n" | ||
"nlr_jump_ptr: .word nlr_jump\n" | ||
); | ||
|
||
/* | ||
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]); | ||
} | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
void pendsv_init(void); | ||
void pendsv_nlr_jump(mp_obj_t o); | ||
void pendsv_isr_handler(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters