From e0b3573be9a975f37984990cf921e8b53d8c90af Mon Sep 17 00:00:00 2001 From: Filipriec Date: Mon, 13 Apr 2026 09:35:15 +0200 Subject: [PATCH] finally working oh my gash --- .gitignore | 2 ++ flake.nix | 1 + tiva/build.zig | 6 +++++- tiva/src/main.zig | 45 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 36960cd..d90adfc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .zig-cache/ main +test/ +test.zip diff --git a/flake.nix b/flake.nix index ff807f2..5231663 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,7 @@ echo "Entering Zig Master Environment" echo "Zig: $(zig version)" echo "ZLS: $(zls --version)" + echo "openocd -f board/ek-tm4c123gxl.cfg -c \"program tiva/zig-out/bin/tiva-app verify reset exit\"" ''; }; }; diff --git a/tiva/build.zig b/tiva/build.zig index 4ffee74..c32bbcb 100644 --- a/tiva/build.zig +++ b/tiva/build.zig @@ -4,7 +4,7 @@ pub fn build(b: *std.Build) void { const target = b.resolveTargetQuery(.{ .cpu_arch = .thumb, .os_tag = .freestanding, - .abi = .eabi, + .abi = .eabihf, .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m4 }, }); @@ -29,6 +29,10 @@ pub fn build(b: *std.Build) void { .root_module = tiva_module, }); + const bin = elf.addObjCopy(.{ .format = .bin }); + const install_bin = b.addInstallBinFile(bin.getOutput(), "tiva-app.bin"); + b.getInstallStep().dependOn(&install_bin.step); + elf.root_module.strip = true; elf.root_module.omit_frame_pointer = true; elf.root_module.unwind_tables = .none; diff --git a/tiva/src/main.zig b/tiva/src/main.zig index 77822f9..88f775c 100644 --- a/tiva/src/main.zig +++ b/tiva/src/main.zig @@ -3,29 +3,52 @@ const cmsis = @cImport({ @cInclude("TM4C123GH6PM.h"); }); -const GPIO_PORTF_CLK_EN: u32 = 0x20; // Bit 5 (Port F) -const GPIO_PORTF_PIN1_EN: u32 = 0x02; // Bit 1 +const GPIO_PORTF_CLK_EN = @as(u32, 0x20); // Bit 5 (Port F) +const GPIO_PORTF_PIN1_EN = @as(u32, 0x02); // Bit 1 -const LED_ON1: u32 = 0x02; +const LED_RED = @as(u32, 0x02); -export fn _start() noreturn { +// export const vector_table linksection(".isr_vector") = [2]usize{ +// 0x20008000, +// @intFromPtr(&_start), +// }; + +const VectorEntry = extern union { + stack_top: u32, + handler: *const fn () callconv(.c) noreturn, +}; + +export const vector_table linksection(".vector_table") = [_]VectorEntry{ + .{ .stack_top = 0x20008000 }, + .{ .handler = &_start }, +}; + +export fn _start() callconv(.c) noreturn { + cmsis.SYSCTL.*.RCGC2 |= GPIO_PORTF_CLK_EN; cmsis.SYSCTL.*.RCGCGPIO |= GPIO_PORTF_CLK_EN; _ = cmsis.SYSCTL.*.RCGCGPIO; + _ = cmsis.SYSCTL.*.RCGCGPIO; + _ = cmsis.SYSCTL.*.RCGCGPIO; - cmsis.GPIOF.*.DEN |= GPIO_PORTF_PIN1_EN; - cmsis.GPIOF.*.DIR |= GPIO_PORTF_PIN1_EN; + cmsis.SYSCTL.*.GPIOHBCTL |= GPIO_PORTF_CLK_EN; - const DELAY_VALUE = 4000000; + cmsis.GPIOF_AHB.*.DEN |= GPIO_PORTF_PIN1_EN; + cmsis.GPIOF_AHB.*.DIR |= GPIO_PORTF_PIN1_EN; + // cmsis.GPIOF_AHB.*.DATA ^= LED_RED; + + const gpiof_data_pf1: *volatile u32 = @ptrFromInt(@intFromPtr(cmsis.GPIOF_AHB) + (LED_RED << 2)); + + const DELAY_VALUE = 400000; while (true) { - cmsis.GPIOF.*.DATA = LED_ON1; // LED On + gpiof_data_pf1.* = LED_RED; // LED On Delay(DELAY_VALUE); - cmsis.GPIOF.*.DATA = 0; // LED On + gpiof_data_pf1.* = 0; // LED off Delay(DELAY_VALUE); } } -fn Delay(delay: usize) void{ - for(0..delay) |_| { +fn Delay(delay: usize) void { + for (0..delay) |_| { asm volatile ("nop"); } }