cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with Running Application from Bootloader

Pushpalatha
Associate II

I seek your assistance with an issue I'm encountering while attempting to jump from my bootloader to an application stored at a specific address in the flash memory of an STM32H723ZGT6TR microcontroller.

 

 

I am able to successfully flash the application binary from a USB to the flash memory using bootloader code. However, I am unable to run the application using the following code snippet:

 

 
 
 uint32_t msp_value = *(__IO uint32_t *)0x08040000;
__set_MSP(msp_value);
uint32_t resethandler_address = *(__IO uint32_t *) (0x08040000 + 4);
app_reset_handler = (void*) resethandler_address;
app_reset_handler();
NVIC_SystemReset();
 
I would appreciate your review and any  suggestions or corrections you might have. Thank you for your time and assistance.
12 REPLIES 12

Yes, I understand. To avoid passing pointers to HAL_FLASH_Program, we need to ensure we are passing the actual data, correctly formatted for the function. Here is the revised code to reflect this:

 

void Flash_Write(uint32_t address, uint8_t* data, uint32_t size)
{
    SCB_DisableICache();
    static FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t SECTORError;

    HAL_FLASH_Unlock();
    uint32_t StartSector = GetSector(address);
    uint32_t EndSectorAddress = address + size;
    uint32_t EndSector = GetSector(EndSectorAddress);

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector = StartSector;

    // The proper BANK to erase the Sector
    if (address < 0x08100000)
        EraseInitStruct.Banks = FLASH_BANK_1;
    EraseInitStruct.NbSectors = (EndSector - StartSector) + 1;

    HAL_FLASH_Lock();

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
    {
        return HAL_FLASH_GetError();
    }

    uint32_t data_word = (uint32_t)data;
    HAL_FLASH_Unlock();

    for (uint32_t i = 0; i < size; i += 32)
    {
        printf("operating to HAL_FLASH_Program\n");
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address + i, data_word + i) != HAL_OK)
        {
            printf("Error writing to flash at address 0x%08X\n", address);
            HAL_FLASH_Lock();
            return HAL_FLASH_GetError();
        }
        else
        {
            printf("write to flash\n");
            printf("Wrote 0x%08X to flash at address 0x%08X\n", (uint32_t)data_word + i, address);
        }

        if ((address + i) % FLASH_SECTOR_SIZE == 0)
        {
            // Perform sector erase if needed
            // Adjust this part based on your specific requirements
            printf("Erasing sector at address 0x%08X\n", address + i);
            // FLASH_If_Erase(address + i);
        }
    }

    HAL_FLASH_Lock();
    SCB_EnableICache();
}
FIL       file;                                          /* File object */
FRESULT   res;                                           /* FatFs function common result code */
uint32_t  total_space, free_space;                       /* Total Space and Free Space */
DWORD     fre_clust;                                     /* Free Cluster */
uint32_t  byteswritten, bytesread;                       /* File write/read counts */
uint8_t   rd_data[256];                                  /* Read buffer */
char      file_name[] = "write.bin";                     /* File name */
uint8_t retvalue;

do {
    res = f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0);
    if (res != FR_OK) {
        break;
    }

    FATFS *fatFs = &USBDISKFatFs;
    f_getfree("", &fre_clust, &fatFs);
    total_space = (uint32_t)((USBDISKFatFs.n_fatent - 2) * USBDISKFatFs.csize * 0.5);
    free_space = (uint32_t)(fre_clust * USBDISKFatFs.csize * 0.5);
    printf("USB Device Total Space = %lu MB\n", total_space / 1024);
    printf("USB Device Free Space  = %lu MB\n", free_space / 1024);

    HAL_NVIC_SetPriority(FLASH_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(FLASH_IRQn);

    // Unlock the flash memory
    FLASH->KEYR1 = 0x45670123;
    FLASH->KEYR1 = 0xCDEF89AB;

    // Wait for the flash memory to be ready
    while (FLASH->SR1 & FLASH_SR_BSY);

    // Erase sector 6
    FLASH->CR1 |= FLASH_CR_SER; // Set the sector erase bit
    FLASH->CR1 |= (6 << FLASH_CR_SNB_Pos); // Select sector 6
    FLASH->CR1 |= FLASH_CR_START; // Start the erase operation

    // Wait for the erase operation to finish
    while (FLASH->SR1 & FLASH_SR_BSY);

    // Check for any errors
    if (FLASH->SR1 & (FLASH_SR_PGSERR | FLASH_SR_WRPERR)) {
        // Handle errors here
    }

    // Clear the end of operation flag
    FLASH->SR1 |= FLASH_SR_EOP;

    // Lock the flash memory
    FLASH->CR1 |= FLASH_CR_LOCK;

    res = f_open(&file, "write.bin", FA_READ);
    if (res != FR_OK) {
        printf("Error while opening file");
        break;
    }

    uint32_t flash_address = Flash_ADDRESS;
    uint32_t bytes_read;

    while (1) {
        res = f_read(&file, rd_data, sizeof(rd_data), &bytes_read);
        if (res != FR_OK || bytes_read == 0) {
            // Error reading or end of file
            break;
        }
        printf("Wrote %u to flash at address \n", rd_data);
        printf("rd_data = %s\n", rd_data);

        // Flash write function
        Flash_Write(flash_address, rd_data, bytes_read);

        // Update the flash address for the next write
        flash_address += bytes_read;
    }

    // Close the file after reading
    f_close(&file);

    // Unmount the device
    f_mount(NULL, USBDISKPath, 0);

    // Unlink the USB disk driver
    FATFS_UnLinkDriver(USBDISKPath);

} while (0);

 

Yes, I did try not passing a pointer to HAL_FLASH_Program, but the program is not working as expected with the above code. Additionally, I am not able to access the memory area properly with the current implementation.

I have attempted to use both the DataAddress parameter to specify the address of the data and the Data parameter to specify the actual data to be programmed, but unfortunately, neither approach has been successful.

Could you please provide me with the proper procedure or guidelines for effectively using the HAL_FLASH_Program function on the STM32H723?