semaphore example

Thread Starter

rt694157

Joined Dec 15, 2019
57
anybody know how semaphore may be useful in free rtos https://en.wikipedia.org/wiki/Semaphore_(programming) from given page I do not understand fundamentals

semaphore is used when there is only one shared resource

Let's take below example

Task 1 : blink red LED
Task 2 : blink green LED
Task 3 : blink Yellow LED

Can semaphore be used in example ? if its not suitable then please provide one suitable example
 

bug13

Joined Feb 13, 2012
1,864
This is not FreeRTOS, but all RTOS should be something similar like this...

C:
/* assuming you are refering to binary semaphore
* and use it as mutex
*/

/* share resource */
volatile rs232_t rs232;

/* assuming this is setup as binary semaphore */
volatile semaphore_t sema;

void task_1(arg_t arg0, arg_t arg1 ...){
    /* some init stuff */
    while(1){
        /* do other stuff */
        semaphore_pend(sema, RTOS_WAIT_FOREVER);
        rs232.printf("Hello from task 1\n");
        semaphore_post(sema);
    }
}

void task_2(arg_t arg0, arg_t arg1 ...){
    /* some init stuff */
    while(1){
        /* do other stuff */
        semaphore_pend(sema, RTOS_WAIT_FOREVE);
        rs232.printf("Hello from task 2\n");
        semaphore_post(sema);
    }
}
 
Top