Title: CS372 Programming Project
1CS372 Programming Project
- Instructor Prof. Richard Brice
- TA Fei Xie
- Unique Number52215
2Programming Project Overview
- To be conducted by extending TinyOS
- Part 1 Understand TinyOS and play with the shell
application (5pt) - Part 2 Add several task schedulers (15pt)
3Todays Agenda
- Overview of TinyOS
- A TinyOS application example blink
- Part 1 of the project (Due Apr. 2)
- Part 2 of the project (Due Apr. 29)
4Overview of TinyOS
- Industrial motivations behind TinyOS
- What is TinyOS?
- TinyOS vs. traditional OS
- Why we choose TinyOS?
- TinyOS design models
- TinyOS structure
5Industrial Motivations behind TinyOS
- Low power wireless communication devices
- Particularly wireless networked sensors
- Physical limitations
- Computation ability
- Memory
- Power supply
- High-level concurrency
6What is TinyOS?
- An event-based operating system designed for
wireless networked sensors. - Designed to support concurrency-intensive
operations required by networked sensors with
minimal hardware requirements. - Developed by the EECS Department of U.C.
Berkeley. - C and Assembly languages
- Source code size 500KB, 16KB commented lines
7TinyOS vs. Traditional OS
- Special purpose (not general purpose)
- Resource constraint
- 4MHz ATMEL 8535 8bit MCU
- 512 byte RAM and 8K Flash
- No dedicated I/O controller (missed deadline
means loss data) - One program at one time (no multi-programming)
- Thin-threads (tasks)
8Why we choose TinyOS?
- It is small.
- It is easy to learn.
- It provides a simulation environment.
- It can be played with easily.
- Its structure is highly modular.
- It can be extended very easily.
9TinyOS Design Models
- Component-based model (modularity)
- Simple functions are incorporated in components
with clean interfaces - Complex functions can be implemented by composing
components. - Event-based model
- Interact with outside by events (no command
shell) - There are two kinds of events for TinyOS
- External events Clock events and message
events - Internal events triggered by external events.
10TinyOS Structure
Main (scheduler)
Application
Actuating
Sensing
Communication
Communication
Hardware Abstractions (ADC, CLOCK, I2C, LEDS,
PHOTO, UART, RFM)
- Consists of a scheduler and a graph of components.
11Todays Agenda
- Overview of TinyOS
- A TinyOS application example blink
- Part 1 of the project (Due Apr. 2)
- Part 2 of the project (Due Apr. 29)
12A TinyOS Application Example
Main (scheduler)
BLINK
Communication
Hardware Abstractions (CLOCK and LEDS)
13TinyOS Component Structure
- A TinyOS Component
- Frame (storage)
- Tasks (computation concurrency)
- Time consuming computations
- Have no hard real-time requirements
- Commands and events (interfaces)
14TinyOS Component Structure (Cont.)
- TOS_MODULE ltModule Namegt
- TOS_FRAME_BEGIN (CLOCK_frame) / Frame for
Data Storage / - TOS_FRAME_END (CLOCK_frame)
- ACCEPTS / The commands provided by the
component for other components/ -
-
- SIGNALS / The events sent out by the component
to other components/ - ...
-
- HANDLES/ The events received by the component
from other components/ -
-
- USES / The commands of other components that
are used by the component / -
15main.comp
TOS_MODULE MAIN USES char MAIN_SUB_INIT(void)
char MAIN_SUB_START(void)
- Issues INIT and Start commands to application
- Required to be in all applications
- Includes and calls the task scheduler.
16main.c
- Int main (int argc, char argv)
- Parse input parameter
- Set Ctrl-C handler
- Set debug mode
- Defined in dbg_modes.h
- Set environment variable DBG to change debug mode
- define DBG_DEFAULT DBG_ALL
- Init simulation environment
- For each node
- MAIN_SUB_INIT() -- call BLINKBLINK_INIT
- MAIN_SUB_START() -- call BLINKBLINK_START
- While there is event coming
- Process coming events
- Schedule tasks
17clock.comp
TOS_MODULE CLOCK TOS_FRAME_BEGIN (CLOCK_frame)
int cnt int time TOS_FRAME_END
(CLOCK_frame) ACCEPTS char CLOCK_INIT (char
interval, char scale) char CLOCK_GET_TIME (int
time) char CLOCK_SET_TIME (int
time) SIGNALS void CLOCK_FIRE_EVENT
(void)
- Send out clock fire event at a specified
interval - Required when implementing periodic jobs.
18LEDS.comp
TOS_MODULE LEDS TOS_FRAME_BEGIN(AM_temp_frame)
char leds_on TOS_FRAME_END(AM_temp_fr
ame) ACCEPTS char LEDS_INIT(void) char
RED_LED_ON(void) char RED_LED_OFF(void) char
RED_LED_TOGGLE(void) char GREEN_LED_ON(void) c
har GREEN_LED_OFF(void) char GREEN_LED_TOGGLE(vo
id) char YELLOW_LED_ON(void) char
YELLOW_LED_OFF(void) char YELLOW_LED_TOGGLE(void
)
19blink.comp the application component
TOS_MODULE BLINK TOS_FRAME_BEGIN (BLINK_frame)
char state TOS_FRAME_END
(BLINK_frame) ACCEPTS char BLINK_INIT
(void) char BLINK_START
(void) HANDLES void
BLINK_CLOCK_EVENT (void) USES char
BLINK_SUB_INIT (char interval, char scale)
char BLINK_LEDy_on ( ) char BLINK_LEDy_off (
) char BLINK_LEDr_on ( ) char
BLINK_LEDr_off ( ) char BLINK_LEDg_on (
) char BLINK_LEDg_off ( )
20blink.c
- BLINK_INIT Clear all the LEDs and initialize
state - char TOS_COMMAND(BLINK_INIT)()
- TOS_CALL_COMMAND (BLINK_LEDr_off)() //call
BLINKRED_LED_OFF - TOS_CALL_COMMAND (BLINK_LEDy_off)()
- TOS_CALL_COMMAND (BLINK_LEDg_off)()
- VAR (state) 0
- TOS_CALL_COMMAND(BLINK_SUB_INIT)(tick1ps) //
call CLOCKCLOCK_INIT - return 1
-
- BLINK_START
- Clock Event Handler Toggle the Red LED on each
clock tick. - void TOS_EVENT(BLINK_CLOCK_EVENT)()
- char state VAR(state)
- if (state 0)
- VAR (state) 1
- TOS_CALL_COMMAND (BLINK_LEDr_on)()
- else
- VAR (state) 0
- TOS_CALL_COMMAND (BLINK_LEDr_off)()
21Composition
- Layering
- Components issue commands to lower-level
components - Components send events to higher-level
components - No cycle in the call chain is allowed.
22TinyOS application example blink
include modules MAIN BLINK CLOCK LEDS BLI
NKBLINK_INIT MAINMAIN_SUB_INIT BLINKBLINK_
START MAINMAIN_SUB_START BLINKBLINK_LEDy_on
LEDSYELLOW_LED_ON BLINKBLINK_LEDy_off
LEDSYELLOW_LED_OFF BLINKBLINK_LEDr_on
LEDSRED_LED_ON BLINKBLINK_LEDr_off
LEDSRED_LED_OFF BLINKBLINK_LEDg_on
LEDSGREEN_LED_ON BLINKBLINK_LEDg_off
LEDSGREEN_LED_OFF BLINKBLINK_SUB_INIT
CLOCKCLOCK_INIT BLINKBLINK_CLOCK_EVENT
CLOCKCLOCK_FIRE_EVENT
MAIN
BLINK
LEDS
CLOCK
The composition is defined in blink.desc
23How to run blink application
- Download tos-0.6.1-nest-release.tgz from the
class web site - Untar it under selected directory
- tar xzvf tos-0.6.1-nest-release.tgz
- Compile application blink
- cd INSTALLPATH/nest/apps/blink
- make pc
- Run application blink
- cd INSTALLPATH/nest/apps/blink/binpc
- ./main p 1 1
- Usage ./main -h--help -r ltstaticsimplegt
-p sec num_nodes - In the projects, we will use
- r simple which is default
- -p pause-seconds
- num_nodes 1
24- 0 SIM event queue initialized.
- 0 SIM Could not initiate connection to port
10580 for messages. - 0 SIM Could not initiate connection to port
10581 for messages. - 0 SIM Could not initiate connection to port
10576 for messages. - 0 SIM Could not initiate connection to port
10577 for messages. - 0 SIM Could not initiate connection to port
10578 for messages. - 0 Created server socket 3 listening on port
10579. - 0 SIM Incoming radio messages initialized at fd
3. - 0 Time for mote 0 initialized to 1003424 from
765933. - 0 R_off
- 0 Set bit 5 of port C
- 0 Y_off
- 0 Set bit 3 of port C
- 0 G_off
- 0 Set bit 4 of port C
- 0 Clock initialized for mote 0 to 2000000 ticks.
- 0 Inserting event with time 3003424.
- 0 Popping event for mote 0 with time 3003424.
- 0
25Todays Agenda
- Overview of TinyOS
- A TinyOS application example blink
- Part 1 of the project (Due Apr. 2)
- Part 2 of the project (Due Apr. 29)
26Part 1 requirement
- Understand the structure of TinyOS and its
applications - Play with a TinyOS application that supports
interactive user inputs - Understand its five components MAIN, CLOCK,
SHELL, KEYBOARD, and SCREEN - Compile and link the application into TinyOS
- Execute the application and get the trace.
27A TinyOS Application Example
Main (scheduler)
SHELL
SCREEN
KEYBORD
Communication
Hardware Abstractions (CLOCK)
28Necessary Files for Shell Application
MAIN
SHELL
SCREEN
KEYBOARD
CLOCK
- Makefile (Same to the one of blink)
- shell.desc (Describing the shell application)
- SHELL.comp and SHELL.c (Declaring and
implementing the shell component) - SCREEN.comp and SCREEN.c
- KEYBOARD.comp and KEYBOARD.c
29Basic Ideas behind Shell Application
- CLOCK interrupts KEYBOARD with a clock signal at
a given interval. - KEYBOARD actively polls the user input from a
terminal (xterm) when a clock signal is received.
- If there is no input, the signal handler just
returns. - If there is some input, the signal handler reads
the input until the enter key is encountered or
the input is empty. - If the enter key is encountered, KEYBOARD
passes the inputted line to SHELL through
interrupt. - SHELL then calls SCREEN to echo the inputted line.
30External Behavior of Shell Application
- squaw.cs.utexas.edu main -p 0.5 1
- TinyOSgtls
- Executing ls
- TinyOSgtprint
- Executing print
- TinyOSgtps
- Executing ps
- TinyOSgtexit
- System halting...
- squaw.cs.utexas.edu
31Todays Agenda
- Overview of TinyOS
- A TinyOS application example blink
- Part 1 of the project (Due Apr. 2)
- Part 2 of the project (Due Apr. 29)
32Part 2 requirement
- Extend the shell provided
- Add a set of shell commands besides exit
- Define 2 TinyOS tasks
- Support reading lttask_filegt for task information.
- Implement non-preemptive schedulers
- Introduce a new way to post tasks with task
information - Realize LIFO, EDF, and PRIORITY schedulers.
33Outline
- TinyOS schedulers
- TinyOS task
- Define and post tasks
- Extend shell component
- Implement non preemptive task schedulers
34TinyOS schedulers
- Event scheduler (Only in simulation)
- Discrete event simulation
- Events are generated and assigned a time to be
executed. - Events are placed on a queue ordered by time.
- The event scheduler fetches the events from the
queue and invokes the handler for each event. - External events (e.g. clock events) are scheduled
in this way.
35TinyOS schedulers (Cont.)
- Task scheduler
- Implement FIFO scheduler in sched.c
- Data structure
- Circular queue TOS_sched_entry_T
TOS_queueMAX_THREADS - Functions
- TOS_sched_init()
- TOS_empty()
- TOS_post()
- TOS_schedule_task()
MAIN Component
Schedule next task in queue (TOS_schedule_task)
TOS_queue
Post task into queue (TOS_post)
Any Component (e.g. SHELL)
36TinyOS schedulers (Cont.)
Main.c
Initialization
Scheduling policy decides which task is the next
one to be scheduled.
N
Has task?
Y
Schedule next task
Event queue empty?
Y
Update system simulation time to next events time
Events time decides which event is the next one
to be scheduled.
Schedule next event
37TinyOS task
- Some programming concepts
- Macros
- All macros are defined in nest/tos/include/tos.h.
- Function pointer
- Define task
- TOS_TASK(task_name)
- Post task
- TOS_POST_TASK(task_name)
- Call a function implemented in sched.c, TOS_post.
38TinyOS task (Cont.)
In nest/tos/include/tos.h define
TOS_TASK(task_name) void task_name_TASK()
define TOS_POST_TASK(task_name)
TOS_post(task_name_TASK) In nest/tos/system/sch
ed.c char TOS_post (void (tp) ()) // tp
is a function pointer int
TOS_schedule_task() Void
(func)(void) // declare func as a function
pointer func() // use function
pointer to call function
39TinyOS task (Cont.)
TOS_TASK(produce) TOS_POST_TASK(produce)
void produce_TASK() TOS_post(produce_TASK)
40FIFO scheduler
- Data StructureTOS_queue (A circular queue)
typedef struct void (tp) TOS_sched_entry_T
TOS_sched_entry_T TOS_queueMAX_THREADS
null
41FIFO scheduler (Cont.)
- char TOS_post (void (tp) ())
TOS_post(t)
t( )
TOS_sched_free TOS_sched_full 0
TOS_sched_free 1 TOS_sched_full 0
42FIFO scheduler (Cont.)
TOS_schedule_task()
TOS_sched_free 1 TOS_sched_full 0
TOS_sched_free TOS_sched_full 1
43Define two new tasks
- Define and post your own tasks
- Produce
- Consume
- Where
- In your shell component
- The two tasks share a counter that simulates a
number of buffers. - Defined in the data frame of SHELL.
44New way to post tasks
- Extend data structure to store more task
information (e.g. ltTASK_IDgt) - TOS_sched_entry_T
- Add one parameter to pass the task information
in - The macro TOS_POST_TASK
- The function TOS_post
45Extend shell
- More commands
- time
- sched
- sched ltnew_schedulergt
- post lttask_filegt
- ltTASK_IDgt ltTASK_ARRIVAL_TIMEgt ltTASK_NAMEgt
ltTASK_PROPERTYgt - Future task list
- Another clock event for regular check of the
future task list.
46Implement non-preemptive schedulers
- LIFO
- Same data structure with FIFO
- EDF and PRIOIRTY
- Data structure
- Recommend to make use of heap structure which is
already implemented in nest/tos/platform/pc/heap_a
rray. - Consult nest/tos/platform/pc/event_queue. on how
to use the heap.
47Questions
- Check the web site from time to time for any
possible update - Come to my TA office hours
- Post a message in the newsgroup
- Email me at feixie_at_cs.utexas.edu
- http//www.cs.utexas.edu/users/feixie/cs372/projec
t.html