From 36f2cc60f527235aaf1b3af0888f3255c0214030 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Fri, 19 Jul 2019 23:37:48 +0800 Subject: [PATCH] further ivt and isr implementation general (dummy) exception or interrupt handlers show exception number (from VTOR status register) --- include/mmap.h | 1 + ivt.c | 79 +++++++++++++++++++++++++++++----------------------------- link.ld | 9 ++++--- main.c | 7 +----- start.asm | 4 +-- systick.c | 12 ++------- uart.c | 11 ++++---- 7 files changed, 57 insertions(+), 66 deletions(-) diff --git a/include/mmap.h b/include/mmap.h index d0f9338..6f4fabb 100644 --- a/include/mmap.h +++ b/include/mmap.h @@ -15,6 +15,7 @@ /* SYSTEM CONTROL BLOCK REGISTER */ #define SCB_VTOR ((volatile uint32_t *)( 0xE000ED08)) // VECTOR TABLE +#define SCB_VTOR_ST ((volatile uint32_t *)( 0xE000ED04)) // STATUS OF VECTOR /* NESTED VECTOR INTERRUPT CONTROL REGISTER */ #define NVIC_ISER0 ((volatile uint32_t*)( 0xE000E100)) // interrupt set enable register diff --git a/ivt.c b/ivt.c index 6df1e8b..183713f 100644 --- a/ivt.c +++ b/ivt.c @@ -32,36 +32,47 @@ struct interrupt_frame { * service routine: * * interrupt vector 1-15: processor exceptions - * interrupt vector 16-64: irq0 - irq .. + * interrupt vector 16-92: irq0 - irq .. * */ -uint32_t ivt[64]; +uint32_t ivt[92]; /* each message corresponds to each and every exception. * We get the correct message by accessing * exception_message[interrupt_number] * exception_message[0] is not used (=MSP)*/ -unsigned char *exception_messages[] = -{ - "--", - "RESET", - "NMI", - "HARD FAULT", - "MEMMANAGE FAULT", - "BUS FAULT", - "USAGE FAULT", - "RESERVED", - "SVC", - "DEBUG MONITOR", - "RESERVED", - "PENDSV", - "SYSTICK", - "IRQ1", - "IRQ2", - "IRQ3", - "IRQ4", - // add more if needed -}; + +char * exception_message(uint8_t intnr) { + + char * messages[] = { + "--", + "RESET", + "NMI", + "HARD FAULT", + "MEMMANAGE FAULT", + "BUS FAULT", + "USAGE FAULT", + "RESERVED", + "SVC", + "DEBUG MONITOR", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "PENDSV", + "SYSTICK", + "IRQ1", + "IRQ2", + "IRQ3", + "IRQ4", + // add more if needed + }; + + if (intnr < 20) // TODO: strlen + return messages[intnr]; + + return NULL; +} void ivt_set_gate(unsigned char num, void * isr(), short pri) { @@ -75,21 +86,11 @@ void ivt_set_gate(unsigned char num, void * isr(), short pri) { __attribute__ ((interrupt)) void * dummy_isr(struct interrupt_frame * frame) { - - uint32_t * p = (volatile uint32_t *) 0x21000000; - - addrtohex(frame->r0); - addrtohex(frame->r1); - addrtohex(frame->r2); - addrtohex(frame->r3); - addrtohex(frame->r12); - addrtohex(frame->lr); - addrtohex(frame->pc); - addrtohex(frame->psr); + uint8_t nr = *SCB_VTOR_ST & 0xFF; - //__asm__ __volatile__ ("MRS r0, IPSR"); - //addrtohex(frame->r0); - uart_puts("EXCEPTION X: SYSTEM HALTED\n"); + uart_puts("EXCEPTION: "); + uart_puts(exception_message(nr)); + uart_puts("\nSYSTEM HALTED\n"); for(;;); } @@ -98,13 +99,13 @@ void * dummy_isr(struct interrupt_frame * frame) { void ivt_init() { /* clear entiry IVT, in SRAM location for SRAM + .data (in .bss section) */ - memset(&ivt, 0, (sizeof(uint32_t) * 87)); + memset(&ivt, 0, (sizeof(uint32_t) * 92)); // stack top is loaded from the first entry table on boot/reset // don't need to relocate or init this here extern void * reset, * nmi, * hardfault; - for (int i = 1; i < 7; i++) { + for (int i = 1; i <= 6 ; i++) { ivt_set_gate(i, dummy_isr, 0); } diff --git a/link.ld b/link.ld index 8301b2c..085ab79 100644 --- a/link.ld +++ b/link.ld @@ -14,9 +14,9 @@ SECTIONS { /* (.vector_table */ *(.text) - - } - . = 0x20000000; + *(.rodata) + } + . = 0x20000000; .data : { *(.data) @@ -24,5 +24,6 @@ SECTIONS .bss : ALIGN(256) { *(.bss) - } + } + _endofbss = .; } diff --git a/main.c b/main.c index 4134c95..c8a47aa 100644 --- a/main.c +++ b/main.c @@ -46,8 +46,8 @@ void main() ivt_init(); // clock_init(); uart_init(); -// systick_init(); uart_puts("LOADING SYSTEM 0.1 ...\n"); + systick_init(); sysinfo(); mm_init(); @@ -61,11 +61,6 @@ void main() //regw_u32(p, 0x0CCCCCCCC, 4, 0x01); //regw_u8(p, 0xFF, 0, 0x02); - - //addrtohex(*p); - - //addrtohex((volatile uint32_t) 0x12345678 ); - //addrtohex((volatile uint32_t) SCB_VTOR ); // asm("cpsie i"); // enable irq , cpsied f (disable faukts( diff --git a/start.asm b/start.asm index d8c7244..e4ca237 100644 --- a/start.asm +++ b/start.asm @@ -34,7 +34,7 @@ stub: mov R1,#0 udiv.w R2, R0, R1 */ -/* .data - .word 'x' */ + .data + .word 'x' .end diff --git a/systick.c b/systick.c index 48065ef..e90714a 100644 --- a/systick.c +++ b/systick.c @@ -16,19 +16,11 @@ struct interrupt_frame { uint32_t psr; // N-4 }; - -/* void * systick_handler() { - -// *RANDOM_ADDR = (volatile uint32_t) 0x10101010 ; -// uart_puts("TEST"); -} */ - __attribute__ ((interrupt)) void * systick_handler(struct interrupt_frame * frame) { + uint32_t volatile status; uart_puts("TICKING\n"); -// addrtohex((uint32_t) 0x12345678); - // for(;;); } @@ -41,7 +33,7 @@ void systick_init() { /* The counter reload register here holds * 0x1000 -- that's 4096 clock cycles -- if * it is down to zero it is restores the value */ - *STK_RELOAD = (volatile uint32_t) 0x00000400; + *STK_RELOAD = (volatile uint32_t) 0x00400000; /* Every time the counter counts down to zero * a systick exception is asserted. Systick has diff --git a/uart.c b/uart.c index 4751645..86bf9fe 100644 --- a/uart.c +++ b/uart.c @@ -12,13 +12,14 @@ void * uart_handler() { - uart_puts("\n echo: "); + uart_puts("echo: "); while (RXNE) { char echochar = *USART1_DR; - // regw_u8(USART1_DR, echochar, 0, O_WRITE); - uart_putc(echochar); - } -//for(;;); + //regw_u32(USART1_DR, echochar, 0, O_WRITE); + uart_putc(echochar); + } + uart_putc('\n'); + } void uart_init() { -- 2.7.4