Battery Capacity Measurement And Analysis Using

advertisement
PALOS:
Power Aware Light-weight Operating System
(Pseudo-realtime, Application-specific, Light-weight
Operating System)
Andreas Savvides
EE Department, Yale University
December 6, 2003
Work in Progress…
Based on initial tutorial by
Sung Park
Electrical Engineering Departments
University of California, Los Angeles
September 18, 2002
Hardware Overview: MK-2
ARM/THUMB 40MHz
Running uCos-ii
PALOS
RS-485 &
External Power
ADXL 202E
MEMS Accelerometer
MCU I/F
2Host Computer, GPS, etc
UI: Pushbuttons
Medusa MK-2 Architecture
Ultrasnd RX/TX
Accesory
Board
ADXL202
Connector 2
Light &
Temp
Mega128L
PButton
UART
PMTU
Connector 1
UART &
JTAG
ADC/SPI/
GPIO
UART
PButton
RS-485
3
Radio
AT91FR4081
UART,
JTAG,
GPS
A Simple Embedded Program
4
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
5
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
6
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
7
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
8
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
9
PALOS architecture
10
PALOS features
•
Stripped Core
•
•
•
ATmega128
•
•
•
FLASH size(Code): 128Kbytes
RAM Size (Memory): 4Kbytes.
Typical( 3 drivers, 3 user tasks)
•
•
11
Code Size: 956 Bytes
Mem Size: 548 Bytes
Code Size: 8 KBytes
Mem Size: 1.3 KBytes
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)
12
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
13
Tasks
• A task
belongs to the
palos main
control loop
• Each task has
an entry in
palos task
table (along
with eventQs)
14
Inter-task Communication
• Events are exchanged using the
service provided by PALOS core
15
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
16
Event Driven Task
• Typical task routine
while (eventQ != isEmpty){
dequeue event;
process event;
}
17
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.
18
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
19
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;
20
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)();
}
}
}
}
21
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 );
22
Manager
Drivers (Hardware Abstraction Layer)
23
TASK N
TASK 3
TASK 2
PALOS
Core
TASK 1
TASK 4
TASK 5
PALOS architecture – Drivers and
Managers
Drivers (HAL)
• Abstracts hardware (processorspecific 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
24
Managers (Optional)
• Extra abstraction layer that
handles a protocol or handshaking
with external modules
• iBadge: Bluetooth Manager
25
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 );
26
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
27
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
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)();
}
}
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;
28
}
}
/* should never get here */
return;
}
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
29
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;
30
stringOutTask.c
SHORT stringOut_init() {
stringOut_hoqValid=false;
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;
// stringOut initialization
UART0_Init();
return 0;
}
31
}
initTask.c : initTask()
initTask() {
SYS_Init();
stringOutID=palosEvent_register(stringOut_init,
void
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);
}
32
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,
}
33
&outgoingMsgEvent, TASK_NON_ATOMIC);
MICA demo
34
menuTask.c
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));
}
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;
}
}
35
PALOS Development Environment
Directory
Structure
PALOS v0.1
Driver
ATmega128L
MK2
TMS320
iBadge
iBadge
Processor
MICA
timer1.c ,.h
Processor
uart0.c ,.h
timer1.c ,.h
36
palosCore
StrongThumb
sampleProject
-MICA
palosMain.c, .h
menuTask.c, .h
palosEvent.c, .h
stringInTask.c, .h
palosSched.c, .h
stringOutTask.c, .h
MK2
Processor
PALOS Development Environment
• Compiler: CVAVR (possible port to
AVR-GCC)
• Current release v.01
• https://sourceforge.net/project/showfiles.php?gro
up_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
37
Installing CVS client under Windows
• Windows CVS client setup(wincvs)
• Download and install wincvs from
http://prdownloads.sourceforge.net/cvsgu
i/WinCvs13b8.zip?download
• Download and install ssh from
http://prdownloads.sourceforge.net/sfset
up/ssh-1.2.14-win32bin.zip?download
• Download and install sourcefourge setup
utility “sfssetup” from
http://prdownloads.sourceforge.net/sfset
up/sfsetup-1.3.zip?download
38
Setting up wincvs
PALOS CVS Repository Setup: Admin->Preferences
39
Setting up wincvs
Checking Out Palos0.1 module: Create->Check Out Module
40
41
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!
42
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
43
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
44
Download