Skip to content

Commit 772a9ff

Browse files
committed
move almost all functionality into core
1 parent 3a29b92 commit 772a9ff

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

core/src/core.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub const experimental = @import("core/experimental.zig");
22
pub const heap = @import("core/heap.zig");
3+
pub const Io = @import("core/Io.zig");
34
/// USB data types and helper functions
45
pub const usb = @import("core/usb.zig");
56

port/raspberrypi/rp2xxx/src/hal/Io.zig renamed to core/src/core/Io.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
//! I'm not sure how much of this functionality could be moved into core.
2-
//! Context switches could probably made available project-wide,
3-
//! but that might be enough scope for a separate project.
4-
51
const builtin = @import("builtin");
62
const std = @import("std");
7-
const microzig = @import("microzig");
3+
const drivers = @import("drivers");
84
const assert = std.debug.assert;
9-
const rp2xxx = microzig.hal;
10-
const time = microzig.drivers.time;
5+
const time = drivers.time;
116

127
/// Unsigned integer with the same alignment as the stack.
138
const StackUint = usize;
@@ -271,6 +266,7 @@ pub fn prepare_task_stack(comptime F: type, f: *const F, stack: *PauseReason) st
271266
pub const RoundRobin = struct {
272267
next_swap: usize,
273268
tasks: []*PauseReason,
269+
vtable: VTable,
274270

275271
/// Pause the current task allow others to run.
276272
pub fn pause(this: *@This(), reason: *const PauseReason) void {
@@ -305,7 +301,13 @@ pub const RoundRobin = struct {
305301
}
306302

307303
pub fn monotonic_clock(this: *@This()) time.Absolute {
308-
_ = this;
309-
return rp2xxx.time.get_time_since_boot();
304+
return this.vtable.monotonic_clock();
310305
}
311306
};
307+
308+
/// Common functionality between all implementations.
309+
/// Needs to be specified by every port.
310+
pub const VTable = struct {
311+
/// A clock source that only ever goes up, not synchronized with epoch.
312+
monotonic_clock: *const fn () time.Absolute,
313+
};

examples/raspberrypi/rp2xxx/src/async_blinky.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33
const time = microzig.drivers.time;
4+
const Io = microzig.core.Io;
45

56
const rp2xxx = microzig.hal;
6-
const Io = rp2xxx.Io;
77

88
pub const microzig_options = microzig.Options{
99
.log_level = .info,
@@ -43,7 +43,7 @@ pub fn main() !void {
4343
for (&task_stacks, &task_stacks_data) |*dst, *src|
4444
dst.* = Io.prepare_empty_stack(src);
4545

46-
var io: Io.RoundRobin = .{ .next_swap = 0, .tasks = &task_stacks };
46+
var io: Io.RoundRobin = .{ .next_swap = 0, .tasks = &task_stacks, .vtable = rp2xxx.Io.vtable };
4747

4848
// Mixing (xoring) two squarewaves of almost the same frequency produces a beat frequency.
4949
io.async(task_blink, .{ &io, 24_000 });

port/raspberrypi/rp2xxx/src/hal.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ pub const dma = @import("hal/dma.zig");
1919
pub const drivers = @import("hal/drivers.zig");
2020
pub const flash = @import("hal/flash.zig");
2121
pub const gpio = @import("hal/gpio.zig");
22-
pub const Io = @import("hal/Io.zig");
22+
pub const Io = struct {
23+
pub const vtable: microzig.core.Io.VTable = .{
24+
.monotonic_clock = time.get_time_since_boot,
25+
};
26+
};
2327
pub const multicore = @import("hal/multicore.zig");
2428
pub const mutex = @import("hal/mutex.zig");
2529
pub const pins = @import("hal/pins.zig");

0 commit comments

Comments
 (0)