6LoWPAN: The Wireless Embedded Internet Companion Exercise Slides Zach Shelby, Martti Huttunen This work is licensed under the Creative Commons Attribution- Noncommercial-Share Alike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA Figures on slides with book symbol from 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann, ISBN: 978-0-470-74799-5, (c) 2009 John Wiley & Sons Ltd
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
6LoWPAN: The Wireless Embedded Internet
Companion Exercise Slides
Zach Shelby, Martti Huttunen
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA
Figures on slides with book symbol from 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann, ISBN: 978-0-470-74799-5, (c) 2009 John Wiley & Sons Ltd
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 8
Microcontroller
• Main processing units of embedded devices
• Special purpose and highly integrated– Integrated RAM, ROM, I/O, peripherals– Extremely good power to performance ratio– Cheap, typically 0.25 - 10.00 USD
• Executes programs including embedded system control, measurement & communications– Usually time-critical requiring guarantees– Real-time performance a common requirement
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 17
Example: CC2420
• IEEE 802.15.4 compliant radio
• 2.4 GHz band using DSSS at 250 kbps
• Integrated voltage regulator
• Integrated digital baseband and MAC functions– Clear channel assessment– Energy detection (RSSI)– Synchronization– Framing– Encryption/authentication– Retransmission (CSMA)
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 20
Power Consumption
A simple approximation for power consumption:
= Time that takes to go from sleep state to awake state= Transmitter setup time, i.e. time it takes for the transmitter to be ready= Time in the Tx state= Receiver setup time, i.e. time it takes for the receiver to be ready= Time in the Rx state= Time in the idle state= Time in the sleep state= Average number of times per frame that the transmitter is used= Average number of times per frame that the receiver is used= Duration of the time frame= Power used in the Tx state= Power used in the Rx state= Power used in the idle state= Power used in the sleep state= Average power used by the transceiver
€
Pavg =1
TFPRxTwk− up + PRx(NTxTTx− up + NRxTRx− up ) + PTxTTx + PRxTRx + PidleTidle + PsleepTsleep{ }
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 21
Sensors & Actuators
• Sensors measure real-world phenomena and convert them to electrical form– Analogue sensors require an ADC– Digital sensors use e.g. I2C or SPI interfaces– Human interface can also be a sensor (button)
• IEEE 1451 standard becoming important– Defines standard interfaces and auto-configuration – Also some protocol specifications
• Actuators convert an electrical signal to some action– Analogue and digital interfaces both common– A motor servo is a good example
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 24
Real-time Issues
• Wireless embedded systems usually are real-time– Watch, robot, building sensor, control node
• A RTOS only facilitates real-time system creation– Still requires correct software development
• RTOS is not necessarily high performance– Can meet general system deadlines (soft real-time)– or deterministically (hard real-time)
• Deadlines can be met using – Specialized pre-emptive scheduling algorithms– Proper inter-task design & communication – Semaphores and queues to avoid racing
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 28
OS Examples
• Example embedded operating systems– Contiki (www.sics.se/~adam/contiki)– FreeRTOS (www.freertos.org)– TinyOS (www.tinyos.org)– and thousands of others...
• For higher powered MCUs (e.g. ARMs)– VX Works– Microcontroller Linux (Android, Maemo etc.)– Windows CE– Symbian
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 31
Embedded Development
• Software resides in system non-volatile memory– External or internal flash/eeprom/prom memories– Modern microcontrollers are capable of writing the internal flash
memory run-time and often do not have an external memory bus
• Development is done outside the system
• Cross-compilers are used to create binary files– Cross-compiler creates binary files for a different architecture than
it is running on– Various commercial and free compilers available
• System is programmed by uploading the binary– In-system programming tools or external flashers
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 32
Cross-compiler Environments
• Integrated development environments (IDEs)– Commercial compilers are usually of this type– Usually dependent on a specific OS (Windows)– Integrate a text editor, compiler tools and project management
along with C library– System programmer tool usually tightly integrated– Also open-source IDEs available
• Open-source IDEs usually employ “plugin” architecture
• General-purpose extensible environments
• Include scripting tools for running any command line tools: compilers, linkers, external editors and programmers
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 33
Cross-compiler Environments
• Command line utilities– Separate compiler/linker, editor and project management tools,
architecture-dependent C library
• Project management: make– make is an automated software building tool– Based on target-dependency-operation style blocks– Allows use of project templates and separate platform build rules
by using “include files”– Most common way of managing open-source software projects– automake and autoconf tools extend functionality to platform-
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 34
Cross-compiler Environments
• Command line compilers– most common is gcc: available for a multitude of microcontroller
and -processor architectures– sdcc: Small Device C Compiler: PICs, 8051's etc.– single-architecture compilers
• System programming tools– usually specific to a single microcontroller family– vary greatly in their ease of use and interface type– most require some sort of programming cable or a programmer
device to upload software– dependent on the microcontroller programming algorithm
• standard buses (SPI, UART, JTAG) vs. proprietary buses
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 35
Cross-compiler Environments
• Command-line tools vs. (commercial) IDEs– IDEs are easily accessible: single installer, single GUI– Commercial IDEs vary greatly in usability, standards compliance
and are (usually) tied to a single architecture -> bad portability– Most commercial IDEs don't really support templates
• Programmer must go through various dialogs to create a new project• Often project files can not just be copied (contain directory paths and
such) and may be binary format
– Command line tools have a steeper learning curve• Once learned, applicable to most architectures• Higher flexibility and ease of duplicating projects
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 50
Contiki processes
• Contiki core is event-driven– Interrupts and HW drivers generate events– Events are dispatched to event handlers by the Contiki core– Event handlers must return control to core as soon as possible– Co-operative multitasking
• Basic processes are implemented using protothreads– Easier to create sequential operations– An abstraction to avoid complex state-machine programming
• In more complex applications, the amount of states may be huge
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 52
Contiki processes
• Contiki core is event-driven– Interrupts and HW drivers generate events– Events are dispatched to event handlers by the Contiki core– Event handlers must return control to core as soon as possible– Co-operative multitasking
• Basic processes are implemented using protothreads– Easier to create sequential operations– An abstraction to avoid complex state-machine programming
• In more complex applications, the amount of states may be huge
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 57
Contiki Protocol Stacks
• Contiki has 2 different protocol stacks: uIP and Rime
• uIP provides a full TCP/IP stack– For interfaces that allow protocol overhead– Ethernet devices– Serial line IP– Includes IPv4 and IPv6/6LoWPAN support
• Rime provides compressed header support– Application may use MAC layer only
• Protocol stacks may be interconnected– uIP data can be transmitted over Rime and vice versa
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 60
Rime: Sending
• Sending broadcast data using RimeDeclarations:static struct broadcast_conn bc;In main process:packetbuf_copyfrom("Hello everyone", 14);broadcast_send(&bc);
• Sending unicast data using RimeDeclarations:static struct unicast_conn uc;In your function:rimeaddr_t *addr;addr.u8[0] = first_address_byte;addr.u8[1] = second_address_byte;packetbuf_copyfrom("Hello you", 9);unicast_send(&uc, &addr);
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 61
Creating Contiki Ports
• First step: see if your cpu already has code– If yes, configure your platform to use it– If not, see other cpu directories for implementation models
• Second step: see if your hardware is close to other platforms– If yes, copy code from example platform and modify– If not, see other platforms for minimal model
• Create a test application– Start with LEDs in platform/myplatform/contiki-myplatform-
main.c– Use for loops to make sure that your compiler works– Continue by adding printf's to see if your UART works
• First real application– Create an etimer for your test process: flash LEDs, print info– Try different timeouts to see if your clocks are correct
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 64
Contiki Advanced
• Use an etimer to set a timer event for your own process after a certain period (for example to flash a LED).
• Make two processes, and use process events between them.
• Make a Contiki application to read the NanoSensor accelerometer and light sensors using the ADC on the CC2430. Convert the values using the sensor data sheets and print them out periodically.
• Make Contiki sensor libraries for the NanoSensor buttons and sensors (using code from above) and use them to print events.
v4.3.2010 6LoWPAN: The Wireless Embedded Internet, Shelby & Bormann 65
Contiki uIP
• Make an application which uses uIP protosockets, and spawns protothreads to handle both UDP and TCP sockets.
• Make a client application and a server application which communicate between each other over UDP.
• Design a publish/subscribe protocol implementation over UDP/6LoWPAN using the MQTT-S specification (simplified). Implement Publisher, Subscriber and Broker applications.
• Use MQTT-S to publish and subscribe to NanoSensor buttons and sensor values.