Title: Introduction to TinyOS 2.x
1Introduction to TinyOS 2.x
- David Moss
- Rincon Research Corp.
2Installing TinyOS 2.x
- Read the installation tutorials on
- http//docs.tinyos.net
- Windows (using cygwin)
- Linux
- Ubuntu
- Mac OS
- Gentoo
- XubunTOS
3Compiling
- After installing TinyOS, start compiling!
- 1. Go into the Blink application directory
- cd tinyos-2.x/apps/Blink
- 2. Compile the application
- make mica2dot
- Make this application for the Mica2Dot platform
4Compiling
5Compiling
- Output from compiling
- build/mica2dot/main.exe
- build/mica2dot/main.ihex
- build/mica2dot/main.srec
- Next step get it on the microcontroller.
6Installing to a Mica2Dot
- 1. Connect your node to the programmer.
- 2. Plug the programmer into the computer.
- Assuming your programmer is connected to COM1
(ttyS0) - Compile and install
- make mica2dot install mib510,/dev/ttyS0
- Install an application youve previously
compiled - make mica2dot reinstall mib510,/dev/ttyS0
7TinyOS Basics
8TinyOS Features
- Developed for resource constrained wireless
sensor networks - Ultimately not limited by WSNs.
- Features a task scheduler
- Automatically puts the microcontroller to sleep
when there is no code to execute. - Allows for concurrency
- Lets execution be split-phase
- Event-driven operating system
- No shell.
9TinyOS Applications
- A program is a set of components
- Components can be reused
- Components can be replaced
- Components can be hardware or software
10TinyOS Architecture
Main (scheduler)
Application
Sensing
Comms
Other Libraries
Hardware Abstractions (ADC, CLOCK, I2C, LEDS,
PHOTO, UART, SPI)
11Compiling
TinyOS
nesC
GCC
12Component-Oriented Programming
13Component-Oriented Programming
- Programming in nesC / TinyOS is like programming
in a hardware description language.
14Component-Oriented Programming
- Object-Oriented Programming
- Focuses on the relationships between classes that
are combined into one large binary executable - Component-Oriented Programming
- Focuses on interchangeable code modules that work
independently and don't require you to be
familiar with their inner workings to use them.
15Component-Oriented Programming
- 3 file types in TinyOS
- Module (suffixed with P.nc)
- Configuration (suffixed with C.nc)
- Interface (suffixed with .nc)
- Collectively, a Configuration and a Module are
combined to create a Component.
16Component-Oriented Programming
- Modules
- Define functionality that make your application
run. - Provide interfaces to other modules.
- Use interfaces from other modules.
- Dont care whats behind the interface.
- Modules are like the ICs on a circuit board that
actually do something.
17Component-Oriented Programming
- Configurations
- Wire interfaces from several modules together.
- May forward interfaces from external modules to
internal modules. - May contain sub-configurations.
- Configurations are like a Printed Circuit Board,
laying out wiring that connects modules together. - I recommend each Module be wrapped in its own
Configuration to take care of dependencies.
18Component-Oriented Programming
- Interfaces
- Define the interactions between modules
- Commands
- Implemented by the module providing the interface
- Called by the module using the interface
- Events
- Signaled by the module providing the interface
- Captured by the module using the interface
19Hello World
20Component Diagram
HelloC
CONFIGURATION
21HelloC Configuration
- configuration HelloC
-
- implementation
22Component Diagram
HelloP
HelloC
MODULE
23HelloC Configuration
- configuration HelloC
-
- implementation
- components HelloP
24HelloP Module
- module HelloP
-
- implementation
-
25Component Diagram
void myFunctions()
HelloP
HelloC
FUNCTIONALITY
26HelloP Module
- module HelloP
- uses
- interface Boot
- interface Leds
-
-
- implementation
-
27HelloP Module
- module HelloP
- uses
- interface Boot
- interface Leds
-
-
- implementation
-
28Boot Interface
- interface Boot
- event void booted()
29HelloP Module
- module HelloP
- uses
- interface Boot
- interface Leds
-
-
- implementation
- event void Boot.booted()
-
-
USE an interface, CAPTURE all of its events!
30HelloP Module
- module HelloP
- uses
- interface Boot
- interface Leds
-
-
- implementation
- event void Boot.booted()
-
-
31Leds Interface
- interface Leds
- command void led0On()
- command void led0Off()
- command void led0Toggle()
-
- command void set(uint8_t val)
-
32HelloP Module
- module HelloP
- uses
- interface Boot
- interface Leds as MyLeds
- interface Leds as MySecondLeds
-
-
- implementation
- event void Boot.booted()
- call Leds.led0On()
-
-
33Component Diagram
Leds
void myFunctions()
HelloP
HelloC
INTERFACES
34Component Diagram
Leds
void myFunctions()
LedsC
HelloP
MainC
HelloC
35HelloC Configuration
- configuration HelloC
-
- implementation
- components HelloP,
- MainC,
- LedsC,
- SecondLedsC
- // USES -gt PROVIDES
- HelloP.Boot -gt MainC.Boot
- HelloP.MyLeds -gt LedsC
- HelloP.MySecondLeds -gt SecondLedsC
36Component Diagram
PROVIDES
Leds
void myFunctions()
LedsC
HelloP
Boot
USES
MainC
HelloC
37Hello Application
38Hello Application
39TinyOS Task Scheduler
40Task Scheduler
- Tasks are functions
- that are queued up to run at a later time.
41Task Scheduler
- Task Rules
- Tasks do not take or return arguments.
- Tasks cannot preempt other tasks.
- Tasks run to completion.
- Tasks are synchronous
- Interrupts are asynchronous
- All tasks have the same priority.
- Cannot post/queue the same task twice, but a task
can post itself.
42Task Scheduler
- Tasks result in Split-Phase execution
ComponentA
ComponentB
call Interface.doSomething()
return SUCCESS
signal Interface.done()
43Task Scheduler
- command void Scheduler.taskLoop()
- for ()
- uint8_t nextTask
- atomic
- while ((nextTask popTask())
NO_TASK) - call McuSleep.sleep()
-
-
-
- signal TaskBasic.runTasknextTask()
-
44When should you post a task?
- When you have a lot of processing to do that will
take a long time to execute. - When your code can possibly form a recursive loop
in execution. - When you want to use something thats currently
busy (i.e. the radio or flash file system). - When you received an interrupt and need to get
out of async context (hardware implementation
layers)
45Tasks in Algorithms / Recursion
- uint8_t myData // global memory
- task void fft() // prototype
- command error_t FFT.transform(uint8_t data)
- myData data
- post fft()
- return SUCCESS // This split-phase command
will finish later. -
- task void fft()
- // do work.
- if(notDoneYet)
- post fft()
- else
- signal FFT.transformed(transformedData) //
finished the command -
-
-
46Sub-system Polling
- message_t myMsg // Message to send
- task void sendResponse() // prototype
- event message_t Receive.receive(message_t msg,
void payload, uint8_t length) - post sendResponse()
- return msg
-
- task void sendResponse()
- if(call AMSend.send(AM_BROADCAST_ADDR,
- myMsg, sizeof(my_payload_t)) ! SUCCESS)
- post sendMsg()
-
-
-
47TinyOS Communications
48CC1000 Radio
49CC1000 Radio
- Byte-based radio
- 300-1000 MHz
- Your matching network is around 915 MHz
- FSK Modulation
- 7.4 mA Rx Current
- 10.4 mA Tx Current
- 38.4 kbps (manchester encoding)
50Radio Stacks
Your Application
SplitControl
Receive
AMSend
Message Queue
ActiveMessage
CSMA / Acknowledgements
Transmit / Receive / Init
Radio Hardware
51Main Radio Interfaces
- SplitControl
- Provided by ActiveMessageC
- AMSend
- Provided by AMSenderC
- Receive
- Provided by AMReceiverC
52Main Serial Interfaces
- SplitControl
- Provided by SerialActiveMessageC
- AMSend
- Provided by SerialAMSenderC
- Receive
- Provided by SerialAMReceiverC
53Setting up the Radio Configuration
- configuration MyAppC
-
- implementation
- components MyAppP,
- MainC,
- ActiveMessageC,
- new AMSenderC(0), // send an AM type 0
message - new AMReceiverC(0) // receive an AM type 0
message - MyAppP.Boot -gt MainC
- MyAppP.SplitControl -gt ActiveMessageC
- MyAppP.AMSend -gt AMSenderC
- MyAppP.Receiver -gt AMReceiverC
54Setting up the Radio Module
- module MyAppP
- uses
- interface Boot
- interface SplitControl
- interface AMSend
- interface Receive
-
-
- implementation
-
-
55Turn on the Radio
- event void Boot.booted()
- call SplitControl.start()
-
- event void SplitControl.startDone(error_t
error) - post sendMsg()
-
- event void SplitControl.stopDone(error_t error)
-
56Send Messages
- message_t myMsg
- task void sendMsg()
- if(call AMSend.send(AM_BROADCAST_ADDR,
- myMsg, 0) ! SUCCESS)
- post sendMsg()
-
-
- event void AMSend.sendDone(message_t msg,
- error_t error)
- post sendMsg()
57Receive a Message
- event message_t Receive.receive(message_t msg,
void - payload, uint8_t length)
- call Leds.led0Toggle()
- return msg
58Payloads
- A message consists of
- Header
- Payload
- Optional Footer
59message_t
- typedef nx_struct message_t
- nx_uint8_t headersizeof(message_header_t)
- nx_uint8_t dataTOSH_DATA_LENGTH
- nx_uint8_t footersizeof(message_footer_t)
- nx_uint8_t metadatasizeof(message_metadata_t)
- message_t
60Payloads Use Network Types
- (MyPayload.h)
- ifndef MYPAYLOAD_H
- define MYPAYLOAD_H
- typedef nx_struct MyPayload
- nx_uint8_t count
- MyPayload
- enum
- AM_MYPAYLOAD 0x50,
-
- endif
61Example Filling out a Payload
- void createMsg()
- MyPayload payload (MyPayload ) call
AMSend.getPayload(myMsg) - payload-gtcount (myCount)
- post sendMsg()
62Example Receiving a Payload
- event void Receive.receive(message_t msg, void
payload, uint8_t len) - MyPayload payload (MyPayload ) payload
- call Leds.set(payload-gtcount)
- signal RemoteCount.receivedCount(payload-gtcount)
- return msg
-
63Learn More
- http//docs.tinyos.net
- Stuff we havent covered yet
- -gt vs.
- Timers
- Parameterized Interfaces
- Generic configurations
- Generic modules
- unique() and uniqueCount()
- Interfacing with a computer
- Channels / Groups
- Tinyos-2.x-contrib
- Unit Testing