Problem reading Flash Memory
Hello guys,
I am working with a STM32L071CB MCU.
Currently I am trying to backup GPS data to internal memory flash when there is not LTE connection to send later when the LTE service is restored.
Writing data to memory has been done right. I am having problem with reading back the data.
First I am using bank1(start addr 0x08000000) to store the program code and the bank 2 (start addr 0x08010000) to backup data.
The problems I am having are as follow:
If I don't unlock the memory for writing, when I try to read starting the first address of bank2 (page 512), it reads starting the first address of bank 1 (page0).
Now when unlocking the flash, I can read some data right, however some values are still read from page0 and not from page 512 as its supposed to be. Its driving me crazy.
I am attaching the relevant information:
Here is the function to read the flash:
void flashRead (uint32_t readAddr, uint32_t *readBuf, uint16_t wordCnt){
flashUnlock();
while (wordCnt--){
*readBuf = *(__IO uint32_t *)(readAddr);
readAddr += 4;
readBuf++;
}
flashLock();
}
Unlock function taken from RM:
void flashUnlock(void){
/****UNLOCK PELOCK****/
/* (1) Wait till no operation is on going */
/* (2) Check if the PELOCK is unlocked */
/* (3) Perform unlock sequence */
while ((FLASH->SR & FLASH_SR_BSY) != 0){ /* (1) */
/* For robust implementation, add here time-out management */
}
if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0){ /* (2) */
FLASH->PEKEYR = FLASH_PEKEY1; /* (3) */
FLASH->PEKEYR = FLASH_PEKEY2;
}
/****UNLOCK PRGLOCK****/
/* (1) Wait till no operation is on going */
/* (2) Check that the PELOCK is unlocked */
/* (3) Check if the PRGLOCK is unlocked */
/* (4) Perform unlock sequence */
while ((FLASH->SR & FLASH_SR_BSY) != 0){ /* (1) */
/* For robust implementation, add here time-out management */
}
if ((FLASH->PECR & FLASH_PECR_PELOCK) == 0){ /* (2) */
if ((FLASH->PECR & FLASH_PECR_PRGLOCK) != 0){ /* (3) */
FLASH->PRGKEYR = FLASH_PRGKEY1; /* (4) */
FLASH->PRGKEYR = FLASH_PRGKEY2;
}
}
}
Lock function taken from RM:
void flashLock(void){
/* (1) Wait till no operation is on going */
/* (2) Locks the NVM by setting PELOCK in PECR */
while ((FLASH->SR & FLASH_SR_BSY) != 0){ /* (1) */
/* For robust implementation, add here time-out management */
}
FLASH->PECR |= FLASH_PECR_PELOCK; /* (2) */
}
Here is the data I saved in bank2 page512:

Here is the data stored in flash bank1 page0:
This is the data I am reading:
Read Buffer:
20000FC0 080000D9 0800119D 08000695
210C000B 792C3010 182CC326 FFFF2501
011B0000 00000000 001B0700 08001273
FFFFFFFF FFFFFFFF 0800119F 08001275
As you can see, underlined values not are only wrong but they belong to data in the page0 and I don't have any idea how the iteration can fetch these values out of the range of the address.
If this help, I am using multiple interrupts in my program..
Please any help would be greatly appreciated.
