Hi, I’ve been banging my head against the wall on this problem for a while now and need some advice or clarity on what should be a simple problem.
I have a ‘main task’ that takes a static binary semaphore. The semaphore is supposed to be given or released by a PIT timer ISR. In debug, I can see that the ISR is correctly giving the semaphore, and I can also see that contex is being given directly back to the main task. However once back in the main task, the task never takes the semaphore and seemingly continues to sleep on it even though it was given by the ISR.
**Edit:
I noticed that after the ISR gives the semaphore, the uxMessageWaiting variable (inside the semaphore/queue struct) is 1 (as it should be since there is an item given in the queue). But after returning to the main task, the variable is changed to 0. So when the main task tries to take the semaphore, it looks and see’s uxMessageWaiting is still 0. I’m thinking there may be a stack or memory issue at hand here. Any advice is welcome.
The following is my code; the task is initialized in main.c.
~~~
define SLEEP 0xFFFF
/
Semaphore handles/
static SemaphoreHandle
t _semmainTask;
if ( 0 != configSUPPORTSTATICALLOCATION )
static StaticSemaphore
t _semmainTask_buffer;
endif
TaskHandle_t mainTaskHandle = NULL;
static void Task_Main( void *pvParameters )
{
(void) pvParameters; /* not used */
int test = 5;
for(;;)
{
long sleep = SLEEP;
BaseType_t _semGiven = pdFALSE;
_semGiven = xSemaphoreTake(_sem_mainTask, ( TickType_t )SLEEP);
if( _semGiven == pdTRUE)
{
test++;
//vTaskSuspend(NULL);
}
}
}
void Task
MainInitialize( TaskHandle
t *taskhandle )
//void Task
MainInitialize( void )
{
/* Create RTOS task
/
xTaskCreate( Task_Main, / task function
/
“main”, / name of task
/
256, / stack space
/
NULL, / no private parameters to pass into the task
/
10, / priority */
task_handle
);
//create the binary semaphore
#if ( 1 != configSUPPORT_STATIC_ALLOCATION )
_sem_mainTask = xSemaphoreCreateBinaryStatic( &_sem_mainTask_buffer );
#else /* configSUPPORT_DYNAMIC_ALLOCATION */
_sem_mainTask = xSemaphoreCreateBinary();
#endif
//give the binary semaphore to kick it off
xSemaphoreGive( _sem_mainTask );
}
//dedicated to yielding to the main task
void PIT1_IRQHandler(void)
{
//CLEARN THE INTERRUPT FLAG!!!!
PIT_StopTimer(PIT, 1);
PIT_ClearStatusFlags(PIT, 1, kPIT_TimerFlag);
PIT_StartTimer(PIT,1);
BaseType_t reschedule;
xSemaphoreGiveFromISR(_sem_mainTask, &reschedule);
if(reschedule){
//go to sleeping task
portYIELD_FROM_ISR(reschedule);
}
}
~~~