2024-09-16 05:10 AM - last edited on 2024-09-16 06:10 AM by SofLit
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, §orError);
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);
}
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 --
Solved! Go to Solution.
2024-09-16 07:43 AM
Hello @andy0105 ,
Better to refer to this example (FLASH_EraseProgram) provided in STM32CubeF7 under the path:
2024-09-16 07:43 AM
Hello @andy0105 ,
Better to refer to this example (FLASH_EraseProgram) provided in STM32CubeF7 under the path: