vPortMalloc cause HardFault

pjoter wrote on Friday, November 23, 2018:

I have problem with HardFault in my app.
Demos will execute on my hardware.

Descritpion of problem:
I have Task1 which wait for queue from antoher Task2 (1Byte length). (On SPI Rx, My input, Slave output).
In Task1, I use pvPortMalloc to allocate memory to save incoming packet (Always 30 bytes).
During execution program Task1 runs correctly, but after a few times of execution I get HardFault.
Follow the instruction from : https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html
I check pc counter, and in disassembly, i find that HardFault occured in

rtel wrote on Friday, November 23, 2018:

Are you 100% sure the priority of your SPI interrupt is at or below
configMAX_SYSCALL_INTERRUPT_PRIORITY? Often people are sure, then find
out its not, because it is actually complex on the Cortex-M to
understand how priority bits get shifted around. My recommendation be
to update to the very latest release of FreeRTOS 10.1.x, then ensure
configASSERT() is defined - as that will trap nearly all, if not all,
interrupt priority configuration issues. I think the V8.x code will
trap some, but not all.

pjoter wrote on Thursday, December 27, 2018:

Thanks for reply, the problem was fixed by invert memory allocation in time, but i dont find what cause a problem. It seems that @Richar Barry have rigtht. Now i find that pvPortMalloc allocate memory at adress 0x10(RAM started at 0x20000000, 0x10 is boot memory) . I m surely dont having memory leak, configUSE_MALLOC_FAILED_HOOK is set to 1, vApplicationMallocFailedHook is never called by the FreeRTOS. It seems system think that memory allocation works correctly.

richarddamon wrote on Thursday, December 27, 2018:

malloc giving bad memory addresses tends to come from one of two sources, one is having improper frees, either freeing a block twice, with a wrong address, or memory that wasn’t allocated with malloc; or if some piece of code writes to memory outside an allocation block.