Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Add Blinky stm32 example #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zig-*
5 changes: 5 additions & 0 deletions blinky-stm32f4/README.md
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 36 additions & 0 deletions blinky-stm32f4/build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions blinky-stm32f4/microzig
25 changes: 25 additions & 0 deletions blinky-stm32f4/src/main.zig
Original file line number Diff line number Diff line change
@@ -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();
}
}