]> code.bitgloo.com Git - clyne/zig-stm32l476.git/commitdiff
break out into separate files
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 5 Oct 2024 17:14:55 +0000 (13:14 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 5 Oct 2024 17:14:55 +0000 (13:14 -0400)
src/cpu.zig [new file with mode: 0644]
src/gpio.zig [new file with mode: 0644]
src/main.zig

diff --git a/src/cpu.zig b/src/cpu.zig
new file mode 100644 (file)
index 0000000..58f5f40
--- /dev/null
@@ -0,0 +1,8 @@
+pub fn interrupt_disable() void {
+    asm volatile("cpsid i");
+}
+
+pub fn interrupt_enable() void {
+    asm volatile("cpsie i");
+}
+
diff --git a/src/gpio.zig b/src/gpio.zig
new file mode 100644 (file)
index 0000000..1d713e6
--- /dev/null
@@ -0,0 +1,45 @@
+const registers = packed struct {
+    moder: u32,
+    otyper: u32,
+    ospeedr: u32,
+    pupdr: u32,
+    idr: u32,
+    odr: u32,
+    bsrr: u32,
+    lckr: u32,
+    afrl: u32,
+    afrh: u32,
+    brr: u32,
+    ascr: u32,
+};
+
+const driver = struct {
+    port: *registers,
+
+    const mode = enum {
+        input,
+        output,
+        alternate,
+        analog,
+    };
+
+    pub fn set_mode(self: driver, pin: u4, m: mode) void {
+        self.port.moder &= ~(@as(u32, 0x3) << (2 * pin));
+
+        const mask: u32 = switch (m) {
+            .input => 0,
+            .output => 1,
+            .alternate => 2,
+            .analog => 3
+        };
+
+        self.port.moder |= mask << (2 * pin);
+    }
+
+    pub fn toggle(self: driver, pin: u4) void {
+        self.port.odr ^= @as(u32, 1) << pin;
+    }
+};
+
+pub const gpioa = driver { .port = @ptrFromInt(0x48000000) };
+
index 64bc6d443814001812902e4c074f4791cd3daaa0..8ca6ada66c69b4503a975cf361a6c061412b185d 100644 (file)
@@ -1,63 +1,8 @@
-//! By convention, main.zig is where your main function lives in the case that
-//! you are building an executable. If you are making a library, the convention
-//! is to delete this file and start with root.zig instead.
-//const std = @import("std");
+const cpu = @import("cpu.zig");
+const gpio = @import("gpio.zig");
 
-const gpio_registers = packed struct {
-    moder: u32,
-    otyper: u32,
-    ospeedr: u32,
-    pupdr: u32,
-    idr: u32,
-    odr: u32,
-    bsrr: u32,
-    lckr: u32,
-    afrl: u32,
-    afrh: u32,
-    brr: u32,
-    ascr: u32,
-};
-
-const gpio = struct {
-    port: *gpio_registers,
-
-    const mode = enum {
-        input,
-        output,
-        alternate,
-        analog,
-    };
-
-    pub fn set_mode(self: gpio, pin: u4, m: mode) void {
-        self.port.moder &= ~(@as(u32, 0x3) << (2 * pin));
-
-        const mask: u32 = switch (m) {
-            .input => 0,
-            .output => 1,
-            .alternate => 2,
-            .analog => 3
-        };
-
-        self.port.moder |= mask << (2 * pin);
-    }
-
-    pub fn toggle(self: gpio, pin: u4) void {
-        self.port.odr ^= @as(u32, 1) << pin;
-    }
-};
-
-const cpu = struct {
-    pub fn interrupt_disable() void {
-        asm volatile("cpsid i");
-    }
-
-    pub fn interrupt_enable() void {
-        asm volatile("cpsie i");
-    }
-};
-
-const gpioa = gpio { .port = @ptrFromInt(0x48000000) };
 const rcc: *[39]u32 = @ptrFromInt(0x40021000);
+const gpioa = gpio.gpioa;
 
 export fn _start() callconv(.C) noreturn {
     cpu.interrupt_disable();