From 2e6116016ac2b9918f33e1b885d068631d7ffb40 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 13 Jan 2021 11:06:06 +0800 Subject: [PATCH] dfu: refactor for PR --- README.md | 2 +- memory.x | 5 +++-- src/dfu.rs | 49 +++++++++++++++++++++++-------------------------- src/main.rs | 3 ++- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index a6fe73e..89dc63a 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ formatted as line-delimited JSON. | `load [0/1]` | Restore configuration for channel all/0/1 from flash | | `save [0/1]` | Save configuration for channel all/0/1 to flash | | `reset` | Reset the device | -| `dfu` | Reset device and enters USB device firmware update (DFU) mode | +| `dfu` | Reset device and enters USB device firmware update (DFU) mode | | `ipv4 [Y.Y.Y.Y]` | Configure IPv4 address, netmask length, and optional default gateway | diff --git a/memory.x b/memory.x index 5366550..c0ae171 100644 --- a/memory.x +++ b/memory.x @@ -3,12 +3,13 @@ MEMORY FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K /* reserved for config data */ CONFIG (rx) : ORIGIN = 0x8100000, LENGTH = 16K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 111K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 112K - 4 /* reserved for DFU trigger message */ - DFU_MSG (wrx) : ORIGIN = 0x2001BC00, LENGTH = 1K + DFU_MSG (wrx) : ORIGIN = 0x2001BFFC, LENGTH = 4 RAM2 (xrw) : ORIGIN = 0x2001C000, LENGTH = 16K RAM3 (xrw) : ORIGIN = 0x20020000, LENGTH = 64K CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K } _stack_start = ORIGIN(CCMRAM) + LENGTH(CCMRAM); +_dfu_msg = ORIGIN(DFU_MSG); diff --git a/src/dfu.rs b/src/dfu.rs index 0e40935..0af4e38 100644 --- a/src/dfu.rs +++ b/src/dfu.rs @@ -1,28 +1,25 @@ use cortex_m_rt::{pre_init}; -/// RAM location used to store DFU trigger message -const DFU_MSG_ADDR: usize = 0x2001BC00; +const DFU_TRIG_MSG: u32 = 0xDECAFBAD; -/// DFU trigger message -const DFU_TRIG_MSG: usize = 0xDECAFBAD; - -/// Set DFU trigger -pub unsafe fn trig_dfu() { - let dfu_msg_addr = DFU_MSG_ADDR as *mut usize; - *dfu_msg_addr = DFU_TRIG_MSG; +pub unsafe fn set_dfu_trigger() { + extern "C" { + static mut _dfu_msg: u32; + } + _dfu_msg = DFU_TRIG_MSG; } -/// Called by reset handler in lib.rs immediately after reset, checks if booting into dfu is needed +/// Called by reset handler in lib.rs immediately after reset. +/// This function should not be called outside of reset handler as +/// bootloader expects MCU to be in reset state when called. #[pre_init] unsafe fn __pre_init() { + extern "C" { + static mut _dfu_msg: u32; + } - let dfu_msg_addr = DFU_MSG_ADDR as *mut usize; - - // Check DFU trigger message - if *dfu_msg_addr == DFU_TRIG_MSG{ - - // Reset message - *dfu_msg_addr = 0x00000000; + if _dfu_msg == DFU_TRIG_MSG { + _dfu_msg = 0x00000000; // Enable system config controller clock const RCC_APB2ENR: *mut u32 = 0xE000_ED88 as *mut u32; @@ -41,14 +38,14 @@ unsafe fn __pre_init() { SYSCFG_MEMRMP, *SYSCFG_MEMRMP | SYSCFG_MEMRMP_MAP_ROM, ); - - // Set stack pointer to bootloader location - asm!("LDR R0, =0x1FFF0000"); - asm!("LDR SP,[R0, #0]"); - - // Jump to bootloader - asm!("LDR R0,[R0, #4]"); - asm!("BX R0"); + + asm!( + // Set stack pointer to bootloader location + "LDR R0, =0x1FFF0000", + "LDR SP,[R0, #0]", + // Jump to bootloader + "LDR R0,[R0, #4]", + "BX R0", + ); } - } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 142278f..8cf4b6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -429,8 +429,9 @@ fn main() -> ! { channels.power_down(i); } unsafe { - dfu::trig_dfu(); + dfu::set_dfu_trigger(); } + socket.close(); SCB::sys_reset(); } }