cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with Flash Read/Write on STM32H7S78-DK Board

Mehulrana511
Associate III

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");
    }
}

 

 

Issue Faced

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.

Mehulrana511_0-1739790516982.png

 

Could you please help me debug this issue? Any guidance on potential misconfigurations, cache handling, or memory alignment would be appreciated.

Thanks,
Mehul

3 REPLIES 3
Hl_st
ST Employee

Hello,

it seems your code is good, but you program your user data from address 0x08000000. This is the beginning of flash memory and there is programmed firmware. You should write user data at the end of flash. Addresses of last pages depends on your micro and it should be seen in reference manual. I think the last page of the first bank of your device is 0x080E 0000 - 0x080F FFFF, so you can test your code with this address.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks for all your help!

Using the start address 0x0800E000 fixed my issue, and the code is no longer getting stuck.

However, I noticed that the second word is always being read incorrectly. Below is the attached screenshot of my log.

Mehulrana511_0-1740028188290.png


I tried with different values but there is always unnecessary  4 come in value.


/Mehul

While trying to write different values i found that writing 

    FH_FLASH_DATA_INFO flash_data_write =
    {
        .name = 0x01000001,
        .model = 0x01000001,
        .firmware_version = 0x01000001,
    };

above values cause my code stuck. 
here is screen shot of log,

Mehulrana511_0-1740046519925.png

Again if i tried with this value it is running but still comes 4 in value,

    FH_FLASH_DATA_INFO flash_data_write =
    {
        .name = 1,
        .model = 5,
        .firmware_version = 0x01000001,
    };

Mehulrana511_1-1740046804250.png

 

/Mehul