Troubles with flash programming on STM32G474RE. Can`t write anything to bank 2.
Hi everyone, i`ve got trouble with InApp flash programming on STM32G474RE. While i`am trying to load some data to address higher than 0x08040000 (Bank 2) in dual bank mode by the internal bootloader (placed on address range from 0x08000000 to 0x08005000 and it takes data from external QSPI flash memory chip), the data in bank 2 is stay clear (Look at Image1).
But flash programming by internal InApp bootloader is always sucsessful (and Bank 2 too), when memory was erased before device powering up. Unfortunately application does`t allow to turn device off, so it`s necessary to find out another solution for this problem.
And that is really strange, cause all pages in addr range from 0x08005000 to 0x0807FFFF have been erased previosly. Memory protection is off and no Option Bytes checked, which may affect on memory programming processes.
The programming function:
while(!ready());//waiting for BSY flag is cleared
FLASH->CR |= FLASH_CR_PG; //PG bit on
*(volatile uint32_t*)address = (uint32_t)(word1);//uploading first word
*(volatile uint32_t*)(address + 4) = (uint32_t)(word2);//uploading second word
while(!ready()); // waiting for BSY flag is cleared
FLASH->CR &= ~(FLASH_CR_PG); //Set PG bit off
Erase function follows:
void Erase(uint8_t start, uint8_t stop)
{
while (FLASH->SR & FLASH_SR_BSY);
if (stop <= 128) {
for (page_index = start; page_index < (stop); page_index++)
{
MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((page_index & 0xFFU) << FLASH_CR_PNB_Pos));
SET_BIT(FLASH->CR, FLASH_CR_PER);
SET_BIT(FLASH->CR, FLASH_CR_STRT);
while (FLASH->SR & FLASH_SR_BSY);
CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
}
}
else {
for (page_index = start; page_index < (127); page_index++)
{
MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((page_index & 0xFFU) << FLASH_CR_PNB_Pos));
SET_BIT(FLASH->CR, FLASH_CR_PER);
SET_BIT(FLASH->CR, FLASH_CR_STRT);
while (FLASH->SR & FLASH_SR_BSY);
CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
}
SET_BIT (FLASH->CR, FLASH_CR_BKER);
for (page_index = 128; page_index < (stop); page_index++)
{
MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((page_index & 0xFFU) << FLASH_CR_PNB_Pos));
SET_BIT(FLASH->CR, FLASH_CR_PER);
SET_BIT(FLASH->CR, FLASH_CR_STRT);
while (FLASH->SR & FLASH_SR_BSY);
CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
}
CLEAR_BIT (FLASH->CR, FLASH_CR_BKER);
}
}
Does anyone faced with such problem?