Wednesday, April 13, 2016

Writing an application for RIOT OS

An application that runs on RIOT OS should have at least 2 files. The first thing is a Makefile in which we set various variables in order to do the compilation process properly. The second thing is a C program with a main function. Let's go ahead and write a new hello world program for RIOT OS. There's no need to mention that we should have downloaded RIOT OS source code first. It should be saved in somewhere in my file system.

We start creating our program by first creating a new directory for our new program. We can do that anywhere in your file system and I will have it helloworld. Inside it, create a Makefile. The easiest way to do is copying the sample Makefile provided to us by RIOT OS.

mkdir helloworld
cd helloworld
cp ../../RIOT/dist/Makefile ./

Now, edit the Makefile to have a content like the following. We basically changed the application name, modified the path to RIOT OS source code and uncommented few lines.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
####
#### Sample Makefile for building applications with the RIOT OS
####
#### The example file system layout is:
#### ./application Makefile
#### ../../RIOT
####

# Set the name of your application:
APPLICATION = helloworld

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../RIOT

# Uncomment this to enable scheduler statistics for ps:
CFLAGS += -DSCHEDSTATISTICS

# If you want to use native with valgrind, you should recompile native
# with the target all-valgrind instead of all:
# make -B clean all-valgrind

# Uncomment this to enable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
CFLAGS += -DDEVELHELP

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# Modules to include:

USEMODULE += shell
USEMODULE += posix
USEMODULE += xtimer

# If your application is very simple and doesn't use modules that use
# messaging, it can be disabled to save some memory:

#DISABLE_MODULE += core_msg

#export INCLUDES += -Iapplication_include

# Specify custom dependencies for your application here ...
# APPDEPS = app_data.h config.h

include $(RIOTBASE)/Makefile.include

# ... and define them here (after including Makefile.include,
# otherwise you modify the standard target):
#proj_data.h: script.py data.tar.gz
# ./script.py


It's time to add our C program with the main function. Following sample code shows my C program saved with the name main.c inside the same helloworld directory.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include<stdio.h>
#include "xtimer.h"

int main(void)
{
    puts("Starting helloworld application!");
    xtimer_usleep(SEC_IN_USEC);
    puts("returning from the application!");

    return 0;
}

Now we can compile and run our simple program to see how it works. We can do that by first issuing the command make inside the helloworld directory. The executable ELF file will be available inside the same directory but deep inside few other child directories. We can run the program as follows.

./bin/native/helloworld.elf 

It should show the output of our program and hangup. To terminate the execution of RIOT system, we have to give a Ctrl+C.


No comments:

Post a Comment