diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffdb12e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +zig-* \ No newline at end of file diff --git a/blinky-stm32f4/README.md b/blinky-stm32f4/README.md new file mode 100644 index 0000000..18defb4 --- /dev/null +++ b/blinky-stm32f4/README.md @@ -0,0 +1,5 @@ +# STM32 Blinky + +This is an example project for getting started with microzig +on STM32. I used an F407 discovery board, but it should work +with any F4XX based board. diff --git a/blinky-stm32f4/build.zig b/blinky-stm32f4/build.zig new file mode 100644 index 0000000..d95fa78 --- /dev/null +++ b/blinky-stm32f4/build.zig @@ -0,0 +1,36 @@ +const std = @import("std"); +const microzig = @import("microzig/src/main.zig"); + +pub fn build(b: *std.build.Builder) void { + const backing = .{ + .chip = microzig.chips.stm32f407vg + }; + var elf = microzig.addEmbeddedExecutable( + b, + "firmware.elf", + "src/main.zig", + backing, + .{}, + ); + + elf.setBuildMode(.ReleaseSmall); + elf.install(); + + const bin = b.addInstallRaw(elf.inner, "firmware.bin", .{}); + const bin_step = b.step("bin", "Generate binary file to be flashed"); + bin_step.dependOn(&bin.step); + + const flash_cmd = b.addSystemCommand(&[_][]const u8{ + "st-flash", + "write", + b.getInstallPath(bin.dest_dir, bin.dest_filename), + "0x8000000", + }); + + flash_cmd.step.dependOn(&bin.step); + const flash_step = b.step("flash", "Flash and run the app on your STM32 device"); + flash_step.dependOn(&flash_cmd.step); + + b.default_step.dependOn(&elf.inner.step); + b.installArtifact(elf.inner); +} diff --git a/blinky-stm32f4/microzig b/blinky-stm32f4/microzig new file mode 120000 index 0000000..3f3e4ab --- /dev/null +++ b/blinky-stm32f4/microzig @@ -0,0 +1 @@ +/home/connor/workspace/connorrigby/underglow-controller-ng/firmware/microzig/ \ No newline at end of file diff --git a/blinky-stm32f4/src/main.zig b/blinky-stm32f4/src/main.zig new file mode 100644 index 0000000..ddbe4c9 --- /dev/null +++ b/blinky-stm32f4/src/main.zig @@ -0,0 +1,25 @@ +const microzig = @import("microzig"); +const chip = microzig.chip; +const regs = chip.registers; +const cpu = microzig.cpu; + +// The LED PIN we want to use +const PA1 = chip.parsePin("PA1"); + +pub fn main() !void { + // above can also be accomplished with microzig with: + chip.gpio.setOutput(PA1); + + while (true) { + // Read the current state of the gpioa register. + var gpioa_state = regs.GPIOA.ODR.read(); + + // we can use this to invert the current state easily + regs.GPIOA.ODR.modify(.{ .ODR1 = ~gpioa_state.ODR1 }); + + // Burn CPU cycles to delay the LED blink + var i: u32 = 0; + while (i < 600000) : (i += 1) + cpu.nop(); + } +}