Arduino

Arduino in RIOT.

About

This module enables users to run unmodified Arduino sketches in RIOT. For this we aim at supporting the full Arduino API.

The support of the Arduino API in RIOT is useful for multiple reasons:

  • starting point for beginners
  • run your existing sketches on any non-Arduino hardware supported by RIOT
  • makes it easy to move from Arduino to RIOT
  • use Arduino device drivers in RIOT
  • is fun to implement :-)

Refer to Arduino API for the actual API documentation

General usage

To run your Arduino sketch in RIOT, just follow these steps:

  1. create an empty application
  2. add the arduino module to your application, your Makefile should now look something like this:
    1
    2
    3
    4
    5
    6
    7
    APPLICATION = YOUR_APP_NAME
    BOARD ?= YOUR_TARGET_PLATFORM
    RIOTBASE ?= PATH_TO_RIOT_ROOT
    
    USEMODULE += arduino
    
    include $(RIOTBASE)/Makefile.include
    
  3. copy your Arduino sktech(es) into your application folder. Currently they must have the file ending *.sketch to be processed.
  4. build, flash, and run your application the usual RIOT-way: simply call make all, make flash, make term, etc.

Thats all. As bonus you can of course use any existing RIOT code inside your Arduino sketches - you simply have to add the includes to your sketch and the corresponding modules to your Makefile.

Note

So far, all Arduino sketches MUST have the file ending *.sketch to be recognized by RIOT’s build system

Concept

For enabling RIOT to run Arduino sketches, we extended the build system to handle *.sketch files and we implemented the Arduino API using RIOT’s native functions.

Extension of the build system

Building Arduino sketches in RIOT is done in a two step process.

First, the make system calls a dedicated Arduino build script, which is called from the Makefile.include of the RIOT Arduino module.

This script creates a temporary file called ‘_sketches.cpp’ inside the application folder. Into this file, the script copies some Arduino glue code ( pre.snip and post.snip) together with the contents of all *.sketch files contained in the application folder.

Second, the RIOT make system is called as usual, processing the temporary file containing all the Arduino code. Simple :-)

Implementation of the Arduino API

For supporting the Arduino API, we have created our own function and class definitions, using the exact same signatures as in the original Arduino header files. These headers are then implemented using standard RIOT APIs, e.g. the peripheral drivers, xtimer, etc.

Add Arduino support to a board

Note

As prerequisite, the board must have support for C++.

To add Arduino support to a board, it has to provide the following:

In RIOT/board/BOARD/include/arduino_board.h:

  • a mapping of GPIO pins to Arduino pin numbers named arduino_pinmap, e.g.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    static const gpio_t arduino_pinmap[] = {
        GPIO_PIN(PORT_D, 12),
        GPIO_PIN(PORT_D, 13),
        GPIO_PIN(PORT_D, 14),
        GPIO_PIN(PORT_D, 15),
        GPIO_PIN(PORT_A, 12),
        GPIO_PIN(PORT_A, 15),
        GPIO_PIN(PORT_B, 1),
        GPIO_PIN(PORT_B, 2),
        ...
    };
    
  • a mapping of ADC lines to Arduino analogs pins named arduino_analog_map, e.g.
    1
    2
    3
    4
    5
    6
    static const adc_t arduino_analog_map[] = {
        ADC_LINE(3),
        ADC_LINE(2),
        ADC_LINE(1),
        ...
    };
    
  • a define ARDUINO_LED that is mapped to an Arduino pin number connected to any on-board LED, or to pin 0 in case no LED is defined:
    1
    #define ARDUINO_LED         (2)
    
    This links to the third entry in the arduino_pinmap array.
  • a define ARDUINO_UART_DEV that defines the UART to use as the Arduino primary serial port (default uart.h::UART_DEV):
    1
    #define ARDUINO_UART_DEV         (UART_DEV(3))
    

In addition, you have to add the ‘arduino’ feature to the board. For this, just add FEATURES_PROVIDED += arduino to the ‘other features’ section in your board’s `Makefile.features’.

That’s it, your board can now run Ardunio sketches.

Open issues

Make it possible to bootstrap Arduino code manually from any RIOT application. Include a pseudomule as e.g. arduino_base, which does not implement a main function calling setup() and loop(), so these functions have to be called manually from a RIOT application.

Implement analog outputs (PWM mapping)

Implement analog inputs (ADC mapping)

Implement SPI interface class

Add support for the ‘Wire Library’ (I2C)

Add means to include various Arduino Libraries (maybe as pkg?)

Implement anything else that is missing…

Adapt Arduino build script, so sketches do not have to have the file ending *.sketch anymore