HAL_FLASH_Program crashing my MCPU - but not in debug mode
I'm not completely sure where to start. But, I have 20k of data and I want to write to to flash.
I'm using this function to do it (Start address is Bank2 of this processor 0x08080000)
page size is 256 bytes, for this MCPU. (StM32L471)
void Save_to_Flash(uint32_t *data) {
uint32_t src_addr = (uint32_t) data;
uint32_t Address;
uint32_t start_time;
HAL_StatusTypeDef HAL_STATUS;
uint16_t bytes=0;
uint16_t page_size = FLASH_ROW_SIZE * sizeof(uint64_t);
if (!Erase_Flash()) {
return;
}
HAL_FLASH_Unlock();
Address = FLASH_USER_START_ADDR;
while (Address <= FLASH_USER_START_ADDR + (DATA_VOL * sizeof(uint16_t))) {
printf("Writing to address: %#x (%i)", Address, bytes);
HAL_STATUS = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, Address, (uint64_t) src_addr);
if (HAL_STATUS == HAL_OK) {
printf("Wrote to address: %#x (%i)", Address, bytes);
Address = Address + page_size;
src_addr = src_addr + page_size;
bytes+=page_size;
} else {
printf("Flash write error at address %#x", Address);
}
}
HAL_FLASH_Lock();
printf("Flash (%i) written in %ims", bytes, HAL_GetTick()-start_time);
}If I put a break point at the first printf() line, and then "continue" for each iteration the data is written perfectly, every time.
If I run the data outside of the debugger it crashes the MCPU every time.
Now, when I say "crash" I mean it. Not just the fault handler. I can no longer program the MCPU, and I have to power it off and on.
While analyzing this I've captured the following data.
First, I'm using Keil's ARM MDK, and the debug window shows this:
The "Internal command error" occurs after the MCPU crashes.
My output shows writing is occurring, at east for the first page:
And when I look at the memory:
I can see it has stopped just several bytes into the page (the bytes written are correct)
I've been fighting with this for awhile and I don't know what else to try.
Even inserting a HAL_Delay(1000) between writes does not make a different.
(and I wouldn't want that solution)