MBED HardFault with Nucleo-L073 HAL_FLASH_Program

Migrating from an NUCLEO-F091RC where I am using HAL_FLASH_Program to do EEPROM emulation as per ST’s application note, where the code works fine, to a NUCLEO-L073RZ where mbed-os gives a hard fault.

I’ve stripped it out to a very basic program:

#include “mbed.h”

// Blinking rate in microseconds
#define BLINK 2500000

int main()
{

printf("\n\nosblink1TEST\n");
printf(__TIMESTAMP__"\n");
printf("mbed-os version: %d.%d.%d\n",MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION);

DigitalOut led(LED1);
DigitalInOut led2(PB_12);
led2.mode(OpenDrain);    // Set to open drian
led2.output();            // Set to output
led2 = 0;  // Pull low

uint32_t targetAddr = 0x0802FF00;
uint32_t data = 0x12345678;

printf("Write 0x%08x to addr 0x%08x...\n", data, targetAddr);

HAL_FLASH_Unlock();

printf("flash unlocked\n");

volatile HAL_StatusTypeDef status;

printf("HAL_StatusTypeDef created\n");

status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, targetAddr, data);

printf("data written\n");

if(status == HAL_OK) {
    printf("HAL_OK");
} else {
    printf("ERROR");
}

HAL_FLASH_Lock();

printf("flash locked\n");

printf("starting loop\n");

while (true) {

    led = !led;
    led2.output();            // Set to output
    led2 = 0;             // Pull low
    printf("cycle1\n");
    wait_us(BLINK);
    led = !led;
    led2.input();    // Set to open drain
    printf("cycle2\n");
    wait_us(BLINK);
}

}

And the terminal output is

osblink1TEST
Wed Dec 23 11:14:22 2020
mbed-os version: 6.6.0
Write 0x12345678 to addr 0x0802ff00…
flash unlocked
HAL_StatusTypeDef created

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R 0: 00000000
R 1: 0000005B
R 2: 00000042
R 3: 00000175
R 4: 12345678
R 5: 0802FF00
R 6: 0000C350
R 7: 2000206C
R 8: 00000000
R 9: 00000000
R 10: 00000000
R 11: 00000000
R 12: 00000000
SP : 20001400
LR : 080019AF
PC : 08001778
xPSR : 61000000
PSP : 200013E0
MSP : 20004FC8
CPUID: 410CC601
Mode : Thread
Priv : Privileged
Stack: PSP

– MbedOS Fault Handler –

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x8001778
Error Value: 0x20001680
Current Thread: main Id: 0x20001A24 Entry: 0x80058B1 StackSize: 0x1000 StackMem: 0x20000460 SP: 0x20001400
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=NUCLEO_L073RZ
– MbedOS Error Info --``

So it is failing when HAL_FLASH_Program is called.

I’ve tested the following combinations:

L073, mbed 6.6.0 - result: hard fault
L073, mbed 5.15.6 - result: hard fault
L073, mbed 2.0.165 - result: works OK
F091, mbed 6.6.0 - result: works OK
F091, mbed 5.15.6 - result: works OK

Can anyone see a problem in the above code?

I know that the L073 has a dedicated ‘EEPROM’ area but that is besides the point really, I need to be able to call HAL_FLASH_Program

I have found that switching to the baremetal OS profile allows the code to run properly (on both mbed-os 6.6.0 and 5.15.6)