Skip to content

Commit

Permalink
posix: initial support
Browse files Browse the repository at this point in the history
  • Loading branch information
vesim987 committed Apr 26, 2024
1 parent 488b340 commit b535648
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 5 deletions.
38 changes: 38 additions & 0 deletions bsp/posix/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const std = @import("std");
const Build = std.Build;

const MicroZig = @import("microzig/build");
const Target = MicroZig.Target;
const Firmware = MicroZig.Firmware;

fn root() []const u8 {
return comptime (std.fs.path.dirname(@src().file) orelse ".");
}
const build_root = root();

pub fn build(b: *Build) !void {
_ = b;
}

pub const chips = struct {
pub const posix = Target{
.preferred_format = .{ .elf = {} },
.chip = chip,
.hal = hal,
.linker_script = .{ .none = {} },
.board = null,
};
};
pub const boards = struct {};

const hal = .{
.root_source_file = .{ .cwd_relative = build_root ++ "/src/hal.zig" },
};

const chip = .{
.name = "POSIX",
.url = "TODO",
.cpu = .{ .name = "native-posix4", .root_source_file = .{ .path = build_root ++ "/src/startup.zig" }, .target = .{} },
.register_definition = .{ .zig = .{ .path = build_root ++ "/src/empty.zig" } },
.memory_regions = &.{},
};
12 changes: 12 additions & 0 deletions bsp/posix/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.{
.name = "bsp/posix",
.version = "0.0.0",
.dependencies = .{
.@"microzig/build" = .{
.path = "../../build",
},
},
.paths = .{
"build.zig.zon",
},
}
5 changes: 5 additions & 0 deletions bsp/posix/src/empty.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub const devices = struct {
pub const POSIX = struct {
//
};
};
4 changes: 4 additions & 0 deletions bsp/posix/src/hal.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const std = @import("std");
const microzig = @import("microzig");

pub fn init() void {}
9 changes: 9 additions & 0 deletions bsp/posix/src/startup.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn disable_interrupts() void {}
pub const startup_logic = struct {
extern fn microzig_main() noreturn;
pub fn _start() callconv(.C) noreturn {
microzig_main();
}
};

pub fn export_startup_logic() void {}
10 changes: 7 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const example_dep_names: []const []const u8 = &.{
"examples/nxp/lpc",
"examples/microchip/atsam",
//"examples/microchip/avr",
"examples/gigadevice/gd32",
"examples/stmicro/stm32",
// TODO: both are borked for some reasons
// "examples/gigadevice/gd32",
// "examples/stmicro/stm32",
//"examples/espressif/esp",
"examples/raspberrypi/rp2040",
"examples/posix",
};

const bsps = .{
Expand All @@ -23,6 +25,7 @@ const bsps = .{
.{ "bsp/stmicro/stm32", @import("bsp/stmicro/stm32") },
.{ "bsp/espressif/esp", @import("bsp/espressif/esp") },
.{ "bsp/raspberrypi/rp2040", @import("bsp/raspberrypi/rp2040") },
.{ "bsp/posix", @import("bsp/posix") },
};

pub fn build(b: *Build) void {
Expand Down Expand Up @@ -58,7 +61,8 @@ pub fn build(b: *Build) void {
const test_bsps_step = b.step("run-bsp-tests", "Run all platform agnostic tests for BSPs");
inline for (bsps) |bsp| {
const bsp_dep = b.dependency(bsp[0], .{});
test_bsps_step.dependOn(&bsp_dep.builder.top_level_steps.get("test").?.step);
if (bsp_dep.builder.top_level_steps.get("test")) |t|
test_bsps_step.dependOn(&t.step);
}
}

Expand Down
2 changes: 2 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
.@"bsp/stmicro/stm32" = .{ .path = "bsp/stmicro/stm32" },
.@"bsp/espressif/esp" = .{ .path = "bsp/espressif/esp" },
.@"bsp/raspberrypi/rp2040" = .{ .path = "bsp/raspberrypi/rp2040" },
.@"bsp/posix" = .{ .path = "bsp/posix" },

// examples so that we can build them all in one go
.@"examples/nordic/nrf5x" = .{ .path = "examples/nordic/nrf5x" },
Expand All @@ -26,6 +27,7 @@
.@"examples/stmicro/stm32" = .{ .path = "examples/stmicro/stm32" },
.@"examples/espressif/esp" = .{ .path = "examples/espressif/esp" },
.@"examples/raspberrypi/rp2040" = .{ .path = "examples/raspberrypi/rp2040" },
.@"examples/posix" = .{ .path = "examples/posix" },

// used for creating package tarballs
.boxzer = .{
Expand Down
5 changes: 3 additions & 2 deletions build/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ pub fn add_firmware(

// TODO: let the user override which ram section to use the stack on,
// for now just using the first ram section in the memory region list
const first_ram = blk: {
const first_ram: defs.MemoryRegion = blk: {
for (chip.memory_regions) |region| {
if (region.kind == .ram)
break :blk region;
} else @panic("no ram memory region found for setting the end-of-stack address");
// for native targets create dummy ram region
} else if (chip.cpu.target.os_tag == null) break :blk .{ .kind = .ram, .offset = 0, .length = 0 } else @panic("no ram memory region found for setting the end-of-stack address");
};

// On demand, generate chip definitions via regz:
Expand Down
7 changes: 7 additions & 0 deletions core/src/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ pub fn initialize_system_memories() void {
@memcpy(data_start[0..data_len], data_src[0..data_len]);
}
}

// for native(for example linux/windows) targets we need to export the main function
pub usingnamespace if (@import("builtin").os.tag != .freestanding) struct {
pub fn main() void {
microzig.cpu.startup_logic._start();
}
} else struct {};
42 changes: 42 additions & 0 deletions examples/posix/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const std = @import("std");
const MicroZig = @import("microzig/build");
const posix = @import("microzig/bsp/posix");

const available_examples = [_]Example{
.{ .target = posix.chips.posix, .name = "posix_uart", .file = "src/uart.zig" },
};

pub fn build(b: *std.Build) void {
const mz = MicroZig.init(b, .{});
const optimize = b.standardOptimizeOption(.{});

for (available_examples) |example| {

// `add_firmware` 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 = mz.add_firmware(b, .{
.name = example.name,
.target = example.target,
.optimize = optimize,
.root_source_file = .{ .path = example.file },
});

// `install_firmware()` 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`.
mz.install_firmware(b, firmware, .{});

// For debugging, we also always install the firmware as an ELF file
mz.install_firmware(b, firmware, .{ .format = .elf });
}
}

const Example = struct {
target: MicroZig.Target,
name: []const u8,
file: []const u8,
};
13 changes: 13 additions & 0 deletions examples/posix/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.{
.name = "examples/posix",
.version = "0.0.0",
.dependencies = .{
.@"microzig/build" = .{ .path = "../../build" },
.@"microzig/bsp/posix" = .{ .path = "../../bsp/posix" },
},

.paths = .{
"build.zig",
"build.zig.zon",
},
}
15 changes: 15 additions & 0 deletions examples/posix/src/uart.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const std = @import("std");
const microzig = @import("microzig");

pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
std.log.err("panic: {s}", .{message});
@breakpoint();
while (true) {}
}

pub const microzig_options = .{
.log_level = .debug,
// .logFn = rp2040.uart.log,
};

pub fn main() !void {}

0 comments on commit b535648

Please sign in to comment.