# PIC32 UART Driver Accessing From Queue Task 

**URL:** <https://forums.freertos.org/t/pic32-uart-driver-accessing-from-queue-task/5406>\
**Category:** Kernel\
**Created:** [November 5, 2012, 3:10am UTC](https://forums.freertos.org/t/pic32-uart-driver-accessing-from-queue-task/5406 "2012-11-05T03:10:40Z")\
**Posts on this page:** 3\
**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 5, 2012, 3:10am UTC](https://forums.freertos.org/t/pic32-uart-driver-accessing-from-queue-task/5406/1 "2012-11-05T03:10:40Z")

</div>

**[bull98](https://sourceforge.net/u/bull98/)** wrote on [Monday, November 05, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/efd77614/#5eb9):

Hello, I have created a project that was started from the PIC32 UART driver demo.&nbsp; I started out with a simple task that ran periodically and successfully wrote to the UART.&nbsp; I then modified the task so that it uses a queue to get a message and then access the UART.&nbsp; Now it gets thrown into the general exception handler any time it calls any of the functions in the UART driver.

I have tried re-initializing the UART after the queue receive.&nbsp; I am really struggling to understand why I can talk to the UART before the block and wait for queue, but not after.&nbsp; Can anyone please shed some light on what may be going wrong here?&nbsp;

Thank you in advance, I am about 18 hours in to debugging this with no progress 🙂

Setup:  
Processor: PIC32MX795F512L  
Dev Board: PIC32 USB Starter Kit II  
Compiler: C32  
FreeRTOS version: 7.2

Here is the code:

```
#define UART_STACK_SIZE configMINIMAL_STACK_SIZE
#define YIELDING_TIME_UART ( 10 / portTICK_RATE_MS ) //5ms
#define SIZE_OF_BUFFER_IN 63
#define SIZE_OF_BUFFER_OUT (SIZE_OF_BUFFER_IN *50)+1
// ----------------------------------------------
// FUNCTION DECLARATIONS
// ----------------------------------------------
static portTASK_FUNCTION_PROTO( vSerialTestTask, pvParameters );
// ----------------------------------------------
// GLOBAL VARIABLES 
// ----------------------------------------------
xTaskHandle* handle;
static char buffer_in[SIZE_OF_BUFFER_IN];
static char buffer_out[SIZE_OF_BUFFER_OUT];
static char second_buffer_out[SIZE_OF_BUFFER_OUT];
static int position = 0;
static int cr_received;
xQueueHandle xQueueHandleGSMSend2;
LogMessage* lReceivedData2;
// ----------------------------------------------
// FUNCTION DEFINITIONS
// ----------------------------------------------
void StartSerialTestTask( unsigned portBASE_TYPE priority ){
    //initializations
    cr_received = FALSE;
    //UARTInit( 2, 9600 );
    UARTInit( 1, 115200 );
    xQueueHandleGSMSend2 = xQueueCreate(5, sizeof(LogMessage));
    //creates task
    xTaskCreate( vSerialTestTask, ( signed char * ) "Serial test", UART_STACK_SIZE, NULL, priority, ( xTaskHandle * ) handle );
}
static portTASK_FUNCTION( vSerialTestTask, pvParameters ){
    portBASE_TYPE xStatus;
    int retVal;
    int i;
    //cleans buffers
    for(i=0; i<SIZE_OF_BUFFER_OUT; i++){
        buffer_out[i] = '\0';
        second_buffer_out[i] = '\0';
    }
    
    while(TRUE){
       
         //clears input buffer
         for( i=0; i<SIZE_OF_BUFFER_IN; i++)
             buffer_in[i] = '\0';
        //THIS WORKS
        UART1Send("ATE=0\r");
        xStatus = xQueueReceive(xQueueHandleGSMSend2, &lReceivedData2, portMAX_DELAY);
        if(xStatus == pdPASS)
        {
            //THIS BREAKS
            UART1Send("ATE=0\r");
            
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 5, 2012, 9:42am UTC](https://forums.freertos.org/t/pic32-uart-driver-accessing-from-queue-task/5406/2 "2012-11-05T09:42:51Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, November 05, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/efd77614/#f159):

You create a queue that holds five messages of sizeof(LogMessage) bytes. I don’t know how many bytes that is, but will assume it is more than 4 -

```
xQueueHandleGSMSend2 = xQueueCreate(5, sizeof(LogMessage));

```

You create a variable that is a pointer to a LogMessage. A pointer on a PIC32 is 4 bytes -

```
LogMessage* lReceivedData2;

```

You then copy sizeof(LogMessage) bytes into the variable that is 4 bytes -

```
xStatus = xQueueReceive(xQueueHandleGSMSend2, &lReceivedData2, portMAX_DELAY);

```

If sizeof(LogMessage) bytes is more than four then more than four bytes will be copied into the pointer, and you will corrupt your stack. Even if sizeof(LogMessage) is only 4 bytes, you will not end up with a pointer to a log message. I suspect you want to do something like this -

Create a variable that is a LogMessage (not a pointer to one) -

```
LogMessage lReceivedData2;

```

Copy a log message from the queue into the variable that is of type LogMessage -

```
xStatus = xQueueReceive(xQueueHandleGSMSend2, &lReceivedData2, portMAX_DELAY);

```

Not related to your problem, but this code -

```
    //cleans buffers
    for(i=0; i<SIZE_OF_BUFFER_OUT; i++){
        buffer_out = '\0';
        second_buffer_out = '\0';
    }

```

does not appear to do anything except repeatedly set the same two variables to \0.

same comment for this code -

```
      //clears input buffer
         for( i=0; i<SIZE_OF_BUFFER_IN; i++)
             buffer_in = '\0';
```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 5, 2012, 5:33pm UTC](https://forums.freertos.org/t/pic32-uart-driver-accessing-from-queue-task/5406/3 "2012-11-05T17:33:11Z")

</div>

**[bull98](https://sourceforge.net/u/bull98/)** wrote on [Monday, November 05, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/efd77614/#bd46):

Thanks for the help, this fixed it!&nbsp; I have been writing only c# for the last 10 years, I guess my pointer and memory management skills are pretty rusty…. Thanks again for the help!
