STM32 FLASH.
I think regular visitors to the site have noticed that I prefer to do everything with registers, but sometimes I still use SPL. For example, the article about a custom bootloader used library functions to work with flash memory.
Once again connecting this library, I decided that it takes up too much space and decided to implement the necessary functions myself.
There are 7 registers for working with flash memory:
FLASH_KEYR
After an MCU reset, the FPEC (Flash memory program/erase controller) is protected against accidental writing/erasing. To unlock it, two keys must be written sequentially to the FLASH_KEYR register: KEY1 = 0×45670123 and KEY2 = 0xCDEF89AB.
After that, the LOCK bit in the FLASH_CR register will be cleared, and when writing is finished it must be set again by software.
FLASH_OPTKEYR
It allows removing the protection from the option bytes so they can be modified; after an MCU reset they are read-only. Its purpose is similar to FLASH_KEYR, and the keys and unlock sequence are the same. The difference is that after the two keys are written sequentially, the OPTWRE bit in the FLASH_CR register is set, which allows writing data to the option byte area of the flash memory.
FLASH_SR

BSY(Busy) — set during write/erase operations and cleared when they finish or an error occurs.
PGERR(Programming error) — set when trying to program memory that has not been erased, i.e. whose value differs from 0xFFFF.
WRPRTERR(Write protection error) — set when trying to program a write-protected area.
EOP(End of operation) — set by hardware when a write/erase operation completes successfully.
FLASH_CR
We have already used the FLASH_CR register above; let's look at the bit fields it consists of.

PG(Programming) — writing a one to this bit enables flash programming.
PER(Page erase) — writing a one to this bit allows erasing one page of flash memory.
MER(Mass erase) — writing a one to this bit enables a full erase of flash memory.
OPTPG(Option byte programming) — writing a one to this bit enables programming of the option bytes.
OPTER(Option byte erase) — writing a one to this bit allows erasing the option bytes.
STRT(Start) — writing a one to this bit starts the flash erase procedure.
LOCK(LOCK) — writing a one to this bit locks writing/erasing of flash memory.
OPTWRE(Option bytes write enable) — writing a one to this bit enables programming of the option bytes.
ERRIE(Error interrupt enable) — writing a one to this bit enables interrupt generation when errors occur, i.e. when PGERR or WRPRTERR is set in the FLASH_SR register.
EOPIE(End of operation interrupt enable) — writing a one to this bit enables interrupt generation at the end of a write/erase operation, i.e. when EOP is set in the FLASH_SR register.
OBL_LAUNCH(Force option byte loading) — writing a one to this bit...
FLASH_AR
Used to set the address when programming flash memory. To erase a page, you need to write to this register any address belonging to that page, not necessarily the starting one.
FLASH_OBR and FLASH_WRPR
These registers are not involved in write and erase operations; they are used to set up MCU protection. A separate article is planned on this.
The code at the beginning of the article shows that the flash must be erased before writing, and flash on STM32 can be erased either one page at a time or completely. When updating firmware with a bootloader, a full flash erase is not an option (the bootloader itself would be erased), so pages must be erased one at a time. The page size should be checked in the MCU documentation.
Reading a 32-bit variable is very simple; you just need to know its address.
To write data you also need an address and to understand that data is written in 16-bit blocks.
If the function returned zero, you need to check the flags in the FLASH_SR register to find out what caused the error.
That's it: the basic functions needed to work with flash memory are implemented, and many unclear points have been cleared up. All it took was reading a couple of pages of the datasheet)))
if(new_firmware)
{
// we use StdPeriph to work with flash, don't forget to include it
//unlock the flash,
FLASH_Unlock();
//clear the flags
FLASH->SR |= FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR;
//erase the pages
for(erase_counter = 0; (erase_counter <= count_page) && (FLASHStatus == FLASH_COMPLETE); erase_counter++)
{
FLASHStatus = FLASH_ErasePage(APPLICATION_ADDRESS + (FLASH_PAGE_SIZE * erase_counter));
}
/*update*/
//lock the flash
FLASH_Lock();
}
Once again connecting this library, I decided that it takes up too much space and decided to implement the necessary functions myself.
There are 7 registers for working with flash memory:
- Key register (FLASH_KEYR)
- Option byte key register (FLASH_OPTKEYR)
- Flash control register (FLASH_CR)
- Flash status register (FLASH_SR)
- Flash address register (FLASH_AR)
- Option byte register (FLASH_OBR)
- Write protection register (FLASH_WRPR)
FLASH_KEYR
After an MCU reset, the FPEC (Flash memory program/erase controller) is protected against accidental writing/erasing. To unlock it, two keys must be written sequentially to the FLASH_KEYR register: KEY1 = 0×45670123 and KEY2 = 0xCDEF89AB.
#define FLASH_KEY1 ((uint32_t)0x45670123)
#define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
void FLASH_Unlock(void)
{
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
}
After that, the LOCK bit in the FLASH_CR register will be cleared, and when writing is finished it must be set again by software.
void FLASH_Lock(void)
{
FLASH->CR |= FLASH_CR_LOCK;
}
FLASH_OPTKEYR
It allows removing the protection from the option bytes so they can be modified; after an MCU reset they are read-only. Its purpose is similar to FLASH_KEYR, and the keys and unlock sequence are the same. The difference is that after the two keys are written sequentially, the OPTWRE bit in the FLASH_CR register is set, which allows writing data to the option byte area of the flash memory.
FLASH_SR

BSY(Busy) — set during write/erase operations and cleared when they finish or an error occurs.
PGERR(Programming error) — set when trying to program memory that has not been erased, i.e. whose value differs from 0xFFFF.
WRPRTERR(Write protection error) — set when trying to program a write-protected area.
EOP(End of operation) — set by hardware when a write/erase operation completes successfully.
FLASH_CR
We have already used the FLASH_CR register above; let's look at the bit fields it consists of.

PG(Programming) — writing a one to this bit enables flash programming.
PER(Page erase) — writing a one to this bit allows erasing one page of flash memory.
MER(Mass erase) — writing a one to this bit enables a full erase of flash memory.
OPTPG(Option byte programming) — writing a one to this bit enables programming of the option bytes.
OPTER(Option byte erase) — writing a one to this bit allows erasing the option bytes.
STRT(Start) — writing a one to this bit starts the flash erase procedure.
LOCK(LOCK) — writing a one to this bit locks writing/erasing of flash memory.
OPTWRE(Option bytes write enable) — writing a one to this bit enables programming of the option bytes.
ERRIE(Error interrupt enable) — writing a one to this bit enables interrupt generation when errors occur, i.e. when PGERR or WRPRTERR is set in the FLASH_SR register.
EOPIE(End of operation interrupt enable) — writing a one to this bit enables interrupt generation at the end of a write/erase operation, i.e. when EOP is set in the FLASH_SR register.
OBL_LAUNCH(Force option byte loading) — writing a one to this bit...
FLASH_AR
Used to set the address when programming flash memory. To erase a page, you need to write to this register any address belonging to that page, not necessarily the starting one.
FLASH_OBR and FLASH_WRPR
These registers are not involved in write and erase operations; they are used to set up MCU protection. A separate article is planned on this.
The code at the beginning of the article shows that the flash must be erased before writing, and flash on STM32 can be erased either one page at a time or completely. When updating firmware with a bootloader, a full flash erase is not an option (the bootloader itself would be erased), so pages must be erased one at a time. The page size should be checked in the MCU documentation.
_Bool flash_ready(void)
{
return !(FLASH->SR & FLASH_SR_BSY);
}
_Bool check_EOP(void)
{
if(FLASH->SR & FLASH_SR_EOP)
{
FLASH->SR |= FLASH_SR_EOP;
return 1;
}
return 0;
}
_Bool FLASH_Erase_Page(uint32_t address)
{
while(!flash_ready()); //Wait until the flash is ready for writing
FLASH->CR|= FLASH_CR_PER; //Set the single page erase bit
FLASH->AR = address; // Set its address
FLASH->CR|= FLASH_CR_STRT; // Start erasing
while(!flash_ready()); //Wait until the page is erased
FLASH->CR &= ~FLASH_CR_PER; //Clear the single page erase bit
return check_EOP();//operation complete, clear the flag
}
Reading a 32-bit variable is very simple; you just need to know its address.
data= (*(__IO uint32_t*) address)To write data you also need an address and to understand that data is written in 16-bit blocks.
_Bool FLASH_Program_Word(uint32_t address,uint32_t data)
{
while(!flash_ready()); //Wait until the flash is ready for writing
FLASH->CR |= FLASH_CR_PG; //Enable flash programming
*(__IO uint16_t*)address = (uint16_t)data; //Write the lower 2 bytes
while(!flash_ready());//Wait for the operation to complete
if(!check_EOP())return 0;
address+=2;//Add two bytes to the address
data>>=16;//Shift the data
*(__IO uint16_t*)address = (uint16_t)data; //Write the upper 2 bytes
while(!flash_ready());//Wait for the operation to complete
FLASH->CR &= ~(FLASH_CR_PG); //Disable flash programming
return check_EOP();
}If the function returned zero, you need to check the flags in the FLASH_SR register to find out what caused the error.
That's it: the basic functions needed to work with flash memory are implemented, and many unclear points have been cleared up. All it took was reading a couple of pages of the datasheet)))