Page 1 of 1

Problem with OS_WaitBinSem

PostPosted: Tue Dec 27, 2011 2:08 pm
by carlgonz
I need start a task that send some telemetry (once) by rs232 only when recive some char by the UART module. So, I created a task, initially waiting for a binary semaphore and this semaphore is signaled by the ISR when a new char has been recived. The problem: when the char arrives the semaphore is signaled, then the task starts but it continues the execution ignoring the 'wait sema' instruction. ¿What is the problem? ¿Is it the correct behavior?

The code is the following:

Code: Select all
void _ISR _U1RXInterrupt(void)
{
    OSSignalBinSem(BINSEM_BUFFER_RS232);
    IFS0bits.U1RXIF = 0;
}   

void ReadCommand(void)
{
    static unsigned char read[]="READ_";
    while(1)
    {   
        OS_WaitBinSem(BINSEM_BUFFER_RS232,OSNO_TIMEOUT);
        LedE9_TOGGLE;
        SendRS232(read,5,M_UART1);
        //OS_Yield();
    }// END WHILE(1)
}


And as result, when I send a char, I receive: "READ_", "READ_, "READ_" ... forever, I mean, the task will never stop again.
Thanks and best regards!

Re: Problem with OS_WaitBinSem

PostPosted: Tue Dec 27, 2011 2:57 pm
by Andrew
1. You should always clear an interrupt flag as the first thing in the ISR ...

2. ReadCommand() has to be a task ... it cannot be a function.

Other than that, this looks OK. No need for OS_Yield() when OS_WaitBinSem() is present.

If the task is not "waiting", it means that the incoming characters at ISR_U1Rx are coming in faster than you are stripping them out via successful OS_WaitBinSems() ...

You could check for different behavior by using a semaphore instead of a binSem ...

Re: Problem with OS_WaitBinSem

PostPosted: Tue Dec 27, 2011 6:41 pm
by carlgonz
Thanks for the answer Andrew,

- Actually 'ReadCommand' is a task, and as first thing I do is initialize all events and task.
- The task really waits the semaphore but only the first time, then it continues active and running. Is it correct? It seems to me, that when the task checks for the sema condition again, the sema will be in 0, so the task will wait for a new SignalSem, rigth? But... it doesn't happen.
- The incoming characters aren't the problem becouse I have tried with individual sendings.
- I'll try another resource, like semaphore or messages.

My question basically is: what should be the correct behavior of this code?

In aditions, I'm working with a PIC24FJ256GA110 using the MPLAB C30 compiler.

Best regards!

Re: Problem with OS_WaitBinSem

PostPosted: Wed Dec 28, 2011 10:17 am
by Andrew
You asked
carlgonz wrote:My question basically is: what should be the correct behavior of this code?


and I (previously) wrote
Other than that, this looks OK. No need for OS_Yield() when OS_WaitBinSem() is present.


So yes, that is the correct code, and it will wait each time until the binary semaphore has been signaled.

Since the behavior you describe is not correct, there is some other issue in your code. I suspect a fault in your ISR.