Skip to content

Commit 7bf72fc

Browse files
committed
initial commit
1 parent 9f6b705 commit 7bf72fc

File tree

4 files changed

+369
-3
lines changed

4 files changed

+369
-3
lines changed

examples/raspberrypi/rp2xxx/build.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub fn build(b: *std.Build) void {
4949

5050
const chip_agnostic_examples: []const ChipAgnosticExample = &.{
5151
.{ .name = "adc", .file = "src/adc.zig" },
52+
.{ .name = "async-blinky", .file = "src/async_blinky.zig" },
5253
.{ .name = "i2c-accel", .file = "src/i2c_accel.zig" },
5354
.{ .name = "i2c-bus-scan", .file = "src/i2c_bus_scan.zig" },
5455
.{ .name = "i2c-hall-effect", .file = "src/i2c_hall_effect.zig" },
@@ -79,20 +80,20 @@ pub fn build(b: *std.Build) void {
7980
available_examples.appendSlice(specific_examples) catch @panic("out of memory");
8081
for (chip_agnostic_examples) |example| {
8182
available_examples.append(.{
82-
.target = mb.ports.rp2xxx.boards.raspberrypi.pico,
83+
.target = raspberrypi.pico,
8384
.name = b.fmt("pico_{s}", .{example.name}),
8485
.file = example.file,
8586
}) catch @panic("out of memory");
8687

8788
available_examples.append(.{
88-
.target = mb.ports.rp2xxx.boards.raspberrypi.pico2_arm,
89+
.target = raspberrypi.pico2_arm,
8990
.name = b.fmt("pico2_arm_{s}", .{example.name}),
9091
.file = example.file,
9192
}) catch @panic("out of memory");
9293

9394
if (example.works_with_riscv) {
9495
available_examples.append(.{
95-
.target = mb.ports.rp2xxx.boards.raspberrypi.pico2_riscv,
96+
.target = raspberrypi.pico2_riscv,
9697
.name = b.fmt("pico2_riscv_{s}", .{example.name}),
9798
.file = example.file,
9899
}) catch @panic("out of memory");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

port/raspberrypi/rp2xxx/src/hal.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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");
2223
pub const multicore = @import("hal/multicore.zig");
2324
pub const mutex = @import("hal/mutex.zig");
2425
pub const pins = @import("hal/pins.zig");

0 commit comments

Comments
 (0)