Hi,
i use the stm32F3 and stm32f4. I want to implement a bootloader which loads data from an external flash to the internal flash area where the application takes place.
How i can unlock the flash, erase it and write the application data to it? Are there HAL capabilities to do that?
Thanks.
How write to internal flash from a bootloader
- Giovanni
- Site Admin
- Posts: 14893
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: How write to internal flash from a bootloader
No API in the HAL for flash yet, you need to operate at register level while running from RAM.
Giovanni
Giovanni
Re: How write to internal flash from a bootloader
Each section of the flash is independent unlockable isn't it? So i do not have to run from RAM or? If there is a static bootloader and application section i should also work from flash or?
- Giovanni
- Site Admin
- Posts: 14893
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: How write to internal flash from a bootloader
neums wrote:Each section of the flash is independent unlockable isn't it? So i do not have to run from RAM or? If there is a static bootloader and application section i should also work from flash or?
See the RM, if I remember well the flash is not accessible while being programmed.
Giovanni
Re: How write to internal flash from a bootloader
neums wrote:Each section of the flash is independent unlockable isn't it? So i do not have to run from RAM or? If there is a static bootloader and application section i should also work from flash or?
The problem is that the Flash is locked while erasing and writing to it.
If you have already all the data available somewhere and there are no realtime requirements you can execute code from flash while writing, but the execution will freeze during this time.
Generally you can use a loop while writing waiting for the completion.
Re: How write to internal flash from a bootloader
Hi and thanks,
i erase and wrote to a page within the internal flash by using the following functions and the STM32F3:
The code is located in the internal flash. Do i have to lock the global interrupt during erasing and writing to the flash because of a possible memory error?
In fact it is not always necessary to execute from RAM.
i erase and wrote to a page within the internal flash by using the following functions and the STM32F3:
Code: Select all
void app_init_erase_flash(void){
uint32_t memory_p = 0x803F800;
FLASH_TypeDef* flash = FLASH;
flash->KEYR = FLASH_KEY1;
flash->KEYR = FLASH_KEY2;
while((flash->SR & FLASH_SR_BSY) != 0);
flash->CR = FLASH_CR_PER;
flash->AR = memory_p;
flash->CR |= FLASH_CR_STRT;
while((flash->SR & FLASH_SR_BSY) != 0);
while((flash->SR & FLASH_SR_EOP) == 0);
flash->SR &= ~FLASH_SR_EOP;
}
void app_init_write_flash(void){
uint16_t* memory_p = (uint16_t*) 0x803F800;
FLASH_TypeDef* flash = FLASH;
flash->KEYR = FLASH_KEY1;
flash->KEYR = FLASH_KEY2;
while((flash->SR & FLASH_SR_BSY) != 0);
flash->CR = FLASH_CR_PG;
*memory_p = 0xBEEF;
while((flash->SR & FLASH_SR_BSY) != 0);
while((flash->SR & FLASH_SR_EOP) == 0);
flash->SR &= ~FLASH_SR_EOP;
}
The code is located in the internal flash. Do i have to lock the global interrupt during erasing and writing to the flash because of a possible memory error?
In fact it is not always necessary to execute from RAM.