2021-03-05 08:39 AM
Hi!
I'm working in a project that uses flash memory to store calibration data. At the first power on (before writing data in flash) everything works fine. After calibration, if I turn off the board and turn on again, my USART2 stops working (only send a few wrong bytes).
Any ideia what could be?
MCU: STM32F413VG
uint32_t flash_write (uint32_t StartSectorAddress, uint32_t * DATA_32, int numberofwords)
{
__disable_irq();
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SECTORError;
int sofar=0;
//int numberofwords = (strlen(DATA_32)/4) + ((strlen(DATA_32) % 4) != 0);
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
uint32_t StartSector = GetSector(StartSectorAddress);
uint32_t EndSectorAddress = StartSectorAddress + numberofwords*4;
uint32_t EndSector = GetSector(EndSectorAddress);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = StartSector;
EraseInitStruct.NbSectors = (EndSector - StartSector) + 1;
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
DCRST and ICRST bits in the FLASH_CR register. */
HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
return HAL_FLASH_GetError ();
}
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
while (sofar<numberofwords)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartSectorAddress, DATA_32[sofar]) == HAL_OK)
{
StartSectorAddress += 4; // use StartPageAddress += 2 for half word and 8 for double word
sofar++;
}
else
{
/* Error occurred while writing data in Flash memory*/
return HAL_FLASH_GetError ();
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
__enable_irq();
return 0;
}
2021-03-05 08:52 AM
Multiple exit paths where interrupts are disabled.
Try adding telemetry output (via polled UART access), and follow what's going on inside your code rather than running blind.