ECE 532 Embedded Systems Winter 2005 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

ECE 532 Embedded Systems Winter 2005

Description:

Buffer Management Using Semaphores. Counting Semaphore ... When all semaphores used, task requesting buffer is suspended until semaphore available ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 17
Provided by: johng49
Category:

less

Transcript and Presenter's Notes

Title: ECE 532 Embedded Systems Winter 2005


1
ECE 532 Embedded SystemsWinter 2005
  • Session 9

2
Semaphores
  • Invented the Edgser Dijkstra in the 1960s
  • Use to
  • control access to a shared resource (mutual
    exclusion)
  • signal the occurrence of an event
  • allow two tasks to synchronize their activities
  • highest priority task waiting for semaphore gets
    it

OSSemPost(SharedDataSem)
OSSemPend(SharedDataSem)
3
Semaphores in uC/OS-II
  • Two types
  • binary
  • counting (0 65,535)
  • Creating a semaphore
  • declare its name (global)
  • OS_EVENT SampleSem
  • create it prior to use (main or in task but not
    inside loop)
  • SampleSem OSSemCreate(INT16U value)
  • if a counting semaphore, init value to zero (it
    will track the number of times it is called)
  • if value 1, binary semaphore
  • Using a semaphore
  • task that is waiting for signal calls
    OSSemPend(OS_EVENT event, INT16U timeout, INT8U
    err)
  • OSSemPend(SampleSem, 0, err)
  • task that is sending the signal calls
    OSSemPost(OS_EVENT event)
  • OSSemPost(SampleSem)
  • Semaphores must be enabled before use
  • In file OS_CFG.H set OS_SEM_EN to 1

4
Accessing Peripherals with Semaphores
  • Use binary semaphore

5
Buffer Management Using Semaphores
  • Counting Semaphore
  • Use when resource can be used by more than one
    task at same time
  • Example10 buffers
  • Buffer manager satisfies first 10 buffer requests
    directly
  • When all semaphores used, task requesting buffer
    is suspended until semaphore available

6
Deadlocks and Semaphores
  • Situation where two tasks are waiting for
    resource held by the other
  • E.g. Task 1 has exclusive use of resource R1 and
    Task 2 has exclusive use of resource R2.
  • T1 requires exclusive use of R2 and T2 requires
    exclusive use of R1
  • Neither task can complete
  • Avoid by
  • Acquire all resources before beginning
  • Acquire resources in same order
  • Release resource in reverse order
  • If kernel allows a timeout to be specified with
    semaphore (uC/OS-II does), this allows deadlock
    to be broken
  • Deadlocks generally occur in large, multi-tasking
    systems

7
Task Synchronization
  • Semaphores
  • Event Flags

8
Synchronization
  • Synchronize tasks with a semaphore
  • Will work with ISR or another task

9
Event Flags
  • Used to synchronize task with multiple event
    occurrence
  • Synchronize when any of the events have occurred
  • disjunctive synchronization
  • effectively the logical OR of event occurrence

10
Event Flags (Cont)
  • Synchronize when all of the events have occurred
  • conjunctive synchronization
  • effectively the logical AND of event occurrence

11
Multiple Task Signaling
  • Common events may signal multiple tasks

12
Services
  • OS_FLAG_EN must be set to 1 in OS_CFG.H
  • Create a flag group before using it
  • OSFlagCreate()
  • OS_FLAG_GRP EngineStatusFlags
  • OSFlagCreate(OS_FLAGS flags, INT8U err)
  • flags contains the initial values to store in the
    flag group
  • example
  • OS_FLAG_GRP EngineStatus
  • void main (void)
  • INT8U err
  • .
  • .
  • OS_INIT() //Initialize OS
  • .
  • . //create flag group containing engine status
  • EngineStatusFlags OSFlagCreate(0x00,err)
  • .
  • .
  • OSStart() //start multitasking

13
Services (Cont)
  • Set an Event Flag use OSFlagPost()
  • OSFlagPost(pgrp, flags, opt, err)
  • pgrp is a pointer to event flag group
  • flags sets corresponding bit in the flag group
  • opt determines whether set to one or zero
  • OS_FLAG_SET or OS_FLAG_CLR
  • err is pointer to error code
  • OS_NO_ERR call successful
  • OS_FLAG_INVALID_PGRP passed a null pointer
  • OS_ERR_EVENT_TYPE not pointing to event flag
    group
  • OS_FLAG_INVALID_OPT invalid option
  • Example
  • define ENGINE_OIL_PRESSURE_OK 0x01
  • define ENGINE_OIL_TEMP_OK 0x02
  • define ENGINE_START 0x04
  • .
  • .
  • err OSFlagPost(EngineStatusFlags, EngineStart,
    OS_FLAG_SET, err)

14
Services (Cont)
  • To wait for a condition, use OSFlagPend()
  • OSFlagPend(pgrp, flags, wait_type, timeout,err)
  • pgrp is pointer to flag group
  • flags is bit pattern indicating which bit(s) to
    check1 implies check
  • wait_type specifies all or any bits
  • OS_FLAG_WAIT_CLR_ALL check all bits in flags to
    be zero
  • OS_FLAG_WAIT_CLR_ANY check any of bits in flags
    to be zero
  • OS_FLAG_WAIT_SET_ALL check all bits in flags to
    be one
  • OS_FLAG_WAIT_SET_ANY check any of bits in flags
    to be one
  • consume flag by adding OS_FLAG_CONSUME (checks
    flag and clears it)
  • timeout is number of ticks to wait
  • err is pointer to error code
  • OS_NO_ERR no error
  • OS_ERR_PEND_ISR tried to pend from an ISR which
    is nono
  • OS_FLAG_INVALID_PGRP passed null pointer
  • OS_ERR_EVENT_TYPE not pointing to event flag
    group
  • OS_TIMEOUT flags not available within specified
    amount of time
  • OS_FLAG_WAIT_TYPE improper wait_type argument
  • example

15
Additional Intertask Communication Methods
  • Message Mailboxes
  • task can place a pointer in a mailbox for another
    task to access
  • kernel provides services
  • Enable Set OS_Mbox_En to 1 in OS_CFG.h
  • Create OSMboxCreate() create before use
  • Pend OSMboxPend() task waiting for message
  • Post OSMboxPost() task sending message
  • Query OSMboxQuery() get information about
    message mailbox

16
Additional Intertask Communication Methods
  • Message Queues
  • array of mailboxes
  • messages may be accessed FIFO or LIFO
Write a Comment
User Comments (0)
About PowerShow.com