• Stars
    star
    1,128
  • Rank 41,247 (Top 0.9 %)
  • Language
    Zig
  • License
    zlib License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Unified abstraction layer and HAL for several microcontrollers
logo text auto
824493524413710336
Note
This is in development; breaks in the API are bound to happen.

What version of Zig to use

Right now we are following master, but once 0.11.0 is released, we will be switching to the latest stable version of Zig.

Contributing

Please see the project page, it’s used as a place to brainstorm and organize work in ZEG. There will be issues marked as good first issue or drafts for larger ideas that need scoping/breaking ground on.

Introduction

This repo contains the infrastructure for getting started in an embedded Zig project; it "gets you to main()". Specifically, it offers:

  • a single easy-to-use builder function that:

    • generates your linker script

    • sets up packages and startup code

  • generalized interfaces for common devices, such as UART.

  • device drivers for interacting with external hardware

  • an uncomplicated method to define interrupts

Design

For MicroZig internals please see the Design Document.

Does MicroZig support X hardware?

MicroZig is designed to cover as wide a swath of hardware as possible. The Zig Embedded Group has some repositories that contain hardware-specific code. You will find them with the hardware-support-package label. If you can’t find your specific device, it doesn’t mean that you can’t run Zig on it, it’s likely you’re just the first! In that case, see Getting MicroZig on New Hardare.

Start with an empty Zig project by running zig init-exe, and add the hardware support package as a submodule. We’ll use microchip-atmega in our example:

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/hardware_support_package/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.installArtifact(b);
}

zig build and now you have an executable for an Arduino Nano. In your application you can import microzig in order to interact with the hardware:

const microzig = @import("microzig");

// `microzig.config`: comptime access to configuration
// `microzig.chip`: access to register definitions, generated code
// `microzig.board`: access to board information
// `microzig.hal`: access to hand-written code for interacting with the hardware
// `microzig.cpu`: access to AVR5 specific functions

pub fn main() !void {
    // your program here
}

Getting MicroZig on New Hardware

If you have a board/chip that isn’t defined in microzig, you can set it up yourself! You need to have:

  • SVD or ATDF file defining registers

  • flash and ram address and sizes

First, use Regz to generate the register definitions for your chip and save them to a file. Then define the chip:

const nrf52832 = Chip{
    .name = "nRF52832",
    .source = .{
        .path = "path/to/generated/file.zig",
    },
    .cpu = cpus.cortex_m4,
    .memory_regions = &.{
        MemoryRegion{ .offset = 0x00000000, .length = 0x80000, .kind = .flash },
        MemoryRegion{ .offset = 0x20000000, .length = 0x10000, .kind = .ram },
    },
};

const backing = .{
    .chip = nrf52832,
};

It’s important that the chip name actually matches one of the entries under devices in the generated code.

Optional: JSON Register Schema

You can also invoke regz to generate a JSON representation of the hardware:

regz --json <path to svd/atdf>

This file could then be used by tooling. You can add it to a Chip like so:

const nrf52832 = Chip{
    .name = "nRF52832",
    .json_register_schema = .{
        .path = "path/to.json",
    },
    // ...
};

Interrupts

The currently supported architectures for interrupt vector generation are ARM and AVR. To define the Interrupt Service Routine (ISR) for a given interrupt, you create a function with the same name in an interrupts namespace, which is nested in a microzig_options namespace:

pub const microzig_options = struct {
  pub const interrupts = struct {
      pub fn PCINT0() void {
        // interrupt handling code
      }
  };
}

pub fn main() !void {
    // my application
}

We’re using compile-time checks along with the generated code to determine the list of interrupts. If a function is defined whose name is not in this list, you’ll get a compiler error with the list of interrupts/valid names.

More Repositories

1

regz

Generate zig code from ATDF or SVD files for microcontrollers.
Zig
55
star
2

getting-started-pico-zig

[WIP] Code and tooling for getting started with Zig on the Raspberry Pi Pico
Zig
35
star
3

serial

Serial port configuration library for Zig
Zig
32
star
4

raspberrypi-rp2040

MicroZig Hardware Support Package for Raspberry Pi RP2040
Zig
29
star
5

microzig-examples

Examples for embedded zig!
Zig
22
star
6

sycl-badge

MicroZig running on the PyBadge! https://badgesim.microzig.tech/
Zig
20
star
7

espressif-esp

[WIP] ESP32 microzig package
Zig
14
star
8

aviron

Configurable AVR simulator
Zig
12
star
9

uf2

USB Flashing Format (UF2) for your build.zig
Zig
5
star
10

avrboot

AVR bootloader protocols
Zig
5
star
11

website

The website for ZEG, contains documents and articles as well as a static site gen
Zig
5
star
12

stmicro-stm32

HAL for stm32 (STMicro) devices
Zig
5
star
13

zfat

Generic purpose platform-independent FAT driver for Zig
C
4
star
14

sycl-workshop-2022

Our workshop repo
Zig
4
star
15

nordic-nrf5x

Hardware support for Nordic Semiconductor nRF5x devices
Zig
3
star
16

lpcboot

Implementation of the bootloader protocol for NXP LPC microcontrollers
Zig
3
star
17

microchip-atmega

Hardware support package for Microchip ATmega devices
Zig
3
star
18

libusb

libusb with batteries...build.zig included
C
2
star
19

usb

Package for USB code that can be reused across devices
Zig
2
star
20

hardware-support-template

Template for hardware support packages
Zig
2
star
21

nxp-lpc

Hardware support package for NXP LPC devices
Zig
2
star
22

zcom

Cross-platform serial communications monitor.
Zig
2
star
23

gigadevice-gd32

Hardware support library for Gigadevice GD32 devices
Zig
2
star
24

umm-zig

Allocator for targets with limited amount of RAM
Zig
1
star
25

roadmap

Central location for ZEG project management and brainstorming
1
star
26

hal-thinkspace

A scratch repository to work out and discuss the HAL API for MicroZig
Zig
1
star