Strange hard fault

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

Strange hard fault

479 Views
gauravbanyal
Contributor IV

In the code below, I am trying to read the flash memory and store it in a buffer.

When I read and assign to a buffer, I get a hard fault. When I only read and print it to UART, there is no problem and it prints all the addresses with the data I expect to be there.

 

Can someone please help me? (refer to the in-line comments to understand the problematic statements).

If there is no direct hint, could you please give me a HardFault_Handler implementation which will help me get more information of this problem?

 

======snip start=======

void read_session_from_flash()
{
uint32_t read_buffer[(destAdrss - zeroAdrss)/4];
uint32_t *flash_ptr;

PRINTF("\r\nNUMBER OF TIMESTAMPS IN FLASH = %d\r\n",(destAdrss - zeroAdrss)/4);

flash_ptr = (uint32_t*)zeroAdrss;
for (uint8_t i = 0; i < (destAdrss - zeroAdrss)/4; i++)
{
//read_buffer[i] = *(flash_ptr++); GBA: Something crazy here!! Enable this code and it leads to a hard fault
//PRINTF("FLASH_READ[%d] = 0x%x\r\n", i, read_buffer[i]); // should be enabled when the line above is enabled
PRINTF("FLASH_READ[%d] = 0x%x\r\n", i, *(flash_ptr++)); //This works like a charm!!!
}
}

======snip end=======

Labels (1)
Tags (2)
0 Kudos
1 Reply

337 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding the pointer uint32_t *flash_ptr; if you assign an immediate value to it, you have to guarantee that the immediate value ranges in RAM space. If the immediate value is not in RAM space, a hardware fault will be triggered.

Secondly, I suggest you define a global variables for the two variables:

uint32_t read_buffer[(destAdrss - zeroAdrss)/4];
uint32_t *flash_ptr;

As you know that if you define an array as local variables(the array is defined inside of a function), they are saved in stack, if the array size exceeds the stack size, there is error.

How about using the code:

uint32_t read_buffer[(destAdrss - zeroAdrss)/4];

uint32_t write_buffer[(destAdrss - zeroAdrss)/4];
uint32_t *flash_ptr;

flash_ptr=write_buffer;

....................

Hope it can help you

BR

Xiangjun Rong

0 Kudos