dfu: refactor for PR

pull/46/head
topquark12 2021-01-13 11:06:06 +08:00
parent a320b6104e
commit 2e6116016a
4 changed files with 29 additions and 30 deletions

View File

@ -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 <X.X.X.X/L> [Y.Y.Y.Y]` | Configure IPv4 address, netmask length, and optional default gateway |

View File

@ -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);

View File

@ -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",
);
}
}

View File

@ -429,8 +429,9 @@ fn main() -> ! {
channels.power_down(i);
}
unsafe {
dfu::trig_dfu();
dfu::set_dfu_trigger();
}
socket.close();
SCB::sys_reset();
}
}