From 6bc3fc094bbd1cb0b5a9116ea0294fcefada45db Mon Sep 17 00:00:00 2001 From: Matt Knight Date: Sun, 10 Jul 2022 09:03:20 -0700 Subject: [PATCH] add hal or app level clock configuration (#62) * add hal or app level clock configuration * fix control flow and typo * use hal.init(), thanks kuon for the improvement --- src/core/empty.zig | 0 src/core/microzig.zig | 9 +++++++++ src/main.zig | 13 +++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/core/empty.zig diff --git a/src/core/empty.zig b/src/core/empty.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/core/microzig.zig b/src/core/microzig.zig index aab89b8..d3decbe 100644 --- a/src/core/microzig.zig +++ b/src/core/microzig.zig @@ -176,6 +176,15 @@ export fn microzig_main() noreturn { if (info.Fn.calling_convention == .Async) @compileError("TODO: Embedded event loop not supported yet. Please try again later."); + // A hal can export a default init function that runs before main for + // procedures like clock configuration. The user may override and customize + // this functionality by providing their own init function. + // function. + if (@hasDecl(app, "init")) + app.init() + else if (@hasDecl(hal, "init")) + hal.init(); + if (@typeInfo(return_type) == .ErrorUnion) { main() catch |err| { // TODO: diff --git a/src/main.zig b/src/main.zig index cd30986..af23dbf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -160,12 +160,13 @@ pub fn addEmbeddedExecutable( exe.addPackage(chip_pkg); exe.addPackage(cpu_pkg); - if (options.hal_package_path) |hal_package_path| - exe.addPackage(.{ - .name = "hal", - .source = hal_package_path, - .dependencies = &.{pkgs.microzig}, - }); + exe.addPackage(.{ + .name = "hal", + .source = if (options.hal_package_path) |hal_package_path| + hal_package_path + else .{ .path = root_path ++ "core/empty.zig" }, + .dependencies = &.{pkgs.microzig}, + }); switch (backing) { .board => |board| {