diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0cb064a --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.zig text=auto eol=lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..af725e6 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,24 @@ +name: Build +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, macos-latest, ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup Zig + uses: goto-bus-stop/setup-zig@v2 + with: + version: 0.11.0 + + - name: Build examples + run: zig build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f975728 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +zig-cache/ +dev-scripts/ +zig-out +.envrc diff --git a/README.md b/README.md index c3bfa27..dda5683 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # microzig-examples + Examples for embedded zig! diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..4064772 --- /dev/null +++ b/build.zig @@ -0,0 +1,84 @@ +const std = @import("std"); +const rp2040 = @import("rp2040"); +const stm32 = @import("stm32"); +const lpc = @import("lpc"); +const gd32 = @import("gd32"); +const nrf5x = @import("nrf5x"); +const esp = @import("esp"); +const atmega = @import("atmega"); + +pub fn build(b: *std.Build) void { + const microzig = @import("microzig").init(b, "microzig"); + const optimize = b.standardOptimizeOption(.{}); + + const TargetDesc = struct { + target: @import("microzig").Target, + name: []const u8, + }; + + const available_targets = [_]TargetDesc{ + // RP2040 + .{ .name = "pico", .target = rp2040.boards.raspberry_pi.pico }, + .{ .name = "rp2040-eth", .target = rp2040.boards.waveshare.rp2040_eth }, + .{ .name = "rp2040-plus-4m", .target = rp2040.boards.waveshare.rp2040_plus_4m }, + .{ .name = "rp2040-plus-16m", .target = rp2040.boards.waveshare.rp2040_plus_16m }, + .{ .name = "rp2040-matrix", .target = rp2040.boards.waveshare.rp2040_matrix }, + + // STM32 + .{ .name = "stm32f103x8", .target = stm32.chips.stm32f103x8 }, + .{ .name = "stm32f303vc", .target = stm32.chips.stm32f303vc }, + .{ .name = "stm32f407vg", .target = stm32.chips.stm32f407vg }, + .{ .name = "stm32f429zit6u", .target = stm32.chips.stm32f429zit6u }, + .{ .name = "stm32f3discovery", .target = stm32.boards.stm32f3discovery }, + .{ .name = "stm32f4discovery", .target = stm32.boards.stm32f4discovery }, + .{ .name = "stm3240geval", .target = stm32.boards.stm3240geval }, + .{ .name = "stm32f429idiscovery", .target = stm32.boards.stm32f429idiscovery }, + + // NXP LPC + .{ .name = "lpc176x5x", .target = lpc.chips.lpc176x5x }, + .{ .name = "mbed-lpc1768", .target = lpc.boards.mbed.lpc1768 }, + + // GigaDevice GD32 + .{ .name = "gd32vf103xb", .target = gd32.chips.gd32vf103xb }, + .{ .name = "gd32vf103x8", .target = gd32.chips.gd32vf103x8 }, + .{ .name = "sipeed-longan_nano", .target = gd32.boards.sipeed.longan_nano }, + + // Nordic Nrf5x + .{ .name = "nrf52832", .target = nrf5x.chips.nrf52832 }, + .{ .name = "nrf52840", .target = nrf5x.chips.nrf52840 }, + .{ .name = "nrf52840-dongle", .target = nrf5x.boards.nordic.nRF52840_Dongle }, // TODO: Add support for DFU files! + + // RISC-V Espressif ESP + .{ .name = "esp32-c3", .target = esp.chips.esp32_c3 }, // TODO: Add support for Espressif Update Binaries + + // Microchip ATmega + // TODO: Fix compiler bugs + // - https://github.com/ziglang/zig/issues/17219 + // .{ .name = "atmega328p", .target = atmega.chips.atmega328p }, + // .{ .name = "arduino-nano", .target = atmega.boards.arduino.nano }, + // .{ .name = "arduino-uno-rev3", .target = atmega.boards.arduino.uno_rev3 }, + }; + + for (available_targets) |dest| { + // `addFirmware` basically works like addExecutable, but takes a + // `microzig.Target` for target instead of a `std.zig.CrossTarget`. + // + // The target will convey all necessary information on the chip, + // cpu and potentially the board as well. + const firmware = microzig.addFirmware(b, .{ + .name = b.fmt("empty-{s}", .{dest.name}), + .target = dest.target, + .optimize = optimize, + .source_file = .{ .path = "src/empty.zig" }, + }); + + // `installFirmware()` is the MicroZig pendant to `Build.installArtifact()` + // and allows installing the firmware as a typical firmware file. + // + // This will also install into `$prefix/firmware` instead of `$prefix/bin`. + microzig.installFirmware(b, firmware, .{}); + + // For debugging, we also always install the firmware as an ELF file + microzig.installFirmware(b, firmware, .{ .format = .elf }); + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..7166259 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,38 @@ +.{ + .name = "microzig-examples", + .version = "0.1.0", + .dependencies = .{ + .microzig = .{ + .url = "https://github.com/ZigEmbeddedGroup/microzig/archive/c6c9ec4516f57638e751141085c9d76120990312.tar.gz", + .hash = "1220af58bdaa721b8189f3a7adfda660517dd354463463388e96d69fe4ceccf80b92", + }, + .rp2040 = .{ + .url = "https://github.com/ZigEmbeddedGroup/raspberrypi-rp2040/archive/67d36eebb0fbd89633db1a51d6d2bcb049f2066a.tar.gz", + .hash = "122094bf268f45b188f3916f9e5964f4257414afaafba98a455ac47d25389a456832", + }, + .stm32 = .{ + .url = "https://github.com/ZigEmbeddedGroup/stmicro-stm32/archive/cb2893707efa6aa289fa72f02959ad5f2d9db2a1.tar.gz", + .hash = "12208cab5f60ef97cac4165ad694f3ba0c7b28f279538c1539b74f7c152f34fe306d", + }, + .lpc = .{ + .url = "https://github.com/ZigEmbeddedGroup/nxp-lpc/archive/130a1316c0892415e7da958a5e9548ed87bba54d.tar.gz", + .hash = "1220165879f85a1d51656d35b3963a95f3585dc665fc7414f76aa6aad4e6635536cf", + }, + .gd32 = .{ + .url = "https://github.com/ZigEmbeddedGroup/gigadevice-gd32/archive/9324753cc3b8e7afe83fcda085bcfe76681a3be3.tar.gz", + .hash = "122043ff4dcbc342f25dbb936b0d9eaa701ac3509e2cbe6764be37b90d31c7a385d0", + }, + .nrf5x = .{ + .url = "https://github.com/ZigEmbeddedGroup/nordic-nrf5x/archive/0ab136860ccf7eb1d07969c3ef523f3cd898e2ff.tar.gz", + .hash = "1220980da06f9634dcff06afefa7aa111bd030018fea49f79e86657dab69621e1d08", + }, + .esp = .{ + .url = "https://github.com/ZigEmbeddedGroup/espressif-esp/archive/59b8ca028915c0d6224ec88dbf4db19afbb559c0.tar.gz", + .hash = "1220f6e5f22416fdc63442cd8869fcaa35f9abf30d878ea3d80073176677dc6f8a65", + }, + .atmega = .{ + .url = "https://github.com/ZigEmbeddedGroup/microchip-atmega/archive/feefcb87a63c0aae31afb783d4e388e90c4d922f.tar.gz", + .hash = "1220048dc5d22729ee119a496f8b8ca3556838af1f3bd32ce6acd5f76480ec942965", + }, + }, +} diff --git a/ezpkg.sh b/ezpkg.sh new file mode 100755 index 0000000..caafa03 --- /dev/null +++ b/ezpkg.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +exec ezpkg \ + microzig=/home/felix/projects/zeg/microzig \ + microzig.uf2=/home/felix/projects/zeg/uf2 \ + microzig.regz=/home/felix/projects/zeg/regz \ + rp2040=/home/felix/projects/zeg/device-support-package/rp2040 \ + stm32=/home/felix/projects/zeg/device-support-package/stmicro-stm32 \ + lpc=/home/felix/projects/zeg/device-support-package/nxp-lpc \ + gd32=/home/felix/projects/zeg/device-support-package/gigadevice-gd32 \ + esp=/home/felix/projects/zeg/device-support-package/espressif-esp \ + nrf5x=/home/felix/projects/zeg/device-support-package/nordic-nrf5x \ + atmega=/home/felix/projects/zeg/device-support-package/microchip-atmega \ No newline at end of file diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..b1724c4 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +{pkgs ? import {}}: +pkgs.mkShell { + nativeBuildInputs = [ + pkgs.zig_0_11_0 + pkgs.picotool + pkgs.llvmPackages_16.bintools + ]; + buildInputs = []; +} diff --git a/src/blinky.zig b/src/blinky.zig new file mode 100644 index 0000000..5632fe3 --- /dev/null +++ b/src/blinky.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const microzig = @import("microzig"); +const rp2040 = microzig.hal; +const time = rp2040.time; + +const pin_config = rp2040.pins.GlobalConfiguration{ + .GPIO25 = .{ + .name = "led", + .direction = .out, + }, +}; + +pub fn main() !void { + const pins = pin_config.apply(); + + while (true) { + pins.led.toggle(); + time.sleep_ms(250); + } +} diff --git a/src/empty.zig b/src/empty.zig new file mode 100644 index 0000000..7c6dbbe --- /dev/null +++ b/src/empty.zig @@ -0,0 +1,6 @@ +const std = @import("std"); +const microzig = @import("microzig"); + +pub fn main() void { + // +}