Zig avr microcontroller build system

Hello, I was wondering if anyone have had any success in using zig as a build system for avr microcontrollers? I am struggling to link avr-libc. I tried the following, it compiles but the program is empty (not recognized by avrdude)

const std = @import("std");
const Builder = std.Build;

pub fn build(b: *std.Build) !void {
    const uno = std.Target.Query{
        .cpu_arch = .avr,
        .cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
        .os_tag = .freestanding,
        .ofmt = .elf
    };

    const exe = b.addExecutable(.{
        .name = "blinker",
        .target = b.resolveTargetQuery(uno),
        .use_llvm = true,
        .optimize = .ReleaseSafe,
    });

    exe.addCSourceFile(.{ .file = b.path("src/main.c"), .flags = &.{"-mmcu=atmega328p","-DF_CPU=16000000UL","-nostartfiles"} });
    exe.addIncludePath(Builder.LazyPath{.cwd_relative = "/usr/avr/include/"});
    b.installArtifact(exe);

I don’t know anything, but have you taken a look at Zig Embedded Group · GitHub ?

They list support for AVR.

I had some (little) experience with AVR in Zig, but I used bash script to build, something like this

#!/bin/bash

rm -f prog.*
rm -f *.o
ZC=/opt/zig-0.12/zig
export LANG=C
$ZC build-obj -O ReleaseFast -target avr-freestanding-none -mcpu atmega328p main.zig &&
avr-ld -o prog.elf main.o -Tdata 0x800100 &&
avr-objcopy -j .text -j .data -O ihex prog.elf prog.hex &&
avr-objdump -d prog.elf > prog.dmp && rm -f *.o

However I guess those commands can be invoked from build.zig (I personally do not know how exactly).

1 Like

How do you declare your main() function?
I do not remember where I took it from, but I used

export fn main () noreturn {

not pub fn.

1 Like

Just in case, virtually all my experience in Zig+AVR8 is here and here.

1 Like