Skip to content

Commit 3a29b92

Browse files
committed
add clock interface
1 parent 2650067 commit 3a29b92

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

examples/raspberrypi/rp2xxx/src/async_blinky.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const microzig = @import("microzig");
33
const time = microzig.drivers.time;
44

55
const rp2xxx = microzig.hal;
6-
const get_time_since_boot = rp2xxx.time.get_time_since_boot;
76
const Io = rp2xxx.Io;
87

98
pub const microzig_options = microzig.Options{
@@ -24,7 +23,7 @@ const uart = rp2xxx.uart.instance.num(0);
2423

2524
// Blink the led with given half-period.
2625
fn task_blink(io: *Io.RoundRobin, delay: u32) callconv(.c) noreturn {
27-
var deadline: time.Absolute = get_time_since_boot();
26+
var deadline: time.Absolute = io.monotonic_clock();
2827
while (true) {
2928
pins.led.toggle();
3029
deadline = deadline.add_duration(.from_us(delay));
@@ -50,7 +49,7 @@ pub fn main() !void {
5049
io.async(task_blink, .{ &io, 24_000 });
5150
io.async(task_blink, .{ &io, 25_000 });
5251

53-
var deadline: time.Absolute = get_time_since_boot();
52+
var deadline: time.Absolute = io.monotonic_clock();
5453
var cnt: u32 = 0;
5554
while (true) {
5655
try uart.writer().print("Hello! {}\r\n", .{cnt});

port/raspberrypi/rp2xxx/src/hal/Io.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub const PauseReason = union(enum) {
3535
/// Task volutarily gave up execution, but is ready to continue.
3636
yield,
3737
sleep_until: time.Absolute align(@alignOf(StackUint)),
38+
bits_mask_any_high: struct { ptr: *const usize, mask: usize },
3839
/// This value means there is no context stored on this stack
3940
/// so it can be used to launch a new task.
4041
no_task,
@@ -46,11 +47,13 @@ pub const PauseReason = union(enum) {
4647
/// Check if the task should be resumed.
4748
/// The io interface may not be necessary.
4849
pub fn can_resume(this: *const @This(), io: anytype) bool {
49-
_ = io;
5050
return switch (this.*) {
5151
.no_task => false,
5252
.yield => true,
53-
.sleep_until => |t| t.is_reached_by(rp2xxx.time.get_time_since_boot()),
53+
.bits_mask_any_high => |info| {
54+
return @atomicLoad(usize, info.ptr, .acquire) & info.mask != 0;
55+
},
56+
.sleep_until => |t| t.is_reached_by(io.monotonic_clock()),
5457
};
5558
}
5659

@@ -300,4 +303,9 @@ pub const RoundRobin = struct {
300303
// Maybe we could wait for them to complete instead?
301304
std.debug.panic("Cannot launch more tasks.", .{});
302305
}
306+
307+
pub fn monotonic_clock(this: *@This()) time.Absolute {
308+
_ = this;
309+
return rp2xxx.time.get_time_since_boot();
310+
}
303311
};

0 commit comments

Comments
 (0)