Arm Mbed OS support forum

Flash memory throws a hard fault error

Hello,
I’m having trouble using the HAL_FLASH_Program, every time it throws a hard fault error. Even when I’ve checked the memory I’m accessing and the page size.
The code is
#include <mbed.h>
#include “stm32l4xx_hal.h”

#define page249addr 0x807C800

void save(uint32_t data, uint32_t address)

{
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
HAL_StatusTypeDef statusprogram;
statusprogram = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,address,data);
uint32_t error = HAL_FLASH_GetError();
if (statusprogram != HAL_OK){
printf(“Error %lu\n”,error);
}
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}

int main() {
// put your setup code here, to run once:
HAL_Init();
save(30.8,page249addr);

while(1) {
// put your main code here, to run repeatedly:
}
}

It would be great if someone can point out what I’m doing wrong?
Thank you in advance

Hello,
please add ``` before and after your inserted code snippets, so that the code stays formatted and is more readable.

Regarding your problem:
Did you restrict the flash size for mbed OS by adding something like "target.restrict_size": "0x807C800" to the mbed_app.json file?

For easier usage of the MCU flash you can also use the Flash IAP interface.