From d8e7b249319b5eeb23c2648953b6f295f4953ff8 Mon Sep 17 00:00:00 2001 From: imi415 Date: Wed, 7 Sep 2022 22:12:54 +0800 Subject: [PATCH] Refactored exception routine, working trapa handler. Signed-off-by: imi415 --- .clang-format | 12 ++++++++++ include/sh4_core.h | 8 +++++++ include/stx7105.h | 2 ++ src/main.c | 25 ++++++++++---------- src/stx7105_exc.c | 57 ++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 .clang-format create mode 100644 include/sh4_core.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6afe3ff --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: Google +IndentWidth: 4 +AlignConsecutiveMacros: AcrossEmptyLines +AlignConsecutiveDeclarations: AcrossEmptyLines +AlignConsecutiveAssignments: AcrossEmptyLinesAndComments +AllowShortFunctionsOnASingleLine: None +BreakBeforeBraces: Custom +BraceWrapping: + AfterEnum: false + AfterStruct: false + SplitEmptyFunction: false +ColumnLimit: 120 \ No newline at end of file diff --git a/include/sh4_core.h b/include/sh4_core.h new file mode 100644 index 0000000..3cb821d --- /dev/null +++ b/include/sh4_core.h @@ -0,0 +1,8 @@ +#ifndef SH4_CORE_H +#define SH4_CORE_H + +#include + +#define __trapa(code) asm volatile("trapa %0" :: "i" (code)) + +#endif // SH4_CORE_H diff --git a/include/stx7105.h b/include/stx7105.h index ca9b103..cab213f 100644 --- a/include/stx7105.h +++ b/include/stx7105.h @@ -3,6 +3,8 @@ #include +#include "sh4_core.h" + #define __PACKED __attribute__((packed, aligned(1))) #define __IO volatile diff --git a/src/main.c b/src/main.c index a3104e6..8184b9e 100644 --- a/src/main.c +++ b/src/main.c @@ -34,10 +34,12 @@ void uart_init(void) { } static void memory_test(void) { + printf("Begin memory test from 0x%08x to 0x%08x\r\n", MEMTEST_START, MEMTEST_END); + for (uint32_t i = MEMTEST_START; i < MEMTEST_END; i += 4) { *(uint32_t *)i = i; - if (i % 0x10000 == 0U) { + if (i % 0x100000 == 0U) { printf("Write to 0x%08lx...\r\n", i); } } @@ -49,10 +51,12 @@ static void memory_test(void) { return; } - if (i % 0x10000 == 0U) { + if (i % 0x100000 == 0U) { printf("Read from 0x%08lx...\r\n", i); } } + + printf("Memory test finished.\r\n"); } int main(void) { @@ -66,16 +70,7 @@ int main(void) { printf("Hello world\r\n"); - printf("Size of int: %d\r\n", sizeof(int)); - printf("Size of short: %d\r\n", sizeof(short)); - printf("Size of char: %d\r\n", sizeof(char)); - printf("Size of long: %d\r\n", sizeof(long)); - printf("Size of pointer: %d\r\n", sizeof(uint8_t *)); - printf("Size of uint8_t: %d\r\n", sizeof(uint8_t)); - printf("Size of uint16_t: %d\r\n", sizeof(uint16_t)); - printf("Size of uint32_t: %d\r\n", sizeof(uint32_t)); - - delay_ms(5000); + __trapa(34); /* TRAPA test code.. */ memory_test(); @@ -88,3 +83,9 @@ int main(void) { return 0; } + +/* tra #34 is SysCall.. */ +int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { + set_led(LED_RED_GPIO, LED_RED_PIN, 1U); + return 0; +} \ No newline at end of file diff --git a/src/stx7105_exc.c b/src/stx7105_exc.c index 3722b0e..8df5e1d 100644 --- a/src/stx7105_exc.c +++ b/src/stx7105_exc.c @@ -1,8 +1,8 @@ #include "stx7105.h" -#define __WEAK __attribute__((weak)) -#define __IRQ __attribute__((interrupt_handler)) -#define __WEAK_IRQ __attribute__((weak, interrupt_handler)) +#define WEAK_ATTR __attribute__((weak)) +#define IRQ_ATTR __attribute__((interrupt_handler)) +#define WEAK_IRQ_ATTR __attribute__((weak, interrupt_handler)) typedef enum { EXP_TYPE_TRAP = 0x160, @@ -13,24 +13,50 @@ typedef enum { INT_TYPE_TMU_TNUI1 = 0x420, INT_TYPE_TMU_TNUI2 = 0x440, INT_TYPE_TMU_TICPI2 = 0x460, + INT_TYPE_ASC_UART0 = 0x1160, + INT_TYPE_ASC_UART1 = 0x1140, + INT_TYPE_ASC_UART2 = 0x1120, + INT_TYPE_ASC_UART3 = 0x1100, } intevt_type_t; typedef enum { TRA_TYPE_SYSCALL = 34, } tra_type_t; +/* ========================= TMU 0/1/2 Underrun Interrupt Handlers ================================= */ -__WEAK int tuni0_handler(void) { +WEAK_ATTR int tuni0_handler(void) { /* Does nothing */ return 0; } -__WEAK int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { +WEAK_ATTR int tuni1_handler(void) { + /* Does nothing */ return 0; } -__WEAK int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { - tra_type_t tra = CSP->TRA; +WEAK_ATTR int tuni2_handler(void) { + /* Does nothing */ + return 0; +} + +/* ========================= ASC(UART) 0/1/2 Interrupt Handlers ================================= */ + +WEAK_ATTR int asc2_handler(void) { + /* Does nothing */ + return 0; +} + +/* ========================= Different Trap Code Handlers ================================= */ + +WEAK_ATTR int syscall_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { + return 0; +} + +/* ========================= TRAPA(Trap) Exception Handlers ================================= */ + +WEAK_ATTR int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { + tra_type_t tra = (CSP->TRA >> 2U) & 0xFFU; /* TRA[9:2] */ switch (tra) { case TRA_TYPE_SYSCALL: @@ -43,7 +69,9 @@ __WEAK int trap_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { return 0; } -__WEAK_IRQ int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { +/* ========================= System Exception Handlers ================================= */ + +WEAK_IRQ_ATTR int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { expevt_type_t expevt = CSP->EXPEVT; switch (expevt) { case EXP_TYPE_TRAP: @@ -56,12 +84,23 @@ __WEAK_IRQ int general_exc_handler(uint32_t p1, uint32_t p2, uint32_t p3, uint32 return 0; } -__WEAK_IRQ int general_int_handler(void) { +/* ========================= System Interrupt Handlers ================================= */ + +WEAK_IRQ_ATTR int general_int_handler(void) { intevt_type_t intevt = CSP->INTEVT; switch (intevt) { case INT_TYPE_TMU_TNUI0: return tuni0_handler(); break; + case INT_TYPE_TMU_TNUI1: + return tuni1_handler(); + break; + case INT_TYPE_TMU_TNUI2: + return tuni2_handler(); + break; + case INT_TYPE_ASC_UART2: + return asc2_handler(); + break; default: break; }