Using Queue in several c files.

modiallen wrote on Friday, February 24, 2012:

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

modiallen wrote on Friday, February 24, 2012:

for more details:

i am working on MSP430f5438 with CC.

in the hal_usb.c  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.

rtel wrote on Friday, February 24, 2012:

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

Regards.

modiallen wrote on Friday, February 24, 2012:

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.

richard_damon wrote on Friday, February 24, 2012:

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.

modiallen wrote on Friday, February 24, 2012:

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

modiallen wrote on Saturday, February 25, 2012:

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

paulgcoleman wrote on Monday, February 27, 2012:

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;    // 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.

modiallen wrote on Thursday, March 15, 2012:

thanx a lot Paul.

spyhabs wrote on Thursday, March 15, 2012:

Thanks

modiallen wrote on Friday, April 27, 2012:

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 );
    __bic_SR_register_on_exit(LPM3_bits);
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

modiallen wrote on Sunday, April 29, 2012:

can anyone help in this topic?

anonymous wrote on Monday, April 30, 2012:

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

nathanwiebe wrote on Tuesday, May 01, 2012:

Can you post a simplified version of the two files?  It is probably something obvious to an experienced user of FreeRTOS.  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.  If you can do that without your error going away, I would be happy to look at your code… :slight_smile:

modiallen wrote on Wednesday, May 02, 2012:

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", 0,  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 );
       __bic_SR_register_on_exit(LPM3_bits);
      portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

this implementation is working without any problem.