2025-02-14 01:36 AM - edited 2025-02-17 03:08 AM
Hi,
I am working with the STM32H7S78-DK board and trying to implement Flash read/write functionality in my project (Touchgfx).
Below is my code for handling Flash read and write operations:
#define FLASH_USER_START_ADDR ((uint32_t)0x08000000)
typedef struct
{
uint32_t name;
uint32_t model;
uint32_t firmware_version;
uint32_t reserve;
} FH_FLASH_DATA_INFO;
static uint32_t GetFlashSector(uint32_t startAddress)
{
uint16_t address = startAddress - FLASH_BASE;
uint32_t sector = address/FLASH_SECTOR_SIZE; // Each Sector is 8 KB
return sector;
}
/**
* @brief Erase the flash sector at the given address.
*/
static uint32_t FH_FlashErase(uint32_t StartAddress)
{
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SECTORError;
printf("Unlocking Flash...\n");
HAL_FLASH_Unlock();
/* Calculate the sector number for the provided address (for STM32H7) */
uint32_t sector = GetFlashSector(StartAddress);
printf("GetFlashSector sector :%d\n",(int)sector);
/* Erase Flash */
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.Sector = sector;
EraseInitStruct.NbSectors = 1;
/* Disable DCache to ensure correct Flash programming */
SCB_CleanDCache();
/* Perform the erase */
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
printf("Flash Erase Failed! Error: %d\n", (int)HAL_FLASH_GetError());
HAL_FLASH_Lock();
return HAL_FLASH_GetError();
}
printf("Flash Erase Successful!\n");
HAL_FLASH_Lock();
return 0;
}
/**
* @brief Writes data to Flash memory.
*/
uint32_t FH_FlashWriteData(uint32_t StartAddress, FH_FLASH_DATA_INFO *data)
{
uint32_t *data_ptr = (uint32_t *)data;
uint16_t numberofwords = sizeof(FH_FLASH_DATA_INFO) / 4;
/* Erase flash before writing */
if (FH_FlashErase(StartAddress) != 0)
{
return HAL_ERROR; // Erase failed
}
printf("Unlocking Flash for writing...\n");
HAL_FLASH_Unlock();
printf("Writing Data to Flash...\n");
printf("numberofwords. : %d\n",numberofwords);
/* Write data to Flash */
for (uint16_t i = 0; i < numberofwords; i++)
{
printf("Writing Word %d: 0x%08X\n", i, (unsigned int)data_ptr[i]);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartAddress + (i * 4), data_ptr[i]) != HAL_OK)
{
printf("Flash Write Failed at word %d! Error: %d\n", i, (int)HAL_FLASH_GetError());
HAL_FLASH_Lock();
return HAL_FLASH_GetError();
}
}
printf("Flash Write Successful!\n");
/* Ensure cache is cleaned after writing */
SCB_CleanInvalidateDCache();
/* Lock the Flash after writing */
HAL_FLASH_Lock();
return 0;
}
/**
* @brief Reads data from Flash memory.
*/
void FH_FlashReadData(uint32_t StartAddress, FH_FLASH_DATA_INFO *data)
{
printf("Reading Data from Flash...\n");
uint32_t *data_ptr = (uint32_t *)data;
uint16_t numberofwords = sizeof(FH_FLASH_DATA_INFO) / 4;
for (uint16_t i = 0; i < numberofwords; i++)
{
data_ptr[i] = *(__IO uint32_t *)(StartAddress + (i * 4));
printf("Read Word %d: 0x%08X\n", i, (unsigned int)data_ptr[i]);
}
}
/**
* @brief Initializes and tests Flash read/write functionality.
*/
void FH_InitFlashDataHandler(void)
{
printf("Initializing Flash Data Handler...\n");
FH_FLASH_DATA_INFO flash_data_write =
{
.name = 1,
.model = 5,
.firmware_version = 0x01020304,
};
FH_FLASH_DATA_INFO flash_data_read = {0};
if (FH_FlashWriteData(FLASH_USER_START_ADDR, &flash_data_write) == 0)
{
FH_FlashReadData(FLASH_USER_START_ADDR, &flash_data_read);
printf("Name: %d\n", (int)flash_data_read.name);
printf("Model: %d\n", (int)flash_data_read.model);
printf("Firmware Version: 0x%08X\n", (unsigned int)flash_data_read.firmware_version);
}
else
{
printf("Flash Write Failed. Cannot proceed with reading.\n");
}
}
When I execute the flash write function, the program gets stuck after writing the data. (No error print during write operation) but The "Flash Write Successful!" message is never printed, and I am unable to verify the written data.
Note: My code was working perfectly before adding the Flash read/write functionality. However, after integrating it, the whole code is stuck.
Could you please help me debug this issue? Any guidance on potential misconfigurations, cache handling, or memory alignment would be appreciated.
Thanks,
Mehul