cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to flash memory on stm32f746 discovery

andy0105
Associate

Hello all 

I'm trying to write to the flash memory on an STM32F746 discovery board. 

This is the code so far, just a test to write a single word..

 

#define FLASH_USER_START_ADDR 0x080c0000
int Flash_Write(void)
{
    HAL_StatusTypeDef status;
    uint32_t address = FLASH_USER_START_ADDR; // Start address for writing

    // 1. Unlock the Flash memory
    HAL_FLASH_Unlock();
    printf("memory unlocked\n");

     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR ); //| FLASH_FLAG_PGSERR );
     FLASH_Erase_Sector(FLASH_SECTOR_7, VOLTAGE_RANGE_3);

    // 2. Erase the required flash sector
    FLASH_EraseInitTypeDef eraseInitStruct;
    uint32_t sectorError = 0;

    eraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
    eraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;  // Voltage range 2.7 to 3.6V
    eraseInitStruct.Sector = 7;                             // Change sector as per the location
    eraseInitStruct.NbSectors = 1;                         // Erasing 1 sector

    printf("erasing .\n");

    status = HAL_FLASHEx_Erase(&eraseInitStruct, &sectorError);

    if (status != HAL_OK)
    {
        // Error occurred while erasing
        HAL_FLASH_Lock();
        return(-1);
    }


    status = HAL_FLASH_Program((uint32_t)  FLASH_TYPEPROGRAM_WORD, 0x080c0000, (uint32_t) 0x00000123);


    return(0);
}
 
The erasing part works. If I add a memory lock and return after the memory erase the code runs ok and I can use the cube programmer to write a few numbers at 080c0000 and then see it has been erased
 
Adding the line to write causes a crash with this output.... 
 

memory unlocked
erasing .

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R 0: 080C0000
R 1: 00000123
R 2: 40023C10
R 3: 00000201
R 4: 00000123
R 5: 080C0000
R 6: 00000000
R 7: 00000002
R 8: 200064A4
R 9: 00000000
R 10: 00000000
R 11: 00000000
R 12: 200064C0
SP : 20005550
LR : 080037E3
PC : 080024A8
xPSR : 81000000
PSP : 200054E8
MSP : 2004FFD0
CPUID: 410FC271
HFSR : 40000000
MMFSR: 00000082
BFSR : 00000000
UFSR : 00000000
DFSR : 0000000B
AFSR : 00000000
MMFAR: 080C0000
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

 

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x80024A8
Error Value: 0x20005A34
Current Thread: main Id: 0x20005DD8 Entry: 0x800B009 StackSize: 0x1000 StackMem: 0x200045A0 SP: 0x20005550
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=DISCO_F746NG
-- MbedOS Error Info --

 
 
 You can see the printf() messages memory unlocked and erasing, then the crash ..

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @andy0105 ,

Better to refer to this example (FLASH_EraseProgram) provided in STM32CubeF7 under the path: 

https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32746G-Discovery/Examples/FLASH/FLASH_EraseProgram

 

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.

View solution in original post

1 REPLY 1
SofLit
ST Employee

Hello @andy0105 ,

Better to refer to this example (FLASH_EraseProgram) provided in STM32CubeF7 under the path: 

https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32746G-Discovery/Examples/FLASH/FLASH_EraseProgram

 

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.