Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite STM32 blinky to easily add other boards/chips. #374

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions examples/stmicro/stm32/src/blinky.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@ const std = @import("std");
const microzig = @import("microzig");
const stm32 = microzig.hal;

const pin_config = stm32.pins.GlobalConfiguration{
.GPIOC = .{
.PIN13 = .{ .name = "led", .mode = .{ .output = .general_purpose_push_pull } },
},
};
fn delay() void {
var i: u32 = 0;
while (i < 800_000) {
asm volatile ("nop");
i += 1;
}
}

pub fn main() !void {
const pins = pin_config.apply();
const pins, const all_leds = res: {
if (comptime std.mem.eql(u8, microzig.config.chip_name, "STM32F103C8")) {
const pins = (stm32.pins.GlobalConfiguration{ .GPIOC = .{
.PIN13 = .{ .name = "led", .mode = .{ .output = .general_purpose_push_pull } },
} }).apply();
const all_leds = .{
pins.led,
};
break :res .{ pins, all_leds };
} else {
@compileError("blinky is not (yet?) implemented for this target");
}
};
_ = pins;

while (true) {
var i: u32 = 0;
while (i < 800_000) {
asm volatile ("nop");
i += 1;
delay();
for (0..all_leds.len) |k| {
switch (@as(u3, @intCast(k))) {
inline else => |i| {
if (i >= all_leds.len) unreachable;
all_leds[i].toggle();
},
}
}
pins.led.toggle();
}
}
Loading