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

Zig libc #2

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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-*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libc/lib/ziglibc"]
path = libc/lib/ziglibc
url = https://github.com/marler8997/ziglibc.git
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();
}
}
4 changes: 4 additions & 0 deletions libc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# MicroZig LibC

This is an example project for getting started with microzig
and linking LibC for compilation with C projects.
47 changes: 47 additions & 0 deletions libc/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const std = @import("std");
const microzig = @import("microzig/src/main.zig");
const ziglibc = @import("lib/ziglibc/ziglibcbuild.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,
.{},
);

// Link the Zig LibC implementation
var libc = ziglibc.addLibc(b, .{
.variant = .only_std,
.link = .static,
.start = .ziglibc,
.trace = false,
.target = microzig.Backing.getTarget(backing),
});
libc.install();

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 libc/lib/ziglibc
Submodule ziglibc added at d6b695
1 change: 1 addition & 0 deletions libc/microzig
5 changes: 5 additions & 0 deletions libc/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const microzig = @import("microzig");

pub fn main() !void {

}