cancel
Showing results for 
Search instead for 
Did you mean: 

Writing and reading from Flash on STM32F446

peppea82
Associate II

I have a Nucleo-64 Dev Board where I'm developing and testing my automation solution.

I'm using the latest STM32CubeIDE and updated STM32 FW Package (1.24.1), FreeRTOS and FATFS as Middleware.

I'm trying to wrap HAL Flash Write function, aside a simple read one, to implement non-volatile storage for the MCU.

Relevant portions of code, in write function, are:

	HAL_StatusTypeDef 					halFlashStatus;
	uint32_t
		FirstSector = FLASH_FIRST_SECTOR,
		NbOfSectors = 1,
		flashAddress = FLASH_START_ADDRESS,
		SECTORError = 0;
 
	uint8_t tempData;
 
	FLASH_EraseInitTypeDef EraseInitStruct;
	EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
	EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;
	EraseInitStruct.Sector        = FirstSector;
	EraseInitStruct.NbSectors     = NbOfSectors;
 
	HAL_FLASH_Unlock();
 
	halFlashStatus = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
 
	for(int i = 0; i < FLASH_STORAGE_SIZE; i++)
	{
		tempData = handle->_backingStorage[i];
		halFlashStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, flashAddress, tempData);
		flashAddress++;
	}
 
	HAL_FLASH_Lock();

Where the handle variable is a simple uint8_t array sufficiently large to hold my data.

FLASH_FIRST_SECTOR is 7 and flash storage size is 32 bytes

Relevant code for the read function is:

	uint32_t *flashAddress = FLASH_START_ADDRESS;
	for(int i = 0; i < FLASH_STORAGE_SIZE; i++)
	{
		handle->_backingStorage[i] = *flashAddress;
		flashAddress++;
	}

While it seems to be working, by looking at flash through STM32CubeProgrammer at the relevant address, the MCU seems to crash in a way that is no more reachable by the STLink Programmer (DEV_NOT_HALTED). The only way to program again the MCU is to mass erase the flash by using the CubeProgrammer and flash again the application.

Flash is only written sporadically and write function is wrapped inside a FreeRTOS critical section (taskENTER/taskEXIT CRITICAL).

Thanks for your help.

Giuseppe

0 REPLIES 0