Skip to content

Commit

Permalink
Rewrite STM32 blinky to easily add other boards/chips.
Browse files Browse the repository at this point in the history
  • Loading branch information
marnix committed Feb 1, 2025
1 parent 68b5ad1 commit 3e487e0
Showing 1 changed file with 29 additions and 11 deletions.
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();
}
}

0 comments on commit 3e487e0

Please sign in to comment.