From 9a29506823896c53b4334c5ee202ce01306d921f Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sat, 16 Nov 2024 15:03:22 -0700 Subject: Fix exception handlers not being included during linking. --- CMakeLists.txt | 6 ++++-- include/exc.c | 43 ------------------------------------------- linker/ls.ld | 6 +++--- src/exc.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 48 deletions(-) delete mode 100644 include/exc.c create mode 100644 src/exc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fdfa58..e53af7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,9 +56,11 @@ add_custom_command( DEPENDS libmain libgloss_stub COMMENT "Link main.elf" COMMAND ${TC_PREFIX}gcc -nostartfiles -lgcc - -static -L ${CMAKE_BINARY_DIR}/lib -lmain -T ${LINKER_SCRIPT} + -static -L ${CMAKE_BINARY_DIR}/lib -T ${LINKER_SCRIPT} -o ${CMAKE_BINARY_DIR}/main.elf - ${CMAKE_BINARY_DIR}/lib/libmain.a + -Wl,--no-whole-archive + -Wl,--whole-archive ${CMAKE_BINARY_DIR}/lib/libmain.a + -Wl,--no-whole-archive ) # Generates the binary with objcopy. diff --git a/include/exc.c b/include/exc.c deleted file mode 100644 index 707d3b0..0000000 --- a/include/exc.c +++ /dev/null @@ -1,43 +0,0 @@ -#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/linker/ls.ld b/linker/ls.ld index 4ecac96..6f11aee 100644 --- a/linker/ls.ld +++ b/linker/ls.ld @@ -16,7 +16,7 @@ SECTIONS /* The ch573 starts execution at address 0x0000, so we have to make sure the * on_reset function is put at the beginning of the flash. */ - *(.isr_routines.on_reset); + KEEP(*(.isr_routines.on_reset)); /* The rest of the code. */ *(.text); @@ -33,8 +33,8 @@ SECTIONS ISR_VECTOR_IN_FLASH = LOADADDR(.isr_vector); .isr_vector : ALIGN(0x04) { ISR_VECTOR_START = .; - *(.isr_vector); - *(.isr_vector.routines); + KEEP(*(.isr_vector)) + KEEP(*(.isr_vector.routines)) ISR_VECTOR_STOP = .; } >sram AT>flash diff --git a/src/exc.c b/src/exc.c new file mode 100644 index 0000000..707d3b0 --- /dev/null +++ b/src/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); +} -- cgit