|
| 1 | +const std = @import("std"); |
| 2 | +const microzig = @import("microzig"); |
| 3 | +const time = microzig.drivers.time; |
| 4 | + |
| 5 | +const rp2xxx = microzig.hal; |
| 6 | +const get_time_since_boot = rp2xxx.time.get_time_since_boot; |
| 7 | +const Io = rp2xxx.Io; |
| 8 | + |
| 9 | +pub const microzig_options = microzig.Options{ |
| 10 | + .log_level = .info, |
| 11 | + .logFn = rp2xxx.uart.log, |
| 12 | +}; |
| 13 | + |
| 14 | +const pin_config: rp2xxx.pins.GlobalConfiguration = .{ |
| 15 | + .GPIO0 = .{ .function = .UART0_TX }, |
| 16 | + .GPIO25 = .{ |
| 17 | + .name = "led", |
| 18 | + .direction = .out, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const pins = pin_config.pins(); |
| 23 | +const uart = rp2xxx.uart.instance.num(0); |
| 24 | + |
| 25 | +// Blink the led with given half-period. |
| 26 | +fn task_blink(io: *Io.RoundRobin, delay: u32) callconv(.c) noreturn { |
| 27 | + var deadline: time.Absolute = get_time_since_boot(); |
| 28 | + while (true) { |
| 29 | + pins.led.toggle(); |
| 30 | + deadline = deadline.add_duration(.from_us(delay)); |
| 31 | + io.pause(&.{ .sleep_until = deadline }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub fn main() !void { |
| 36 | + pin_config.apply(); |
| 37 | + uart.apply(.{ .baud_rate = 1_000_000, .clock_config = rp2xxx.clock_config }); |
| 38 | + rp2xxx.uart.init_logger(uart); |
| 39 | + |
| 40 | + // Set up stacks. A helper function that automates this would be nice. |
| 41 | + const max_tasks = 2; |
| 42 | + var task_stacks_data: [max_tasks][1024]usize = undefined; |
| 43 | + var task_stacks: [max_tasks]*Io.PauseReason = undefined; |
| 44 | + for (&task_stacks, &task_stacks_data) |*dst, *src| |
| 45 | + dst.* = Io.prepare_empty_stack(src); |
| 46 | + |
| 47 | + var io: Io.RoundRobin = .{ .next_swap = 0, .tasks = &task_stacks }; |
| 48 | + |
| 49 | + // Mixing (xoring) two squarewaves of almost the same frequency produces a beat frequency. |
| 50 | + io.async(task_blink, .{ &io, 24_000 }); |
| 51 | + io.async(task_blink, .{ &io, 25_000 }); |
| 52 | + |
| 53 | + // We might want both a monotonic clock and an epoch-synchronized one. |
| 54 | + var deadline: time.Absolute = get_time_since_boot(); |
| 55 | + var cnt: u32 = 0; |
| 56 | + while (true) { |
| 57 | + try uart.writer().print("Hello! {}\r\n", .{cnt}); |
| 58 | + cnt += 1; |
| 59 | + deadline = deadline.add_duration(.from_ms(1000)); |
| 60 | + io.pause(&.{ .sleep_until = deadline }); |
| 61 | + } |
| 62 | +} |
0 commit comments