RTOS MicroC tasking

268 Views Asked by At

I've been working on some RTOS MicroC project and whenever I've implemented some function it works just fine outside the task, but whenever I put in the task it just wouldn't do anything. I know I might not get answer to this, but any tips where to start looking would be a big help, thanks in advance.

a_sem = OSSemCreate(1);

static void AppTask1(void *p_arg)
{
    (void) p_arg;
    INT8U perr;
    while (1)
    {
        OSSemPend(a_sem, 0, &perr);
        planeAngles();// Functon that works outside the task
        OSSemPost(a_sem);
        OSTimeDly(OS_TICKS_PER_SEC/20);
    }
}

static void AppTask2(void *p_arg)
{
    (void) p_arg;
    INT8U perr;
    while (1)
    {
        OSSemPend(a_sem, 0, &perr);
        servoTurns(); // Functon that works outside the task
        OSSemPost(a_sem);
        OSTimeDly(OS_TICKS_PER_SEC/20);
    }
}
2

There are 2 best solutions below

3
On

Somewhere in your code, before AppTask1 and AppTask2 are created, you should have a line of code like this:

OSSemCreate(a_sem, 1, &perr);

You are creating a semaphore, a_sem with an initial value of 1 so that the first task that calls OSSemPend will successfully acquire the semaphore.

Also, you should not block forever on OSSemPend. Wait for awhile and then check the error status:

OSSemPend(a_sem, 10, &perr);
if(perr == OS_ERR_NONE)
{
    /* You have the semaphore */
}
else
{
    /* Error! Maybe a timeout */
}
4
On

Both tasks wait on a semaphore, but it is not clear where that semaphore is initially given. It seems likely that neither task ever returns from the OSSemPend call.