commit 26e061b4f8959a6f009285f6ce88ccc628069065
parent 242a75221bb06f3b5703da9bd4012a54d8e07836
Author: Sylvia Ivory <git@sivory.net>
Date: Tue, 20 Jan 2026 14:13:51 -0800
Remove gpio.initialize
Diffstat:
8 files changed, 19 insertions(+), 52 deletions(-)
diff --git a/build.zig b/build.zig
@@ -108,7 +108,6 @@ fn lab(name: []const u8, units: []const []const u8, b: *std.Build, add_exe: add_
\\ const lab = @import("{s}");
\\ export fn abort() void {{ while(true) {{}} }}
\\ pub export fn kmain() void {{
- \\ @import("devices/gpio.zig").initialize(0x2000_0000);
\\ _ = lab.{s}() catch {{}};
\\ }}
;
diff --git a/fake-pi/pi.zig b/fake-pi/pi.zig
@@ -1,4 +1,6 @@
pub const mem = struct {
+ pub const BASE_ADDRESS: usize = 0x2000_0000;
+
const Operation = enum {
Read,
Write,
diff --git a/fake-pi/root.zig b/fake-pi/root.zig
@@ -35,7 +35,6 @@ fn trace(comptime format: []const u8, args: anytype) void {
pub fn initialize() void {
c.fake_random_init();
- gpio.initialize(BASE_ADDRESS);
trace("calling pi code\n", .{});
}
diff --git a/pi/mem.zig b/pi/mem.zig
@@ -1,3 +1,5 @@
+pub const BASE_ADDRESS: usize = 0x2000_0000;
+
// Forces all outstanding explicit memory transactions to complete before executing
// another **instruction**. Other instructions can execute out of order
inline fn dsb() void {
diff --git a/src/devices/gpio.zig b/src/devices/gpio.zig
@@ -2,11 +2,11 @@ const std = @import("std");
const mem = @import("pi").mem;
// Page 90, Table 6-1: GPIO Register Assignment
-const BASE_OFFSET: usize = 0x0020_0000;
-const GPFSEL0_OFFSET: usize = BASE_OFFSET + 0x0000;
-const GPSET0_OFFSET: usize = BASE_OFFSET + 0x001C;
-const GPCLR0_OFFSET: usize = BASE_OFFSET + 0x0028;
-const GPLEV0_OFFSET: usize = BASE_OFFSET + 0x0034;
+const BASE_ADDRESS: usize = mem.BASE_ADDRESS + 0x0020_0000;
+const GPFSEL0: usize = BASE_ADDRESS + 0x0000;
+const GPSET0: usize = BASE_ADDRESS + 0x001C;
+const GPCLR0: usize = BASE_ADDRESS + 0x0028;
+const GPLEV0: usize = BASE_ADDRESS + 0x0034;
pub const MAX_GPIO: u8 = 53;
const GPIO_PER_FSEL: u8 = 10;
@@ -27,13 +27,9 @@ const FunctionSelect = enum(u3) {
pub const Error = error{
PinOutOfRange,
- NotInitialized,
};
-var base_address: usize = 0;
-
fn gpio_check(pin: u8) Error!void {
- if (base_address == 0) return Error.NotInitialized;
if (pin > MAX_GPIO) return Error.PinOutOfRange;
}
@@ -42,13 +38,13 @@ fn get_address(pin: u8, offset: usize, chunk: ?u32) Error!*u32 {
// One might say "@bitSizeOf(u32)" is redundant but I do not care
const index = @divFloor(pin, chunk orelse @bitSizeOf(u32)) * @sizeOf(u32);
- const address: *u32 = @ptrFromInt(base_address + offset + index);
+ const address: *u32 = @ptrFromInt(offset + index);
return address;
}
pub fn fn_sel(pin: u8, sel: FunctionSelect) Error!void {
- const address = try get_address(pin, GPFSEL0_OFFSET, GPIO_PER_FSEL);
+ const address = try get_address(pin, GPFSEL0, GPIO_PER_FSEL);
const offset: u5 = @truncate((pin % GPIO_PER_FSEL) * 3);
const mask = ~(@as(u32, 0b111) << offset);
@@ -70,19 +66,15 @@ fn addr_bitset(pin: u8, address: *u32) void {
// Turn on
fn output_set(pin: u8) Error!void {
- addr_bitset(pin, try get_address(pin, GPSET0_OFFSET, null));
+ addr_bitset(pin, try get_address(pin, GPSET0, null));
}
// Turn off
fn output_clear(pin: u8) Error!void {
- addr_bitset(pin, try get_address(pin, GPCLR0_OFFSET, null));
+ addr_bitset(pin, try get_address(pin, GPCLR0, null));
}
// Public API
-pub fn initialize(base_addr: u32) void {
- base_address = base_addr;
-}
-
pub fn set_output(pin: u8) Error!void {
try fn_sel(pin, .output);
}
@@ -100,7 +92,7 @@ pub fn set_off(pin: u8) Error!void {
}
pub fn read(pin: u8) Error!bool {
- const address = try get_address(pin, GPLEV0_OFFSET, null);
+ const address = try get_address(pin, GPLEV0, null);
const offset: u5 = @truncate(pin % 32);
const mask = (@as(u32, 1) << offset);
diff --git a/src/devices/hc-sr04.zig b/src/devices/hc-sr04.zig
@@ -1,20 +0,0 @@
-const gpio = @import("../gpio.zig");
-
-pub const Error = error{Timeout} | gpio.Error;
-
-pub const Device = struct {
- echo_pin: u8,
- trigger_pin: u8,
-
- pub fn setup(echo_pin: u8, trigger_pin: u8) Error!Device {
- if (echo_pin >= gpio.MAX_GPIO) return Error.PinOutOfRange;
- if (trigger_pin >= gpio.MAX_GPIO) return Error.PinOutOfRange;
-
- return .{ echo_pin, trigger_pin };
- }
-
- pub fn get_distance(d: *Device, timeout: bool) Error!Device {
- _ = d;
- _ = timeout;
- }
-};
diff --git a/src/devices/timer.zig b/src/devices/timer.zig
@@ -1,19 +1,13 @@
const std = @import("std");
const mem = @import("pi").mem;
-var base_address: usize = 0;
-
-const BASE_OFFSET: usize = 0x3000;
-const COUNTER_LOWER_OFFSET: usize = 0x04;
-const COUNTER_UPPER_OFFSET: usize = 0x08;
-
-pub fn initialize(base_addr: u32) void {
- base_address = base_addr;
-}
+const BASE_ADDRESS: usize = mem.BASE_ADDRESS + 0x3000;
+const COUNTER_LOWER: usize = 0x04;
+const COUNTER_UPPER: usize = 0x08;
pub fn current_count() u64 {
- const upper = mem.get_u32_barrier(base_address + COUNTER_UPPER_OFFSET);
- const lower = mem.get_u32_barrier(base_address + COUNTER_LOWER_OFFSET);
+ const upper = mem.get_u32_barrier(COUNTER_UPPER);
+ const lower = mem.get_u32_barrier(COUNTER_LOWER);
const extended: u64 = @intCast(upper);
return (extended << 32) | lower;
diff --git a/src/test-runner.zig b/src/test-runner.zig
@@ -59,6 +59,5 @@ export fn gpio_set_function(pin: u32, func: u32) void {
}
pub fn kmain() void {
- gpio.initialize(0x2000_0000);
c.notmain();
}