Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Task does not take semaphore given by ISR

Posted by psjapex on February 6, 2017

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 SemaphoreHandlet _semmainTask;

if ( 0 != configSUPPORTSTATICALLOCATION )

static StaticSemaphoret _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 TaskMainInitialize( TaskHandlet *taskhandle ) //void TaskMainInitialize( 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);
}

}

~~~


Task does not take semaphore given by ISR

Posted by rtel on February 6, 2017

Is this a PIC24? Guessing from the way the ISR is written.

Not related to your problem, but reschedule (the variable) should be initialised to pdFALSE (0) before it is used in xSemaphoreGiveFromISR.

Is there a portYIELDFROMISR() function for the PIC24? Other than that, I can't see anything standing out as being wrong. Can you step into the portYIELDFROMISR() function to see what happens? If this is a PIC24 it should take you into the function the task being unblocked is blocked in - from there you might be able to see why its not executing.


Task does not take semaphore given by ISR

Posted by psjapex on February 6, 2017

This is actually an K64 Arm cortex m4 chip on a custom board. This is being built out in Kinetis Design Studio IDE. Based on the functionality, I feel like theres an underlying memory issue in my system that is overwriting the semaphore values. See edit in original post.


Task does not take semaphore given by ISR

Posted by rtel on February 6, 2017

In which case the if statement:

if(reschedule){ }

is not required. The if is done for you inside the portYIELDFROMISR() macro.

On this device the yield should pend the PendSV interrupt. Is the interrupt being entered?

If so, the handler will call vTaskSwitchContext(). Is vTaskSwitchContext() selecting the task you expect? (I think you said it was, but want to be sure).


Task does not take semaphore given by ISR

Posted by heinbali01 on February 7, 2017

Not sure if it matters, but you create a task that will use a semaphore that hasn't been created yet.

Task_Main has a high priority of 10. If Task_Main_Initialize runs at a lower priority, chances are big that Task_Main calls xSemaphoreTake() for a non-existing semaphore.

Can you try swap the two statements?


Task does not take semaphore given by ISR

Posted by heinbali01 on February 8, 2017

Any news Peter?


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists