From 886234b88284bd90c951b930c98f28681140435b Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Mon, 15 May 2023 23:12:22 -0700 Subject: [PATCH] improve timer API (#60) --- examples/usb_device.zig | 4 ++-- examples/usb_hid.zig | 4 ++-- src/hal/time.zig | 35 +++++++++++++++++------------------ src/hal/uart.zig | 4 ++-- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/usb_device.zig b/examples/usb_device.zig index 3395286..252db52 100644 --- a/examples/usb_device.zig +++ b/examples/usb_device.zig @@ -154,7 +154,7 @@ pub fn main() !void { rp2040.usb.Usb.init_clk(); // Then initialize the USB device using the configuration defined above rp2040.usb.Usb.init_device(&DEVICE_CONFIGURATION) catch unreachable; - var old: u64 = time.get_time_since_boot().us_since_boot; + var old: u64 = time.get_time_since_boot().to_us(); var new: u64 = 0; while (true) { // You can now poll for USB events @@ -162,7 +162,7 @@ pub fn main() !void { false, // debug output over UART [Y/n] ) catch unreachable; - new = time.get_time_since_boot().us_since_boot; + new = time.get_time_since_boot().to_us(); if (new - old > 500000) { old = new; led.toggle(); diff --git a/examples/usb_hid.zig b/examples/usb_hid.zig index 051ad74..a19a3e0 100644 --- a/examples/usb_hid.zig +++ b/examples/usb_hid.zig @@ -169,7 +169,7 @@ pub fn main() !void { rp2040.usb.Usb.init_clk(); // Then initialize the USB device using the configuration defined above rp2040.usb.Usb.init_device(&DEVICE_CONFIGURATION) catch unreachable; - var old: u64 = time.get_time_since_boot().us_since_boot; + var old: u64 = time.get_time_since_boot().to_us(); var new: u64 = 0; while (true) { // You can now poll for USB events @@ -177,7 +177,7 @@ pub fn main() !void { true, // debug output over UART [Y/n] ) catch unreachable; - new = time.get_time_since_boot().us_since_boot; + new = time.get_time_since_boot().to_us(); if (new - old > 500000) { old = new; led.toggle(); diff --git a/src/hal/time.zig b/src/hal/time.zig index 3b568bf..d7f815b 100644 --- a/src/hal/time.zig +++ b/src/hal/time.zig @@ -1,8 +1,19 @@ const microzig = @import("microzig"); const TIMER = microzig.chip.peripherals.TIMER; -pub const Absolute = struct { - us_since_boot: u64, +/// Using an enum to make it a distinct type, the underlying number is +/// time since boot in microseconds. +pub const Absolute = enum(u64) { + _, + + pub fn reached(time: Absolute) bool { + const now = get_time_since_boot(); + return now.to_us() >= time.to_us(); + } + + pub fn to_us(time: Absolute) u64 { + return @enumToInt(time); + } }; pub fn get_time_since_boot() Absolute { @@ -12,23 +23,14 @@ pub fn get_time_since_boot() Absolute { var low_word = TIMER.TIMERAWL; const next_high_word = TIMER.TIMERAWH; if (next_high_word == high_word) - break Absolute{ - .us_since_boot = @intCast(u64, high_word) << 32 | low_word, - }; + break @intToEnum(Absolute, @intCast(u64, high_word) << 32 | low_word); high_word = next_high_word; } else unreachable; } pub fn make_timeout_us(timeout_us: u64) Absolute { - return Absolute{ - .us_since_boot = get_time_since_boot().us_since_boot + timeout_us, - }; -} - -pub fn reached(time: Absolute) bool { - const now = get_time_since_boot(); - return now.us_since_boot >= time.us_since_boot; + return @intToEnum(Absolute, get_time_since_boot().to_us() + timeout_us); } pub fn sleep_ms(time_ms: u32) void { @@ -36,9 +38,6 @@ pub fn sleep_ms(time_ms: u32) void { } pub fn sleep_us(time_us: u64) void { - const end_time = Absolute{ - .us_since_boot = time_us + get_time_since_boot().us_since_boot, - }; - - while (!reached(end_time)) {} + const end_time = make_timeout_us(time_us); + while (!end_time.reached()) {} } diff --git a/src/hal/uart.zig b/src/hal/uart.zig index 6a08b85..b7a9b11 100644 --- a/src/hal/uart.zig +++ b/src/hal/uart.zig @@ -219,8 +219,8 @@ pub fn log( if (uart_logger) |uart| { const current_time = time.get_time_since_boot(); - const seconds = current_time.us_since_boot / std.time.us_per_s; - const microseconds = current_time.us_since_boot % std.time.us_per_s; + const seconds = current_time.to_us() / std.time.us_per_s; + const microseconds = current_time.to_us() % std.time.us_per_s; uart.print(prefix ++ format ++ "\r\n", .{ seconds, microseconds } ++ args) catch {}; }