Hard Fault in FLASH_Program when source address not aligned

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Hard Fault in FLASH_Program when source address not aligned

596 Views
ppd
Contributor II

Hi,

I have the following code:

uint8_t pData[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,};

flash_config_t s_flashDriver;

memset(&s_flashDriver, 0, sizeof(flash_config_t));

FLASH_Init(&s_flashDriver);

FLASH_Program(&s_flashDriver, 0x55000, (uint32_t*)(pData+1), 4);‍‍‍‍‍‍‍‍‍

which causes a hard fault in function FLASH_Program() defined in the flash driver (fsl_flash.c) at this line:

kFCCOBx[1] = *src++;

 where src is the pointer to the data I want to write to the flash (namely pData+1).

If, however, I pass pData, or pData+4 to FLASH_Program() no hard fault is given and program behaves as expected.

So it seems that you must have a word aligned source address (not destination address, which I understand why it has to be word aligned!). Why is that? Or am I overlooking something?

Thanks!

Paul.

0 Kudos
2 Replies

457 Views
mjbcswitzerland
Specialist V

Paul

You haven't specified which processor this is on.
Cortex-M4 core can read unaligned long words from pointers but the Cortex-M0+ core can't and will hard fault.

The flash driver that you are using looks to work on Cortex-M4 without the caller needing to take notice of this but can fail on aCortex-M0+, meaning that it is not portable.

If you change the code to read the input pointer (in the function) as a byte pointer and copy 4 bytes to a local long word variable before writing it to the flash register you will make it (more) portable (and easier to use).

Regards

Mark


uTasker - for more performance and faster, cheaper product development

457 Views
ppd
Contributor II

Thanks Mark! This is on a Cortex-M0+, indeed. I'm using the flash driver that comes with the KW41 SDK. I'll make the suggested changes.

0 Kudos