Skip to content

Commit b535648

Browse files
committed
posix: initial support
1 parent 488b340 commit b535648

File tree

12 files changed

+157
-5
lines changed

12 files changed

+157
-5
lines changed

bsp/posix/build.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const std = @import("std");
2+
const Build = std.Build;
3+
4+
const MicroZig = @import("microzig/build");
5+
const Target = MicroZig.Target;
6+
const Firmware = MicroZig.Firmware;
7+
8+
fn root() []const u8 {
9+
return comptime (std.fs.path.dirname(@src().file) orelse ".");
10+
}
11+
const build_root = root();
12+
13+
pub fn build(b: *Build) !void {
14+
_ = b;
15+
}
16+
17+
pub const chips = struct {
18+
pub const posix = Target{
19+
.preferred_format = .{ .elf = {} },
20+
.chip = chip,
21+
.hal = hal,
22+
.linker_script = .{ .none = {} },
23+
.board = null,
24+
};
25+
};
26+
pub const boards = struct {};
27+
28+
const hal = .{
29+
.root_source_file = .{ .cwd_relative = build_root ++ "/src/hal.zig" },
30+
};
31+
32+
const chip = .{
33+
.name = "POSIX",
34+
.url = "TODO",
35+
.cpu = .{ .name = "native-posix4", .root_source_file = .{ .path = build_root ++ "/src/startup.zig" }, .target = .{} },
36+
.register_definition = .{ .zig = .{ .path = build_root ++ "/src/empty.zig" } },
37+
.memory_regions = &.{},
38+
};

bsp/posix/build.zig.zon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.{
2+
.name = "bsp/posix",
3+
.version = "0.0.0",
4+
.dependencies = .{
5+
.@"microzig/build" = .{
6+
.path = "../../build",
7+
},
8+
},
9+
.paths = .{
10+
"build.zig.zon",
11+
},
12+
}

bsp/posix/src/empty.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub const devices = struct {
2+
pub const POSIX = struct {
3+
//
4+
};
5+
};

bsp/posix/src/hal.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const std = @import("std");
2+
const microzig = @import("microzig");
3+
4+
pub fn init() void {}

bsp/posix/src/startup.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub fn disable_interrupts() void {}
2+
pub const startup_logic = struct {
3+
extern fn microzig_main() noreturn;
4+
pub fn _start() callconv(.C) noreturn {
5+
microzig_main();
6+
}
7+
};
8+
9+
pub fn export_startup_logic() void {}

build.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ const example_dep_names: []const []const u8 = &.{
88
"examples/nxp/lpc",
99
"examples/microchip/atsam",
1010
//"examples/microchip/avr",
11-
"examples/gigadevice/gd32",
12-
"examples/stmicro/stm32",
11+
// TODO: both are borked for some reasons
12+
// "examples/gigadevice/gd32",
13+
// "examples/stmicro/stm32",
1314
//"examples/espressif/esp",
1415
"examples/raspberrypi/rp2040",
16+
"examples/posix",
1517
};
1618

1719
const bsps = .{
@@ -23,6 +25,7 @@ const bsps = .{
2325
.{ "bsp/stmicro/stm32", @import("bsp/stmicro/stm32") },
2426
.{ "bsp/espressif/esp", @import("bsp/espressif/esp") },
2527
.{ "bsp/raspberrypi/rp2040", @import("bsp/raspberrypi/rp2040") },
28+
.{ "bsp/posix", @import("bsp/posix") },
2629
};
2730

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

build.zig.zon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
.@"bsp/stmicro/stm32" = .{ .path = "bsp/stmicro/stm32" },
1717
.@"bsp/espressif/esp" = .{ .path = "bsp/espressif/esp" },
1818
.@"bsp/raspberrypi/rp2040" = .{ .path = "bsp/raspberrypi/rp2040" },
19+
.@"bsp/posix" = .{ .path = "bsp/posix" },
1920

2021
// examples so that we can build them all in one go
2122
.@"examples/nordic/nrf5x" = .{ .path = "examples/nordic/nrf5x" },
@@ -26,6 +27,7 @@
2627
.@"examples/stmicro/stm32" = .{ .path = "examples/stmicro/stm32" },
2728
.@"examples/espressif/esp" = .{ .path = "examples/espressif/esp" },
2829
.@"examples/raspberrypi/rp2040" = .{ .path = "examples/raspberrypi/rp2040" },
30+
.@"examples/posix" = .{ .path = "examples/posix" },
2931

3032
// used for creating package tarballs
3133
.boxzer = .{

build/build.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ pub fn add_firmware(
8282

8383
// TODO: let the user override which ram section to use the stack on,
8484
// for now just using the first ram section in the memory region list
85-
const first_ram = blk: {
85+
const first_ram: defs.MemoryRegion = blk: {
8686
for (chip.memory_regions) |region| {
8787
if (region.kind == .ram)
8888
break :blk region;
89-
} else @panic("no ram memory region found for setting the end-of-stack address");
89+
// for native targets create dummy ram region
90+
} 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");
9091
};
9192

9293
// On demand, generate chip definitions via regz:

core/src/start.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,10 @@ pub fn initialize_system_memories() void {
195195
@memcpy(data_start[0..data_len], data_src[0..data_len]);
196196
}
197197
}
198+
199+
// for native(for example linux/windows) targets we need to export the main function
200+
pub usingnamespace if (@import("builtin").os.tag != .freestanding) struct {
201+
pub fn main() void {
202+
microzig.cpu.startup_logic._start();
203+
}
204+
} else struct {};

examples/posix/build.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const std = @import("std");
2+
const MicroZig = @import("microzig/build");
3+
const posix = @import("microzig/bsp/posix");
4+
5+
const available_examples = [_]Example{
6+
.{ .target = posix.chips.posix, .name = "posix_uart", .file = "src/uart.zig" },
7+
};
8+
9+
pub fn build(b: *std.Build) void {
10+
const mz = MicroZig.init(b, .{});
11+
const optimize = b.standardOptimizeOption(.{});
12+
13+
for (available_examples) |example| {
14+
15+
// `add_firmware` basically works like addExecutable, but takes a
16+
// `microzig.Target` for target instead of a `std.zig.CrossTarget`.
17+
//
18+
// The target will convey all necessary information on the chip,
19+
// cpu and potentially the board as well.
20+
const firmware = mz.add_firmware(b, .{
21+
.name = example.name,
22+
.target = example.target,
23+
.optimize = optimize,
24+
.root_source_file = .{ .path = example.file },
25+
});
26+
27+
// `install_firmware()` is the MicroZig pendant to `Build.installArtifact()`
28+
// and allows installing the firmware as a typical firmware file.
29+
//
30+
// This will also install into `$prefix/firmware` instead of `$prefix/bin`.
31+
mz.install_firmware(b, firmware, .{});
32+
33+
// For debugging, we also always install the firmware as an ELF file
34+
mz.install_firmware(b, firmware, .{ .format = .elf });
35+
}
36+
}
37+
38+
const Example = struct {
39+
target: MicroZig.Target,
40+
name: []const u8,
41+
file: []const u8,
42+
};

0 commit comments

Comments
 (0)