Home  >  Edison  >  Current State

MRAA and UPM

MRAA and UPM are libraries that simplify the development of applications that interface with sensors. Documentation can be found here.

The following sections are deprecated and kept for historical reason only. You may want to jump to LibIIO now.

MRAA

MRAA 2.2.0 is available in Yocto Honister with support for C/C++ and additional packages are built to support python3 and nodejs.

For manual installation use:

dpkg -i node-libmraa.deb

or

dpkg -i python3-libmraa.deb

Alternatively, add these packages to edison-image.bb for automatic installation into you image.

HSU

High speed uart (HSU) works.

I2C

I2C works see example below.

GPIO

Settings GPIO’s works with the non-ACPI image only.

Selecting a pinmux on vanilla linux is supposed to be done by devicetree or ACPI, which has been disabled that in mraa for Linux kernels > 4.0. Currently we have I2C, SPI and HSU selectable configurations available in the form of ACPI tables. For the non-ACPI image, I2C and HSU pinmuxes are preset in the platform code in the kernel.

UPM

UPM 2.0 is available in Yocto Honister with support for C/C++ and additional packages are built to support python3 and nodejs.

For manual installation use:

dpkg -i node-upm.deb

or

dpkg -i python3-upm.deb

Alternatively, add these packages to edison-image.bb for automatic installation into you image.

Example using UPM and MRAA on the TSL2561

The UPM documentation is very comprehensive, but does not give you a walk through on how to start. In the following we hope to provide step-by-step instructions for a sample sensor, the TSL2561, which is a simple I2C based light sensor.

C example building on the Edison

In the UPM repository you can find the following sample code:

//Modified: Abhishek Malik <abhishek.malik@intel.com>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "tsl2561.h"

#include "upm_utilities.h"

#define TSL2561_Address 0x39

int main()
{
    tsl2561_context dev = tsl2561_init(6, TSL2561_Address, GAIN_0X, INTEGRATION_TIME1_101MS);
    float abc = 0;
    if(tsl2561_get_lux(dev, &abc) != UPM_SUCCESS){
        printf("ERROR !! ERROR !! ERROR!!");
    }
    printf("value retrieved: %f\n", abc);

    return 0;
}

We need to modify the bus to i2c-6 and the address to 0x39 for this example to work.

On Edison build using:

gcc -I /usr/include/upm/ -lupmc-tsl2561  tsl2561.c

Then run

root@edison:~# ./a.out 
value retrieved: 12.000000

Nodejs example on the Edison

On Edison we need to make the tsl2561 module known to node:

npm install /usr/lib/node_modules/jsupm_tsl2561/

The example code needs similar modification as the C version.

/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var digitalLightSensor = require('jsupm_tsl2561');

// Instantiate a digital light sensor TSL2561 on I2C
var myDigitalLightSensor = new digitalLightSensor.TSL2561(6, 57, 0, 1);


setInterval(function()
{
	console.log("Light value is " + myDigitalLightSensor.getLux());
}, 1000);

// Print message when exiting
process.on('SIGINT', function()
{
	console.log("Exiting...");
	process.exit(0);
});

Then run:

root@edison:~# node tsl2561.js
Light value is 4712
Light value is 4715
Light value is 4661
Light value is 3546
Light value is 509
Light value is 208
Light value is 76
Light value is 118
Light value is 3919

Python3 example on Edison

The example code needs similar modification as the C version.

#!/usr/bin/env python
# Author: Zion Orent <zorent@ics.com>
# Copyright (c) 2015 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_tsl2561 as upmTsl2561

def main():
    # Instantiate a digital light sensor TSL2561 on I2C
    myDigitalLightSensor = upmTsl2561.TSL2561(6, 57, 0, 1)

    ## Exit handlers ##
    # This function stops python from printing a stacktrace when you hit control-C
    def SIGINTHandler(signum, frame):
        raise SystemExit

    # This function lets you run code on exit, including functions from myDigitalLightSensor
    def exitHandler():
        print("Exiting")
        sys.exit(0)

    # Register exit handlers
    atexit.register(exitHandler)
    signal.signal(signal.SIGINT, SIGINTHandler)

    while(1):
        print("Light value is " + str(myDigitalLightSensor.getLux()))
        time.sleep(1)

if __name__ == '__main__':
    main()

Then run:

root@edison:~# python3 tsl2561.py 
Light value is 109
Light value is 61
Light value is 17165
Light value is 25085
Light value is 21554
Light value is 101
Light value is 177
Light value is 110

C example building with the SDK

You need to prepare the working directory for the SDK.

After this, put the C source example along with a suitable make file.

OBJECTS = 

ifdef DEBUG
  OPT_FLAGS= -g -O0
else
  OPT_FLAGS=-O3 -DNDEBUG -flto
endif
INCDIRS = -I=/usr/include/upm/
WARNING_FLAGS=-Wall -Wextra -Wno-sign-compare 
CXXFLAGS+= $(WARNING_FLAGS) $(OPT_FLAGS) $(INCDIRS)
CFLAGS+= $(WARNING_FLAGS) $(OPT_FLAGS) $(INCDIRS)

BINARIES=tsl2561
all: $(BINARIES)

tsl2561: tsl2561.o $(OBJECTS)
	$(CXX) -o $@ $^ $(CXXFLAGS) -lupmc-tsl2561
	
clean:
	$(RM) $(BINARIES) *.o

%.o: %.c
	$(CC) $(CFLAGS) -o $@ -c $<
	
%.o: %.cc
	$(CXX) $(CXXFLAGS) -o $@ -c $<

and run make all.

The copy tsl2561 to Edison and:

root@edison:~# ./tsl2561
value retrieved: 0.000000
root@edison:~# ./tsl2561
value retrieved: 14.000000
root@edison:~# ./tsl2561
value retrieved: 1523.000000