User Tools

Site Tools


lab7

Contiki OS and Sparrow

Contiki considers itself the “The Operating System for Connecting the Next Billion Devices - the Internet of Things.” It's an Internet-connected multi-tasking OS for low-end platforms, like Microcontrollers. Contiki is great for the amazing level of connected functionality it will squeeze out of a five dollar chip. It runs on a vast array of platforms and CPU's.

Installing Contiki

Toolchain

There are two approaches to the toolchain. One is to install all the necessary tools natively on your machine. The other is to get instant-contiki, which is a VM image that works on Windows, Mac or Linux.

Download Contiki

You need to download a special fork of Contiki to try these changes out. Open a Terminal, and then:

$ cd ~ 
$ git clone https://github.com/narcisaam/contiki-sparrow.git -b sparrow 
$ cd contiki-sparrow/platform/sparrow

If Contiki has a problem with the AVR tools, you will need to follow these steps. It ships with an old version of the tools which does not support the Atmega128rfa1 found on the Sparrow, and then sets those as the default AVR tools even though the tools that ship with this version of Ubuntu are newer.

Open a Terminal, and type these:

$ sed -i '/avr/d' /home/user/.profile 
$ sudo aptitude update 
$ sudo apt-get install avr-libc gcc-avr binutils-avr avrdude git libc6-i386

Then log out of the machine and back in again (or restart it).

Setting Up

Default avrdude port

Connect the Sparrow board to your computer through USB. Once this is done, you'll likely see a new ttyUSB port appear in /dev. In the folder /contiki-sparrow/platform/sparrow edit Makefile.sparrow, and change the default avrdude port to match your local settings. This only needs to be done once ever. From now on in this tutorial we'll assume the board is connected to ttyUSB0, but you can modify according to your local settings.

 AVRDUDE_PORT ?= /dev/ttyUSB0 

MAC Address / Node Number

Each node needs a unique MAC address. The sparrow platform expects to find the MAC address as the first 8 bytes in EEPROM.

There is a special program just to program the MAC address. In the /contiki-sparrow/platform/sparrow folder, check up tools/set-eeprom/set-eeprom.c - it should look like this:

typedef unsigned int uint8_t __attribute__((__mode__(__QI__)));
uint8_t rf_channel[2] __attribute__((section(".eeprom"))) = {26, ~26};
uint8_t mac_address[8] __attribute__((section(".eeprom"))) = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, NODE};

It uses the same first 7 bytes, and lets you choose a different number for the last. From this point forward, we will refer to that last digit of the MAC address as the “Node number”. To assign a node number to your node:

$ cd tools/set-eeprom 
$ make NODE=3 AVRDUDE_PORT=/dev/ttyUSB0 

Hello World

Go back in your terminal to /contiki-sparrow/platform/sparrow and type…

$cd tests/hello-world 
$make upload 
$make login 

Working correctly, you should see this:

*******Booting Contiki 2.5*******
MAC address 2:11:22:ff:fe:33:44:3
nullmac sicslowmac, channel 26, check rate 65535 Hz
Routing Enabled
Autostart other processes

IP addresses [4 max]
fdfd::3
fe80::11:22ff:fe33:4403
Hello, world

Ping

Now that we know the node itself is working and printing OK, we want to make sure two nodes can talk. The best way to do this is with a ping.

This requires two nodes. If you already have hello-world on one node, the easiest thing to do is to put ping-ipv6 on a second node, and ping the other. So for this example, say we already have hello-world working on node 3 on /dev/ttyUSB0.

We'll connect a second node to the computer, and presume it's connected to /dev/ttyUSB1 and it's already been configured with node address 1. For this node, we'll need to compile and upload the ping-ipv6 app and specify the node we'll want to ping.

$ cd tests/ping-ipv6 
$ make upload AVRDUDE_PORT=/dev/ttyUSB1 NODE=3 
$ make login AVRDUDE_PORT=/dev/ttyUSB1 

Note that you have to pass the node number of the node you want to ping (NODE=3 in this example). Working correctly, you will see this:

connecting to /dev/ttyUSB1 (57600) [OK] Power-on reset. External reset! 
*******Booting Contiki 2.5******* 
MAC address 2:11:22:ff:fe:33:44:1 nullmac sicslowmac, channel 26 
In Process PING6 Wait for DAD IP addresses [4] 
fdfd::1 fe80::11:22ff:fe33:4401 
Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Echo reply received. Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Echo reply received. Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Echo reply received. 
Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Echo reply received. Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Echo reply received. END PING6 

UDP

Finally, we will want to be sure that two nodes can exchange real application data between them. We can do so with the UDP client/server test. Again, this requires two nodes. One node contains the server program, the other contains the client. When making the client, we need to tell it where to find the server by specifying NODE= the Node# of the server.

$ cd tests/udp-ipv6 
$ make NODE=3 -j10 
$ make udp-client.sparrow.u AVRDUDE_PORT=/dev/ttyUSB1 NODE=3 
$ make udp-server.sparrow.u login AVRDUDE_PORT=/dev/ttyUSB0 

Working correctly, you'll see something like the following. The exact numbers will depend on the timing of when you do the 'make login'.

 connecting to /dev/ttyUSB0 (57600) [OK] 
Power-on reset. External reset! 
*******Booting Contiki 2.5******* 
MAC address 2:11:22:ff:fe:33:44:3 nullmac sicslowmac, channel 26 
IP addresses [4] fdfd::3 fe80::11:22ff:fe33:4403 
UDP server started Server IPv6 addresses: fdfd::3 fe80::11:22ff:fe33:4403 
+READY Server received: 'Hello 2 from the client' from fdfd::1 
Responding with message: Hello from the server! (1) 
Server received: 'Hello 3 from the client' from fdfd::1 
Responding with message: Hello from the server! (2) 
Server received: 'Hello 4 from the client' from fdfd::1 
Responding with message: Hello from the server! (3) 
Server received: 'Hello 5 from the client' from fdfd::1 
Responding with message: Hello from the server! (4) 
Server received: 'Hello 6 from the client' from fdfd::1 
Responding with message: Hello from the server! (5) 
Server received: 'Hello 7 from the client' from fdfd::1 +OK PASS 

The Shell

The shell is a very powerful feature of Contiki. Within this OS lies a fully-featured command shell. The shell in the sparrow platform is a subset of the full Contiki shell, including only the shell modules which have been proven to work on this platform.

Building

$ cd tests/shell 
$ make upload 
$ make login

Running

If successful, you'll see this:

connecting to /dev/ttyUSB1 (57600) [OK] 
Power-on reset. External reset! 
*******Booting Contiki 2.5******* 
MAC address 2:11:22:ff:fe:33:44:1 nullmac sicslowmac, channel 26 
Contiki command shell 
Type '?' and return for help 68.1: 
Contiki> IP addresses [4] fdfd::1 fe80::11:22ff:fe33:4401 

help

Now, try some of the commands, like 'help':

Available commands: 
?: shows this help 
binprint: print binary data in decimal format 
blink [num]: blink LEDs ([num] times) 
echo <text>: print <text> 
exit: exit shell 
hd: print binary data in hexadecimal format 
help: shows this help 
kill <command>: stop a specific command 
killall: stop all running commands 
netstat: show UDP and TCP connections 
null: discard input 
ping <host>: ping an IP host 
ps: list all running processes 
quit: exit shell 
randwait <maxtime> <command>: wait for a random time before running a command 
repeat <num> <time> <command>: run a command every <time> seconds 
size: print the size of the input 
time [seconds]: output time in binary format, or set time in seconds since 1970 
timestamp: prepend a timestamp to data 
68.1: Contiki> 

Try “blink 100 &” to start blinking the LED's for 100 times. Notice the '&' at the end of that! You should now see Sparrow's LED flash on and off repeatedly.

Then type “kill blink” when you're done admiring your blinking lights.

ping

We can also ping from within the shell!

68.1: Contiki> ping fdfd::3 SEND 13 bytes ping fdfd::3 
Sending Echo Request to fdfd:0000:0000:0000:0000:0000:0000:0003 
from fdfd:0000:0000:0000:0000:0000:0000:0001 
Other ICMP6 message received. 
Echo reply received. 
lab7.txt · Last modified: 2018/12/23 15:19 by dan.tudose