Skip to content

Commit 91e9327

Browse files
authored
Add PICO_PANIC_FUNCTION define to allow replacement of the default panic function (#463)
1 parent b3e1d2d commit 91e9327

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/rp2_common/pico_runtime/runtime.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,34 @@ void __attribute__((noreturn)) panic_unsupported() {
220220
panic("not supported");
221221
}
222222

223+
// PICO_CONFIG: PICO_PANIC_FUNCTION, Name of a function to use in place of the stock panic function or empty string to simply breakpoint on panic, default=panic, group=pico_runtime
224+
// note the default is not "panic" it is undefined
225+
#ifdef PICO_PANIC_FUNCTION
226+
#define PICO_PANIC_FUNCTION_EMPTY (__CONCAT(PICO_PANIC_FUNCTION, 1) == 1)
227+
#if !PICO_PANIC_FUNCTION_EMPTY
228+
extern void __attribute__((noreturn)) __printflike(1, 0) PICO_PANIC_FUNCTION(__unused const char *fmt, ...);
229+
#endif
230+
// Use a forwarding method here as it is a little simpler than renaming the symbol as it is used from assembler
231+
void __attribute__((naked, noreturn)) __printflike(1, 0) panic(__unused const char *fmt, ...) {
232+
// if you get an undefined reference here, you didn't define your PICO_PANIC_FUNCTION!
233+
asm (
234+
"push {lr}\n"
235+
#if !PICO_PANIC_FUNCTION_EMPTY
236+
"bl " __XSTRING(PICO_PANIC_FUNCTION) "\n"
237+
#endif
238+
"bkpt #0\n"
239+
"1: b 1b\n" // loop for ever as we are no return
240+
:
241+
:
242+
:
243+
);
244+
}
245+
#else
223246
// todo consider making this try harder to output if we panic early
224247
// right now, print mutex may be uninitialised (in which case it deadlocks - although after printing "PANIC")
225248
// more importantly there may be no stdout/UART initialized yet
226249
// todo we may want to think about where we print panic messages to; writing to USB appears to work
227250
// though it doesn't seem like we can expect it to... fine for now
228-
//
229251
void __attribute__((noreturn)) __printflike(1, 0) panic(const char *fmt, ...) {
230252
puts("\n*** PANIC ***\n");
231253
if (fmt) {
@@ -246,6 +268,7 @@ void __attribute__((noreturn)) __printflike(1, 0) panic(const char *fmt, ...) {
246268

247269
_exit(1);
248270
}
271+
#endif
249272

250273
void hard_assertion_failure(void) {
251274
panic("Hard assert");

0 commit comments

Comments
 (0)