]> code.bitgloo.com Git - clyne/zig-stm32l476.git/commitdiff
abstract timer driver
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 6 Oct 2024 19:00:11 +0000 (15:00 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 6 Oct 2024 19:00:11 +0000 (15:00 -0400)
src/main.zig
src/timer.zig

index deac1690d57367c61d55c26a42b38261c79e804d..d6a1aa4b3c04892ba01cd9d1bf3c6fc61fc48433 100644 (file)
@@ -6,12 +6,13 @@ const timer = @import("timer.zig");
 const rcc: *[39]u32 = @ptrFromInt(0x40021000);
 const gpioa = gpio.gpioa;
 const gpioc = gpio.gpioc;
+const tick = timer.systick;
 
 export fn _start() callconv(.C) noreturn {
     cpu.interrupt_disable();
     interrupt.initialize();
     interrupt.register(.SVCall, svcall);
-    timer.initialize(1000);
+    tick.initialize(1000);
     cpu.interrupt_enable();
 
     rcc[19] |= 5; // gpio a and c
@@ -21,8 +22,8 @@ export fn _start() callconv(.C) noreturn {
     while (true) {
         //asm volatile("svc 0");
         gpioa.toggle(5);
-        const next = timer.ticks() + 1000;
-        while (timer.ticks() < next) {
+        const next = tick.ticks() + 1000;
+        while (tick.ticks() < next) {
             asm volatile("nop");
         }
     }
index 21151c77494ca16254280ac59fd32bb63ec6b53c..b30cfcf2ed417728f2b3de4e71f2766abbafb181 100644 (file)
@@ -1,6 +1,6 @@
 const interrupt = @import("interrupt.zig");
 
-const sys_tick_type = packed struct {
+const driver_armcortex = packed struct {
     enable: u1,
     tickint: u1,
     unused: u30,
@@ -8,24 +8,36 @@ const sys_tick_type = packed struct {
     rvr: u32,
     cvr: u32,
     calib: u32,
-};
 
-var sys_tick: *volatile sys_tick_type = @ptrFromInt(0xE000E010);
+    pub fn initialize(self: *driver_armcortex, freq: u32) void {
+        self.rvr = 4000000 / 8 / freq;
+        self.tickint = 1;
+        self.enable = 1;
+    }
 
-pub var ticks_raw: u32 = 0;
+};
 
-pub fn initialize(freq: u32) void {
-    interrupt.register(.SysTick, tick);
-    sys_tick.rvr = 4000000 / 8 / freq;
-    sys_tick.tickint = 1;
-    sys_tick.enable = 1;
-}
+var ticks_raw: u32 = 0;
 
 fn tick() void {
     ticks_raw += 1;
 }
 
-pub fn ticks() u32 {
-    return @atomicLoad(u32, &ticks_raw, .acquire);
-}
+const driver = struct {
+    const lld_type = driver_armcortex;
+
+    lld: *driver_armcortex,
+
+    pub fn initialize(self: driver, freq: u32) void {
+        interrupt.register(.SysTick, tick);
+        self.lld.initialize(freq);
+    }
+
+    pub fn ticks(self: driver) u32 {
+        _ = self;
+        return @atomicLoad(u32, &ticks_raw, .acquire);
+    }
+};
+
+pub const systick = driver { .lld = @ptrFromInt(0xE000E010) };