# PIC32 Application using queue crashes

**URL:** <https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131>\
**Category:** Kernel\
**Created:** [November 1, 2012, 2:44pm UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131 "2012-11-01T14:44:41Z")\
**Posts on this page:** 5\
**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:** [November 1, 2012, 2:44pm UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131/1 "2012-11-01T14:44:41Z")

</div>

**[aethamyn](https://sourceforge.net/u/aethamyn/)** wrote on [Thursday, November 01, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/dfef08af/#d06e):

Hi everybody,

I am facing a problem with PIC32 port when I try to use queues. I got a simple application, only one task, one queue. There are correctly initialised (task and queue handles aren’t null), vTaskStartScheduler() is called, then my task tries to receive a message from the queue with xQueueReceive().

I get stuck in vListInsert(), but :

1. My stack task doesn’t overflow (I use method 2 for stack checking and vApplicationStackOverflowHook() is never called)

2. I don’t think I have trouble with interrupts priorities : Kernell priority is 1, max interrupt priority is set to 4. Plus, I don’t have used any other interrupts than those used for the kernell.

3. When xQueueReceived() is called, the scheduler is running (xSchedulerRunning = 1 and uxSchedulerSuspended = 0) but in vListInsert(), scheduler is suspended (is that ok ?). Actually it is suspended when calling vTaskSuspendAll in xQueueGenericReceive().

4. Queue has been initialised, no pending message.

Then, a bus exception is thrown (DBE). I have the same problem if I create a second task that put a message into the queue.

If it could help, I am using XC32 v1.11. I can reproduce the problem with MPLAB Sim and when I am debugging with a Digilent Cerebot MX7ck board with built-in licensed debugger.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 1, 2012, 2:59pm UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131/2 "2012-11-01T14:59:21Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, November 01, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/dfef08af/#0dc9):

As your application is so small, please post the code. Make sure to enclose it with the Codify Text tags (button just above the text entry box in the forum).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 2, 2012, 7:23am UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131/3 "2012-11-02T07:23:43Z")

</div>

**[aethamyn](https://sourceforge.net/u/aethamyn/)** wrote on [Friday, November 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/dfef08af/#a04d):

- Main():

```
int main(void) 
{
    vSetupHardware();
   Create_Ressources_OSTracing();
    vTaskStartScheduler();
    return 0;
}

```

- Create\_Ressources\_OSTracing():

```
ErrorCode_t Create_Ressources_OSTracing (void)
{
	ErrorCode_t ErrorCode = {0};
	#ifdef OS_TRACE
	// 1. Create Task
	if((ErrorCode.Code&ERRCODEMASK) == CODE_000_NO_ERROR)
	{
		if(xTaskCreate(_OS_Trace_TaskHandler,
					   "RTOS Trace Task",
					   STACKSIZE_OS_TRACE,
					   NULL,
					   PRIORITY_OS_TRACE,
					   &TASK_OSTracing) != pdPASS)
		{
			ErrorCode.Code = LAYER_Service | TASKID_OSTrace | CODE_002_MEMORY_ALLOCATION_ERROR;
		}
	}
	
	// 2. Create Message to Send Queue
	if((ErrorCode.Code&ERRCODEMASK) == CODE_000_NO_ERROR)
	{
		Q_OSTracing = xQueueCreate(MAX_MSG_Q_OS_TRACE, sizeof(OS_TRACE_Msg_t));
		if(Q_OSTracing == NULL)
		{
			ErrorCode.Code = LAYER_Service | TASKID_OSTrace | CODE_002_MEMORY_ALLOCATION_ERROR;
		}
		else
		{
			#if __DEBUG
			vQueueAddToRegistry(Q_OSTracing, "OS Tracing Queue");
			#endif
		}	
	}
	#endif
	return ErrorCode;
}

```

- The task - \_OS\_Trace\_TaskHandler():

```
void _OS_Trace_TaskHandler (void * pvTaskParam)
{
	OS_TRACE_Msg_t TraceMsg;
	OS_TRACE_Time_t TraceTime;
	
	vTaskDelayMs(2000);
	
	while(1)
	{
		if(xQueueReceive(&Q_OSTracing, &TraceMsg, portMAX_DELAY) == pdPASS)
		{
			if(TraceMsg.DateFlag)
			{
				_OS_Trace_GetTime(&TraceTime);
				_OS_Trace_FormatTime(&TraceTime, &TraceMsg);
			}
			
			_OS_Trace_LogTrace(&TraceMsg);
		}
	}
}

```

vTaskDelayMs() is a macro that calls vTaskDelay and converts parameter in ms to ticks with portTICK\_RATE\_MS.

---

<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:** [November 2, 2012, 10:01am UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131/4 "2012-11-02T10:01:12Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, November 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/dfef08af/#995b):

I’m curious why you test

```
if((ErrorCode.Code&ERRCODEMASK) == CODE_000_NO_ERROR)

```

when ErrorCode is a local variable and just been initialised so can’t hold any value other than 0, but that is probably not relevant.

What is probably relevant is the following call:

```
if(xQueueReceive(&Q_OSTracing, &TraceMsg, portMAX_DELAY) == pdPASS)

```

You don’t show the definition of Q\_OSTracing, but I suspect you don’t need the ‘&’ in front of it.

If you declare it as:

```
xQueueHandle Q_OSTracing;

```

…and it the queue has been created, then you can read from it using:

```
xQueueReceive(Q_OSTracing, &TraceMsg, portMAX_DELAY);

```

As you are using portMAX\_DELAY, you only need to test the return value if INCLUDE\_vTaskSuspend is not set to 1 in FreeRTOSConfig.h.&nbsp; That is because if INCLUDE\_vTaskSuspend is set to one portMAX\_DELAY will act as an indefinite wait.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 2, 2012, 12:25pm UTC](https://forums.freertos.org/t/pic32-application-using-queue-crashes/5131/5 "2012-11-02T12:25:22Z")

</div>

**[aethamyn](https://sourceforge.net/u/aethamyn/)** wrote on [Friday, November 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/dfef08af/#255e):

> when ErrorCode is a local variable and just been initialised so can’t hold any value other than 0, but that is probably not relevant.

Yes it is, actually it’s more a “way” that I try to respect, I agree this test isn’t usefull 😉

> What is probably relevant is the following call:&nbsp; You don’t show the definition of Q\_OSTracing, but I suspect you don’t need the ‘&’ in front of it. If you declare it as:  
> xQueueHandle Q\_OSTracing;

Exact ! I’m more familiar with EmbOS from Segger, in which queue type is a structure, not a handle, which is actually just a void pointer … I made the same mistake when I wanted to put a message into the queue :

```
if(xQueueIsQueueFullFromISR(Q_OSTracing) != pdFALSE)
	{
		xQueueSend(&Q_OSTracing, &TraceMsg, MS_TO_TICK(50));
	}

```

That’s why I got troubles in sending messages too.

> As you are using portMAX\_DELAY, you only need to test the return value if INCLUDE\_vTaskSuspend is not set to 1 in FreeRTOSConfig.h. That is because if INCLUDE\_vTaskSuspend is set to one portMAX\_DELAY will act as an indefinite wait.

Yes, my task is suspended. I will let the test because this task maybe be used with different configuration of FreeRTOS, it is some kind of generic method to trace OS and application events for a few different&nbsp; FreeRTOS based softwares.

Anyway, thank you for your help !
