aboutsummaryrefslogtreecommitdiffstats
path: root/src/gpio.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpio.zig')
-rw-r--r--src/gpio.zig45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/gpio.zig b/src/gpio.zig
new file mode 100644
index 0000000..1d713e6
--- /dev/null
+++ b/src/gpio.zig
@@ -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) };
+