From: phfr24 Date: Tue, 14 Jul 2026 04:04:18 +0000 (-0300) Subject: panic () X-Git-Url: https://git.wirehaze.ovh/stm32f411ceu6.git/commitdiff_plain/refs/heads/main panic () --- diff --git a/sys/core/exceptions.c b/sys/core/exceptions.c index 77e67d7..ad76fc9 100644 --- a/sys/core/exceptions.c +++ b/sys/core/exceptions.c @@ -2,6 +2,7 @@ #include #include #include +#include /* Initialize just enough to be able to handle a fault. */ __attribute__ ((noinline, used)) static void @@ -46,8 +47,17 @@ reset_handler (void) asm ("b kentry"); } +/* TODO write proper exception handlers, this is just a test */ +__attribute__ ((noreturn)) static void +fault_handler (void) +{ + panic ("Fault"); +} + __attribute__ ((section (".vector_table"), used)) static unsigned long const vector_table[] = { - (unsigned long) &_sram_end, - (unsigned long) reset_handler, + [0] = (unsigned long) &_sram_end, + [1] = (unsigned long) reset_handler, + [3] = (unsigned long) fault_handler, /* ??? */ + [6] = (unsigned long) fault_handler, }; diff --git a/sys/core/kentry.c b/sys/core/kentry.c index 7fcc4b8..c78af6b 100644 --- a/sys/core/kentry.c +++ b/sys/core/kentry.c @@ -20,14 +20,14 @@ kentry (void) { unsigned int line; - /* Test UART and LED ---------------------------------------------------- */ + for (line = 0; line < sizeof text / sizeof *text; ++line) + kputs_busytc (text[line]); + + /* Trigger a fault to test kernel panic */ + asm ("udf #0"); + + kputs_busytc ("Unreachable"); while (1) - { - for (line = 0; line < sizeof text / sizeof *text; ++line) - { - kputs_busytc (text[line]); - led_toggle (); - } - } + asm ("wfi"); } diff --git a/sys/core/panic.c b/sys/core/panic.c new file mode 100644 index 0000000..3594a4f --- /dev/null +++ b/sys/core/panic.c @@ -0,0 +1,17 @@ +#include +#include +#include + +void +panic (const char *msg) +{ + asm ("cpsid f"); + + led_on (); + kputs_busytc ("\r\n\nKernel panic: "); + kputs_busytc (msg); + kputs_busytc ("\r\n\nStop."); + + while (1) + asm ("wfi"); +} diff --git a/sys/dev/led.c b/sys/dev/led.c index 222bcb5..529414d 100644 --- a/sys/dev/led.c +++ b/sys/dev/led.c @@ -6,6 +6,10 @@ led_init (void) { /* GPIOCEN: IO port C clock enable */ *RCC_AHB1ENR |= (1u << 2); /* 1: IO port C clock enabled */ + + /* Default to LED being turned off */ + *GPIOC_ODR |= (1u << 13); + /* MODERy[1:0]: Port x configuration bits (y = 0..15) */ *GPIOC_MODER &= ~(0b11u << 26); /* Clear PC13 bits */ *GPIOC_MODER |= (1u << 26); /* 01: General purpose output mode */ diff --git a/sys/include/panic.h b/sys/include/panic.h new file mode 100644 index 0000000..b050fb0 --- /dev/null +++ b/sys/include/panic.h @@ -0,0 +1,6 @@ +#ifndef _PANIC_H_ +#define _PANIC_H_ + +__attribute__ ((noreturn)) void panic (const char *msg); + +#endif /* _PANIC_H_ */