diff --git a/build.zig b/build.zig index 62771e2..eaa0192 100644 --- a/build.zig +++ b/build.zig @@ -16,7 +16,7 @@ pub fn build(b: *std.build.Builder) !void { const BuildConfig = struct { name: []const u8, backing: Backing, supports_uart: bool = true }; const all_backings = [_]BuildConfig{ - //BuildConfig{ .name = "boards.arduino_nano", .backing = Backing{ .board = pkgs.boards.arduino_nano } }, + //BuildConfig{ .name = "boards.arduino_nano", .backing = Backing{ .board = boards.arduino_nano } }, BuildConfig{ .name = "boards.mbed_lpc1768", .backing = Backing{ .board = boards.mbed_lpc1768 } }, //BuildConfig{ .name = "chips.atmega328p", .backing = Backing{ .chip = pkgs.chips.atmega328p } }, BuildConfig{ .name = "chips.lpc1768", .backing = Backing{ .chip = chips.lpc1768 } }, diff --git a/src/main.zig b/src/main.zig index 0141522..aa43c73 100644 --- a/src/main.zig +++ b/src/main.zig @@ -63,6 +63,18 @@ pub fn addEmbeddedExecutable( }; { + // 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: { + for (chip.memory_regions) |region| { + if (region.kind == .ram) + break :blk region; + } else { + std.log.err("no ram memory region found for setting the end-of-stack address", .{}); + return error.NoRam; + } + }; + std.fs.cwd().makeDir(std.fs.path.dirname(config_file_name).?) catch {}; var config_file = try std.fs.cwd().createFile(config_file_name, .{}); defer config_file.close(); @@ -74,6 +86,7 @@ pub fn addEmbeddedExecutable( try writer.print("pub const chip_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)}); try writer.print("pub const cpu_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)}); + try writer.print("pub const end_of_stack = 0x{X:0>8};\n\n", .{first_ram.offset + first_ram.length}); } const microzig_pkg = Pkg{ @@ -81,6 +94,11 @@ pub fn addEmbeddedExecutable( .path = .{ .path = root_path ++ "core/microzig.zig" }, }; + const config_pkg = Pkg{ + .name = "microzig-config", + .path = .{ .path = config_file_name }, + }; + const chip_pkg = Pkg{ .name = "chip", .path = .{ .path = chip.path }, @@ -90,16 +108,11 @@ pub fn addEmbeddedExecutable( Pkg{ .name = "cpu", .path = .{ .path = chip.cpu.path }, - .dependencies = &[_]Pkg{ microzig_pkg, pkgs.mmio }, + .dependencies = &[_]Pkg{ microzig_pkg, pkgs.mmio, config_pkg }, }, }, }; - const config_pkg = Pkg{ - .name = "microzig-config", - .path = .{ .path = config_file_name }, - }; - const exe = builder.addExecutable(name, root_path ++ "core/start.zig"); // might not be true for all machines (Pi Pico), but diff --git a/src/modules/cpus/cortex-m3/cortex-m3.zig b/src/modules/cpus/cortex-m3/cortex-m3.zig index 4f24e7d..8512f26 100644 --- a/src/modules/cpus/cortex-m3/cortex-m3.zig +++ b/src/modules/cpus/cortex-m3/cortex-m3.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const config = @import("microzig-config"); pub fn sei() void { asm volatile ("cpsie i"); @@ -56,8 +57,7 @@ pub const startup_logic = struct { }; export const vectors linksection("microzig_flash_start") = VectorTable{ - // TODO: How to compute/get the initial stack pointer? - .initial_stack_pointer = 0x1000_7FFC, // HACK: hardcoded, do not keep! + .initial_stack_pointer = config.end_of_stack, .reset = _start, }; diff --git a/src/modules/cpus/cortex-m4/cortex-m4.zig b/src/modules/cpus/cortex-m4/cortex-m4.zig index 832279e..8512f26 100644 --- a/src/modules/cpus/cortex-m4/cortex-m4.zig +++ b/src/modules/cpus/cortex-m4/cortex-m4.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const config = @import("microzig-config"); pub fn sei() void { asm volatile ("cpsie i"); @@ -56,8 +57,7 @@ pub const startup_logic = struct { }; export const vectors linksection("microzig_flash_start") = VectorTable{ - // TODO: How to compute/get the initial stack pointer? - .initial_stack_pointer = 0x2000_A000, // HACK: hardcoded, do not keep! + .initial_stack_pointer = config.end_of_stack, .reset = _start, };