improve timer API (#60)

wch-ch32v003
Matt Knight 1 year ago committed by GitHub
parent a2ccaff13f
commit 886234b882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -154,7 +154,7 @@ pub fn main() !void {
rp2040.usb.Usb.init_clk(); rp2040.usb.Usb.init_clk();
// Then initialize the USB device using the configuration defined above // Then initialize the USB device using the configuration defined above
rp2040.usb.Usb.init_device(&DEVICE_CONFIGURATION) catch unreachable; 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; var new: u64 = 0;
while (true) { while (true) {
// You can now poll for USB events // You can now poll for USB events
@ -162,7 +162,7 @@ pub fn main() !void {
false, // debug output over UART [Y/n] false, // debug output over UART [Y/n]
) catch unreachable; ) catch unreachable;
new = time.get_time_since_boot().us_since_boot; new = time.get_time_since_boot().to_us();
if (new - old > 500000) { if (new - old > 500000) {
old = new; old = new;
led.toggle(); led.toggle();

@ -169,7 +169,7 @@ pub fn main() !void {
rp2040.usb.Usb.init_clk(); rp2040.usb.Usb.init_clk();
// Then initialize the USB device using the configuration defined above // Then initialize the USB device using the configuration defined above
rp2040.usb.Usb.init_device(&DEVICE_CONFIGURATION) catch unreachable; 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; var new: u64 = 0;
while (true) { while (true) {
// You can now poll for USB events // You can now poll for USB events
@ -177,7 +177,7 @@ pub fn main() !void {
true, // debug output over UART [Y/n] true, // debug output over UART [Y/n]
) catch unreachable; ) catch unreachable;
new = time.get_time_since_boot().us_since_boot; new = time.get_time_since_boot().to_us();
if (new - old > 500000) { if (new - old > 500000) {
old = new; old = new;
led.toggle(); led.toggle();

@ -1,8 +1,19 @@
const microzig = @import("microzig"); const microzig = @import("microzig");
const TIMER = microzig.chip.peripherals.TIMER; const TIMER = microzig.chip.peripherals.TIMER;
pub const Absolute = struct { /// Using an enum to make it a distinct type, the underlying number is
us_since_boot: u64, /// 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 { pub fn get_time_since_boot() Absolute {
@ -12,23 +23,14 @@ pub fn get_time_since_boot() Absolute {
var low_word = TIMER.TIMERAWL; var low_word = TIMER.TIMERAWL;
const next_high_word = TIMER.TIMERAWH; const next_high_word = TIMER.TIMERAWH;
if (next_high_word == high_word) if (next_high_word == high_word)
break Absolute{ break @intToEnum(Absolute, @intCast(u64, high_word) << 32 | low_word);
.us_since_boot = @intCast(u64, high_word) << 32 | low_word,
};
high_word = next_high_word; high_word = next_high_word;
} else unreachable; } else unreachable;
} }
pub fn make_timeout_us(timeout_us: u64) Absolute { pub fn make_timeout_us(timeout_us: u64) Absolute {
return Absolute{ return @intToEnum(Absolute, get_time_since_boot().to_us() + timeout_us);
.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;
} }
pub fn sleep_ms(time_ms: u32) void { 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 { pub fn sleep_us(time_us: u64) void {
const end_time = Absolute{ const end_time = make_timeout_us(time_us);
.us_since_boot = time_us + get_time_since_boot().us_since_boot, while (!end_time.reached()) {}
};
while (!reached(end_time)) {}
} }

@ -219,8 +219,8 @@ pub fn log(
if (uart_logger) |uart| { if (uart_logger) |uart| {
const current_time = time.get_time_since_boot(); const current_time = time.get_time_since_boot();
const seconds = 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.us_since_boot % 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 {}; uart.print(prefix ++ format ++ "\r\n", .{ seconds, microseconds } ++ args) catch {};
} }

Loading…
Cancel
Save