From a6466c847a89033dbaf81e7ef82fc1666c99631e Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 13 Jan 2021 16:55:56 +0800 Subject: [PATCH 1/4] flash store: initial prototype --- memory.x | 2 ++ src/flash_store.rs | 32 ++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/memory.x b/memory.x index c0ae171..4d61b10 100644 --- a/memory.x +++ b/memory.x @@ -13,3 +13,5 @@ MEMORY _stack_start = ORIGIN(CCMRAM) + LENGTH(CCMRAM); _dfu_msg = ORIGIN(DFU_MSG); +_config_start = ORIGIN(CONFIG); +_flash_start = ORIGIN(FLASH); diff --git a/src/flash_store.rs b/src/flash_store.rs index f69e534..6374562 100644 --- a/src/flash_store.rs +++ b/src/flash_store.rs @@ -8,18 +8,37 @@ use sfkv::{Store, StoreBackend}; /// 16 KiB pub const FLASH_SECTOR_SIZE: usize = 0x4000; pub const FLASH_SECTOR: u8 = 12; -pub const FLASH_SECTOR_OFFSET: usize = 0x10_0000; static mut BACKUP_SPACE: [u8; FLASH_SECTOR_SIZE] = [0; FLASH_SECTOR_SIZE]; +extern "C" { + static _config_start: usize; + static _flash_start: usize; +} + pub struct FlashBackend { flash: FLASH, } +unsafe fn get_offset() -> usize { + let config = core::mem::transmute::<*const usize, usize>(&_config_start); + let flash = core::mem::transmute::<*const usize, usize>(&_flash_start); + config - flash +} + impl StoreBackend for FlashBackend { - type Data = [u8]; + type Data = [u8]; fn data(&self) -> &Self::Data { - &self.flash.read()[FLASH_SECTOR_OFFSET..(FLASH_SECTOR_OFFSET + FLASH_SECTOR_SIZE)] + unsafe { + // This works + // let config = core::mem::transmute::<*const usize, usize>(&_config_start); + // let flash = core::mem::transmute::<*const usize, usize>(&_flash_start); + // let config_offset = config - flash; + + // This doesn't + // let config_offset = (&_config_start - &_flash_start) as usize; + &self.flash.read()[get_offset()..(get_offset() + FLASH_SECTOR_SIZE)] + } } type Error = Error; @@ -29,11 +48,12 @@ impl StoreBackend for FlashBackend { } fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> { - self.flash.unlocked() - .program(FLASH_SECTOR_OFFSET + offset, payload.iter().cloned()) + unsafe { + self.flash.unlocked() + .program(get_offset() + offset, payload.iter().cloned()) + } } - fn backup_space(&self) -> &'static mut [u8] { unsafe { &mut BACKUP_SPACE } } -- 2.42.0 From cef9cf6f48c80a77bb643014b43794e994a92b92 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 13 Jan 2021 17:13:18 +0800 Subject: [PATCH 2/4] flash store: remove transmute --- src/flash_store.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/flash_store.rs b/src/flash_store.rs index 6374562..27b55ba 100644 --- a/src/flash_store.rs +++ b/src/flash_store.rs @@ -20,9 +20,7 @@ pub struct FlashBackend { } unsafe fn get_offset() -> usize { - let config = core::mem::transmute::<*const usize, usize>(&_config_start); - let flash = core::mem::transmute::<*const usize, usize>(&_flash_start); - config - flash + (&_config_start as *const usize as usize) - (&_flash_start as *const usize as usize) } impl StoreBackend for FlashBackend { @@ -30,13 +28,6 @@ impl StoreBackend for FlashBackend { fn data(&self) -> &Self::Data { unsafe { - // This works - // let config = core::mem::transmute::<*const usize, usize>(&_config_start); - // let flash = core::mem::transmute::<*const usize, usize>(&_flash_start); - // let config_offset = config - flash; - - // This doesn't - // let config_offset = (&_config_start - &_flash_start) as usize; &self.flash.read()[get_offset()..(get_offset() + FLASH_SECTOR_SIZE)] } } -- 2.42.0 From 0200aa69bdfe969c40c4487e8a3524a7c2c18807 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 13 Jan 2021 17:16:29 +0800 Subject: [PATCH 3/4] flash store: clean up --- src/flash_store.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/flash_store.rs b/src/flash_store.rs index 27b55ba..92dcc91 100644 --- a/src/flash_store.rs +++ b/src/flash_store.rs @@ -11,6 +11,7 @@ pub const FLASH_SECTOR: u8 = 12; static mut BACKUP_SPACE: [u8; FLASH_SECTOR_SIZE] = [0; FLASH_SECTOR_SIZE]; extern "C" { + // These are from memory.x static _config_start: usize; static _flash_start: usize; } @@ -24,7 +25,7 @@ unsafe fn get_offset() -> usize { } impl StoreBackend for FlashBackend { - type Data = [u8]; + type Data = [u8]; fn data(&self) -> &Self::Data { unsafe { -- 2.42.0 From 1c71fbb6a7f0e7b3eed25b4ca4bdac5f61e64caf Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 13 Jan 2021 17:23:29 +0800 Subject: [PATCH 4/4] flash store: move unsafe, organise memory.x --- memory.x | 6 +++--- src/flash_store.rs | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/memory.x b/memory.x index 4d61b10..c4751ec 100644 --- a/memory.x +++ b/memory.x @@ -11,7 +11,7 @@ MEMORY CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K } -_stack_start = ORIGIN(CCMRAM) + LENGTH(CCMRAM); -_dfu_msg = ORIGIN(DFU_MSG); -_config_start = ORIGIN(CONFIG); _flash_start = ORIGIN(FLASH); +_config_start = ORIGIN(CONFIG); +_dfu_msg = ORIGIN(DFU_MSG); +_stack_start = ORIGIN(CCMRAM) + LENGTH(CCMRAM); diff --git a/src/flash_store.rs b/src/flash_store.rs index 92dcc91..ef9f651 100644 --- a/src/flash_store.rs +++ b/src/flash_store.rs @@ -20,17 +20,17 @@ pub struct FlashBackend { flash: FLASH, } -unsafe fn get_offset() -> usize { - (&_config_start as *const usize as usize) - (&_flash_start as *const usize as usize) +fn get_offset() -> usize { + unsafe { + (&_config_start as *const usize as usize) - (&_flash_start as *const usize as usize) + } } impl StoreBackend for FlashBackend { type Data = [u8]; fn data(&self) -> &Self::Data { - unsafe { - &self.flash.read()[get_offset()..(get_offset() + FLASH_SECTOR_SIZE)] - } + &self.flash.read()[get_offset()..(get_offset() + FLASH_SECTOR_SIZE)] } type Error = Error; @@ -40,10 +40,8 @@ impl StoreBackend for FlashBackend { } fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> { - unsafe { - self.flash.unlocked() - .program(get_offset() + offset, payload.iter().cloned()) - } + self.flash.unlocked() + .program(get_offset() + offset, payload.iter().cloned()) } fn backup_space(&self) -> &'static mut [u8] { -- 2.42.0