# Using Queue in several c files.

**URL:** <https://forums.freertos.org/t/using-queue-in-several-c-files/4478>\
**Category:** Kernel\
**Created:** [February 24, 2012, 3:11pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478 "2012-02-24T15:11:23Z")\
**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:** [February 24, 2012, 3:11pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/1 "2012-02-24T15:11:23Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#8809):

Hallo everyone!

i want to use a queue in two different c files and in one of it is ISR and from it i am sending an queue item.  
but the task in the another c file cannot receive the item in this queue.

did anybody know what can be the problem?

i hope someone can help me in answering this.

Regards,  
Mohammad

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 24, 2012, 3:33pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/2 "2012-02-24T15:33:32Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#989a):

for more details:

i am working on MSP430f5438 with CC.

in the hal\_usb.c&nbsp; i added this code to the ISR and the ISR is now:

#pragma vector=USCI\_A1\_VECTOR  
\_\_interrupt void USCI\_A1\_ISR (void)  
{  
char cChar= NULL;  
portBASE\_TYPE xHigherPriorityTaskWoken = pdFALSE;  
cChar = UCA1RXBUF;  
xQueueSendFromISR( xPROTQueue, &cChar, &xHigherPriorityTaskWoken );  
\_\_bic\_SR\_register\_on\_exit(LPM3\_bits);  
}

the xQueueSendFromISR stops and dont send the queue.  
with the debugging tool i find out that the code stops at the following part in queue.c:

signed portBASE\_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void \* const pvItemToQueue, signed portBASE\_TYPE \*pxHigherPriorityTaskWoken, portBASE\_TYPE xCopyPosition )  
{  
signed portBASE\_TYPE xReturn;  
unsigned portBASE\_TYPE uxSavedInterruptStatus;

configASSERT( pxQueue );

i hope that it is now more clear and some one can explain what the is problem.

---

<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:** [February 24, 2012, 4:03pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/3 "2012-02-24T16:03:58Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#42f5):

If the assert is failing then pxQueue must be NULL.&nbsp; Are you creating the queue before the interrupt tries to use it?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 24, 2012, 4:39pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/4 "2012-02-24T16:39:47Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#2234):

Hi richard,

yes, i am creating it bevor the interrupt tries to use it.  
the creation as i said is done in the other c file. in which i placed the receiving task too.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [February 24, 2012, 5:23pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/5 "2012-02-24T17:23:25Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#25f9):

Since the code is in two files, make sure the queue handle is in a global and both files are accessing the same exact variable. if a file declares that handle as static, or inside of a function, then the other file will use a different version of the variable even if they have the same name.

there should probably be an extern declaration in a header they both include, and a global definition in just one file.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 24, 2012, 7:36pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/6 "2012-02-24T19:36:55Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Friday, February 24, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#d017):

thank you a lot for your reply.

i declare the variable as extern in the hal\_usb.c and i write the defintion in the other c file.  
now it works for the first sending and receiving and then it stops for abother problem due to micro controller i think.

thanks again.

Regards.  
Mohammad

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 25, 2012, 5:46pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/7 "2012-02-25T17:46:29Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Saturday, February 25, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#6587):

Hallo again,

i have now a new problem:

after sending the first queue item, the application stops in the next sending at this line of code in tasks.c :

if( listCURRENT\_LIST\_LENGTH( &( pxReadyTasksLists ) ) \> ( unsigned portBASE\_TYPE ) 1 )

did any one have an ideaa, why this is happening?

thanx again a lot for the previos answers.

Regards,  
Mohammad

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 27, 2012, 12:47pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/8 "2012-02-27T12:47:05Z")

</div>

**[paulgcoleman](https://sourceforge.net/u/paulgcoleman/)** wrote on [Monday, February 27, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#6784):

Is your problem caused by the fact that you are not returning xHigherPriorityTaskWoken from your ISR? I had a similar problem where a task wasn’t responding to a queue and it was because I didn’t understand how that mechanism works and therefore the task was not getting unblocked. My ISRs all have the following format…

\_\_attribute\_\_((\_\_noinline\_\_))  
portBASE\_TYPE IntHandlerNonNaked(void)  
{  
portBASE\_TYPE xHigherPriorityTaskWoken = pdFALSE;

portENTER\_CRITICAL();  
xQueueSendFromISR(…, &xHigherPriorityTaskWoken);  
portEXIT\_CRITICAL();

return xHigherPriorityTaskWoken;&nbsp;&nbsp;&nbsp; // this does actually get used!!!!  
}

\_\_attribute\_\_((\_\_naked\_\_))  
void IntHandler(void)  
{  
portENTER\_SWITCHING\_ISR();  
IntHandlerNonNaked();  
portEXIT\_SWITCHING\_ISR();  
}

It looks as though return xHigherPriorityTaskWoken doesn’t actually get used but it does!!! The macro portEXIT\_SWITCHING\_ISR retrieves the value directly from the register. All of this is port specific so you need to check the demo for your particular architecture.

Paul.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 15, 2012, 2:28pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/9 "2012-03-15T14:28:29Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Thursday, March 15, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#9d4a):

thanx a lot Paul.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 15, 2012, 8:29pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/10 "2012-03-15T20:29:57Z")

</div>

**[spyhabs](https://sourceforge.net/u/spyhabs/)** wrote on [Thursday, March 15, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#26f3):

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 27, 2012, 8:21pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/11 "2012-04-27T20:21:20Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Friday, April 27, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#6306):

Hallo Everyone again,

i tried your suggestion Paul, but it did not work.

did anyone have an idea, how to use the queue from isr?  
here is my code, that cosing problems:

#pragma vector=USCI\_A1\_VECTOR  
\_\_interrupt void USCI\_A1\_ISR(void)  
{  
portBASE\_TYPE xHigherPriorityTaskWoken= pdFALSE;  
char ReceivedByte;  
ReceivedByte = UCA1RXBUF;  
xQueueSendFromISR( xprotReceiveByteQueue, &ReceivedByte, &xHigherPriorityTaskWoken );  
&nbsp;&nbsp;&nbsp; \_\_bic\_SR\_register\_on\_exit(LPM3\_bits);  
&nbsp;&nbsp;&nbsp; portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken );  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 29, 2012, 5:39pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/12 "2012-04-29T17:39:13Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Sunday, April 29, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#f02b):

can anyone help in this topic?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 30, 2012, 3:36pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/13 "2012-04-30T15:36:02Z")

</div>

**anonymous** wrote on [Monday, April 30, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#5ba1):

What is the current problem?&nbsp; You jumped around a bit there.&nbsp; Can you still send and recieve only one time?&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 1, 2012, 1:03am UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/14 "2012-05-01T01:03:18Z")

</div>

**[nathanwiebe](https://sourceforge.net/u/nathanwiebe/)** wrote on [Tuesday, May 01, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#d501):

Can you post a simplified version of the two files?&nbsp; It is probably something obvious to an experienced user of FreeRTOS.&nbsp; Try to hack out any application-specific code until you have nothing but the creation and reception from your queue in one file and the ISR in the other.&nbsp; If you can do that without your error going away, I would be happy to look at your code… 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 2, 2012, 6:18pm UTC](https://forums.freertos.org/t/using-queue-in-several-c-files/4478/15 "2012-05-02T18:18:23Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Wednesday, May 02, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/c2d6a5ae/#ea3a):

i think, i found the reason for the problem now.

it was not detected stackoverflow, the freertos could not find it out every time it happend.

here is the code:

the receiving task:

void SemaphorTestTask(void \*pvParameters)  
{  
int count=0;  
char ReceivedByte;  
for( ;; )  
{

if (xQueueReceive(xPROTQueue,&ReceivedByte,portMAX\_DELAY)== pdPASS)  
{  
halLcdPrintSingleChar(‘P’,0);  
++ count;  
if (count == 136)  
{  
halLcdClearScreen();  
halLcdPrintLine( " [www.FreeRTOS.org](http://www.FreeRTOS.org)", 0,&nbsp; OVERWRITE\_TEXT );  
count=0;  
}  
}  
}  
}

and here ist the ISR:

#pragma vector=USCI\_A1\_VECTOR  
\_\_interrupt void USCI\_A1\_ISR(void)  
{  
portBASE\_TYPE xHigherPriorityTaskWoken = pdFALSE;  
char ReceivedByte;  
ReceivedByte = UCA1RXBUF;  
xQueueSendFromISR(xPROTQueue,&ReceivedByte,&xHigherPriorityTaskWoken );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \_\_bic\_SR\_register\_on\_exit(LPM3\_bits);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken );  
}

this implementation is working without any problem.
