From 7b1008111baa95b4a8c3195f9f5a94bfdbe8d7d2 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sat, 16 Nov 2024 14:37:24 -0700 Subject: Move exc and nmi handlers to their own files. --- include/exc.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/panic.h | 9 +++++++++ src/main.c | 29 ----------------------------- 3 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 include/exc.c create mode 100644 include/panic.h diff --git a/include/exc.c b/include/exc.c new file mode 100644 index 0000000..707d3b0 --- /dev/null +++ b/include/exc.c @@ -0,0 +1,43 @@ +#include + +#include "isr_vector.h" +#include "panic.h" + +#include "ch573/gpio.h" + +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF + +void delay(); + +IRQ(exc) +{ + uint32_t mcause, mepc, mtval, *sp; + + asm volatile("csrr %0, mcause" : "=r"(mcause)); + asm volatile("csrr %0, mepc" : "=r"(mepc)); + asm volatile("csrr %0, mtval" : "=r"(mtval)); + + printf("Hardware Exception Caught:\n"); + printf(" mcause: 0x%80x\n", mcause); + printf(" mepc: 0x%80x\n", mepc); + printf(" mtval: 0x%80x\n", mtval); + + panic(mcause); +} + +IRQ(nmi) +{ + uint32_t mcause, mepc, mtval, *sp; + + asm volatile("csrr %0, mcause" : "=r"(mcause)); + asm volatile("csrr %0, mepc" : "=r"(mepc)); + asm volatile("csrr %0, mtval" : "=r"(mtval)); + + printf("Unhandled NMI Caught:\n"); + printf(" mcause: 0x%80x\n", mcause); + printf(" mepc: 0x%80x\n", mepc); + printf(" mtval: 0x%80x\n", mtval); + + panic(mcause); +} diff --git a/include/panic.h b/include/panic.h new file mode 100644 index 0000000..e52bfc8 --- /dev/null +++ b/include/panic.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +/** flashes a code forever. */ +void flash_code(uint32_t code); + +/** Panics. Call this function when an error is unrrecoverable. */ +void panic(uint32_t code); diff --git a/src/main.c b/src/main.c index c2796a9..ed82a3a 100644 --- a/src/main.c +++ b/src/main.c @@ -185,32 +185,3 @@ void print_hex(uint32_t x) ++i; } } - - -IRQ(exc) -{ - uint32_t mcause, mepc, mtval, *sp; - - asm volatile("csrr %0, mcause" : "=r"(mcause)); - asm volatile("csrr %0, mepc" : "=r"(mepc)); - asm volatile("csrr %0, mtval" : "=r"(mtval)); - - printf("Hardware Exception Caught:\n"); - printf(" mcause: 0x%80x\n", mcause); - printf(" mepc: 0x%80x\n", mepc); - printf(" mtval: 0x%80x\n", mtval); - - panic(mcause); - - while (1); -} - -IRQ(nmi) -{ - while (1) { - GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); - delay(); - GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); - delay(); - } -} -- cgit