From 09d4a95a8eec1b57bc18264fab2a5c1beb61aabc Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 15 Jan 2016 15:23:44 +0100 Subject: ucoo/hal/self_programming: add function to query erase size --- ucoo/hal/self_programming/self_programming.hh | 4 ++++ ucoo/hal/self_programming/self_programming.stm32f1.cc | 15 ++++++++++++--- ucoo/hal/self_programming/self_programming.stm32f4.cc | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ucoo/hal/self_programming/self_programming.hh b/ucoo/hal/self_programming/self_programming.hh index 4e06709..c1aceb1 100644 --- a/ucoo/hal/self_programming/self_programming.hh +++ b/ucoo/hal/self_programming/self_programming.hh @@ -31,6 +31,10 @@ namespace ucoo { int self_programming_flash_size (); +/// Return erase size at given address, which must be aligned on erase size. +int +self_programming_erase_size (uint32_t addr); + /// Erase flash, must be aligned on erase size. void self_programming_erase (uint32_t addr, int count); diff --git a/ucoo/hal/self_programming/self_programming.stm32f1.cc b/ucoo/hal/self_programming/self_programming.stm32f1.cc index 5d84561..8cbbb24 100644 --- a/ucoo/hal/self_programming/self_programming.stm32f1.cc +++ b/ucoo/hal/self_programming/self_programming.stm32f1.cc @@ -35,14 +35,23 @@ self_programming_flash_size () return DESIG_FLASH_SIZE * 1024; } -void -self_programming_erase (uint32_t addr, int count) +int +self_programming_erase_size (uint32_t addr) { - assert (static_cast (addr - FLASH_BASE + count) + assert (static_cast (addr - FLASH_BASE) <= self_programming_flash_size ()); uint32_t idcode = DBGMCU_IDCODE & DBGMCU_IDCODE_DEV_ID_MASK; int page_size = idcode >= 0x414 ? 2048 : 1024; assert ((addr & (page_size - 1)) == 0); + return page_size; +} + +void +self_programming_erase (uint32_t addr, int count) +{ + assert (static_cast (addr - FLASH_BASE + count) + <= self_programming_flash_size ()); + int page_size = self_programming_erase_size (addr); assert ((count & (page_size - 1)) == 0); while (count) { diff --git a/ucoo/hal/self_programming/self_programming.stm32f4.cc b/ucoo/hal/self_programming/self_programming.stm32f4.cc index 912eb15..3956b1e 100644 --- a/ucoo/hal/self_programming/self_programming.stm32f4.cc +++ b/ucoo/hal/self_programming/self_programming.stm32f4.cc @@ -52,6 +52,17 @@ self_programming_flash_size () return DESIG_FLASH_SIZE * 1024; } +int +self_programming_erase_size (uint32_t addr) +{ + for (int sector = 0; sector < lengthof (sector_addr) - 1; sector++) + { + if (addr == sector_addr[sector]) + return sector_addr[sector + 1] - addr; + } + assert_unreachable (); +} + void self_programming_erase (uint32_t addr, int count) { -- cgit v1.2.3