# xQueueReceive() causes hard fault

**URL:** <https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949>\
**Category:** Kernel\
**Created:** [July 20, 2012, 3:04am UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949 "2012-07-20T03:04:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2012, 3:04am UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/1 "2012-07-20T03:04:21Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#4d90):

xQueueReceive() function is in once instance in my code causing a Hard Fault. The fault occurs when it gets to this function: vPortYieldFromISR()

I’m new to using RTOS’s. Please help.

MCU: Freescale Kinetis K10 (ARM M4 Cortex)

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2012, 8:16am UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/2 "2012-07-20T08:16:36Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#d157):

Have you followed all the guidelines on the following pages?

[http://www.freertos.org/RTOS-Cortex-M3-M4.html](http://www.freertos.org/RTOS-Cortex-M3-M4.html)  
[http://www.freertos.org/FAQHelp.html](http://www.freertos.org/FAQHelp.html)

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2012, 3:46pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/3 "2012-07-20T15:46:54Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#f465):

Richard,  
I did read these articles last night and had a few clarification questions:

1.) None of my MCU’s interrupts call RTOS functions. Therefore, I can set my MCU interrupt priorities to ANY priority (0 through 15), right?  
I have configured 10 interrupts in my system (unrelated to the RTOS). Below is also their priority number. I’m using a Periodic Interrupt Timer (PIT) as my MCU SysTick because I wasn’t sure if it was okay to share the standard SysTick with the RTOS. Do you see any concerns with this setup or have suggestions?

&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 8,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 6,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 5,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 7,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 7,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 0, /\* SysTick \*/  
&nbsp;&nbsp;&nbsp;&nbsp; = 10,  
&nbsp;&nbsp;&nbsp;&nbsp; = 10,  
&nbsp;&nbsp;&nbsp;&nbsp; = 10,  
&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; 1,

2.) Do I need to set the priority for the FreeRTOS interrupts (PendableSrvReq, SysTick, etc…), or are they setup by the ported code?

3.) Step 5 in the FAQ document says: “Do not use any method for disabling and enabling interrupts other than calls to portENTER\_CRITICAL() and portEXIT\_CRITICAL().”  
This caught me completely off guard. Is this referring to my MCU’s interrupts, or the RTOS specific functions? If it’s mine, then I think we’ve identified one major thing I’m doing wrong.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2012, 4:36pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/4 "2012-07-20T16:36:52Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#ff8e):

> Therefore, I can set my MCU interrupt priorities to ANY priority (0 through 15), right?

Yes.

> I’m using a Periodic Interrupt Timer (PIT) as my MCU SysTick because I wasn’t sure if it was okay to share the standard SysTick with the RTOS. Do you see any concerns with this setup or have suggestions?

Do you mean you are using the PIT to generate a tick interrupt for your application, and you have left the SysTick for the RTOS kernel?&nbsp; That will be fine.

> 2.) Do I need to set the priority for the FreeRTOS interrupts (PendableSrvReq, SysTick, etc…), or are they setup by the ported code?

The FreeRTOS interrupts are set by the FreeRTOSConfig.h parameter configKERNEL\_INTERRUPT\_PRIORITY, which should always be set to 255.

> Step 5 in the FAQ document says: “Do not use any method for disabling and enabling interrupts other than calls to portENTER\_CRITICAL() and portEXIT\_CRITICAL().”  
> This caught me completely off guard. Is this referring to my MCU’s interrupts, or the RTOS specific functions? If it’s mine, then I think we’ve identified one major thing I’m doing wrong.

It depends what you are doing.&nbsp; The kernel expects to have control of the BASEPRI value, which is used as an interrupt mask.&nbsp; The kernel never globally disables interrupt (for example, by using an cpsid instruction).&nbsp; If you code is setting and clearing the global interrupt flag, but leaving the BASEPRI alone, that should be ok.&nbsp; If you are changing the BASEPRI then that should also be ok provided you always reset it to its previous value and don’t yield (make a blocking API call) while you have it modified.&nbsp; The value you set it to (before resetting it) must not decrease its value (by decrease its value I mean its logical priority value) either.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2012, 4:43pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/5 "2012-07-20T16:43:18Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#c36d):

Richard,  
Thanks for the reply.

Sounds like everything there checks out. I’m at a loss?  
Any other ideas of things I can check?

vPortYieldFromISR() is only one line of code:  
\*(portNVIC\_INT\_CTRL) = portNVIC\_PENDSVSET;

When I get to the place in my code that causes this, I set my breakpoint on “\*(portNVIC\_INT\_CTRL) = portNVIC\_PENDSVSET;”  
The Hard Fault does not occur the first time, in here, but does consistently trigger the 2nd time through.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2012, 5:29pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/6 "2012-07-20T17:29:54Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#0b84):

Which interrupt is calling vPortYieldFromISR()?&nbsp; You said before the interrupts were not using the FreeRTOS code, although that line in itself should be safe as all it does is set a low priority interrupt pending, in fact the lowest, so running that line from an interrupt should not have \*any\* effect on the execution pattern until all executing and pending interrupts have completed.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2012, 7:53pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/7 "2012-07-20T19:53:53Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#ab34):

Richard,  
This isn’t being called from an interrupt. I don’t have any FreeRTOS functions called from my interrupts.

This is being called from xQueueReceive().

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2012, 8:05pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/8 "2012-07-20T20:05:05Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#ee75):

Is it feasible that you are running out of interrupt stack?&nbsp;

On the Cortex-M the stack used by main() is then re-used as the interrupt stack.&nbsp; The stack overflow checking only checks the task stacks, not the interrupt stack.

How big is the stack used by main()?&nbsp; How it is defined depends on your compiler.&nbsp; Can you fill the stack used by main with a known value at start up, then when the crash occurs, view it in the debugger to see if it is full or not?

Some systems will just define the stack at one end of the free RAM and the heap at the other, not really dimension either, and then just let them run into each other to chaotically let you know you ran out of either stack or heap.&nbsp; Which heap implementation are you using (heap\_1.c, heap\_2.c, heap\_3.c, or one you have done yourself)?

Regards.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2012, 9:23pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/9 "2012-07-20T21:23:47Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#8af7):

I think the next thing to try is working out which instruction was executing (or attempting to execute) when the hard fault occurs.&nbsp; When the hard fault occurs the stack frame of the fault handler includes the CPU registers as they were when the fault occurred, and the fist thing of interest in the program counter (PC).&nbsp; That tells you the instruction.&nbsp; The other registers will then give you the operand values, which can help track down things like null pointer use as a source of the fault.

To decode the stack frame, provide the following hard fault handler (the function name needs to be correct for the fault handler in your system, and you may need to write it all in assembler depending on how your system is currently set up).

```
static void HardFault_Handler(void)
{
	__asm volatile
	(
		" tst lr, #4 \n"
		" ite eq \n"
		" mrseq r0, msp \n"
		" mrsne r0, psp \n"
		" ldr r1, [r0, #24] \n"
		" ldr r2, handler2_address_const \n"
		" bx r2 \n"
		" handler2_address_const: .word pop_registers_from_fault_stack	\n"
	);
}

```

This code just sets up everything ready, then jumps to a function called pop\_registers\_from\_fault\_stack - which is defined below:

```
void pop_registers_from_fault_stack(unsigned int * hardfault_args)
{
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr;
	stacked_r0 = ((unsigned long) hardfault_args[0]);
	stacked_r1 = ((unsigned long) hardfault_args[1]);
	stacked_r2 = ((unsigned long) hardfault_args[2]);
	stacked_r3 = ((unsigned long) hardfault_args[3]);
	stacked_r12 = ((unsigned long) hardfault_args[4]);
	stacked_lr = ((unsigned long) hardfault_args[5]);
	stacked_pc = ((unsigned long) hardfault_args[6]);
	stacked_psr = ((unsigned long) hardfault_args[7]);
	/* Inspect stacked_pc to locate the offending instruction. */
	for( ;; );
}

```

Put a break point in the for(;😉 loop in this function, then when the break point is hit, look at the value of stacked\_pc to find the instruction that caused the fault.

Once you know the address of the instruction, open up the assembly view in your debugger, go to that address, and put a break point there.&nbsp; Next reset your system so it runs from the start again, and when the break point is hit, you should be able to see the line of C code that is executing.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2012, 11:15pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/10 "2012-07-20T23:15:20Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Friday, July 20, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#7903):

Richard,  
We’ve been beating our heads against the wall for days trying to track down the root of this nasty bug. This Hard Fault Handler code that you’ve provided is brilliant! It showed us exactly what was causing the issue.

A pointer in our code (not the FreeRTOS) is getting filled/overwritten with garbage. I’ve still yet to hunt that down, but that’ll be easy in comparison.

THANK YOU SOOOO MUCH!!!!  
Where I can I send the support check?&nbsp; 🙂

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 21, 2012, 1:06pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/11 "2012-07-21T13:06:27Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Saturday, July 21, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#2855):

I’m pleased you are making progress, and that I could help.

Donations are always welcome ;o)&nbsp; [http://www.freertos.org/donate.html](http://www.freertos.org/donate.html)

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 2, 2012, 4:50pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/12 "2012-09-02T16:50:52Z")

</div>

**[porcupineg](https://sourceforge.net/u/porcupineg/)** wrote on [Sunday, September 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#9ce6):

i have the same problem, it is very weird. Hard fault appears when i write a value to specific filed in&nbsp; a structure,then i have hard fault on xQueueRecieve, did you find couse of that?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 2, 2012, 5:58pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/13 "2012-09-02T17:58:31Z")

</div>

**[ignisuti](https://sourceforge.net/u/ignisuti/)** wrote on [Sunday, September 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#b0a2):

Richard shared some code with me that helped me understand which instruction caused the hard fault. I’m sorry I’m pressed for time right now, or I’d explain it better. I’m hoping you’ll be able to make sense of this code. In the end, I found it was an issue in my code (outside the RTOS).

```
/* ===============================================================================================
== FUNCTION DESCRIPTION:
==	Arm Core Hard Fault Interrupt (Address 0x0000000C).
==	Review ARM documentation to determine which registers are relevant.
==
== OUTPUTS:
== N/A
== =============================================================================================*/
static void INTERRUPT__handle_hard_fault
  (
  void
  )   
    {/* ARM Core - Hard Fault */
    
    asm
    	(
	tst lr, #4
	ite eq
	mrseq r0, msp
	mrsne r0, psp
	ldr r1, [r0, #24]
	ldr r2, =pop_registers_from_fault_stack
	bx r2
	//handler2_address_const: .word 
    	);
    
    } /* INTERRUPT__handle_hard_fault() */
void pop_registers_from_fault_stack(unsigned int * hardfault_args)
    {
    unsigned int stacked_r0 = ((unsigned long) hardfault_args[0]);
    unsigned int stacked_r1 = ((unsigned long) hardfault_args[1]);
    unsigned int stacked_r2 = ((unsigned long) hardfault_args[2]);
    unsigned int stacked_r3 = ((unsigned long) hardfault_args[3]);
    unsigned int stacked_r12 = ((unsigned long) hardfault_args[4]);
    unsigned int stacked_lr = ((unsigned long) hardfault_args[5]);
    unsigned int stacked_pc = ((unsigned long) hardfault_args[6]);/* stacked_pc should contain offending address. */
    unsigned int stacked_psr = ((unsigned long) hardfault_args[7]);               
    
    /* Display stacked_pc to user. */	
    DEBUG__print_error_message( "Hard Fault caused by instruction at address 0x%X", stacked_pc );
    snprintf( DEBUG__buffer, LCD_MULTILINE_LEN, "CORE FAULT:|0x%X", stacked_pc );              
    MENU__write( DEBUG__buffer, UI __LCD, LCD__ LINE __1_WITH_CLEAR, LCD__ ALIGN__CENTER, FALSE );
    
    EVENT_LOG__add( DIAG_CODE__WARNING, DIAG_CODE __WARNING__ HARD_FAULT );
    SHUTDOWN__reset_mcu();
    }
```

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 2, 2012, 6:14pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/14 "2012-09-02T18:14:36Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, September 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#bce3):

I since wrote this up at [http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html](http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 8, 2017, 4:10pm UTC](https://forums.freertos.org/t/xqueuereceive-causes-hard-fault/949/15 "2017-06-08T16:10:03Z")

</div>

**[jaysfk](https://sourceforge.net/u/jaysfk/)** wrote on [Thursday, June 08, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/2d9bfcad/#c774):

This is a very nice solution.

However, there is problem. If the endless loop at the end of pop\_registers\_from\_fault\_stack is removed. The code will return to the offending point that triggers HardFault Handler, resulting in re-entry into the hard fault handler again.

I solved this by calling system reset inside and at the end of pop\_registers\_from\_fault\_stack().

However, is it possible to force pop\_registers\_from\_fault\_stack to return to the hardfault handler. Because I wish to log the pc counter inside the hardfault handler.
