palos: power aware light-weight operating system (pseudo-realtime, application-specific,...
TRANSCRIPT
PALOS:Power Aware Light-weight Operating System
(Pseudo-realtime, Application-specific, Light-weight Operating System)
Sung ParkElectrical Engineering DepartmentsUniversity of California, Los Angeles
September 18, 2002
2
Features of RTOS (hard)
• Structure - OS components and user defined Tasks
• Multi-tasking• Provides hard time guarantee
• Schedulability test guarantees task deadline
• Priority Support• Pre-emption of tasks
3
Drawbacks of RTOS
• Interrupt latency of context switching may introduce inefficiency
• Pre-emption requires extra attention to shared memory
• RTOS is difficult to debug• Commercial RTOS
• Portability can be difficult• License Fee• Learning Curve
4
Do we need RTOS in Wireless Sensor Network?
• Application Specific – Need for RTOS• Sensor network monitoring plane’s position and
orientation – a missed deadline could lead to plane crash
• Sensor network monitoring nuclear plant – a missed deadline could lead to nuclear melt down
• Other cases• If a missed deadline results in minor degradation
of performance or data qaulity, we can live without an RTOS
• As a research organization, we are more concerned about algorithms and development flexibility -> need to address possible migration to RTOS
5
PALOS: Pseudo-Realtime (soft)
• Structure: OS functions and user defined Tasks
• Multi-tasking (Event-Driven)• Each tasks is a routine which processes
events that are stored in event queues
• Supports Inter-task Communication• By queuing events to other tasks, the data
between tasks can be exchanged
6
PALOS: Pseudo-Realtime (soft)
• Instead of hard-realtime, palos provides soft-realtime gaurantee (best-effort)
• Tasks cannot be pre-emptied (by other tasks)• Tasks run one after the other• No shared memory protection required
• Has mechanisms to provide priority, stopping and resuming of tasks
7
PALOS architecture
PALOSCore
Drivers (Hardware Abstraction Layer)
ManagerT
AS
K 1
TA
SK
2
TA
SK
5
TA
SK
N
TA
SK
3
TASK 4
8
PALOS features
• Stripped Core• Code Size: 956 Bytes• Mem Size: 548 Bytes
• ATmega128 • FLASH size(Code): 128Kbytes• RAM Size (Memory): 4Kbytes.
• Typical( 3 drivers, 3 user tasks)• Code Size: 8 KBytes• Mem Size: 1.3 KBytes
9
Compared to tinyOS
• Notion of well-defined tasks• Inter-task communication through
the use of separate event queues • More elaborate scheduling scheme
where multiple tasks can be periodically or aperiodically scheduled
• Easier to debug (minimum use of macros)
10
PALOS Core
• Processor independent algorithms• Provides means of managing event
queues and exchanging events among tasks
• Provides means of task execution control(slowing, stopping, and resuming)
• Supports a scheduler: periodic, and aperiodic functions can be scheduled
11
Tasks
• A task belongs to the palos main control loop
• Each task has an entry in palos task table (along with eventQs)
PALOSTaskTable
TASK 1
TASK 2
TASK 3
TASK N
Task Routine
Event Q
Task Routine
Event Q
Task Routine
Event Q
Task Routine
Event Q
MainControl
Loop
12
Inter-task Communication
• Events are exchanged using the service provided by PALOS core
Task 2
Task 3 Event Q
Task 1
Task 3
PALOSCore
13
Scheduling with Software Timer
• Periodic or aperiodic events can be scheduled using Delta Q and Timer Interrupt
• When event expires appropriate event handler is called
Task 2
Delta Q
Task 1
TimerInterrupt
Expired Event Q
TimerTask
Handler 1
Handler 2
Handler 3
PALOSCore
14
Event Driven Task
• Typical task routine
while (eventQ != isEmpty){
dequeue event;
process event;
}
15
Task Execution Control
• Execution Control using task counter• A task counter is associated with each
task• Counters are initialized to pre-defined
values• Counters are decremented 1) every main
control loop iteration (relative timing) 2) by timer interrupts (exact timing)
• When counter reaches zero, the task routine is called. The counter is reset to reload value.
16
Task Execution Control
• Normal Task: Set the counter to 0 and reload value to 0
• Task slow down: Set the counter to large positive value
• Task Suspension: Set the counter to pre-set value (e.x. –1)
• Task Resumption: Reset the counter to positive value
17
PALOS v0.1 implementation – Task Table Structure
/* Generic event queue structure */
typedef struct {
SHORT (*initHandler)(void);
SHORT (*taskHandler)(void);
SHORT execCounter; /* Counter to be used for task speed control */
/* when counter reaches zero the task is executed */
SHORT reloadCounter; /* execCouter is reset the reload counter value after it */
/* reaches zero */
SHORT maxEvent; /* stores max number of events that can be processed per */
/* iteration. can be used to give priority */
BOOL isExactTiming; /* indicates whether the counter is decremented */
/* following exact timing */
USHORT header; /* header ptr */
USHORT trailer; /* trailer ptr */
USHORT eventStrSize; /* member structure size */
USHORT maxQsize; /* max number queue size */
USHORT curQsize; /* current queue size */
CHAR isValid; /* indicates whether this is valid entry */
void *event;
} taskStr;
18
PALOS v0.1 implementation – Main Control Loop
// main loop while (1){ // run each task in order for (i=0; i< globalTaskID; i++){ isExact = qArray[i].isExactTiming; tmpCntr=qArray[i].execCounter; if ( tmpCntr != TASK_DISABLED) { /* task is not disabled */
if ( tmpCntr ) { /* counter hasn't expired */ if (!isExact) qArray[i].execCounter--;}else { /* exec counter expired */ if (isExact) PALOSSCHED_TIMER_INTR_DISABLE; qArray[i].execCounter = qArray[i].reloadCounter; if (isExact) PALOSSCHED_TIMER_INTR_ENABLE; /* run the task routine */ (*qArray[i].taskHandler)();}
} } }
19
PALOS Core functions
SHORT palosEvent_register(SHORT (*initFunc)(void), SHORT (*taskFunc)(void), LONG xCounter, LONG rCounter, USHORT maxEv,
BOOL exactTiming,USHORT eventStrSize, USHORT maxQsize, void *ev);
SHORT palosEvent_putEvent( USHORT taskID, void *ev, CHAR isAtomic);
SHORT palosEvent_getEvent( USHORT taskID, void *ev, CHAR isAtomic);
SHORT palosEvent_start(USHORT taskID, LONG excCntr, LONG reldCntr);
SHORT palosEvent_stop(USHORT taskID);
SHORT palosEvent_maxEvent(USHORT taskID, USHORT maxEv);
SHORT palosEvent_exactTiming(USHORT taskID, BOOL exactTiming);
SHORT palosSched_schedule( USHORT tid, ULONG param, hndlrWrapper *tmrHandler, ULONG ticks, CHAR isPeriodic);
SHORT palosSched_cancel( USHORT tid, hndlrWrapper *tmrHandler );
20
PALOS architecture – Drivers and Managers
PALOSCore
Drivers (Hardware Abstraction Layer)
ManagerT
AS
K 1
TA
SK
2
TA
SK
5
TA
SK
N
TA
SK
3
TASK 4
21
Drivers (HAL)
• Abstracts hardware (processor-specific and platform-specific) from the task level
• The layering supports portability• Processors: ATmega103, ATmega128L,
TMS320, STrongThumb• Platforms: iBadge, MICA, MK2
• Examples: • Processor-specific: UART, SPI, Timers.. • Platform-specific: RFM, LEDs, Sensors
22
Managers (Optional)
• Extra abstraction layer that handles a protocol or handshaking with external modules• iBadge: Bluetooth Manager
23
UART0 Driver
void UART0_Init();
void UART0_Enable();
void UART0_Disable();
UCHAR UART0_NewData();
UCHAR UART0_GetByte();
USHORT UART0_Get2Bytes();
UCHAR UART0_GetNBytes( UCHAR * ptr_ch, UCHAR nN );
USHORT UART0_Check2Bytes();
UCHAR UART0_GetError();
UCHAR UART0_FreeSpace();
BOOL UART0_WriteByte( UCHAR ch );
BOOL UART0_Write2Bytes( USHORT sh );
BOOL UART0_WriteNBytes( UCHAR * ptr_ch, UCHAR nN );
24
Task Implementation with PALOS
• Need to define event structure• Implement initialization function• Implement main task function• Implement initTask()
• Performs system initialization• Registers different task to PALOS core
• Implement initSched()• Initial scheduling of events
25
Task Implementation with PALOS
void main(void){ SHORT i; USHORT tmpCntr; BOOL isExact; // event handler initialization palosEvent_init(); // The user's task is registered and // scheduled by this function
initTask(); // initialize each function for (i=0; i< globalTaskID; i++){ (*qArray[i].initHandler)(); } // User needs to define this function to // schedule events
initSched();// main loop while (1){ // run each task in order for (i=0; i< globalTaskID; i++){ isExact = qArray[i].isExactTiming; tmpCntr=qArray[i].execCounter;
if ( tmpCntr != TASK_DISABLED) { /* task is not disabled */if ( tmpCntr ) { /* counter hasn't expired */ if (!isExact) qArray[i].execCounter--;}else { /* exec counter expired */ if (isExact) PALOSSCHED_TIMER_INTR_DISABLE; qArray[i].execCounter = qArray[i].reloadCounter; if (isExact) PALOSSCHED_TIMER_INTR_ENABLE; /* run the task routine */ (*qArray[i].taskHandler)();}
} } } /* should never get here */ return;}
26
Example Application for MICA node
• StringIn Task: gets string from stdin• StringOut Task: outputs string to stdout• Menu Task: runs the menu state machine to
control the frequency of LED flashing frequency
PALOSCore
Timer1Driver
StringOutTask
StringInTask
MenuTask
UART0 DriverLED
Driver
27
stringOutTask.h
/* stringOut task event structure */
typedef struct {
UCHAR *str; /* pointer to string */
UCHAR size; /* size of the string */
USHORT eventID;
ULONG eventParam;
hndlrWrapper stringOut_TXdone; /* handler called when tx is done */
} stringOut_Event;
28
stringOutTask.c
SHORT stringOut_init() { stringOut_hoqValid=false;
// stringOut initialization
UART0_Init();
return 0;
}
SHORT stringOut_task() { UCHAR availSize;
while ((stringOut_hoqValid == true) ||
(palosEvent_getEvent(stringOutID, &stringOut_hoq, TASK_NON_ATOMIC) !=PALOSEVENT_QEMPTY)){
availSize = UART0_FreeSpace(); if ( stringOut_hoq.size <= availSize ) { UART0_WriteNBytes(stringOut_hoq.str,
stringOut_hoq.size); HANDLER_CALL(stringOut_hoq.stringOut_TXdone,
stringOut_hoq.eventID, stringOut_hoq.eventParam);
stringOut_hoqValid=false; } else { UART0_WriteNBytes(stringOut_hoq.str, availSize); stringOut_hoq.str += availSize; stringOut_hoq.size -= availSize; stringOut_hoqValid=true; break; } } return PALOSEVENT_TASK_DONE;}
29
initTask.c : initTask()void initTask() {SYS_Init();stringOutID=palosEvent_register(stringOut_init, stringOut_task,
STRINGOUT_DEF_CNTR, STRINGOUT_DEF_RCNTR, STRINGOUT_DEF_MAXEVENT, false, sizeof(stringOut_Event), STRINGOUT_Q_SIZE, (void *)stringOutEvent);
stringInID=palosEvent_register(stringIn_init, stringIn_task, STRINGIN_DEF_CNTR, STRINGIN_DEF_RCNTR, STRINGIN_DEF_MAXEVENT, false, sizeof(stringIn_Event), STRINGIN_Q_SIZE, (void *)stringInEvent);
menuID=palosEvent_register(menu_init, menu_task, MENU_DEF_CNTR, MENU_DEF_RCNTR, MENU_DEF_MAXEVENT, false, sizeof(menu_Event), MENU_Q_SIZE, (void *)menuEvent);
palosSchedID=palosEvent_register(palosSched_init, palosSched_task, PALOSSCHED_DEF_CNTR, PALOSSCHED_DEF_RCNTR, PALOSSCHED_DEF_MAXEVENT, false, sizeof(palosSched_Event), PALOSSCHED_EVENTQ_SIZE, (void *)tEvent);
}
30
initTask.c : initSched()
void initSched() { //schedule an event
stringOut_msg(initMsg, MENU_START,0, HANDLER_WRAP(menu_handler));}
stringOutTask.c : void stringOut_msg(CHAR *str, USHORT id, ULONG param, hndlrWrapper *hnd){ stringOut_Event outgoingMsgEvent;
outgoingMsgEvent.str=str;
outgoingMsgEvent.size=strLength(str);
outgoingMsgEvent.eventID=id;
outgoingMsgEvent.eventParam=param;
HANDLER_COPY(&(outgoingMsgEvent.stringOut_TXdone), hnd);
palosEvent_putEvent(stringOutID, &outgoingMsgEvent, TASK_NON_ATOMIC);}
31
MICA demo
32
menuTask.c
void menu_ledsched(USHORT id, ULONG param){ CHAR tmpID;
tmpID=(CHAR)id; switch(tmpID){ case LEDCHOICE_RED: redToggle(); break; case LEDCHOICE_GREEN: greenToggle(); break; case LEDCHOICE_YELLOW: yellowToggle(); break; default: /* shouldn't get here */ redOn(); yellowOn(); greenOn(); break; }}
case MENU_FREQ_CHECK: periodVal=strToShort(incomingMsg); if ( periodVal > 0 && periodVal <=10000) { palosSched_cancel(ledChoice,HANDLER_WRAP(menu_ledsched)); palosSched_schedule(ledChoice, 0, HANDLER_WRAP(menu_ledsched),
periodVal, PALOSSCHED_PERIODIC); stringOut_msg(msg5, MENU_RESTART_TX, 0,
HANDLER_WRAP( menu_handler)); }
33
PALOS Development Environment
Directory Structure
PALOS v0.1
palosCoreDriversampleProject
-MICA
palosMain.c, .h
palosEvent.c, .h
palosSched.c, .h
menuTask.c, .h
stringInTask.c, .h
stringOutTask.c, .h
ATmega128L TMS320
Processor
iBadge
MICA
MK2 iBadge
StrongThumb
MK2
uart0.c ,.h
timer1.c ,.h
timer1.c ,.h
Processor Processor
34
PALOS Development Environment
• Compiler: CVAVR (possible port to AVR-GCC)
• Current release v.01• https://sourceforge.net/project/showfiles.php?grou
p_id=61125
• If you want to do development• Needs to obtain account from
http://sourceforge.net• Email me your account id and what part of the
code you will be working on• Need to use CVS client to access CVS repository
35
Installing CVS client under Windows
• Windows CVS client setup(wincvs)• Download and install wincvs from
http://prdownloads.sourceforge.net/cvsgui/WinCvs13b8.zip?download
• Download and install ssh from http://prdownloads.sourceforge.net/sfsetup/ssh-1.2.14-win32bin.zip?download
• Download and install sourcefourge setup utility “sfssetup” from http://prdownloads.sourceforge.net/sfsetup/sfsetup-1.3.zip?download
36
Setting up wincvsPALOS CVS Repository Setup: Admin->Preferences
37
Setting up wincvsChecking Out Palos0.1 module: Create->Check Out Module
38
39
Software Engineering Practices
• Make use of CVS for development coordination (also backup and revision control)
• If you write something, make it modular(drivers, task, library)
• Spend extra time to refine the interface so that other people can easily use it
• Write a simple example code that can test your contributed module
• More you contribute, more useful it becomes Everybody counts!
40
Contributions Needed
• Communication Module: • RFM driver, Encoding and Decoding Task, MAC
layer etc..
• Drivers • SPI, timer0, timer2, watchdog timer, ADC, etc..
• Tasks• Sensing, Networking related tasks
• PALOS core• Random number generator, and other library
functions
• Porting to AVR-GCC
41
References
• Michael Melkonian, “Get by Without an RTOS”, Embedded Systems Programming Mag, vol 3. No. 10 Sept., 2000
• Jack W. Crenshaw, “Mea Culpa (Is RTOS needed?)”, http://www.embedded.com/story/OEG20020222S0023
• Karl Fogel, “Open Source Development with CVS”, http://cvsbook.red-bean.com/
• CVS FAQ, http://www.cs.utah.edu/dept/old/texinfo/cvs/FAQ.txt