From e7e70cb96c5aaae4044ea903df1ac278f7d30f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=22xq=22=20Quei=C3=9Fner?= Date: Sat, 8 Oct 2022 13:02:39 +0200 Subject: [PATCH] LED 1 is RED. Reset loop tho --- build.zig | 59 +++++++++++++++++++-------------- src/example/blinky.zig | 29 ++++++++++++++++ src/main.zig | 24 -------------- src/package/esp32-c3.zig | 45 +++++++++++++++++++++++++ src/package/espressif-riscv.zig | 12 +++++++ vendor/microzig | 2 +- 6 files changed, 122 insertions(+), 49 deletions(-) create mode 100644 src/example/blinky.zig delete mode 100644 src/main.zig create mode 100644 src/package/esp32-c3.zig create mode 100644 src/package/espressif-riscv.zig diff --git a/build.zig b/build.zig index 035b12a..3f49cf7 100644 --- a/build.zig +++ b/build.zig @@ -1,34 +1,45 @@ const std = @import("std"); +const microzig = @import("zpm.zig").sdks.microzig; pub fn build(b: *std.build.Builder) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. - const target = b.standardTargetOptions(.{}); - - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const mode = b.standardReleaseOptions(); - const exe = b.addExecutable("esp32-c3-bringup", "src/main.zig"); - exe.setTarget(target); - exe.setBuildMode(mode); - exe.install(); + const esp32_c3_cpu = microzig.Cpu{ + .name = "Espressif RISC-V", + .path = "src/package/espressif-riscv.zig", + .target = std.zig.CrossTarget{ + .cpu_arch = .riscv32, + .cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 }, + .cpu_features_add = std.Target.riscv.featureSet(&.{ + std.Target.riscv.Feature.c, + std.Target.riscv.Feature.m, + }), + .os_tag = .freestanding, + .abi = .eabi, + }, + }; - const run_cmd = exe.run(); - run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); - } + const esp32_c3 = microzig.Chip{ + .name = "ESP32 C3", + .path = "src/package/esp32-c3.zig", + .cpu = esp32_c3_cpu, + .memory_regions = &.{ + .{ .kind = .flash, .offset = 0x4200_0000, .length = 0x0080_0000 }, // external memory, ibus + .{ .kind = .ram, .offset = 0x3FC8_0000, .length = 0x0006_0000 }, // sram 1, data bus + }, + }; - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + var exe = microzig.addEmbeddedExecutable( + b, + "esp-bringup", + "src/example/blinky.zig", + .{ .chip = esp32_c3 }, + .{}, + ); + exe.setBuildMode(mode); + exe.install(); - const exe_tests = b.addTest("src/main.zig"); - exe_tests.setTarget(target); - exe_tests.setBuildMode(mode); + const raw_step = exe.installRaw("firmware.bin", .{}); - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&exe_tests.step); + b.getInstallStep().dependOn(&raw_step.step); } diff --git a/src/example/blinky.zig b/src/example/blinky.zig new file mode 100644 index 0000000..6bba15b --- /dev/null +++ b/src/example/blinky.zig @@ -0,0 +1,29 @@ +const std = @import("std"); +const microzig = @import("microzig"); + +pub fn main() !void { + // const led_r_mux = @ptrToInt(*volatile u32, IO_MUX_BASE + IO_MUX_GPIOn_REG(LED_R_PIN)); + // const led_g_mux = @ptrToInt(*volatile u32, IO_MUX_BASE + IO_MUX_GPIOn_REG(LED_G_PIN)); + // const led_b_mux = @ptrToInt(*volatile u32, IO_MUX_BASE + IO_MUX_GPIOn_REG(LED_B_PIN)); + + // led_r_mux.* = 0x80; + + const gpio_out = @intToPtr(*volatile u32, GPIO_BASE + GPIO_OUT_REG); + const gpio_ena = @intToPtr(*volatile u32, GPIO_BASE + GPIO_ENABLE_REG); + gpio_ena.* = (1 << LED_R_PIN) | (1 << LED_G_PIN) | (1 << LED_B_PIN); + gpio_out.* = (1 << LED_R_PIN) | (1 << LED_G_PIN) | (1 << LED_B_PIN); +} + +const GPIO_BASE = 0x6000_4000; +const IO_MUX_BASE = 0x6000_9000; + +const GPIO_OUT_REG = 0x0004; +const GPIO_ENABLE_REG = 0x0020; + +fn GPIO_FUNCn_OUT_SEL_CFG_REG(comptime n: comptime_int) comptime_int { + return 0x0554 + 4 * n; +} + +const LED_R_PIN = 3; +const LED_G_PIN = 4; +const LED_B_PIN = 5; diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index c8a3f67..0000000 --- a/src/main.zig +++ /dev/null @@ -1,24 +0,0 @@ -const std = @import("std"); - -pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - - // stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - const stdout_file = std.io.getStdOut().writer(); - var bw = std.io.bufferedWriter(stdout_file); - const stdout = bw.writer(); - - try stdout.print("Run `zig build test` to run the tests.\n", .{}); - - try bw.flush(); // don't forget to flush! -} - -test "simple test" { - var list = std.ArrayList(i32).init(std.testing.allocator); - defer list.deinit(); // try commenting this out and see if zig detects the memory leak! - try list.append(42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); -} diff --git a/src/package/esp32-c3.zig b/src/package/esp32-c3.zig new file mode 100644 index 0000000..0a0f4fe --- /dev/null +++ b/src/package/esp32-c3.zig @@ -0,0 +1,45 @@ +const std = @import("std"); +const microzig = @import("microzig"); + +pub const startup_logic = struct { + comptime { + // See this: + // https://github.com/espressif/esp32c3-direct-boot-example + + // Direct Boot: does not support Security Boot and programs run directly in flash. To enable this mode, make + // sure that the first two words of the bin file downloading to flash (address: 0x42000000) are 0xaedb041d. + + // In this case, the ROM bootloader sets up Flash MMU to map 4 MB of Flash to + // addresses 0x42000000 (for code execution) and 0x3C000000 (for read-only data + // access). The bootloader then jumps to address 0x42000008, i.e. to the + // instruction at offset 8 in flash, immediately after the magic numbers. + + asm (std.fmt.comptimePrint(".equ MICROZIG_INITIAL_STACK, {}", .{microzig.config.end_of_stack})); + + asm ( + \\.extern _start + \\.section microzig_flash_start + \\.align 4 + \\.byte 0x1d, 0x04, 0xdb, 0xae + \\.byte 0x1d, 0x04, 0xdb, 0xae + ); + } + + extern fn microzig_main() noreturn; + + export fn _start() linksection("microzig_flash_start") callconv(.C) noreturn { + asm volatile ( + \\li sp, MICROZIG_INITIAL_STACK + \\lui a0, %%hi(_rv32_trap) + \\addi a0, a0, %%lo(_rv32_trap) + \\sw t0, 0x305(zero) + ); + + microzig.initializeSystemMemories(); + microzig_main(); + } + + export fn _rv32_trap() callconv(.C) noreturn { + while (true) {} + } +}; diff --git a/src/package/espressif-riscv.zig b/src/package/espressif-riscv.zig new file mode 100644 index 0000000..34dbffd --- /dev/null +++ b/src/package/espressif-riscv.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const microzig = @import("microzig"); + +pub inline fn cli() void { + asm volatile (""); +} + +pub inline fn sei() void { + asm volatile (""); +} + +pub const startup_logic = microzig.chip.startup_logic; diff --git a/vendor/microzig b/vendor/microzig index 15bc1fc..0d9721d 160000 --- a/vendor/microzig +++ b/vendor/microzig @@ -1 +1 @@ -Subproject commit 15bc1fc06da3b6c622a21fa438e40be247d9dee1 +Subproject commit 0d9721d9070c356f4ffaf6f4a312bccdb574b8a9