# HardFault when using Queues in Interrupt

**URL:** <https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881>\
**Category:** Kernel\
**Created:** [December 31, 2010, 4:08pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881 "2010-12-31T16:08:31Z")\
**Posts on this page:** 14\
**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:** [December 31, 2010, 4:08pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/1 "2010-12-31T16:08:31Z")

</div>

**[gizm0r](https://sourceforge.net/u/gizm0r/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#221d):

Hi,

I’m running the recent version of FreeRTOS on my Cortex M3 (LPC1768).

It runs fine, but if I want to send anything to a queue from an interrupt it crashed (jumps to HardFault).  
I tried “xSemaphoreGiveFromISR” and “xQueueSendToBackFromISR”, but both of them will crash at the memcpy command.

I used the functions the same way as in the samples, so I don’t see what’s wrong there? Is there anything I need to setup in the config for it to run?  
The usage of the queues from normal tasks works without any problems.

I hope anyone can help me with this problem.

Bye  
Gizmor

---

<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:** [December 31, 2010, 4:29pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/2 "2010-12-31T16:29:48Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#5bc7):

Have you taken into account point 3 on the following page?

> **[FreeRTOS - Open Source RTOS Kernel for small embedded systems](https://www.freertos.org/FAQHelp.html)**
>
> FreeRTOS is a portable, open source, mini Real Time kernel. A free RTOS for small embedded systems

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 31, 2010, 5:00pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/3 "2010-12-31T17:00:05Z")

</div>

**[gizm0r](https://sourceforge.net/u/gizm0r/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#49ff):

Thanks for your response!  
Yes, I already found this, but I’m not fully understandig how to use it 🙂

In FreeRTOSConfig.h there are the following settings (from the included lpc1768 demo):

```
#define configPRIO_BITS 5 /* 32 priority levels */
/* The lowest priority. */
#define configKERNEL_INTERRUPT_PRIORITY ( 31 << (8 - configPRIO_BITS) ) //248
/* Priority 5, or 160 as only the top three bits are implemented. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( 5 << (8 - configPRIO_BITS) ) //40

```

I tried to set different priorities to my Interrupt (UART2). But it doesn’t change anything if I set it to 0 (highest), to 31(lowest) or to anything between.

---

<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:** [December 31, 2010, 5:44pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/4 "2010-12-31T17:44:58Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#c693):

How are you setting the interrupt priority?&nbsp; Directly, or using a function.

Do you have the stack overflow checking switched on?

Can you post your entire ISR code?

Is the UART enabled?&nbsp; – does it work ok without the call to the queue function?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 31, 2010, 6:11pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/5 "2010-12-31T18:11:41Z")

</div>

**[gizm0r](https://sourceforge.net/u/gizm0r/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#afb7):

I use the CMSIS Macro =\> NVIC\_SetPriority(UART2\_IRQn, 10);

```
static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
{
  if(IRQn < 0) {
    SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M System Interrupts */
  else {
    NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for device specific Interrupts */
}

```

So, this should be correct.

The folloging code is the part of my interrupt function where a char is received. The received char is printed out (debug\_printf) correctly in the debug terminal (over jtag). So yes, without the Queue, everything works as expected.

```
/***UART2 Receive Data Available interrupt (RDA)***/
void UART2_ISR_ReceiveChar(void) {
	static portBASE_TYPE xHigherPriorityTaskWoken;
	unsigned char ReceivedChar = LPC_UART2->RBR; //Read received Char from register
	UART2_ReceiveBuffer[UART2_ReceiveBufferPosW] = ReceivedChar;
	
	if(UART2_ReceiveBufferPosW == UART2_BUFSIZE-1) {
		UART2_ReceiveBufferPosW = 0;
	} else {
		UART2_ReceiveBufferPosW++;
	}
	debug_printf("ISRRec: %c\n", ReceivedChar);
    xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreGiveFromISR( UART2_ReceiveCntQueue, &xHigherPriorityTaskWoken );
    if( xHigherPriorityTaskWoken == pdTRUE ) {
		vPortYieldFromISR();
	} 
}

```

configCHECK\_FOR\_STACK\_OVERFLOW is 0.

---

<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:** [December 31, 2010, 6:23pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/6 "2010-12-31T18:23:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#463a):

A couple of things that stick out:

1) Definitely don’t call debug\_printf() from an ISR!&nbsp; Does the queue work when that line is removed?

2) Set configCHECK\_FOR\_STACK\_OVERFLOW to 2, and define a stack overflow hook function.&nbsp; Just something like the following will be ok for a start.

```
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
	( void ) pxTask;
	( void ) pcTaskName;
	
	for( ;; );
}

```

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 31, 2010, 6:48pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/7 "2010-12-31T18:48:20Z")

</div>

**[gizm0r](https://sourceforge.net/u/gizm0r/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#611c):

\> 1) Definitely don’t call debug\_printf() from an ISR! Does the queue work when that line is removed?

No, doesn’t change anything. Even if I put only the xSemaphoreGiveFromISR in the ISR and remove everything else it doesn’t work.

\> 2) Set configCHECK\_FOR\_STACK\_OVERFLOW to 2, and define a stack overflow hook function. Just something like the following will be ok for a start.

Ok, put this in, but it keeps jumping to the hardfault. vApplicationStackOverflowHook isn’t executed.

---

<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:** [December 31, 2010, 7:00pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/8 "2010-12-31T19:00:45Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, December 31, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#57c6):

Running out of ideas here…

Is the queue valid?&nbsp; Was it created, and when it was created did you check that the xQueueCreate() return value was a valid handle (not null)?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 19, 2011, 2:28pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/9 "2011-01-19T14:28:06Z")

</div>

**[frbuisson](https://sourceforge.net/u/frbuisson/)** wrote on [Wednesday, January 19, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#1049):

Hi  
I have the same problem with a PIC32 target, and it occurs when I raise the item number of the Queue.  
When I have 4 Items, the general\_exception become after a little time  
when I have 8 items,&nbsp; the general\_exception become in 1 second.  
I test the configCHECK\_FOR\_STACK\_OVERFLOW to 2 and i don’t go in the vApplicationStackOverflowHook function.

this is my test code&nbsp; :

```
void vU3AInterruptHandler( void )
{
/* Declared static to minimise stack use. */
static portCHAR cByte;
static portBASE_TYPE Resultat;
static portBASE_TYPE xHigherPriorityTaskWoken;
static xComPortHandle pxPort=&UART3A_ComPort;
	xHigherPriorityTaskWoken = pdFALSE;
	Resultat = pdTRUE;
	/* Are any Rx interrupts pending? */
	if( INTGetFlag(INT_U3ARX) )
	{
		while(( UARTReceivedDataIsAvailable( pxPort -> eUARTId ))&&(Resultat == pdTRUE))
		{
			/* Retrieve the received character and place it in the queue of
			received characters. */
			cByte =UARTGetDataByte(pxPort -> eUARTId);
			Resultat = xQueueSendFromISR( pxPort ->xRxCharsBuffer , &cByte, &xHigherPriorityTaskWoken ); // Here the memcpy() in the prvCopyDataToQueue() function in queue.c make a general exception ( 0x00000007 = EXCEP_DBE = bus error (load/store))
		}
		INTClearFlag( INT_U3ARX );
	}
	/* Are any Tx interrupts pending? */
	if( INTGetFlag(INT_U3ATX) )
	{
		while( UARTTransmitterIsReady(pxPort -> eUARTId))
		{
			if( xQueueReceiveFromISR( pxPort -> xTxCharsBuffer, &cByte, &xHigherPriorityTaskWoken ) == pdTRUE )
			{
				/* Send the next character queued for Tx. */
				UARTSendDataByte( pxPort -> eUARTId, cByte );
			}
			else
			{
				/* Queue empty, nothing to send. */
				pxPort -> xTxHasEnded = pdTRUE;
				break;
			}
		}
		INTClearFlag(INT_U3ATX);
		if (pxPort -> xTxHasEnded == pdTRUE) // si c'est fini
			INTEnable(INT_U3ATX, INT_DISABLED);
	}
	/* If sending or receiving necessitates a context switch, then switch now. */
//	portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}

```

Gizm0r, do you find a solution?  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 21, 2011, 5:34pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/10 "2011-01-21T17:34:15Z")

</div>

**[cyanicx](https://sourceforge.net/u/cyanicx/)** wrote on [Friday, January 21, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#b0e1):

hi,

i got exactley the same problem…

the core registers of the 1768 says&nbsp; fault&nbsp;&nbsp;&nbsp; CFSR&nbsp; 0x400&nbsp; and that leads to INVPC&nbsp;&nbsp; (invalid program counter)

I think we are might not allowed to jump back into the isr ?

further hints i found :

when using the nvic set priority function ?&nbsp; whats the matter with negative values ?

```
**
 * @brief Set the priority for an interrupt
 *
 * @param IRQn The number of the interrupt for set priority
 * @param priority The priority to set
 *
 * Set the priority for the specified interrupt. The interrupt
 * number can be positive to specify an external (device specific)
 * interrupt, or negative to specify an internal (core) interrupt.
 *
 * Note: The priority cannot be set for every core interrupt.
 */
static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
{
  if(IRQn < 0) {
    SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */
  else {
    NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for device specific Interrupts */
}

```

and another question :&nbsp; in&nbsp;&nbsp; portmacro.h&nbsp; of freertos&nbsp; I found

#define portSET\_INTERRUPT\_MASK\_FROM\_ISR() \*\*0;\*\*portSET\_INTERRUPT\_MASK()&nbsp; // test  
#define portCLEAR\_INTERRUPT\_MASK\_FROM\_ISR(x) portCLEAR\_INTERRUPT\_MASK();(void)x

whats the matter with the 0;&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:** [January 21, 2011, 6:21pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/11 "2011-01-21T18:21:25Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 21, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#1dd0):

> when using the nvic set priority function ?&nbsp; whats the matter with negative values ?

This is ARM provided code, nothing to do with FreeRTOS, but the answer to your question is “nothing is the matter with negative values”.&nbsp; Read the Cortex-M technical reference manual and the CMSIS manual.

> and another question :&nbsp; in&nbsp;&nbsp; portmacro.h&nbsp; of freertos&nbsp; I found

#define portSET\_INTERRUPT\_MASK\_FROM\_ISR() 0;portSET\_INTERRUPT\_MASK()&nbsp; // test  
#define portCLEAR\_INTERRUPT\_MASK\_FROM\_ISR(x) portCLEAR\_INTERRUPT\_MASK();(void)x

whats the matter with the 0;&nbsp; ?

Again, the answer is “nothing is the matter with 0”.&nbsp; If you try removing it you will find your code won’t compile.&nbsp; It is there to tailor a generic macro to a specific chip in the most efficient manner possible while maintaining common kernel core code across all architectures.

These crashes while using queue functions in interrupts in Cortex-M3 ports are nearly always due to the mistakes made configuring the interrupt controller, due in part to the strange nature of setting interrupt priorities (priority 0 being the highest, not the lowest, and the fact that the most significant bits are used not the least).&nbsp; it is understandable why people get it wrong.&nbsp; People often say “I have read your FAQ, followed it and it still crashes”, but when you drill deeper you find that is not the case.&nbsp; Case in point: [https://sourceforge.net/projects/freertos/forums/forum/382005/topic/4059693](https://sourceforge.net/projects/freertos/forums/forum/382005/topic/4059693) .

I would go as far as to say, I have never come across a case where the issue is caused by either just a standard everyday bug in the application C code, or a misconfiguration of the interrupt controller and/or the kernel configuration constants that relate to the interrupt controller.

I’m not saying there are no issues in the kernel code, nothing can ever give that guarantee, but I would say I am yet to have anybody provide evidence of a problem.

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:** [January 21, 2011, 6:23pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/12 "2011-01-21T18:23:39Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 21, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#b73c):

> I have never come across a case where the issue is caused

…should say “I have never come across a case where the issue is **not** caused”

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 21, 2011, 9:24pm UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/13 "2011-01-21T21:24:10Z")

</div>

**[frbuisson](https://sourceforge.net/u/frbuisson/)** wrote on [Friday, January 21, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#7a9f):

I resolve the problem by raising the stack size of my all my tasks (I give them 600 bytes).  
Now, when I use the vTaskList() function, many tasks use about 500-550 bytes for their own stack.  
It seems the vApplicationStackOverflowHook function didn’t work for me with configCHECK\_FOR\_STACK\_OVERFLOW equal to 1 or 2.  
I’m not sure the stack size is the only reason because I’ve done many corrections of my code.  
Regards  
I hope my answer will be useful.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 22, 2011, 12:21am UTC](https://forums.freertos.org/t/hardfault-when-using-queues-in-interrupt/2881/14 "2011-01-22T00:21:19Z")

</div>

**[gizm0r](https://sourceforge.net/u/gizm0r/)** wrote on [Saturday, January 22, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/81361f93/#190d):

> Gizm0r, do you find a solution? Thanks

For me the problem was an not correctly initialized queue variable.  
Now everything works for me.
