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 <karlp@tweak.net.au>
This commit is contained in:
Bruno Randolf 2017-12-22 12:39:58 +00:00 committed by Karl Palsson
parent 49a3cddba0
commit a1ffdc59f0
2 changed files with 14 additions and 16 deletions

View File

@ -228,7 +228,7 @@ void flash_clear_size_flag(void);
void flash_clear_pgaerr_flag(void); void flash_clear_pgaerr_flag(void);
void flash_clear_wrperr_flag(void); void flash_clear_wrperr_flag(void);
void flash_lock_option_bytes(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_program(uint32_t address, uint8_t *data, uint32_t len);
void flash_erase_page(uint32_t page); void flash_erase_page(uint32_t page);
void flash_erase_all_pages(void); void flash_erase_all_pages(void);

View File

@ -108,15 +108,16 @@ void flash_lock_option_bytes(void)
FLASH_CR |= FLASH_CR_OPTLOCK; FLASH_CR |= FLASH_CR_OPTLOCK;
} }
/** @brief Program a 32 bit Word to FLASH /** @brief Program a 64 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 * This performs all operations necessary to program a 64 bit word to FLASH memory.
* that memory was not properly erased. * 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] 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. */ /* Ensure that all flash operations are complete. */
flash_wait_for_last_operation(); flash_wait_for_last_operation();
@ -124,8 +125,9 @@ void flash_program_word(uint32_t address, uint32_t data)
/* Enable writes to flash. */ /* Enable writes to flash. */
FLASH_CR |= FLASH_CR_PG; FLASH_CR |= FLASH_CR_PG;
/* Program the word. */ /* Program the each word separately. */
MMIO32(address) = data; MMIO32(address) = (uint32_t)data;
MMIO32(address+4) = (uint32_t)(data >> 32);
/* Wait for the write to complete. */ /* Wait for the write to complete. */
flash_wait_for_last_operation(); flash_wait_for_last_operation();
@ -140,16 +142,12 @@ void flash_program_word(uint32_t address, uint32_t data)
* memory was not properly erased. * memory was not properly erased.
* @param[in] address Starting address in Flash. * @param[in] address Starting address in Flash.
* @param[in] data Pointer to start of data block. * @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) void flash_program(uint32_t address, uint8_t *data, uint32_t len)
{ {
/* TODO: Use dword and word size program operations where possible for for (uint32_t i = 0; i < len; i += 8) {
* turbo speed. flash_program_double_word(address+i, *(uint64_t*)(data + i));
*/
uint32_t i;
for (i = 0; i < len; i++) {
flash_program_word(address+i, data[i]);
} }
} }