From a1ffdc59f0c75d29db00d12f827f7d9fd7e1df18 Mon Sep 17 00:00:00 2001 From: Bruno Randolf Date: Fri, 22 Dec 2017 12:39:58 +0000 Subject: [PATCH] stm32:l4: flash: Program in double words According to RM0351 and RM0394 flash needs to be programmed by double words. Also fix flash_program() which was wrong anyways. Reviewed-by: Karl Palsson --- include/libopencm3/stm32/l4/flash.h | 2 +- lib/stm32/l4/flash.c | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/libopencm3/stm32/l4/flash.h b/include/libopencm3/stm32/l4/flash.h index 5f1dddef..fb028a38 100644 --- a/include/libopencm3/stm32/l4/flash.h +++ b/include/libopencm3/stm32/l4/flash.h @@ -228,7 +228,7 @@ void flash_clear_size_flag(void); void flash_clear_pgaerr_flag(void); void flash_clear_wrperr_flag(void); void flash_lock_option_bytes(void); -void flash_program_word(uint32_t address, uint32_t data); +void flash_program_double_word(uint32_t address, uint64_t data); void flash_program(uint32_t address, uint8_t *data, uint32_t len); void flash_erase_page(uint32_t page); void flash_erase_all_pages(void); diff --git a/lib/stm32/l4/flash.c b/lib/stm32/l4/flash.c index cb45081a..6381197b 100644 --- a/lib/stm32/l4/flash.c +++ b/lib/stm32/l4/flash.c @@ -108,15 +108,16 @@ void flash_lock_option_bytes(void) FLASH_CR |= FLASH_CR_OPTLOCK; } -/** @brief Program a 32 bit Word to FLASH - * This performs all operations necessary to program a 32 bit word to FLASH - * memory. The program error flag should be checked separately for the event - * that memory was not properly erased. +/** @brief Program a 64 bit word to FLASH + * + * This performs all operations necessary to program a 64 bit word to FLASH memory. + * The program error flag should be checked separately for the event that memory + * was not properly erased. * * @param[in] address Starting address in Flash. - * @param[in] data word to write + * @param[in] data Double word to write */ -void flash_program_word(uint32_t address, uint32_t data) +void flash_program_double_word(uint32_t address, uint64_t data) { /* Ensure that all flash operations are complete. */ flash_wait_for_last_operation(); @@ -124,8 +125,9 @@ void flash_program_word(uint32_t address, uint32_t data) /* Enable writes to flash. */ FLASH_CR |= FLASH_CR_PG; - /* Program the word. */ - MMIO32(address) = data; + /* Program the each word separately. */ + MMIO32(address) = (uint32_t)data; + MMIO32(address+4) = (uint32_t)(data >> 32); /* Wait for the write to complete. */ flash_wait_for_last_operation(); @@ -140,16 +142,12 @@ void flash_program_word(uint32_t address, uint32_t data) * memory was not properly erased. * @param[in] address Starting address in Flash. * @param[in] data Pointer to start of data block. - * @param[in] len Length of data block. + * @param[in] len Length of data block in bytes (multiple of 8). */ void flash_program(uint32_t address, uint8_t *data, uint32_t len) { - /* TODO: Use dword and word size program operations where possible for - * turbo speed. - */ - uint32_t i; - for (i = 0; i < len; i++) { - flash_program_word(address+i, data[i]); + for (uint32_t i = 0; i < len; i += 8) { + flash_program_double_word(address+i, *(uint64_t*)(data + i)); } }