2024-05-01 01:44 AM
Hello,
I am trying to implement a flash erase function, which is based on the reference manual. I am using the STM32L476RG nucleo board. From there the instructions are:
To erase a page (2 Kbyte), follow the procedure below:
So for that operation I have created a function based on CMSIS which is the following:
uint16_t bl_flash_page_erase(uint32_t page, uint16_t bank)
{
/*Wait if any flash memory operation is ongoing*/
while (READ_BIT(FLASH->SR, FLASH_SR_BSY)) {}
/*Check if any error occurs due to previous programming*/
bl_flash_clear_status_flags();
/*Check the bank of flash memory, specified by the user*/
if (bank == 1) {
/*Set bank 1*/
CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
} else if (bank == 2) {
/*Set bank 2*/
SET_BIT(FLASH->CR, FLASH_CR_BKER);
} else {
/*Undefined bank space*/
return 0;
}
/*Define the page that must be erased*/
MODIFY_REG(FLASH->CR, FLASH_CR_PNB, (page << FLASH_CR_PNB_Pos));
/*Enable page erase operation*/
SET_BIT(FLASH->CR, FLASH_CR_PER);
/*Start the procedure*/
SET_BIT(FLASH->CR, FLASH_CR_STRT);
/*Wait the flash to finish its ongoing operation*/
while (READ_BIT(FLASH->SR, FLASH_SR_BSY)) {}
/*Disable page operation*/
CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
return 1;
}
The thing is that it is doesn't turn on the START bit and the erase operation never happens. I have tried to change the position of this operation (SET STRT bit) and put it at the begging, then it manages to turn it on however the system crashes. What am I doing wrong here?
Solved! Go to Solution.
2024-05-07 03:38 AM - edited 2024-05-07 03:39 AM
Have you unlocked flash access by writing the key registers?
Check LOCK bit 31 in CR.
2024-05-07 02:18 AM
Hello @ngrigoriadis
To implement your function, you can refer to the function FLASH_PageErase() available in stm32l4xx_hal_flash_ex.c.
2024-05-07 03:38 AM - edited 2024-05-07 03:39 AM
Have you unlocked flash access by writing the key registers?
Check LOCK bit 31 in CR.
2024-05-09 12:46 AM
That was the issue thank you very much!