libmraa - Low Level Skeleton Library for Communication on GNU/Linux platforms Libmraa is a C/C++ library with bindings to Python, Java script and Java to interface with the I/O on ruggedBoard, Galileo, Edison & other platforms, with a structured and sane API where port names/numbering matches the board that you are on. Use of libmraa does not tie you to specific hardware with board detection done at runtime you can create portable code that will work across the supported platforms.
The intent is to make it easier for developers and sensor manufacturers to map their sensors & actuators on top of supported hardware and to allow control of low-level communication protocol by high level languages & constructs.
Fig: CODE FLOW
GPIO
UART
I2C
SPI
LibMraa code flow: Code flow starts from arm.c file. In arm.c file, it calls different boards. In arm.c file we have to register our board information, like hardware name and board name. Example: rugged
These interfaces allow you to interact with all libmraa functionality. The C++ classes directly wrap the C API and provide a near 1:1 mapping of functionality
RB Interface/Bus Programming using MRAA
MRAA is a library from Intel that simplifies the logic for connecting to different sensor pins.
Libmraa is a C/C++ library with bindings to Python, Javascript and Java to interface with the I/O on X86, ARM & other platforms.
The intent is to make it easier for developers and sensor manufacturers to map their sensors & actuators on top of supported hardware and to allow control of low level communication protocol by high level languages & constructs
Python Programming using MRAA
1. Toggle three GPIO's
Description:
Here is the simplest OnBoard-Gpio's program in mraa,to toggle three GPIO pins – 61,62,63
Required Hardware:
a5d2x-rugged board
USB cable
Step-by-step guide
Connect the Rugged board to your system
Boot the board with SD card/NOR.
1. Test On-board LEDs – gpio_blink.py
go to data folder after booting the board and copy the below python code.
Run the above code with below command in rugged board.
root@ruggedboard-a5d2x:/data#python3pwm.py
Expected Output :
root@ruggedboard-a5d2x:/data#python3pwm.pyrandom:python3:uninitializedurandomread (24 bytesread)libmraa[125]:libmraaversionv2.0.0initialisedbyuser'root'withEUID0libmraa[125]:gpio:platformdoesn't support chardev, falling back to sysfslibmraa[125]: libmraa initialised for platform 'AtmelSAMA5' of type 200.0100000.0200000.0300000.0400000.0499950.0599950.0699950.0799950.0899950.0999950.1099950.1199950.1299950.1399950.1499950.1600000.1700000.1800000.1900000.2000000.2100000.220000
3. Aio
Description
Connect a pot(potentiometer) to rugged board a5d2x.
Required Hardware
a5d2x-rugged board
USB cable
potentiometer
Step-by-step guide
Connect the Pot (it will have three patch codes(wires), center one connect to "AN" (analog pin) of mikro bus connector.
The remaining two you can connect one to ground (pin 60) & one to vcc (pin 01) on expansion header respectively.
importmraaprint(mraa.getVersion())try:# initialise AIOx=mraa.Aio(6)# read integer valueprint(x.read())# read float valueprint("%.5f"%x.readFloat())except:print("Are you sure you have an ADC?")
Execution :
Run the above code with below command in rugged board
To test the SPI functionality in Kernel with loop-back test.
Required Hardware
a5d2x-rugged board
USB cable
patch cords
Step-by-step guide
Boot the board from MMC. After booting connect the one end of patch card to MOSI pin and other end to MISO pin.
Copy the spi.py python code in data directory of your board.
Run the below python code.
importmraaasmimportrandomasrandimportarray# intialise SPIdev=m.Spi(0)for x inrange(0,100):txbuf=bytearray(4)for y inrange(0,4):txbuf[y]=rand.randrange(0,256)# send and receive data through SPIrxbuf=dev.write(txbuf)ifrxbuf!=txbuf:print("Data mismatch!")exit(1)print(rxbuf)
Execution
Open spi.py program given below in your board terminal.
uninitializedurandomread (24 bytesread)libmraa[135]:libmraaversionv2.0.0initialisedbyuser'root'withEUID0libmraa[135]:gpio:platformdoesn't support chardev, falling back to sysfslibmraa[135]: libmraa initialised for platform 'AtmelSAMA5' of type 20random: python3: uninitialized urandom read (2500 bytes read)Data mismatch!
C Programming using MRAA
1) GPIO
Description
Toggling of 2 user LED's on a5d2x-rugged board.
Required Hardware
a5d2x-rugged board
USB cable
Step-by-step guide
Open the below gpio.c source file in your host Terminal.
follow the procedure given below the gpio.c file.
1. Test On-board LED's – gpio.c
Open the gpio.c program in your host terminal as given below.
/* standard headers */#include<signal.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>/* mraa header */#include"mraa/gpio.h"/* gpio declaration */#defineGPIO_PIN_161#defineGPIO_PIN_262volatilesig_atomic_t flag =1;voidsig_handler(int signum){if (signum == SIGINT) {fprintf(stdout,"Exiting...\n"); flag =0; }}intmain(void){mraa_result_t status = MRAA_SUCCESS; mraa_gpio_context gpio_1, gpio_2; /* install signal handler */signal(SIGINT, sig_handler); /* initialize mraa for the platform (not needed most of the times) */mraa_init(); //! [Interesting] /* initialize GPIO pin */ gpio_1 =mraa_gpio_init(GPIO_PIN_1);if (gpio_1 ==NULL) {fprintf(stderr,"Failed to initialize GPIO %d\n", GPIO_PIN_1);mraa_deinit();return EXIT_FAILURE; } /* initialize GPIO pin */ gpio_2 =mraa_gpio_init(GPIO_PIN_2);if (gpio_2 ==NULL) {fprintf(stderr,"Failed to initialize GPIO %d\n", GPIO_PIN_2);mraa_deinit();return EXIT_FAILURE; } /* set GPIO to output */ status =mraa_gpio_dir(gpio_1, MRAA_GPIO_OUT);if (status != MRAA_SUCCESS) {goto err_exit; } /* set GPIO to output */ status =mraa_gpio_dir(gpio_2, MRAA_GPIO_OUT);if (status != MRAA_SUCCESS) {goto err_exit; } /* toggle both GPIO's */while (flag) { status =mraa_gpio_write(gpio_1,1);if (status != MRAA_SUCCESS) {goto err_exit; } status =mraa_gpio_write(gpio_2,0);if (status != MRAA_SUCCESS) {goto err_exit; }sleep(1); status =mraa_gpio_write(gpio_1,0);if (status != MRAA_SUCCESS) {goto err_exit; } status =mraa_gpio_write(gpio_2,1);if (status != MRAA_SUCCESS) {goto err_exit; }sleep(1); } /* release gpio's */ status =mraa_gpio_close(gpio_1);if (status != MRAA_SUCCESS) {goto err_exit; } /* close GPIO */ status =mraa_gpio_close(gpio_2);if (status != MRAA_SUCCESS) {goto err_exit; } //! [Interesting] /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_SUCCESS;err_exit:mraa_result_print(status); /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_FAILURE;}
Enable the a5d2x tool-chain for cross compilation of c code to generate a binary file
For cross compilation type the following command given below
${CC} gpio.c -o gpiotoggle -lmraa
Execution :
The above command will generate a binary file with the name "gpiotoggle".
Connect the Rugged Board with your system.
Boot the board with SD card/NOR.
Copy the above binary file to board mnt directory with tftp protocol.
root@ruggedboard:/data#./gpiotooglelibmraa[133]:libmraaversionv2.0.0initialisedbyuser'root'withEUID0libmraa[133]:gpio:platformdoesn't support chardev, falling back to sysfslibmraa[133]: libmraa initialised for platform 'AtmelSAMA5' of type 20gpio 77gpio pin 77gpio 81gpio pin 81
2) Button_gpio
Description
User switch Led.
Required Hardware
a5d2x-rugged board
USB cable
Step-by-step guide
Open the below button_gpio.c source file in your host Terminal.
Follow the procedure given below the button_gpio.c file.
1. Test On-board Button- button_gpio.c
Open the button_gpio.c program in your host terminal as given below.
#include<mraa.h>#include<inttypes.h>#defineLED_PIN63 /**< The pin where the LED is connected */#defineBTN_PIN35 /**< Button is connected to this pin */intmain(void){ mraa_gpio_context ledPin; /* Will be used to represnt the LED pin */ mraa_gpio_context btnPin; /* Will be used to represnt the button pin */uint32_t status; /* Used to toggle the LED */uint32_t btnState; /* Used to capture the state of the button */mraa_init(); ledPin =mraa_gpio_init(LED_PIN); btnPin =mraa_gpio_init(BTN_PIN);mraa_gpio_dir(ledPin, MRAA_GPIO_OUT);mraa_gpio_dir(btnPin, MRAA_GPIO_IN);while(1){ status =mraa_gpio_read(btnPin);printf("STATUS = %d..\n\n",status);if(status ==1) {mraa_gpio_write(ledPin,1); printf("LED ON \n"); }else {printf("LED OFF \n");mraa_gpio_write(ledPin,0); // active low 0 is on the led }sleep(2);}return0;}
Enable the a5d2x tool-chain for cross compilation of c code to generate a binary file.
For cross compilation type the following command given below.
${CC} button_gpio.c -o button_gpio -lmraa
Execution :
The above command will generate a binary file with the name "button_gpio".
Connect the Rugged Board with your system.
Boot the board with SD card/NOR.
Copy the above binary file to board data directory with tftp protocol/mmc.
Run the binary by typing the below command.
root@ruggedboard:/data#./button_gpio
Expected Output:
root@ruggedboard:/data#./button_gpiolibmraa[147]:libmraaversionv2.0.0initialisedbyuser'root'withEUID0libmraa[147]:gpio:platformdoesn't support chardev, falling back to sysfslibmraa[147]: libmraa initialised for platform 'AtmelSAMA5' of type 20gpio 83gpio pin 83gpio 76gpio pin 76STATUS = 1..LED ONSTATUS = 1..LED ONSTATUS = 0..LED OFFSTATUS = 0..LED OFFSTATUS = 0..LED OFFSTATUS = 1..LED ON
3) AIO
Description
Connect a pot(potentiometer) to rugged board a5d2x.
Required Hardware
a5d2x-rugged board
USB cable
potentiometer
Step-by-step guide
Connect the Pot (it will have three patch codes(wires)), center one connect to "AN" (analog pin) of mikro bus connector.
The remaining two you can connect one to ground (pin 60) & one to vcc (pin 01) on expansion header respectively.
Open the below aio.c source file in your host Terminal.
follow the procedure given below the aio.c file.
1. Test aio.c with the help of potentiometer
Open the aio.c program in your host terminal as given below.
/* standard headers */#include<signal.h>#include<stdlib.h>#include<unistd.h>/* mraa header */#include"mraa/aio.h"/* AIO port */#defineAIO_PORT6volatilesig_atomic_t flag =1;voidsig_handler(int signum){if (signum == SIGINT) {fprintf(stdout,"Exiting...\n"); flag =0; }}intmain(){mraa_result_t status = MRAA_SUCCESS; mraa_aio_context aio;uint16_t value =0;float float_value =0.0;signal(SIGINT, sig_handler); /* initialize mraa for the platform (not needed most of the times) */mraa_init(); //! [Interesting] /* initialize AIO */ aio =mraa_aio_init(AIO_PORT);if (aio ==NULL) {fprintf(stderr,"Failed to initialize AIO: %d\n",AIO_PORT);mraa_deinit();return EXIT_FAILURE; }while (flag) { value =mraa_aio_read(aio); float_value =mraa_aio_read_float(aio);fprintf(stdout,"ADC A0 read %X - %d\n", value, value);fprintf(stdout,"ADC A0 read float - %.5f\n", float_value); } /* close AIO */ status =mraa_aio_close(aio);if (status != MRAA_SUCCESS) {goto err_exit; } //! [Interesting] /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_SUCCESS;err_exit:mraa_result_print(status); /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_FAILURE;}
Enable the a5d2x tool-chain for cross compilation of c code to generate a binary file
For cross compilation type the following command given below
${CC} aio.c -o aio -lmraa
Execution :
The above command will generate a binary file with the name "aio".
Connect the Rugged Board with your system.
Boot the board with SD card/NOR.
Copy the above binary file to board mnt directory with tftp protocol/mmc.
run the binary by typing the below command.
root@ruggedboard:/data#./aio
Note : kindly note that there will be a screw present on the potentiometer if you rotate it clockwise the values of pot will decrease and if you rotate it anti-clockwise the values of pot should increase.
To test pwm with using of mraa c programming on a5d2x-Rugged board.
Required Hardware
a5d2x-rugged board
USB cable
External LED
Step-by-step guide
Boot the board from NOR/MMC.
Please connect the positive pin of LED to the 16 th pin of the M1 connector.And connect the other pin of led to ground.
Open the below pwm.c source file in your host Terminal.
#include<signal.h>#include<stdlib.h>#include<unistd.h>/* mraa header */#include"mraa/pwm.h"/* PWM declaration */#definePWM72/* PWM period in us */#definePWM_FREQ200volatilesig_atomic_t flag =1;voidsig_handler(int signum){if (signum == SIGINT) {fprintf(stdout,"Exiting...\n"); flag =0; }}intmain(void){mraa_result_t status = MRAA_SUCCESS; mraa_pwm_context pwm;float value =0.0f;float output; /* initialize mraa for the platform (not needed most of the times) */mraa_init(); //! [Interesting] pwm =mraa_pwm_init(PWM);if (pwm ==NULL) {fprintf(stderr,"Failed to initialize PWM\n");mraa_deinit();return EXIT_FAILURE; } /* set PWM period */ status =mraa_pwm_period_us(pwm, PWM_FREQ);if (status != MRAA_SUCCESS) {goto err_exit; } /* enable PWM */ status =mraa_pwm_enable(pwm,1);if (status != MRAA_SUCCESS) {goto err_exit; }while (flag) { value = value +0.01f; /* write PWM duty cyle */ status =mraa_pwm_write(pwm, value);if (status != MRAA_SUCCESS) {goto err_exit; }usleep(50000);if (value >=1.0f) { value =0.0f; } /* read PWM duty cyle */ output =mraa_pwm_read(pwm);fprintf(stdout,"PWM value is %f\n", output); } /* close PWM */mraa_pwm_close(pwm); //! [Interesting] /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_SUCCESS;err_exit:mraa_result_print(status); /* close PWM */mraa_pwm_close(pwm); /* deinitialize mraa for the platform (not needed most of the times) */mraa_deinit();return EXIT_FAILURE;}
Enable the a5d2x tool-chain for cross compilation of c code to generate a binary file.
For cross compilation type the following command given below
$CC pwm.c -o pwm_mraa -lmraa
Copy the pwm_mraa binary to mnt folder of rugged board and run the pwm_mrra binary by giving below command.
root@rugged-board-a5d2x-sd1:/mnt#./pwm_mraa
Observe the external led brightness it will be varying.
Expected Output
root@rugged-board-a5d2x-sd1:/mnt#./pwm_mraalibmraa[144]:libmraaversionv2.0.0initialisedbyuser'root'withEUID0libmraa[144]:gpio:platformdoesn't support chardev, falling back to sysfslibmraa[144]: libmraa initialised for platform 'AtmelSAMA5' of type 20PWM value is 0.010000PWM value is 0.020000PWM value is 0.030000PWM value is 0.040000PWM value is 0.049995PWM value is 0.059995PWM value is 0.069995PWM value is 0.079995PWM value is 0.089995PWM value is 0.099995PWM value is 0.109995PWM value is 0.119995PWM value is 0.129995PWM value is 0.139995PWM value is 0.149995PWM value is 0.160000PWM value is 0.170000PWM value is 0.180000PWM value is 0.190000PWM value is 0.200000PWM value is 0.210000PWM value is 0.220000
4. I2c:
The I2c module, module has a number of different ways of interacting with the i2c bus