blinky.zig
1 const std = @import("std"); 2 const microzig = @import("microzig"); 3 const hal = microzig.hal; 4 const board = microzig.board; 5 6 fn delay() void { 7 var i: u32 = 0; 8 while (i < 800_000) { 9 asm volatile ("nop"); 10 i += 1; 11 } 12 } 13 14 pub fn main() !void { 15 const pins, const all_leds = res: { 16 if (comptime std.mem.eql(u8, microzig.config.chip_name, "STM32F103C8")) { 17 const pins = (hal.pins.GlobalConfiguration{ .GPIOC = .{ 18 .PIN13 = .{ .name = "led", .mode = .{ .output = .general_purpose_push_pull } }, 19 } }).apply(); 20 const all_leds = .{ 21 pins.led, 22 }; 23 break :res .{ pins, all_leds }; 24 } else if (comptime microzig.config.board_name != null and std.mem.eql(u8, microzig.config.board_name.?, "STM32F303NUCLEO")) { 25 const pins = board.leds_config.apply(); 26 const all_leds = .{ 27 pins.LD2, 28 }; 29 break :res .{ pins, all_leds }; 30 } else if (comptime microzig.config.board_name != null and std.mem.eql(u8, microzig.config.board_name.?, "STM32F3DISCOVERY")) { 31 const pins = board.leds_config.apply(); 32 const all_leds = .{ 33 pins.LD3, 34 pins.LD4, 35 pins.LD5, 36 pins.LD6, 37 pins.LD7, 38 pins.LD8, 39 pins.LD9, 40 pins.LD10, 41 }; 42 break :res .{ pins, all_leds }; 43 } else if (comptime microzig.config.board_name != null and std.mem.eql(u8, microzig.config.board_name.?, "STM32L476DISCOVERY")) { 44 const pins = board.leds_config.apply(); 45 const all_leds = .{ 46 pins.LD4, 47 pins.LD5, 48 }; 49 break :res .{ pins, all_leds }; 50 } else { 51 @compileError("blinky is not (yet?) implemented for this target"); 52 } 53 }; 54 _ = pins; 55 56 while (true) { 57 delay(); 58 for (0..all_leds.len) |k| { 59 switch (@as(u3, @intCast(k))) { 60 inline else => |i| { 61 if (i >= all_leds.len) unreachable; 62 all_leds[i].toggle(); 63 }, 64 } 65 } 66 } 67 }