Available Languages?:

This is an old revision of the document!


OSA : Binary semaphores

Introduction

Binary semaphore is a system variable that can accept values 0 and 1. This is simplest way to exchange data between tasks and to synchronize them. All binary semaphores have size of one bit. Programmer can set number of binary semaphores by defining OS_BSEMS constant in OSAcfg.h and system will allocate necessary number of bytes. Therefore memory under binary semaphores reserved on compile stage and their number can't be changed in run-time.

Identification

When working with services controlling binary semaphores it is need to give them semaphore's ID from 0 to OS_BSEMS-1. It is comfortable to assign IDs using enum:

enum OSA_BINSEMS_ENUM
{
    BS_BUTTON_PRESSED,
    BS_USART_FREE,
    BS_WRITE_COMPLETE,
    . . .
};

When using enum we can be sure that each ID is unical.

Work with binary semaphores

After system initialization (OS_Init) all binary semaphores are reseted. Task wich waits binary semaphore will be put into waiting state until binary semaphore will be set.

Here is example of using binary semaphore to accessing common resource - external eeprom:

#include <osa.h>
 
enum OSA_BINSEMS_ENUM {
    BS_EEPROM_FREE
};
 
void Task1 (void)
{
    for (;;) {
        ...
        OS_Bsem_Wait(BS_EEPROM_FREE);     // Wait when EEPROM becames free of other tasks
        // After this service binary semaphore reset automatically
        // thus other task will not get this resource.
 
        // Here we know that EEPROM if free of other tasks.
        eeprom_read(1);
        eeprom_read(2);
        OS_Bsem_Set(BS_EEPROM_FREE);      // After work with EEPROM we free this resource
        ...
    }
}
 
void Task2 (void)
{
    for (;;) {
        ...
        OS_Bsem_Wait(BS_EEPROM_FREE);     // Wait when EEPROM becames free of other tasks
        // After this service binary semaphore reset automatically
        // thus other task will not get this resource.
 
        // Here we know that EEPROM if free of other tasks.
        eeprom_write(5, 10);
        OS_Delay(5);                     // Wait for end of write operation.
                                         // When waiting other tasks are executed. But since
                                         // binary semaphore is reset none of them can start
                                         // to work with EEPROM
        eeprom_write(6, 20);
        OS_Delay(5);
        OS_Bsem_Set(BS_EEPROM_FREE);      // After work with EEPROM we free this resource
        ...
    }
}

Allocation

You can select memory bank to allocate binary semaphores. To do it you need to set constant OS_BANK_BSEMS in OSAcfg.h.

Services for binary semaphores

Service Arguments Description Properties
Menagement
OS_Bsem_Set (bsem) Signal binary semaphore Have alternate service for work in ISR (suffix "_I")
OS_Bsem_Reset (bsem) Reset binary semaphore Have alternate service for work in ISR (suffix "_I")
OS_Bsem_Switch (bsem) Change binary semaphore's state Have alternate service for work in ISR (suffix "_I")
Checking
bool
OS_Bsem_Check
(bsem) Check for binary semaphore is set Have alternate service for work in ISR (suffix "_I")
Waiting
OS_Bsem_Wait (bsem) Wait for binary semaphore is set Allowed only in taskSwitches contexts
OS_Bsem_Wait_TO (bsem, timeout) Wait for binary semaphore is set. Exit if timeout expired. Allowed only in taskSwitches contextsService uses system timer
 
en/osa/ref/services/binary_semaphores.1260572477.txt.gz · Last modified: 12.12.2009 02:01 by osa_chief
 
Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki