From b9448bff169114f8263d11a5b452e141a48c0448 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Mon, 23 Jul 2018 09:07:45 +0000 Subject: [PATCH] stm32l4: flash: fix page erase for second bank l4 is pages, not sectors, so update apis to be consistent. (other families use page/sector as defined in the reference manual) Make sure that pages on the second bank can also be erased. Use the same style in use for f2/4/7 for sector numbers across banks. --- include/libopencm3/stm32/l4/flash.h | 4 ++-- lib/stm32/l4/flash.c | 18 +++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/include/libopencm3/stm32/l4/flash.h b/include/libopencm3/stm32/l4/flash.h index be790d7d..b5442b6d 100644 --- a/include/libopencm3/stm32/l4/flash.h +++ b/include/libopencm3/stm32/l4/flash.h @@ -231,8 +231,8 @@ void flash_clear_status_flags(void); void flash_lock_option_bytes(void); void flash_program_word(uint32_t address, uint32_t data); void flash_program(uint32_t address, uint8_t *data, uint32_t len); -void flash_erase_sector(uint8_t sector); -void flash_erase_all_sectors(void); +void flash_erase_page(uint32_t page); +void flash_erase_all_pages(void); void flash_program_option_bytes(uint32_t data); END_DECLS diff --git a/lib/stm32/l4/flash.c b/lib/stm32/l4/flash.c index c3ec1f5e..e32142d5 100644 --- a/lib/stm32/l4/flash.c +++ b/lib/stm32/l4/flash.c @@ -146,32 +146,28 @@ void flash_program(uint32_t address, uint8_t *data, uint32_t len) } } -/** @brief Erase a Sector of FLASH - * This performs all operations necessary to erase a sector in FLASH memory. - * The page should be checked to ensure that it was properly erased. A sector - * must first be fully erased before attempting to program it. - * See the reference manual or the FLASH programming manual for details. - * @param[in] sector (0 - 11 for some parts, 0-23 on others) +/** @brief Erase a page of FLASH + * @param[in] page (0 - 255 for bank 1, 256-511 for bank 2) */ -void flash_erase_sector(uint8_t sector) +void flash_erase_page(uint32_t page) { flash_wait_for_last_operation(); - FLASH_CR &= ~(FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT); - FLASH_CR |= (sector & FLASH_CR_PNB_MASK) << FLASH_CR_PNB_SHIFT; + /* page and bank are contiguous bits */ + FLASH_CR &= ~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_BKER); + FLASH_CR |= page << FLASH_CR_PNB_SHIFT; FLASH_CR |= FLASH_CR_PER; FLASH_CR |= FLASH_CR_START; flash_wait_for_last_operation(); FLASH_CR &= ~FLASH_CR_PER; - FLASH_CR &= ~(FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT); } /** @brief Erase All FLASH * This performs all operations necessary to erase all sectors in the FLASH * memory. */ -void flash_erase_all_sectors(void) +void flash_erase_all_pages(void) { flash_wait_for_last_operation();