11.07.2015 Views

IAR PowerPac RTOS User Guide

IAR PowerPac RTOS User Guide

IAR PowerPac RTOS User Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Critical RegionsIntroductionCritical regions are program sections during which the scheduler is switched off, meaning that no task switch and noexecution of software timers are allowed except in situations where the active task has to wait. Effectively, preemptionsare switched off.A typical example for a critical region would be the execution of a program section that handles a time-critical hardwareaccess (for example writing multiple bytes into an EEPROM where the bytes have to be written in a certain amount oftime), or a section that writes data into global variables used by a different task and therefore needs to make sure thedata is consistent.A critical region can be defined anywhere during the execution of a task. Critical regions can be nested; the schedulerwill be switched on again after the outermost loop is left. Interrupts are still legal in a critical region. Software timersand interrupts are executed as critical regions anyhow, so it does not hurt but does not do any good either to declarethem as such. If a task switch becomes due during the execution of a critical region, it will be performed right after theregion is left.Critical regions API function overviewRoutineOS_EnterRegion()OS_LeaveRegion()Table 115: Critical regions API overviewDescriptionIndicates to the OS the beginning of a critical region.Indicates to the OS the end of a critical region.OS_EnterRegion()DescriptionIndicates to the OS the beginning of a critical region.Prototypevoid OS_EnterRegion (void);Additional InformationOS_EnterRegion() is not actually a function but a macro. However, it behaves very much like a function but is muchmore efficient. Using the macro indicates to <strong>IAR</strong> <strong>PowerPac</strong> <strong>RTOS</strong> the beginning of a critical region. A critical regioncounter (OS_RegionCnt), which is 0 by default, is incremented so that the routine can be nested. The counter will bedecremented by a call to the routine OS_LeaveRegion(). If this counter reaches 0 again, the critical region ends.Interrupts are not disabled using OS_EnterRegion(); however, disabling interrupts will disable preemptive taskswitches.Examplevoid SubRoutine(void) {OS_EnterRegion();/* this code will not be interrupted by the OS */OS_LeaveRegion();}OS_LeaveRegion()DescriptionIndicates to the OS the end of a critical region.PP<strong>RTOS</strong>-2 107

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!