How write to internal flash from a bootloader

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

How write to internal flash from a bootloader

Post by neums »

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.
User avatar
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

Post by Giovanni »

No API in the HAL for flash yet, you need to operate at register level while running from RAM.

Giovanni
neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: How write to internal flash from a bootloader

Post by neums »

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?
User avatar
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

Post by Giovanni »

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
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: How write to internal flash from a bootloader

Post by Korken »

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.
Alex
Posts: 39
Joined: Sat Feb 22, 2014 2:40 pm
Been thanked: 3 times

Re: How write to internal flash from a bootloader

Post by Alex »

neums
Posts: 137
Joined: Fri May 08, 2015 1:09 pm
Has thanked: 10 times
Been thanked: 5 times

Re: How write to internal flash from a bootloader

Post by neums »

Hi and thanks,
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.
Post Reply