zigembeddedgroup / microzig Goto Github PK
View Code? Open in Web Editor NEWUnified abstraction layer and HAL for several microcontrollers
License: zlib License
Unified abstraction layer and HAL for several microcontrollers
License: zlib License
I am having trouble importing a C library.
I want to import pico-sdk to use the CYW43 driver on a Raspberry Pi Pico W. I found a method firmware.add_include_path()
for specifying an include path, which is likely to work like exe.addIncludePath()
, but it does not work.
build.zig
const MicroZig = @import("microzig/build");
const rp2040 = @import("microzig/bsp/raspberrypi/rp2040");
pub fn build(b: *std.Build) void {
const mz = MicroZig.init(b, .{});
const optimize = b.standardOptimizeOption(.{});
const firmware = mz.add_firmware(b, .{
.name = "main",
.target = rp2040.boards.raspberrypi.pico,
.optimize = optimize,
.root_source_file = .{ .path = "src/main.zig" },
});
firmware.add_include_path(.{ .path = "pico-sdk/src/common/pico_base/include" });
firmware.add_include_path(.{ .path = "pico-sdk/src/common/pico_stdlib/include" });
firmware.add_include_path(.{ .path = "pico-sdk/src/rp2_common/pico_cyw43_arch/include" });
mz.install_firmware(b, firmware, .{});
mz.install_firmware(b, firmware, .{ .format = .elf });
}
build.zig.zon
.{
.name = "project",
.version = "0.1.0",
.dependencies = .{
.@"microzig/build" = .{
.url = "https://downloads.microzig.tech/microzig-0.12.0/build-0.0.0.tar.gz",
.hash = "1220362d63876dd7ad88d0ee55ab24c2eb7fc49b0441703111d98d7b78bf315ebf70",
},
.@"microzig/bsp/raspberrypi/rp2040" = .{
.url = "https://downloads.microzig.tech/microzig-0.12.0/bsp/raspberrypi/rp2040-0.0.0.tar.gz",
.hash = "1220a7f44ffad57f0c59bfc4ae68632c274c5762795854dbe04dacbb086bb05856fa",
},
},
.paths = .{
"LICENSE",
"README.md",
"build.zig",
"build.zig.zon",
"src",
"scripts",
},
.minimum_zig_version = "0.12.0",
}
src/main.zig
const std = @import("std");
const microzig = @import("microzig");
const c = @cImport({
@cInclude("pico/cyw43_arch.h");
});
pub fn main() !void {
c.cyw43_arch_init();
}
pico-sdk is located in /home/***/path/to/project as a git submodule.
result:
$ zig build -Doptimize=ReleaseSmall
install
└─ install generated to main.uf2
└─ run elf2uf2 (main.uf2)
└─ zig build-exe main ReleaseSmall thumb-freestanding-eabi 2 errors
src/main.zig:4:11: error: C import failed
const c = @cImport({
^~~~~~~~
src/main.zig:4:11: note: libc headers not available; compilation does not link against libc
referenced by:
main: src/main.zig:12:5
microzig_main: /home/***/.cache/zig/p/1220d6f088f42b4d34f832990e0cbcf790b4538a7e5a7a720c13bf07a4b1241089c8/src/start.zig:126:18
remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/***/path/to/project/zig-cache/o/2757200903cccd5f17dd400c1960e67d/cimport.h:1:10: error: 'pico/cyw43_arch.h' file not found
#include <pico/cyw43_arch.h>
^
error: the following command failed with 2 compilation errors:
/home/***/.zig/zig-linux-x86_64-0.12.0/zig build-exe -OReleaseSmall -target thumb-freestanding-eabi -mcpu cortex_m0plus -I /home/***/path/to/project/pico-sdk/src/common/pico_stdlib/include -I /home/***/path/to/project/pico-sdk/src/rp2_common/pico_cyw43_arch/include --dep app --dep microzig -Mroot=/home/***/.cache/zig/p/1220d6f088f42b4d34f832990e0cbcf790b4538a7e5a7a720c13bf07a4b1241089c8/src/start.zig --dep microzig -Mapp=/home/***/path/to/project/src/main.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=/home/***/.cache/zig/p/1220d6f088f42b4d34f832990e0cbcf790b4538a7e5a7a720c13bf07a4b1241089c8/src/microzig.zig -Mconfig=/home/***/path/to/project/zig-cache/c/cb74838774d212d46a4b6ad47bca5bcc/options.zig --dep microzig -Mchip=/home/***/path/to/project/zig-cache/o/01a9a7f0711c6fac54cec6ec4ab59da3/chip.zig --dep microzig -Mcpu=/home/***/.cache/zig/p/1220d6f088f42b4d34f832990e0cbcf790b4538a7e5a7a720c13bf07a4b1241089c8/src/cpus/cortex-m.zig --dep microzig -Mhal=/home/***/.cache/zig/p/1220a7f44ffad57f0c59bfc4ae68632c274c5762795854dbe04dacbb086bb05856fa/src/hal.zig --dep microzig --dep bootloader -Mboard=/home/***/.cache/zig/p/1220a7f44ffad57f0c59bfc4ae68632c274c5762795854dbe04dacbb086bb05856fa/src/boards/raspberry_pi_pico.zig -Mbootloader=/home/***/path/to/project/zig-cache/o/e9a2f6a4c63be6395f5681702ab50778/w25q080.bin --cache-dir /home/***/path/to/project/zig-cache --global-cache-dir /home/***/.cache/zig --name main -static -fcompiler-rt --script /home/***/.cache/zig/p/1220a7f44ffad57f0c59bfc4ae68632c274c5762795854dbe04dacbb086bb05856fa/rp2040.ld --listen=-
Build Summary: 8/13 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
├─ install generated to main.uf2 transitive failure
│ └─ run elf2uf2 (main.uf2) transitive failure
│ └─ zig build-exe main ReleaseSmall thumb-freestanding-eabi 2 errors
└─ install generated to main.elf transitive failure
└─ zig build-exe main ReleaseSmall thumb-freestanding-eabi (+6 more reused dependencies)
error: the following build command failed with exit code 1:
/home/***/path/to/project/zig-cache/o/c9cafd921ac2f4c0d5c2c804c98770b0/build /home/***/.zig/zig-linux-x86_64-0.12.0/zig /home/***/path/to/project /home/***/path/to/project/zig-cache /home/***/.cache/zig --seed 0x321ad4c6 -Z5ce5c6b4c9fc2a23 -Doptimize=ReleaseSmall
It seems correct include paths are specified (-I
) in the zig build-exe
command.
Alternatively, using an absolute path works, but #include
s in the C files cannot be resolved.
src/main.zig
const c = @cImport({
@cInclude("/home/***/path/to/project/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h");
});
result:
/home/***/path/to/project/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h:10:10: error: 'pico.h' file not found
#include "pico.h"
^
I found the packages
options that I need to pass to addEmbeddedExecutable
.
Is there a way to either make it work or make calling addPackagePath
afterward an error? If not, maybe add a bigger note in the documentation.
If I clone the repository and run zig build
, zig-out folder isn't generated. Only zig-cache is. Tried with this compiler zig-linux-x86_64-0.11.0-dev.2317+46b2f1f70.
I would expect for test programs to compile.
Right now in raspberrypi-rp2040 we've had to hardcode the linker script to allow a boot2 section, time_critical section, and have all of sram executable so that some of it may be executed by the processor. Linker scripts can be pretty complex, but maybe we can reduce the feature to only what's typically required in embedded systems. If users are already at the point where they need very specific features or linker script layouts then it's already easy enough for them to write their own.
A documentation page for this repo would be very helpful for new embedded programmers who want to get into embedded development with Zig. I would be very willing to aid in a project for making a static site on github of all the documentation, maybe in a javadocs style of documentation. Would this be a feature that people would want? Would there be other people willing to aid in this project?
Revert the #153 workaround for ziglang/zig#17882, once we switch to Zig 0.12.0 (or 0.11.1), assuming that newer version fixes the issue.
Bugs:
Features:
Nice-to-have:
I installed microzig as a dependency in my zig project using zig fetch {tarball from downloads.microzig.tech}, copypasted main.zig file from your examples folder for raspbery pi pico, copipasted build.zig file from examples root, deleted other build projects except blinky.
When I run zig build:
error
zig/blink/build.zig:2:26: error: no module named 'microzig/build' available within module root.@build
const MicroZig = @import("microzig/build");
uf2 or elf file builds ready to flash on my pico board
shows that in microzig build.zig.zon .build dependency not marked with @ so i believe this is a some sort of private dependency. I tried to change import to just @import( microzig)
, but then i receive error init method not found.
zig env(not full removed path variables)
"target": "x86_64-linux.6.9.12...6.9.12-gnu.2.39",
"env": {
"ZIG_GLOBAL_CACHE_DIR": null,
"ZIG_LOCAL_CACHE_DIR": null,
"ZIG_LIB_DIR": null,
"ZIG_LIBC": null,
"ZIG_BUILD_RUNNER": null,
"ZIG_VERBOSE_LINK": null,
"ZIG_VERBOSE_CC": null,
"ZIG_BTRFS_WORKAROUND": null,
"ZIG_DEBUG_CMD": null,
"CC": null,
"NO_COLOR": null,
"CLICOLOR_FORCE": null,
"XDG_CACHE_HOME": null,
Thank you in advance for your help.
I'd like to have a log.zig logFn that I could use to emit debug logs over USB, rather than UART.
Currently Microzig has support for acting as a USB HID device. I believe emitting debug logs over USB would require support for acting as a "serial device", which uses USB CDC/ADC.
New build interface can handle several formats, but the ESP image format is missing:
How do I get started ? The instructions on setting up a new project seem out of date.
I would like to develop for raspberry pi pico.
I think this may be a bug in regz, not microzig, or maybe whatever svd file that was used to generate the register definitions. afaik All Cortex devices have the same definition of this register. I coppied a samd one and it worked without issue
Expose common functions like install()
and setBuildMode()
, also installRaw()
We had a nice support matrix on the ZEG Front Page, but it was hand-maintained. This didn't really scale well, so the idea is that the tools/bundle.py
also creates a file https://downloads.microzig.tech/.data/bsp-overview.svg that shows a table of all board support packages and their current status
Hi all I'm new to the project as studying the source code to get a better handle on how MicroZig works
I have a question regarding the .memory_regions
specified in
microzig/bsp/microchip/avr/build.zig
Lines 26 to 27 in ea809c6
To help me get a better understanding I have pulled the ATmega328P.atdf
directly in conjunction to the hardware reference manual
Based on this the memory address starts at 0x000000
with the following block sizes
Sector | Size | Offset |
---|---|---|
prog | 0x8000 | 0x00000 |
signatures | 0x0003 | 0x08000 |
fuses | 0x0003 | 0x08003 |
lockbits | 0x0001 | 0x08006 |
data.ram | 0x0100 | 0x08007 |
I'm not sure how to get to the ram offset 0x800100
as set here:
.memory_regions = &.{
.{ .offset = 0x000000, .length = 32 * 1024, .kind = .flash },
.{ .offset = 0x800100, .length = 2048, .kind = .ram },
},
My end-game here is to add support for AVR8 - tinyAVR
- specifically the Attiny85
Similar to how an app can have it's own packages, create another option to the build function that adds a "hal" package. This is a package that only depends on microzig and would be used to write drivers for chips with similar peripherals, Eg. an nrf52 hal.
An application would be able to access the hal via microzig.hal
, and with it's access to other microzig namespaces it would be able to do conditional compilation for different chips if the need arose.
While updating the ESP32-C3 .svd
file, I noticed that register groups that had dimIndex
fields added were suddenly missing in chip.zig
and were replaced with reserved
fields.
To reproduce, replace the ESP32-C3 .svd
file with one found in the esp-pacs repo, or patch it and use the patched version and re-build and check the GPIO.PINn
registers.
Currently, USB descriptors have a Serialize
function that returns an array of bytes. The byte order is handled manually for every integer greater than u8. Instead, Serialize
should accept something like the BufferWriter
proposed in #201 (probably with an additional field that indicates buffer endianness) with writeInt
function that utilizes std.mem.writeInt
that would accept buffer endianness. With such implementation we would not have to handle byte order manualy in each serialize implementation.
Something like:
pub fn serialize(self: *@This(), bw: *BufferWriter) !void {
try bw.writeInt(u8, 8);
try bw.writeInt(u8, @intFromEnum(self.descriptor_type));
try bw.writeInt(u16, self.report_length); // u16, BufferWritter will take care of proper byte order
...
}
STM32F103 sad file did not define which cortex-m core it had so it wasn't able to output a vector table. The resulting code causes a compiler error where it should succeed. BUT if the user tries to specify interrupt handlers they should get a compiler error with an explanation.
This work requires upstreaming to https://github.com/ziglang/zig
As a repro compile this program with the following build command:
export fn foo(a: i32, b: i32) i32 {
return a * b;
}
zig build-exe -target avr-freestanding -mcpu=avr5 test.zig -O ReleaseSmall
and you'll see the following output:
LLVM ERROR: Not supported instr: <MCInst 312 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:53>>
note: doing this in a basic program that is being built by the microzig build function, it does not bundle compiler_rt, so you'll see a missing definition:
ld.lld: error: undefined symbol: __mulsi3
This issue tracks the support status for ESP32 series Espressif SoCs and will be updated with new tasks continously:
I have been struggling to run USB device code, e.g. the HID demo, on any rp2040 board because the program always freezes in the call to rp2040.usb.Usb.init_clk()
. However, @arkadiuszwojcik helped me debug tonight and found that it is possible to get it to work with a timing hack.
Modify the example usb_hid.zig
around the clock initialization:
...
std.log.debug("one", .{});
// First we initialize the USB clock
rp2040.usb.Usb.init_clk();
std.log.debug("two", .{});
...
Run zig build -Doptimize=ReleaseSmall
.
When flashing a Raspberry Pi Pico H or similar board, UART prints one
but not two
, and the LED never blinks.
Modify bsp/raspberrypi/rp2040/src/hal/resets.zig
such that the empty while loop has at least one NOP instruction:
pub fn reset(mask: Mask) void {
...
while ((RESETS.RESET_DONE.raw & raw_mask) != raw_mask) {
asm volatile (
\\ nop
);
}
}
...and suddenly, the LED blinks and we see two
in the log. It also suffices to add some log lines inside reset()
to change the timing of operations, although it may not be as consistent.
With -Doptimize=Debug
, there is no issue with or without the asm
block. With ReleaseSmall
, it is as described above. With ReleaseFast
and ReleaseSafe
, I still get freezing even with the asm
block.
As a reference point, the stock pico-examples/usb/device/dev_hid_composite
example from Raspberry Pi / TinyUSB works out of the box for me.
The above findings are true both on main (bb88889) and based on #211.
I've been working on a Keyboard for a while based on the ATmega32U4 and would love to someday have the firmware written in Zig.
@MasterQ32
I have:
COMM_SCK_RATE_3BIT
enum, because it was missing a size
fieldI cannot test until the dependencies are updated to latest zig which removed LazyPath.path
It should just assert/panic if an error happens, and probably give some reason why.
I am currently revisiting computer architecture and design for my personal project of building an operating system. In an attempt to tap into my dormant knowledge of the hardware-software concepts, I would like to contribute and add support for the SparkFun RED-V board. It has a RISC-V chip from SiFive, FE310.
I am having troubles getting examples building with the monorepo. Reporting my issues as requested.
pathspec
with the negate
kwarg in match_tree
, but requirements.txt doesn't pin a version. This used 0.11.0 on my machine, but negate
shows up in 0.12.0--hard-deference
option from tar
, which is not a valid option for OSX tar
. I suggest using tarlib
. I got around this, however, by installing gnutar
with brew
and symlinking it to tar
on my PATH
.For AVR, it must be in RAM to be efficiently accessed, for ARM devices, it is less important
On a personal project I'm working on, I recently ran into an issue in which either importing MicroZig directly or using something that relies on it inside of a test
block causes a compilation error. When running using a test step (e.g. zig build test
), the error is no module named 'microzig' available within module root
. When running using zig test
, the error is no module named 'microzig' available within module test
. The fix for this was adding a line to build.zig
that imports the MicroZig module within the test step:
const test_run_step = b.step("test", "Run unit tests");
const driver_tests = b.addTest(.{
.name = "driver_tests",
.root_source_file = b.path("source/drivers.zig"),
.target = b.graph.host,
});
driver_tests.root_module.addImport("microzig", firmware.modules.microzig); // adding this line fixed the issue
const driver_test_run = b.addRunArtifact(driver_tests);
test_run_step.dependOn(&driver_test_run.step);
Since this is slightly unintuitive, it'd be nice to have direct support for this from within the MicroZig framework.
ziglang/zig#19719 replaced ComptimeStringMap
with StaticStringMap
.
Hey all,
What's the procedure for porting Microzig to MCUs that are neither Microchip/Atmel or ARM? Do we need to create our own SVD/ATDF? Is there documentation on those protocols?
The field exists in TransferConfig
but in trigger_transfer
the .DATA_SIZE
register is always set to .SIZE_BYTE
microzig-0.12.1
zvm use 0.12.1
"I'm using zig version manager"zig version
0.12.1
zig build
got this error
install
└─ install generated to arduino-nano_blinky.elf
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi
└─ run regz (chip.zig) stderr
warning(gen.avr): skipping interrupt: RESET
install
└─ install generated to arduino-nano_blinky.hex
└─ objcopy generated
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
error: error: Alias and aliasee types don't match (Producer: 'zig 0.12.1' Reader: 'LLVM 17.0.6')
error: the following command exited with error code 1:
/home/user/.zvm/0.12.1/zig build-exe -fno-strip -ODebug -target avr-freestanding-eabi -mcpu avr5 --dep app --dep microzig -Mroot=/home/user/Desktop/langs/zig/microzig/core/src/start.zig --dep microzig -Mapp=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/src/blinky.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=/home/user/Desktop/langs/zig/microzig/core/src/microzig.zig -Mconfig=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/c/b7dbf49b12dfe6251df3b8ecfbe05693/options.zig --dep microzig -Mchip=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/o/75bb04ecf231c60cf8e1c8e4d2578691/chip.zig --dep microzig -Mcpu=/home/user/Desktop/langs/zig/microzig/core/src/cpus/avr5.zig --dep microzig -Mhal=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/hals/ATmega328P.zig --dep microzig -Mboard=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/boards/arduino_nano.zig --cache-dir /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache --global-cache-dir /home/user/.cache/zig --name arduino-nano_blinky -static -fcompiler-rt --script /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/o/8ec71f2f41da598cdc3580fe5e9713b2/linker.ld --listen=-
install
└─ install generated to arduino-nano_blinky.elf
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
error: error: Alias and aliasee types don't match (Producer: 'zig 0.12.1' Reader: 'LLVM 17.0.6')
error: the following command exited with error code 1:
/home/user/.zvm/0.12.1/zig build-exe -fno-strip -ODebug -target avr-freestanding-eabi -mcpu avr5 --dep app --dep microzig -Mroot=/home/user/Desktop/langs/zig/microzig/core/src/start.zig --dep microzig -Mapp=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/src/blinky.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=/home/user/Desktop/langs/zig/microzig/core/src/microzig.zig -Mconfig=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/c/532d9857952d6eb83abf0684d7170f3c/options.zig --dep microzig -Mchip=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/o/75bb04ecf231c60cf8e1c8e4d2578691/chip.zig --dep microzig -Mcpu=/home/user/Desktop/langs/zig/microzig/core/src/cpus/avr5.zig --dep microzig -Mhal=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/hals/ATmega328P.zig --dep microzig -Mboard=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/boards/arduino_uno.zig --cache-dir /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache --global-cache-dir /home/user/.cache/zig --name arduino-nano_blinky -static -fcompiler-rt --script /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/o/8ec71f2f41da598cdc3580fe5e9713b2/linker.ld --listen=-
Build Summary: 10/19 steps succeeded; 2 failed (disable with --summary none)
install transitive failure
├─ install generated to arduino-nano_blinky.hex transitive failure
│ └─ objcopy generated transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
├─ install generated to arduino-nano_blinky.elf transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi (+5 more reused dependencies)
├─ install generated to arduino-nano_blinky.hex transitive failure
│ └─ objcopy generated transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
└─ install generated to arduino-nano_blinky.elf transitive failure
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi (+5 more reused dependencies)
error: the following build command failed with exit code 1:
/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache/o/73b2bc3bf8a8a7ad75e3338ebf7cbdab/build /home/user/.zvm/0.12.1/zig /home/user/Desktop/langs/zig/microzig/examples/microchip/avr /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/zig-cache /home/user/.cache/zig --seed 0x407df32b -Z23cd44f0d97efb46
I also tried zig version 0.13.0
on microzig main
branch
main
zvm use 0.13.0
zig version
0.13.0
zig build
got this error:
install
└─ install generated to arduino-nano_blinky.hex
└─ objcopy generated
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
error: error: Alias and aliasee types don't match (Producer: 'zig 0.13.0' Reader: 'LLVM 18.1.6')
error: the following command exited with error code 1:
/home/user/.zvm/0.13.0/zig build-exe -fno-strip -ODebug -target avr-freestanding-eabi -mcpu avr5 --dep app --dep microzig -Mroot=/home/user/Desktop/langs/zig/microzig/core/src/start.zig --dep microzig -Mapp=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/src/blinky.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=/home/user/Desktop/langs/zig/microzig/core/src/microzig.zig -Mconfig=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/c/841e6415eb3670fb63333cba5a60bb1f/options.zig --dep microzig -Mchip=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/o/aae59feccd186b1cf63a4dbef831a0f6/chip.zig --dep microzig -Mcpu=/home/user/Desktop/langs/zig/microzig/core/src/cpus/avr5.zig --dep microzig -Mhal=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/hals/ATmega328P.zig --dep microzig -Mboard=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/boards/arduino_nano.zig --cache-dir /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache --global-cache-dir /home/user/.cache/zig --name arduino-nano_blinky -static -fcompiler-rt --script /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/o/7e0d147b389a39217335478cd3d90c1d/linker.ld --listen=-
install
└─ install generated to arduino-nano_blinky.hex
└─ objcopy generated
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
error: error: Alias and aliasee types don't match (Producer: 'zig 0.13.0' Reader: 'LLVM 18.1.6')
error: the following command exited with error code 1:
/home/user/.zvm/0.13.0/zig build-exe -fno-strip -ODebug -target avr-freestanding-eabi -mcpu avr5 --dep app --dep microzig -Mroot=/home/user/Desktop/langs/zig/microzig/core/src/start.zig --dep microzig -Mapp=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/src/blinky.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=/home/user/Desktop/langs/zig/microzig/core/src/microzig.zig -Mconfig=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/c/c9f84279a663a8e3f95aa28cca0320d5/options.zig --dep microzig -Mchip=/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/o/aae59feccd186b1cf63a4dbef831a0f6/chip.zig --dep microzig -Mcpu=/home/user/Desktop/langs/zig/microzig/core/src/cpus/avr5.zig --dep microzig -Mhal=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/hals/ATmega328P.zig --dep microzig -Mboard=/home/user/Desktop/langs/zig/microzig/bsp/microchip/avr/src/boards/arduino_uno.zig --cache-dir /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache --global-cache-dir /home/user/.cache/zig --name arduino-nano_blinky -static -fcompiler-rt --script /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/o/7e0d147b389a39217335478cd3d90c1d/linker.ld --listen=-
Build Summary: 10/19 steps succeeded; 2 failed (disable with --summary none)
install transitive failure
├─ install generated to arduino-nano_blinky.hex transitive failure
│ └─ objcopy generated transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
├─ install generated to arduino-nano_blinky.elf transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi (+5 more reused dependencies)
├─ install generated to arduino-nano_blinky.hex transitive failure
│ └─ objcopy generated transitive failure
│ └─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi failure
└─ install generated to arduino-nano_blinky.elf transitive failure
└─ zig build-exe arduino-nano_blinky Debug avr-freestanding-eabi (+5 more reused dependencies)
error: the following build command failed with exit code 1:
/home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache/o/f01e92ffc6e794b769d25c8f55d5f3c3/build /home/user/.zvm/0.13.0/zig /home/user/Desktop/langs/zig/microzig/examples/microchip/avr /home/user/Desktop/langs/zig/microzig/examples/microchip/avr/.zig-cache /home/user/.cache/zig --seed 0xbd69eda3 -Z357bb93fef78b7f9
Thanks in advance.
edit:
I forgot to mention that I'm using ubuntu 24.04
I'm attempting to use microzig, and I'm also very new to zig and the instructions in README.adoc
assume that you have a lot of knowledge about zig and how microzig works.
I attempted to use the sample and despite inferring how to make it work, even with modifications, it fails to compile.
run: zig init-exe
undocumented commands (implied via the vague statement: add the hardware support package as a submodule
, this is also the "simple way" as zig init-exe
doesn't do a git init
, so can't do some git submodule
command to add it):
mkdir deps
cd deps
git clone --recursive https://github.com/ZigEmbeddedGroup/microchip-atmega
cd ..
replace build.zig
(undocumented what file, and need to replace hardware_support_package
with microchip-atmega
and replace root_source_file
with source_file
) w/:
const std = @import("std");
const atmega = @import("deps/microchip-atmega/build.zig");
// the hardware support package should have microzig as a dependency
const microzig = @import("deps/microchip-atmega/deps/microzig/build.zig");
pub fn build(b: *std.build.Builder) !void {
const optimize = b.standardOptimizeOption(.{});
var exe = microzig.addEmbeddedExecutable( b, .{
.name = "my-executable",
.source_file = .{
.path = "src/main.zig",
},
.backing = .{
.board = atmega.boards.arduino_nano,
// instead of a board, you can use the raw chip as well
// .chip = atmega.chips.atmega328p,
},
.optimize = optimize,
});
exe.install();
}
replace src/main.zig
with the next block (undocumented what file to put it in).
run zig build
and see the following failure:
$ zig build
zig build-exe my-executable Debug avr-freestanding-eabi: error: the following command terminated unexpectedly:
[xxx]/github/zig/build/zig2 build-exe /private/tmp/mcre/deps/microchip-atmega/deps/microzig/src/start.zig -fno-strip --cache-dir /private/tmp/mcre/zig-cache --global-cache-dir [xxx]/.cache/zig --name my-executable -fno-compiler-rt -fsingle-threaded -target avr-freestanding-eabi -mcpu avr5 --script /private/tmp/mcre/zig-cache/microzig/bad2341405a5fe8c06f6286f91ea1100/ATmega328P_AVR5.ld --mod microzig:config,chip,cpu,hal,board:/private/tmp/mcre/deps/microchip-atmega/deps/microzig/src/microzig.zig --mod chip:microzig:/private/tmp/mcre/deps/microchip-atmega/src/chips/ATmega328P.zig --mod board:microzig:/private/tmp/mcre/deps/microchip-atmega/src/boards/arduino_nano.zig --mod hal:microzig:/private/tmp/mcre/deps/microchip-atmega/src/hals/ATmega328P.zig --mod app:microzig:/private/tmp/mcre/src/main.zig --mod cpu:microzig:/private/tmp/mcre/deps/microchip-atmega/deps/microzig/src/modules/cpus/avr5.zig --mod config::/private/tmp/mcre/zig-cache/microzig/config-6df6165a62f30831cad66fa94379f8d3.zig --deps app,microzig --enable-cache --listen=-
Build Summary: 1/4 steps succeeded; 1 failed (disable with -fno-summary)
install transitive failure
└─ install my-executable transitive failure
└─ zig build-exe my-executable Debug avr-freestanding-eabi failure
└─ linkerscript success
error: the following build command failed with exit code 1:
/private/tmp/mcre/zig-cache/o/b840765e239219fa1562b810d024cd97/build [xxx]/github/zig/build/zig2 /private/tmp/mcre /private/tmp/mcre/zig-cache [xxx]/.cache/zig
environment: MacOS Ventura 13.2
zig version: 0.11.0-dev.2249+dcdb87836
I'm unsure what to do now to make the necessary executable.
This is a self-reported bug for my recent code change in the USB driver that introduced chunked data (packet split). The current implementation allows for max of 2 packets of response data, which is enforced by the TMP buffer size of 128 bytes (2 * 64). This is probably enough for all command response cases. However, there is a bug that could manifest if the buffer size is extended in the future to support 3 or more packets.
Short version:
Bug is one liner, we should use try_peek
instead of try_read
when sending next chunk of data
Long version:
I reproduced this bug in this branch where last commit is solution, and previous commit is bug introduction. To enforce 3 packets in USB hand shake I extended buffor size and put a lot of redundant data. In first case (without fix) bug is reproduced and Windows generates error on USB hand shake. After fix everything is OK, device is detected correctly. We should use try_peek
that returns data to consume and don't advance buffor read cursor, because we do that manualy after we receive confirmation that chunk/packet of data was sent.
I'm sorry for that mistake. I will create PR shortly.
CC: @r4gus
Hello!
Thank you for your work on that project.
I start to use it in development. I want to write some simple apps to understand if it works.
I want to be able to format a string with numbers.
I need help with the allocator issue. I need help finding a way to use the standard library functions that require an allocator instance.
Example:
const allocator = os.heap.page_allocator;
const string1 = try std.fmt.allocPrint(allocator, "{d}", .{count()});
defer allocator.free(string1);
microzig.hal.uart.write(0, string1);
Gives me the error:
error: root struct of file 'start' has no member named 'os'
root.os.heap.page_allocator
Is there a way to get the allocator in the Microzig? or ESP32C2 environment? Can I resolve those issues without the allocator usage?
Thank you in advance.
Right now, the AVR support is degraded. With using avr-gcc
, we can make it work quite well though.
This requires updating the AVR code to use a different logic in the start code, and just implement a extern fn main() c_int
, that jumps into the microzig code and is happy about .bss
, .data
being already set up.
As-is, we cannot implement the whole startup logic ourselves via C backend, as some features are still missing.
Very roughly, the HAL needs to support 4 things:
I created a Google Doc which contains some overview of what features different MCUs have.
Those who want to join can send me a message with their email on discord, i'll invite you.
Some words on the future path:
Hey! Chiming in as someone doing embedded Rust things:
Just wanted to share particularly with respect to STM32 chips, which have an insane number of variants, SVD files that are often wrong, and a lot of challenging details, it might be worth looking at https://github.com/embassy-rs/stm32-data, which https://embassy.rs uses for the unified stm32 HAL. It is typically more actively patched and maintained for correctness than the upstream STM32 SVD data sources.
In general, SVD files often require patching and continued maintenance, https://github.com/embassy-rs/chiptool might be a good inspiration, which uses YAML files to apply patches and groupings, rather than hand-editing SVD files.
Feel free to close, or let me know if y'all would like to share notes on anything. Excited to see what you all build!
(edited title to make it more clear this is just my opinion, not like an "offical embedded rust position")
The new build interface can support several different output files, but DFU is missing
Currently, the number of supported platforms is rather minimal. If the owners are alright with it, I'd like to designate this issue as a tracking issue for boards/chips which are not yet implemented in here with the aim of getting a good selection supported.
To start off the list, here's a few chips I have come across:
Building a microzig project on a Raspberry Pi 2 fails with this error:
/home/pi/.cache/zig/p/1220af58bdaa721b8189f3a7adfda660517dd354463463388e96d69fe4ceccf80b92/build.zig:161:55: error: expected type 'usize', found 'u64'
.elf, .bin, .hex, .dfu, .esp => |val| {
^
/home/pi/.cache/zig/p/1220af58bdaa721b8189f3a7adfda660517dd354463463388e96d69fe4ceccf80b92/build.zig:161:55: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
What could be the reason for this? Maybe because the Pi's CPU is 32 bit, and u64 is hardcoded somewhere?
I'm trying to play around with the synth project from the 2023 sycl conference and I'm getting a build error when trying to use the latest microzig version. I've made some changes to the build script to try get it working, but I'm hitting an error now that seems to me to be related to microzig.
(I ran into a hash issue using the vanilla project deps which is why I'm trying to use the latest microzig).
% /c/src/zig/zig-windows-x86_64-0.12.0/zig.exe build
install
└─ install generated to uart_monitor.elf
└─ zig build-exe uart_monitor Debug thumb-freestanding-eabi 2 errors
C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\start.zig:70:81: error: expected type 'start.Options', found 'type'
pub const microzig_options: Options = if (@hasDecl(app, "microzig_options")) app.microzig_options else .{};
~~~^~~~~~~~~~~~~~~~~
C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\start.zig:46:21: note: struct declared here
pub const Options = struct {
^~~~~~
referenced by:
vector_table: C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\cpus\cortex_m.zig:151:50
remaining reference traces hidden; use '-freference-trace' to see all reference traces
C:\Users\jtebokkel\AppData\Local\zig\p\1220a557b7c940ea0193e8e4f516c6dab6d16dd41e528486889f356a2388930aae42\src\hal\irq.zig:2:39: error: struct 'chip.devices.RP2040.peripherals' has no member named 'NVIC'
const NVIC = microzig.chip.peripherals.NVIC;
~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
zig-cache\o\ce1fd5a7bdf3a724fe567c58a510dd57\chip.zig:66:33: note: struct declared here
pub const peripherals = struct {
^~~~~~
error: the following command failed with 2 compilation errors:
C:\src\zig\zig-windows-x86_64-0.12.0\zig.exe build-exe -fno-strip -ODebug -target thumb-freestanding-eabi -mcpu cortex_m0plus --dep app --dep microzig -Mroot=C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\start.zig --dep microzig --dep workshop -Mapp=C:\src\synth-workshop\demos\01_uart\monitor.zig --dep config --dep chip --dep cpu --dep hal --dep board -Mmicrozig=C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\microzig.zig --dep microzig -Mworkshop=C:\src\synth-workshop\src\workshop.zig -Mconfig=C:\src\synth-workshop\zig-cache\c\cb74838774d212d46a4b6ad47bca5bcc\options.zig --dep microzig -Mchip=C:\src\synth-workshop\zig-cache\o\ce1fd5a7bdf3a724fe567c58a510dd57\chip.zig --dep microzig -Mcpu=C:\Users\jtebokkel\AppData\Local\zig\p\1220a74a829bcd0e0cb6b5918646e20c68c2c47be9401e6f873a445453d4d8102027\src\cpus\cortex_m.zig --dep microzig -Mhal=C:\Users\jtebokkel\AppData\Local\zig\p\1220a557b7c940ea0193e8e4f516c6dab6d16dd41e528486889f356a2388930aae42\src\hal.zig --dep microzig --dep bootloader -Mboard=C:\Users\jtebokkel\AppData\Local\zig\p\1220a557b7c940ea0193e8e4f516c6dab6d16dd41e528486889f356a2388930aae42\src\boards\raspberry_pi_pico.zig -Mbootloader=C:\src\synth-workshop\zig-cache\o\730f41bc6b5f93cab44fdd7241cc9338\w25q080.bin --cache-dir C:\src\synth-workshop\zig-cache --global-cache-dir C:\Users\jtebokkel\AppData\Local\zig --name uart_monitor -static -fcompiler-rt --script C:\Users\jtebokkel\AppData\Local\zig\p\1220a557b7c940ea0193e8e4f516c6dab6d16dd41e528486889f356a2388930aae42\rp2040.ld --listen=-
Build Summary: 80/85 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
├─ install generated to uart_monitor.uf2 transitive failure
│ └─ run elf2uf2 (uart_monitor.uf2) transitive failure
│ └─ zig build-exe uart_monitor Debug thumb-freestanding-eabi 2 errors
└─ install generated to uart_monitor.elf transitive failure
└─ zig build-exe uart_monitor Debug thumb-freestanding-eabi (+9 more reused dependencies)
error: the following build command failed with exit code 1:
C:\src\synth-workshop\zig-cache\o\76617bd811a3831f1b1ec2d766d7f552\build.exe C:\src\zig\zig-windows-x86_64-0.12.0\zig.exe C:\src\synth-workshop C:\src\synth-workshop\zig-cache C:\Users\jtebokkel\AppData\Local\zig --seed 0x37e3cbaa -Z7c6f518096f843be
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.