1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/7/20 - ROBIN KRENS
8 * Set up of basic exceptions and interrupts. These interrupts
9 * don't do much, except for halting the system.
10 * ivt_set_gate(interrupt nr, function, priority) can be used
11 * later to define more appropriate handling. See timer (timer.c)
12 * or serial or (uart.c) handling for non-trivial examples.
14 * The actual code is not much, but there are a lot of details
15 * to consider. Besides that, in case more control is desired over
16 * entering and exiting interrupts (what is pushed on the stack)
17 * A so-called naked function can be used. See below for more
27 #include <sys/robsys.h>
30 #include <lib/stdio.h>
31 #include <lib/string.h>
32 #include <lib/regfunc.h>
33 #include <lib/tinyprintf.h>
36 * These values are pushed on the stack just before
37 * entering the ISR. Normally, this would require
38 * assembly or inline assembly code. I use a so-called
39 * 'naked function' __attribute__ ((interrupt)) which
40 * gives me a little bit more control over the caller
42 * The following register are pushed to the stack
46 struct interrupt_frame {
59 * Vector table, each entry contains an interrupt
61 * interrupt vector 1-15: processor exceptions
62 * interrupt vector 16-92: irq0 - irq ..
63 * Vector table needs to be aligned in memory.
65 uint32_t __attribute__((aligned(0x100))) ivt[92];
67 /* Each message corresponds to each and every exception. */
68 char * exception_message(uint8_t intnr) {
94 if (intnr < 20) // TODO: strlen
95 return messages[intnr];
100 void ivt_set_gate(unsigned char num, void * isr(), short pri) {
102 ivt[num] = (uint32_t) isr;
104 // *NVIC_ISER0 = (1 << ((uint32_t)(num) & 0x1F));
105 /* TODO: Priorities */
109 /* Dummy interrupt: comment out the comment to use a naked
112 __attribute__ ((interrupt))
113 void * dummy_isr( struct interrupt_frame * frame ) {
115 uint8_t nr = *SCB_VTOR_ST & 0xFF;
116 printf("EXCEPTION: %s\n", exception_message(nr));
117 printf("STACK TRACE:\n");
118 printf("R0:%p\n",frame->r0);
119 printf("R1:%p\n",frame->r1);
120 printf("R2:%p\n",frame->r2);
121 printf("R3:%p\n",frame->r3);
122 printf("R12:%p\n",frame->r12);
123 printf("LR:%p\n",frame->lr);
124 printf("PC:%p\n",frame->pc);
125 printf("PSR:%p\n",frame->psr);
130 /* Initialize interrupt vector */
133 /* clear entire IVT location in memory */
134 memset(&ivt, 0, (sizeof(uint32_t) * 92));
136 /* The reset, NMI and hardfault handlers are originally
137 * defined in the assembly start up and can be
138 * reused or overwritten.
140 extern void * reset, * nmi, * hardfault;
142 // set dummy handlers
143 for (int i = 1; i <= 64 ; i++) {
144 ivt_set_gate(i, dummy_isr, 0);
147 /* The vector table is intially at 0x0. The vector table can be
148 * relocated to other memory locations. We can do this by setting
149 * a register in the NVIC called the vector table offset register */
151 rwrite(SCB_VTOR, (uint32_t) &ivt);