]> code.bitgloo.com Git - clyne/zig-stm32l476.git/commitdiff
interrupt driver
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 6 Oct 2024 12:48:02 +0000 (08:48 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 6 Oct 2024 12:48:02 +0000 (08:48 -0400)
link.ld
src/bootstrap.s
src/interrupt.zig [new file with mode: 0644]
src/main.zig

diff --git a/link.ld b/link.ld
index 403ba71a33ca8767a41dc36524b76c1873af815d..5550c7bbf6bfa576d3b369d826b4aee0ef445f85 100644 (file)
--- a/link.ld
+++ b/link.ld
@@ -10,7 +10,7 @@ MEMORY {
 SECTIONS {
        .text : {
                . = ALIGN(8);
-               *(.isr_vector)
+               *(.vector_table)
                *(.text)
                *(.text*)
        } > FLASH
@@ -28,6 +28,7 @@ SECTIONS {
                . = ALIGN(8);
                _sdata = .;
                *(.data)
+               *(.data*)
                . = ALIGN(8);
                _edata = .;
        } > RAM AT > FLASH
@@ -37,8 +38,16 @@ SECTIONS {
                . = ALIGN(8);
                _sbss = .;
                *(.bss)
+               *(.bss*)
                . = ALIGN(8);
                _ebss = .;
        } > RAM
+
+       .stack : {
+               . = ALIGN(4);
+               _bstack = .;
+                . += 2048;
+               _tstack = .;
+       } > RAM
 }
 
index 5e71da0cbba6b3d515e63c3bb2b46d10e648db98..fefa602b93894973d547f7148f49af21c2c5c6b9 100644 (file)
@@ -2,37 +2,10 @@
 .thumb
 
 .extern _start
-.extern fault_handler
+.extern _tstack
 
-.section .text
-
-fault1:
-    mov r0, 1
-    b fault_handler
-fault2:
-    mov r0, 2
-    b fault_handler
-fault3:
-    mov r0, 3
-    b fault_handler
-fault4:
-    mov r0, 4
-    b fault_handler
-fault5:
-    mov r0, 5
-    b fault_handler
-
-.section .bss
-.skip 2048
-stack_top:
-
-.section .isr_vector
-    .word stack_top
+.section .vector_table
+    .word _tstack
     .word _start
-    .word fault1
-    .word fault2
-    .word fault3
-    .word fault4
-    .word fault5
-    .skip 4 * 91
+    .skip 4 * 14
 
diff --git a/src/interrupt.zig b/src/interrupt.zig
new file mode 100644 (file)
index 0000000..60faec6
--- /dev/null
@@ -0,0 +1,40 @@
+var vector_table: [256] *const fn () void = undefined;
+var vtor: *u32 = @ptrFromInt(0xE000ED08);
+
+pub const vector = enum(u8) {
+    NMI = 2,
+    HardFault,
+    MemManage,
+    BusFault,
+    UsageFault,
+    rsvd7,
+    rsvd8,
+    rsvd9,
+    rsvd10,
+    SVCall,
+    DebugMonitor,
+    rsvd13,
+    PendSV,
+    SysTick,
+};
+
+pub fn initialize() void {
+    register(.NMI, nmi);
+    register(.HardFault, hardfault);
+    register(.MemManage, memmanage);
+    register(.BusFault, busfault);
+    register(.UsageFault, usagefault);
+
+    vtor.* = @as(u32, @intFromPtr(&vector_table));
+}
+
+pub fn register(index: vector, handler: *const fn () void) void {
+    vector_table[@intFromEnum(index)] = handler;
+}
+
+fn nmi() void { while (true) {} }
+fn hardfault() void { while (true) {} }
+fn memmanage() void { while (true) {} }
+fn busfault() void { while (true) {} }
+fn usagefault() void { while (true) {} }
+
index 3891e527c542a62accc162c06d0ed17905d86c01..8857583df42234ff50b704aa4622f7305a1412ee 100644 (file)
@@ -1,5 +1,6 @@
 const cpu = @import("cpu.zig");
 const gpio = @import("gpio.zig");
+const interrupt = @import("interrupt.zig");
 
 const rcc: *[39]u32 = @ptrFromInt(0x40021000);
 const gpioa = gpio.gpioa;
@@ -7,18 +8,21 @@ const gpioc = gpio.gpioc;
 
 export fn _start() callconv(.C) noreturn {
     cpu.interrupt_disable();
+    interrupt.initialize();
+    interrupt.register(.SVCall, svcall);
+    cpu.interrupt_enable();
 
     rcc[19] |= 5; // gpio a and c
     gpioa.set_mode(5, .output);
     gpioc.set_mode(13, .input);
 
     while (true) {
-        const state = gpioc.read(13);
-        gpioa.write(5, state);
+        asm volatile("svc 0");
     }
 }
 
-export fn fault_handler() callconv(.C) void {
-    while (true) {}
+fn svcall() void {
+    const state = gpioc.read(13);
+    gpioa.write(5, state);
 }