cancel
Showing results for 
Search instead for 
Did you mean: 

Difficulties with Writing to the Flash Memory of STM32L4R5

Shadow
Associate II

Hi,

I'm novice with the STM32 microcontrollers and this community.

I'm trying to write to the flash memory of STM32L4R5 in 'FLASH_TYPEPROGRAM_FAST' mode of the HAL_FLASH_Program().

The flash of the MCU is configured as Single Bank.

Writing to the flash only works when using 'FLASH_TYPEPROGRAM_DOUBLEWORD'. The flash reads as 0xFFFFFFFF when written in 'FLASH_TYPEPROGRAM_FAST' mode.

This is my test project:

// Page Erase Structure
static FLASH_EraseInitTypeDef EraseInitStruct;
 
// Page Erase Status
uint32_t eraseStatus;
 
// Data Buffer
uint64_t pDataBuf[32] =
{
   0x1111111122222222, 0x3333333344444444,
   0x5555555566666666, 0x7777777788888888,
   0x12345678ABC12345, 0x23456789DEF01234,
   0x34567890AAABBB12, 0x4567890FABCDDD34,
   0x1111111122222222, 0x3333333344444444,
   0x5555555566666666, 0x7777777788888888,
   0x12345678ABC12345, 0x23456789DEF01234,
   0x34567890AAABBB12, 0x4567890FABCDDD34,
   0x1111111122222222, 0x3333333344444444,
   0x5555555566666666, 0x7777777788888888,
   0x12345678ABC12345, 0x23456789DEF01234,
   0x34567890AAABBB12, 0x4567890FABCDDD34,
   0x1111111122222222, 0x3333333344444444,
   0x5555555566666666, 0x7777777788888888,
   0x12345678ABC12345, 0x23456789DEF01234,
   0x34567890AAABBB12, 0x4567890FABCDDD34
};
 
// Flash Page Start Address
uint32_t pageAddr = 0x081FE000;
 
// Fill Erase Init Structure
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks     = FLASH_BANK_1;
EraseInitStruct.Page      = 255;
EraseInitStruct.NbPages   = 1;
 
// Unlocking the FLASH Control Register
HAL_FLASH_Unlock();
 
// Clear OPTVERR Bit Set on Virgin Samples
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
 
// Erasing the Flash Page 
HAL_FLASHEx_Erase(&EraseInitStruct, &Error);
 
#if 0
// Wriring a Doubled Word to Flash. pDataBuf[0] is the 64-bit Word
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, pageAddr, pDataBuf[0]);
#else
// Wriring 32 Double Words. pDataBuf is the Starting Address of the 64-bit Array  
HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST_AND_LAST, pageAddr, pDataBuf);
#endif
 
// Locking the FLASH Control Register
HAL_FLASH_Lock();

Am I doing anything wrong?

Thank you,

Ivan

6 REPLIES 6
gbm
Lead III

AFAIR, Fast mode requires mass erase (not page erase) immediately before writing. Check the Reference Manual.

Shadow
Associate II

Looks like you are right, gbm. This is what the Reference Manual says: "In Single-bank mode (DBANK=0), perform a mass erase. The Fast programing can be performed only if the code is executed from RAM or from bootloader".

Single-bank mode is set in our case. The reason of writing to the Flash is the intention of implementing an external Bootloader in the main Flash memory. The internal Bootloader doesn't work for us. It is using the internal oscillator, which makes the UART communication unreliable, especially at baud rate 115200. Also we need the LCD of our device to be operational and controlled by the Bootloader.

However, Mass Erase will erase the Bootloader code as well. Probably will have to define the Flash memory programming functionality in the RAM.

Thank you!

Ivan

gbm
Lead III

And why don't you simply use the normal, 64-bit at a time programming? That's what I do in all my projects with firmware update capability.

Shadow
Associate II

Using 'FLASH_TYPEPROGRAM_DOUBLEWORD' works but I suppose the advantage of using 'FLASH_TYPEPROGRAM_FAST' is the shorter time of updating the entire firmware of the device by the Bootloader.

Am I correct or there is no significant difference with the speed when flashing big data blocks?

The flash memory characteristics table in a datasheet indicates erase/programming times for your device.

Shadow
Associate II

Timing is not an issue. Looks like there are some limitations with Fast Flash Programming. But I applied Double Word Flash Programming and it works fine.