Page 1 of 1

non blocking semaphore in task

Posted: Thu Feb 17, 2022 11:49 pm
by kyrpav
Hi i have a Task where it waits for a Samaphore to run.
Then it has to go in an infinate loop to control a motor position.
I would like to break this second loop when the stop button is pressed is there any way to call SemaphoreTake function to break from the second loop but not block the while loop of the control ?

Let me try to give some code example

Code: Select all

void startTaskFunction(void *params){ //this is the function
    while (true)
    {
        xSemaphoreTake(StartSema,portMAX_DELAY);//which in order to start waits this Semaphore to be Given
        while(true){

            //do some logic to controll a motor and keep it steady

            /////to stop this while on some stop button if i use the next i think it will stop and waits on the first run of the loop so there is 
            //no control
            xSemaphoreTake(StopSema,portMAX_DELAY);
            break;
            // if i wrap it in an if condition i think it is the same isn't it?

        }
    }
}

Re: non blocking semaphore in task

Posted: Fri Feb 18, 2022 12:08 am
by kyrpav
Unfortunately i was not thinking clearly i can set the delay not to portMax_Delay but to some milliseconds. but i still need to wrap it in if condition in order to know if this happend so the question is that does the semaphoreTake return something i tried to open to see the declaration but i did not understand it.

Re: non blocking semaphore in task

Posted: Mon Feb 21, 2022 7:50 am
by imdaad
yes, it does return a BaseType_t. if it is pdPass you successfully obtained the token, otherwise, you will get pdFALSE which indicated either the token was not available or timeout.
Wbt using an event group if you need to look into multiple conditions?