contiki introduction - cea-iot · contiki introduction antonio liñán colina. ... •...

Post on 03-Apr-2018

236 Views

Category:

Documents

12 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Contiki introduction

Antonio Liñán Colina

https://www.arm.com/products/internet-of-things-solutions/mbed-IoT-device-platform.php

RAMROM/FLASH

Processor

> 1MB RAM/ROM

• Architectures: 8-bit, 16-bit, 32-bit• Open Source (source code openly

available)• IPv4/IPv6/Rime networking• Devices with < 8KB RAM• Typical applications < 50KB Flash• Vendor and platform independent• C language• Developed and contributed by Universities,

Research centers and industry contributors• +10 years development

http://www.tado.com

http://www.lifx.comhttp://cetic.github.io/6lbr/

http://www.riva-lighting.de/

http://www.thingsquare.com

www.contiki-os.org

https://github.com/contiki-os/contiki

Install Contiki

http://www.contiki-os.org/start.html

VMWare virtualized develop environment

“INSTANT CONTIKI” VIRTUAL MACHINE

Password is:user

Approx 2-3GbDownload size

Approx 8GbHard Drive

VMWare image built specific for the workshop

“IOT IN FIVE DAYS” VIRTUAL MACHINE

https://sourceforge.net/projects/zolertia/files/VM/IoT%20Workshop%20VM.7z

Password is:user

Approx 2-3GbDownload size

Approx 8GbHard Drive

CONFIGURE THE VIRTUAL MACHINE AS “BRIDGED”

Install requirements (and extra goodies)

Installing Contiki from scratch (Linux)

Get a Contiki working copy:

sudo add-apt-repository ppa:wireshark-dev/stablesudo apt-get updatesudo apt-get install gcc-arm-none-eabi gdb-arm-none-eabisudo apt-get -y install build-essential automake gettextsudo apt-get -y install gcc-arm-none-eabi curl graphviz unzip wgetsudo apt-get -y install gcc gcc-msp430sudo apt-get -y install openjdk-7-jdk openjdk-7-jre antsudo apt-get –y install git git-core wireshark

git clone --recursive https:github.com/alignan/contikicd contikigit submodule –updategit checkout iot-workshop

Install requirements (only the toolchain)

Installing Contiki from scratch (OSX)

Get a Contiki working copy:

# Install Homebrew - http://brew.sh/brew tap PX4/homebrew-px4brew update

# Install GCC Arm Toolchainbrew install gcc-arm-none-eabi-49

git clone --recursive https:github.com/alignan/contikicd contikigit submodule –updategit checkout iot-workshop

Install requirements (only the toolchain)

Installing Contiki from scratch (Windows)

Get a Contiki working copy:

# Download ARM toolchainhttps://launchpad.net/gcc-arm-embedded/+download

# Download and install MINGWhttps://sourceforge.net/projects/mingw/files/latest/download

git clone --recursive https:github.com/alignan/contikicd contikigit submodule –updategit checkout iot-workshop

https://github.com/alignan/IPv6-WSN-book/blob/master/3.asc#windows-installation-re-mote-only

Contiki structure

MCU implementation: MSP430 (Z1), CC2538 (RE-Mote)

Devices implementation (radio, etc): CC2420 (Z1), CC1200 (RE-Mote)

Examples: IPv6, Zolertia-specific, sensors, etc.

Platform specific implementation (Z1, RE-Mote)

Tools (flashing, emulation, visualization)

Contiki core libraries: IPv4/IPv6, MAC, RDC, etc

Applications/Engines that may be used by other applications: MQTT, CoAP

Specific Z1 applications

Specific sensors and actuators drivers

Makefiles (where the platform specifiesthe files to use and include)

Specific Contiki configurationContikiMain Application, Z1 initialization (booting) and application entry point

Specific platform configuration (pin-out, peripherals)

Sensors and actuators ported

Specific Firefly platform files

Specific RE-Mote platform files

Makefiles (where the platform specifies the files to use and include)

Specific Contiki configuration

Main Application, initialization (booting) and application entry point

Specific RE-Mote initialization

Specific RE-Mote Makefile

Specific RE-Mote examples and demos

Specific Z1 examples and demos

IoT workshop content

Zolertia RE-Mote

Zolertia RE-Mote (Zoul inside)

• ARM Cortex-M3, 32MHz, 32KB RAM, 512KB FLASH

• Double Radio: ISM 2.4GHz & 863-925MHz, IEEE 802.15.4-2006/e/g

• Hardware encryption engine and acceleration

• USB programing ready• Real-Time Clock and Calendar• Micro SD slot and RGB colors• Shutdown mode down to 150nA• USB 2.0 port for applications• Built-in LiPo battery charger to work with

energy harvesting and solar panels• On-board RF switch to use both radios over

the same RP-SMA connector• Pads to use an external 2.4GHz over U.Fl

connector, o solder a chip antenna

01-basics

examples/zolertia/tutorial/01-basics

sudo usermod -a -G dialout user

Fixes a problem related to being allowed to write to USB ports as the /dev/ttyUSB0 used to flash the nodes

For the change to take effect, you need to logout and log back inNot required if using the “IoT in five days” virtual image!

VERIFY THE RE-MOTE IS CONNECTED

examples/zolertia/tutorial/01-basics/01-hello-world.c

examples/zolertia/tutorial/01-basics/Makefile

To compile an application:

make TARGET=zoul 01-hello-world

The TARGET is used to tell the compiler which hardware platform itshould compile for. For the Z1 platform is “z1”, for the RE-Mote (and others zoul-based platforms) is “zoul”

You can save the TARGET so next time you don’t have to type it:

make TARGET=zoul savetarget

This will create a Makefile.target file. If no TARGET argument isadded in the compilation command line, the compiler will search a valid target in the Makefile.target file (if exists).

To compile and program an application to a RE-Mote:

make 01-hello-world.upload

The upload argument tells the compiler to invoke the programming scripts required to program the compiled binary to the device.

If no specific PORT argument is given, it will program all the RE-Motes connected over USB.

make 01-hello-world.upload PORT=/dev/ttyUSB0

Programming over the USB is posible as the “bootloader backdoor” is enabled as default in Contiki.

The CP2104 USB to serial converter creates the USB connection, when flashing the image to be programmed is transferred over USB from the Host to the on-board PIC-BSL microcontroller, which automatically puts the RE-Mote in “flashing mode” and transfers the image to the RE-Mote’s CC2538.

The script invoked by the compiler to program the RE-Mote is the cc2538-bsl, located in tools/cc2538-bsl. It is already included in Contiki.

https://github.com/JelmerT/cc2538-bsl

PIC-BSL

CP2104

CC2538

MicroUSBRE-Mote

https://youtu.be/6vg4f7U_kF4

MANUALLY ENTER THE FLASHING MODE

Press and hold user button, then press and hold reset button… release the reset button and then release the user button

To visualize the application output in a readable format, the following command is used:

make login

If no PORT is specified, it will open a connection to the first connected device it finds over USB.

make login PORT=/dev/ttyUSB0

Note you can execute one command after the other:

make 01-hello-world.upload PORT=/dev/ttyUSB0 && make login PORT=/dev/ttyUSB0

The next command is similar to make login, but it appends an UNIX timestamp:

make serialview

If no PORT is specified, it will open a connection to the first connected device it finds over USB.

make serialview PORT=/dev/ttyUSB0

You can also list the connected devices:

make motelist

Buttons and LEDs

Events and actions can be triggered by pressing the user button: senda message over the radio, take a sensor sample, start a process, etc.

The LEDs (light-emitting diodes) help us to understand what happensin the device, by using different colours and blinking sequences weknow when an event is happening, if there are any errors or whathappens in our application.

examples/zolertia/tutorial/01-basics/02-led-and-button.c

examples/zolertia/tutorial/01-basics/02-led-and-button.c

The application will wait (as it will be paused) until this event happens, saving processing cycles and energy

examples/zolertia/tutorial/01-basics/02-led-and-button.c

Press and hold the button, the LED will be on

When the button is released, the LED will be off

platforms/zoul/remote/board.h

examples/zolertia/tutorial/01-basics/02-led-and-button.c

16 in binary is 00010000Same as (1 << 4) The LED on should be the Green:#define LEDS_GREEN (1 << 4) /**< LED1 (Green) -> PD4 */

platforms/zoul/dev/button-sensor.h

make clean

Always after making a change to the source code, clean the previously compiled objects

examples/zolertia/tutorial/01-basics/02-led-and-button.c

What Color the LEDs will show?What number is printed when the LEDs are on?What will happen each time we press the button?

examples/zolertia/tutorial/01-basics/02-led-and-button.c

Each time we press the button, the LED will toggle on and off showing a yellow-ish color

examples/zolertia/tutorial/01-basics/02-led-and-button.c

We are waiting for ANY event to happen

examples/zolertia/tutorial/01-basics/02-led-and-button.c

The press and hold option extends the possible alternatives the application would have to handle the user input

Timers

Timers allow to execute actions periodically, like measuring a sensor periodically, waiting a few seconds before executing a function, sending hourly reports, etc.

examples/zolertia/tutorial/01-basics/03-timers.c

examples/zolertia/tutorial/01-basics/03-timers.c

The timer and stimer are computing-intensive, whereas the etimer, ctimer and rtimer generate events/callbacks only when the counter expires, saving processing cycles and energy

examples/zolertia/tutorial/01-basics/03-timers.c

CLOCK_SECOND is a constant defined by each platform, specifies the number of ticks a second has according to the platform’s clock. If you want to specify a 5 seconds timer, then use (CLOCK_SECOND * 5), or likewise half a second would be the nearest integer to (CLOCK_SECOND / 2)

core/sys/etimer.h

Each timer has its own API Aapplication Programming Interface) or commands, but mostly are alike:

examples/zolertia/tutorial/01-basics/03-timers.c

Depending on the type of timers we need to use, include a given header and create a timer structure

examples/zolertia/tutorial/01-basics/03-timers.c

RTIMER_NOW() is a macro returning the current tick count RTIMER_SECOND is a platform-defined constant, number of ticks/second

examples/zolertia/tutorial/01-basics/03-timers.c

examples/zolertia/tutorial/01-basics/03-timers.c

Print the value of CLOCK_SECOND and RTIMER_SECOND

Turn on only the red LED each time the “tick” counter variable is even, and the blue LED whenever is odd. Use only the ctimer from the example

Stop the timers whenever the user button is pressed

Restart the timer and set the tick variable value to zero when the user button is pressed again

examples/zolertia/tutorial/01-basics/03-timers.c

Processes

Contiki has two execution contexts: cooperative and preemptiveProcesses are cooperative and sequential, interrupts (button, sensorsevents) and the real-timer are preemptive.

examples/zolertia/tutorial/01-basics/04-processes.chttps://github.com/contiki-os/contiki/wiki/Processes

examples/zolertia/tutorial/01-basics/04-processes.chttps://github.com/contiki-os/contiki/wiki/Processes

The process control block (RAM): contains run-time information about the process such as the name of the process, the state of the process, and a pointer to the process thread of the process.

The process thread (ROM): the code of the process

core/sys/process.hhttps://github.com/contiki-os/contiki/wiki/Processes

examples/zolertia/tutorial/01-basics/04-processes.chttps://github.com/contiki-os/contiki/wiki/Processes

Process A

Process B

process_post()

Event Queue

examples/zolertia/tutorial/01-basics/04-processes.chttps://github.com/contiki-os/contiki/wiki/Processes

Process A

Process B

process_post_synch()

examples/zolertia/tutorial/01-basics/04-processes.chttps://github.com/contiki-os/contiki/wiki/Processes

Process B

process_poll()

Interrupt or function

The only function that can be called from preemptive context

examples/zolertia/tutorial/01-basics/04-processes.c

Process 2

Process 1 Process 3process_start()

Increment a counter and toggle green LED

process_post(&process1, …)

When counter is 10 or higher, request termination

1

2 Process 1 Process 3process_exit()

PROCESS_EVENT_EXITED

process_start()

Toggle red LED for 10 seconds and terminate process3

examples/zolertia/tutorial/01-basics/04-processes.c

examples/zolertia/tutorial/01-basics/04-processes.c

examples/zolertia/tutorial/01-basics/04-processes.c

Create two processes:

One that increments a counter and toggles a LED. When the counter reaches ten (10), it should print a message to the screen.

A second process that each time the user button is pressed, it will change the color of the LED to be toggled (i.e from Green to Blue, then Red, and repeat).

examples/zolertia/tutorial/01-basics/04-processes.c

Conclusions

You should be able to:

• Create and compile a basic Contiki application• Program a Zolertia device• Implements timers, LEDs and buttons in your application• Understand how Contiki processes and protothreads are

implemented• Create processes and tasks interacting with each other• Identify connected devices, visualize the debug output in the

screen

Antonio Liñán Colina

Twitter: @4Li6NaN

LinkedIn: Antonio Liñan Colina

github.com/alignan

hackster.io/alinan

alinan@zolertia.comantonio.lignan@gmail.com

top related