commit 46947c53efa2dd716ba099327a0515215edd40dd
parent 8d9fc0af14640de4e4aeed81401ae7904b154874
Author: Sylvia Ivory <git@sivory.net>
Date: Thu, 22 Jan 2026 19:54:03 -0800
Add timer lab (needs fixing)
Diffstat:
1 file changed, 44 insertions(+), 0 deletions(-)
diff --git a/src/labs/4-timer.zig b/src/labs/4-timer.zig
@@ -0,0 +1,44 @@
+const interrupts = @import("pi").interrupts;
+
+const uart = @import("devices/mini-uart.zig");
+const clock = @import("devices/clock.zig");
+const timer = @import("devices/timer.zig");
+
+pub fn main() !void {
+ try uart.writer.print("entered main safely...\n", .{});
+
+ timer.initialize(.Sixteen, 0x100);
+
+ try uart.writer.print("initialized timer...\n", .{});
+
+ interrupts.enable_interrupts();
+
+ try uart.writer.print("enabled interrupts...\n", .{});
+
+ const start = clock.current_count();
+ var iter: u32 = 0;
+ const N = 20;
+
+ while (timer.get_count() < N) {
+ try uart.writer.print("iter={d}: count={d}, time between interrupts = {d} usec (0x{x})\n", .{
+ iter,
+ timer.get_count(),
+ timer.get_period(),
+ timer.get_period(),
+ });
+ iter += 1;
+ }
+
+ const total = clock.current_count() - start;
+ const total_sec = total / (1000 * 1000);
+ const total_ms = (total / 1000) % 10000;
+ const total_us = (total % 1000);
+
+ try uart.writer.print(
+ \\ total iterations: {}
+ \\ total interrupts: {}
+ \\ iterations / interrupt: {}
+ \\ average period: {}
+ \\ total execution time: {}s.{}ms.{}us
+ , .{ iter, N, iter / N, timer.get_period_sum() / (N - 1), total_sec, total_ms, total_us });
+}