2019-09-11 11:51 AM
I'm trying to erase sectors 11,12 in the STM32F779II, I have been running into that problem for one week without a solution.
So Right now I'm trying to check if there is ANY write protection on sectors 11,12. But There is NO write Protection error.
However when I erase them, I get WriteProtectionError Flag!. That's very strange, I don't know what I'm really doing wrong here.
#define FLASH_STATUS_ALL_FLAGS \
(FLASH_SR_RDERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \
FLASH_SR_WRPERR | FLASH_SR_EOP | FLASH_SR_BSY)
secbool flash_unlock_write(void) {
HAL_FLASH_Unlock();
FLASH->SR |= FLASH_STATUS_ALL_FLAGS; // clear all status flags
return sectrue;
}
and for flash erasing and write protection detection:
#define FLASH_WRP_SECTORS (OB_WRP_DB_SECTOR_11 | OB_WRP_DB_SECTOR_12) /* sectors 6 and 7 */
secbool flash_erase_sectors(const uint8_t *sectors, int len,
void (*progress)(int pos, int len)) {
ensure(flash_unlock_write(), NULL);
// HAL_FLASH_OB_Unlock();
FLASH_OBProgramInitTypeDef OBInit;
__IO uint32_t SectorsWRPStatus = 0xFFF;
HAL_FLASHEx_OBGetConfig(&OBInit);
SectorsWRPStatus = OBInit.WRPSector & FLASH_WRP_SECTORS;
if (SectorsWRPStatus == 0) {
/* If FLASH_WRP_SECTORS are write protected, disable the write protection
*/
/* Allow Access to option bytes sector */
HAL_FLASH_OB_Unlock();
/* Allow Access to Flash control registers and user Flash */
HAL_FLASH_Unlock();
/* Disable FLASH_WRP_SECTORS write protection */
OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPState = OB_WRPSTATE_DISABLE;
OBInit.WRPSector = FLASH_WRP_SECTORS;
HAL_FLASHEx_OBProgram(&OBInit);
/* Start the Option Bytes programming process */
if (HAL_FLASH_OB_Launch() != HAL_OK) {
/* User can add here some code to deal with this error */
while (1) {
}
}
/* Prevent Access to option bytes sector */
HAL_FLASH_OB_Lock();
/* Disable the Flash option control register access (recommended to
protect the option Bytes against possible unwanted operations) */
HAL_FLASH_Lock();
/* Get FLASH_WRP_SECTORS write protection status */
HAL_FLASHEx_OBGetConfig(&OBInit);
SectorsWRPStatus = OBInit.WRPSector & FLASH_WRP_SECTORS;
/* Check if FLASH_WRP_SECTORS write protection is disabled */
if (SectorsWRPStatus == FLASH_WRP_SECTORS) {
printf("flash");
} else {
printf("flaa");
/* Set the LCD Text Color */
}
}
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
if (progress) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
EraseInitStruct.Sector = sectors[i];
uint32_t SectorError;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
ensure(flash_lock_write(), NULL);
return secfalse;
}
// check whether the sector was really deleted (contains only 0xFF)
const uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]],
addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
for (uint32_t addr = addr_start; addr < addr_end; addr += 4) {
if (*((const uint32_t *)addr) != 0xFFFFFFFF) {
ensure(flash_lock_write(), NULL);
return secfalse;
}
}
if (progress) {
progress(i + 1, len);
}
}
ensure(flash_lock_write(), NULL);
return sectrue;
}
2019-09-11 02:19 PM
>>I have been running into that problem for one week without a solution.
Indeed, and you don't seem to have instrumented the code, not dumped FLASH/OB settings outside a debugger. Basically instrument and output internal state until you can understand the flow/mechanics, or document the failure to your ST FAE
https://community.st.com/s/question/0D50X0000BJ0ORLSQ3/sectioning-stm32f779ii-flash-memory
If trezor's code isn't working, perhaps look at some ST's other examples, paying particular attention to banking, and sector numbering.
Unfortunately this isn't something I can bury unpaid hours into.
Instrument the code until you understand what's happening better than trezor did, or pay him to support you.
2019-09-12 05:34 AM
@Community member
I found the problem. I can erase and write ONCE. But I can't do that multiple times. How can I fix that ?
2019-09-12 08:38 AM
Hard to diagnose without seeing sequencing and flash/ob register reporting. Post some logs.
2019-09-12 10:31 AM
Can you tell me please which logs registers that you are interested in ? before and after erasing right ?
2019-09-12 11:19 AM
Basic debugging technique would be to output the half-dozen FLASH peripheral registers on entry/exit to working/not-working paths.
And expand that to the reads and decision making paths as it traverse the problem code. Time spent in wait loops.
Dumping the options bytes would also provide context.
If you can't see differences, keep expanding scope until you can. Add tests to highlight differences if they are disappearing into the noise.