Multiple calls of OSSemInit()

Discussion to talk about software related topics only.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: Multiple calls of OSSemInit()

Post by tod »

I'll put in my .02 worth since I just struggled for a short while getting Post/Pend working before this thread started. First when done correctly the semaphore mechanism of uCOS works reliably on the MOD5272, I use it all the time. I recently made the mistake someone else mentioned in that I moved some code around and had the POST fire before the OSSemInit call. Make doubly sure you aren't making this error.

Per rnixon's suggestion, you could change the Pend to NOT pend forever. Check for the timeout return code and put out a char or blink a LED, that way you know the task is getting CPU cycles, and that the code is actually sitting on the PEND.

Another potential error is not using the same variable for the Post and the Pend. Since you're using a global, this means you have properly defined it in only one place and then properly declared it (using extern) in places where its being used. Redfining should result in a linker error but if you give it file scope or have it in a namespace it's easy to have two variables of the same name. Typically I create my semaphores as public static members of the class that is in charge of the interrrupt.
So if I have a class called Timer2 in the .h it will have

Code: Select all

public:
static OS_SEM m_pendingTimer2;
static OS_SEM& GetSemaphore(){ m_pendingTimer2;}
Normally I would haven't public data but these RTOS objects can be a little tricky since they are typically used in extern "C" interrupt code. I will put the INTERRUPT macro in the same class .cpp file and it will just do a

Code: Select all

OSSemPost(&Timer2:m_pendingTimer2;)
However, that's the ONLY place that will directly use the variable. I could use it other places but I prefer to use the little getter I wrote. So off in the task in another class, prior to entering the loop that pends on the semaphore I do this

Code: Select all

 OS_SEM& timer2_semaphore = Timer2::GetSemaphore();
Then inside the forever loop

Code: Select all

OSSemPend(&timer2_semaphore, WAIT_FOREVER);
This technique means I can't mistype a variable name, create an inadvertent duplicate etc.

If you're still stuck I would suggest creating a new device executable project and just paste in the code relevant to this issue. If you can't get that working then you could upload the project to something like BitBucket (or even just attach it to this topic) and someone might be able to see what you're doing wrong.
Post Reply