From f7483fe42a5122603f6f80fdf1e65c10d411ab0a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 3 Dec 2024 17:50:35 +0100 Subject: [PATCH] Fixed hardfault due to broken memory layout --- firmware/src/bootloader.rs | 21 ++++++++------------ firmware/src/main.rs | 39 +++++++++++++++++++++++++++++--------- memory.x | 16 +--------------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/firmware/src/bootloader.rs b/firmware/src/bootloader.rs index 382e442..712641f 100644 --- a/firmware/src/bootloader.rs +++ b/firmware/src/bootloader.rs @@ -1,4 +1,4 @@ -use core::ptr::addr_of_mut; +use core::{mem::MaybeUninit, ptr::addr_of_mut}; use stm32f4xx_hal::pac; @@ -23,33 +23,28 @@ fn jump_to_bootloader() { const BOOTLOADER_REQUESTED: u32 = 0xdecafbad; -fn magic_mut_ptr() -> *mut u32 { - extern "C" { - #[link_name = "_dfu_magic"] - static mut magic: u32; - } - - unsafe { addr_of_mut!(magic) } -} +#[link_section = ".uninit.DFU_FLAG"] +static mut DFU_FLAG: MaybeUninit = MaybeUninit::uninit(); fn get_bootloader_flag() -> u32 { - unsafe { magic_mut_ptr().read_volatile() } + unsafe { DFU_FLAG.assume_init() } } fn set_bootloader_flag() { - unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; + unsafe { DFU_FLAG.write(BOOTLOADER_REQUESTED) }; } fn clear_bootloader_flag() { - unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; + unsafe { DFU_FLAG.write(0) }; } pub fn init() { let requested = get_bootloader_flag() == BOOTLOADER_REQUESTED; if requested { - clear_bootloader_flag(); jump_to_bootloader(); } + + clear_bootloader_flag(); } pub fn reboot_to_bootloader() -> ! { diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 45bd6b7..c3ea2d7 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -71,7 +71,7 @@ mod app { spi_cs3: gpiob::PB15>, spi1: spi::Spi, - az_enable: gpioa::PA10>, + az_enable: gpiob::PB8>, az_dir: gpioa::PA15>, az_step: gpiob::PB3>, @@ -208,7 +208,7 @@ mod app { defmt::info!("SPI Setup done"); - let mut az_enable = gpioa.pa10.into_push_pull_output(); + let mut az_enable = gpiob.pb8.into_push_pull_output(); az_enable.set_high(); let az_dir = gpioa.pa15.into_push_pull_output(); let az_step = gpiob.pb3.into_push_pull_output(); @@ -351,7 +351,7 @@ mod app { defmt::info!("angle: {:?}", angle_deg); - Mono::delay(50.millis()).await; + Mono::delay(1.millis()).await; } } @@ -361,30 +361,51 @@ mod app { let az_dir = cx.local.az_dir; let az_step = cx.local.az_step; + let mut counter = 0; + let mut az_target = 0; + loop { - let az_target = cx.shared.az_compass.lock(|az_compass| *az_compass); + //let az_target = 1800; //cx.shared.az_compass.lock(|az_compass| *az_compass); let az_angle = cx.shared.az_angle.lock(|az_angle| *az_angle); let diff = az_angle - az_target; - defmt::info!("angle diff: {:?}", diff); + defmt::info!( + "angle diff/target/actual: {:?}/{:?}/{:?}", + diff, + az_target, + az_angle + ); + + let delay = if diff.abs() < 10 { + 10.millis() + } else if diff < 100 { + 5.millis() + } else { + 1.millis() + }; if diff.abs() > 5 { az_enable.set_low(); - if diff > 0 { + if diff < 0 { az_dir.set_high(); } else { az_dir.set_low(); } az_step.set_low(); - Mono::delay(250.micros()).await; + Mono::delay(delay / 2).await; az_step.set_high(); - Mono::delay(250.micros()).await; + Mono::delay(delay / 2).await; } else { az_enable.set_high(); - Mono::delay(500.micros()).await; + Mono::delay(delay).await; } + + counter = (counter + 1) % 1000; + if counter == 0 { + az_target = (az_target + 450) % 3600; + }; } } diff --git a/memory.x b/memory.x index 606d5fd..78b279a 100644 --- a/memory.x +++ b/memory.x @@ -1,23 +1,9 @@ MEMORY { FLASH : ORIGIN = 0x08000000, LENGTH = 256K - UNINIT: ORIGIN = 0x20000000, LENGTH = 0x10 - RAM : ORIGIN = 0x20000010, LENGTH = 64K - LENGTH(UNINIT) + RAM : ORIGIN = 0x20000000, LENGTH = 64K } -SECTIONS { - .uninit_flags (NOLOAD) : ALIGN(4) - { - . = ALIGN(4); - __suninit_flags = .; - _dfu_magic = .; . += 4; - *(.uninit_flags .uninit_flags.*) - . = ALIGN(4); - __euninit_flags = .; - } > UNINIT -} - - /* This is where the call stack will be allocated. */ /* The stack is of the full descending type. */ /* NOTE Do NOT modify `_stack_start` unless you know what you are doing */