better startup: all zig, bss and data init'd

main
Clyne 3 months ago
parent b262bb6b95
commit f4bf565897
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -10,14 +10,13 @@ pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "stm32",
.root_source_file = b.path("src/main.zig"),
.root_source_file = b.path("src/startup.zig"),
.target = target,
//.optimize = .Debug,
.optimize = .ReleaseSafe,
.linkage = .static,
});
exe.addAssemblyFile(b.path("src/bootstrap.s"));
exe.setLinkerScriptPath(b.path("link.ld"));
b.installArtifact(exe);

@ -23,24 +23,24 @@ SECTIONS {
} > FLASH
/* initialized data */
_sidata = LOADADDR(.data);
__data_flash = LOADADDR(.data);
.data : {
. = ALIGN(8);
_sdata = .;
__data = .;
*(.data)
*(.data*)
. = ALIGN(8);
_edata = .;
__data_size = . - __data;
} > RAM AT > FLASH
/* uninitialized data */
.bss : {
. = ALIGN(8);
_sbss = .;
__bss = .;
*(.bss)
*(.bss*)
. = ALIGN(8);
_ebss = .;
__bss_size = . - __bss;
} > RAM
.stack : {

@ -1,13 +0,0 @@
.cpu cortex-m4
.thumb
.extern _start
.extern _tstack
.section .vector_table
.global init_vector_table
init_vector_table:
.word _tstack
.word _start
.skip 4 * 14

@ -8,7 +8,7 @@ const gpioa = gpio.gpioa;
const gpioc = gpio.gpioc;
const tick = timer.systick;
export fn _start() callconv(.C) noreturn {
pub fn main() void {
cpu.interrupt_disable();
interrupt.initialize();
interrupt.register(.SVCall, svcall);

@ -0,0 +1,41 @@
const main = @import("main.zig");
extern const __data_flash: u32;
extern const __data: u32;
extern const __data_size: u32;
extern const __bss: u32;
extern const __bss_size: u32;
comptime {
asm (
\\.extern _start
\\.extern _tstack
\\
\\.section .vector_table
\\.global init_vector_table
\\init_vector_table:
\\ .word _tstack
\\ .word _start
\\ .skip 4 * 14
);
}
inline fn zero_bss() void {
const bss: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__bss));
@memset(bss[0..@intFromPtr(&__bss_size)], 0);
}
inline fn copy_data() void {
const data_flash: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__data_flash));
const data: [*]volatile u8 = @ptrFromInt(@intFromPtr(&__data));
const data_size = @intFromPtr(&__data_size);
@memcpy(data[0..data_size], data_flash[0..data_size]);
}
export fn _start() callconv(.C) noreturn {
zero_bss();
copy_data();
main.main();
while (true) {}
}
Loading…
Cancel
Save