Fixed hardfault due to broken memory layout

This commit is contained in:
Sebastian 2024-12-03 17:50:35 +01:00
parent 57fdf05f00
commit f7483fe42a
3 changed files with 39 additions and 37 deletions

View file

@ -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<u32> = 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() -> ! {

View file

@ -71,7 +71,7 @@ mod app {
spi_cs3: gpiob::PB15<Output<PushPull>>,
spi1: spi::Spi<SPI1>,
az_enable: gpioa::PA10<Output<PushPull>>,
az_enable: gpiob::PB8<Output<PushPull>>,
az_dir: gpioa::PA15<Output<PushPull>>,
az_step: gpiob::PB3<Output<PushPull>>,
@ -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;
};
}
}

View file

@ -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 */