RockpiN10/dev/libmraa
ROCK Pi N10 > Development > Install Libmraa
Contents
Libmraa on ROCK Pi N10
This guide describes how to use libmraa on ROCK Pi N10.
Install essential packages
Package libmraa is in the Radxa APT bionic-stable and buster-stable repositories.
Execute the following commands to add Radxa APT
rock@rockpin10:~$ export DISTRO=bionic-testing # for Bionic
or
rock@rockpin10:~$ export DISTRO= buster-testing # for Buster rock@rockpin10:~$ echo "deb http://apt.radxa.com/$DISTRO/ ${DISTRO%-*} main" | sudo tee -a /etc/apt/sources.list.d/apt-radxa-com.list
Get the pub key
rock@rockpin10:~$ wget -O - apt.radxa.com/buster-testing/public.key | sudo apt-key add -
Install essential packages:
rock@rockpin10:~$ sudo apt-get update rock@rockpin10:~$ sudo apt-get install -y rockchip-overlay libmraa # libmraa version: >= 1.1 rock@rockpin10:~$ sudo apt-get install -y linux-4.4-rockpin10-latest # ROCK Pi N10 kernel version: >= 4.4.167-15
Install compiler:
rock@rockpin10:~$ sudo apt install -y build-essential
Enable interface
See ROCK Pi N10 GPIO pintout. ROCK Pi N10 has a 40-pin colorful expansion header. Each pin is distinguished by color. mraa define is shown below:
Hardware V11
25 GPIO:
PIN03 GPIO2_A7 PIN05 GPIO2_B0 PIN07 GPIO2_B5 PIN08 GPIO4_C4 PIN10 GPIO4_C3 PIN11 GPIO4_C2 PIN12 GPIO0_B1 PIN13 GPIO4_C6 PIN15 GPIO4_C5 PIN16 GPIO0_B0 PIN18 GPIO2_A6 PIN19 GPIO1_B0 PIN21 GPIO1_A7 PIN22 GPIO3_D4 PIN23 GPIO1_B1 PIN24 GPIO1_B2 PIN29 GPIO2_B2 PIN31 GPIO2_B1 PIN32 GPIO3_D5 PIN33 GPIO2_D5 PIN35 GPIO2_A4 PIN36 GPIO2_A2 PIN37 GPIO3_D6 PIN38 GPIO2_A3 PIN40 GPIO1_A2
3 I2C:
PIN27 I2C2_SDA PIN28 I2C2_SCL PIN29 I2C6_SCL PIN31 I2C6_SDA PIN3 I2C7_SDA PIN5 I2C7_SCL
1 SPI:
PIN19 SPI1TX //must disable UART4 PIN21 SPI1RX //must disable UART4 PIN23 SPI1CLK PIN24 SPI1CSN
2 UART:
PIN8 UART2_TX PIN10 UART2_RX PIN19 UART4_TX //must disable SPI1 PIN21 UART4_RX //must disable SPI1
2 PWM:
PIN11 PWM0 PIN13 PWM1
1 ADC:
PIN26 ADC_IN1 //the measure voltage must lower than 1.8v
For those ROCK Pi N10 system images released after March 1st, 2020, the configuration file is /boot/uEnv.txt. For more details, follow this guide, Device tree overlays.
For those ROCK Pi N10 system images released before March 1st, 2020, the configuration file is /boot/hw_intfc.conf.
Test
Package libmraa provides some examples for testing spi, uart, i2c, gpio, etc. We way use the c files in /usr/local/share/mraa/examples/c.
And we copy them to home directory.
rock@rockpin10:~/mraa-examples$ cp -av /usr/local/share/mraa/examples/c /home/rock/mraa-examples
You should pay more attention to the Black Bold words when modifying configuration file /boot/uEnv.txt.
GPIO test
rock@rockpin10:~$ mraa-gpio list 01 3V3: 02 5V: 03 SDA7: GPIO I2C 04 5V: 05 SCL7: GPIO I2C 06 GND: 07 GPIO0_B5: GPIO 08 TXD2: GPIO UART 09 GND: 10 RXD2: GPIO UART 11 PWM0: GPIO PWM 12 GPIO0_B1: GPIO 13 PWM1: GPIO PWM 14 GND: 15 GPIO4_C5: GPIO 16 GPIO0_B0: GPIO 17 3V3: 18 GPIO2_A6: GPIO 19 SPI1TX,TXD4: GPIO SPI UART 20 GND: 21 SPI1RX,RXD4: GPIO SPI UART 22 GPIO3_D4: GPIO 23 SPI1CLK: GPIO SPI 24 SPI1CS: GPIO SPI 25 GND: 26 ADC_IN0: AIO 27 SDA2: GPIO I2C 28 SCL2: GPIO I2C 29 SCL6,SPI2RX: GPIO I2C SPI 30 GND: 31 SDA6,SPI2TX: GPIO I2C SPI 32 GPIO3_D5: GPIO 33 GPIO4_D5: GPIO 34 GND: 35 GPIO2_A4: GPIO 36 GPIO2_A2: GPIO 37 GPIO3_D6: GPIO 38 GPIO2_A3: GPIO 39 GND: 40 GPIO1_A2: GPIO root@rockpin10:~# mraa-gpio set 40 1 #pin40 pull high root@rockpin10:~# mraa-gpio set 40 0 #pin40 pull high
PWM test
ROCK Pi N10 V1.1 provides 2 PWM, PWM0 and PWM1. It is necessary to enable PWM0 and PWM1 at the same time if you need PWM function.
Firstly, modify the /boot/uEnv.txt file to add the following
root@rockpin10:~# cat /boot/uEnv.txt verbosity=7 overlay_prefix=rockchip rootfstype=ext4 fdtfile=rockchip/rk3399pro-rockpi-n10-linux.dtb rk3399pro-pwm0 rk3399pro-pwm1 overlays=rk3399pro-console-on-uart2 rootuuid=11958cc2-af5a-4e6f-9ad4-2b3baef3eebe initrdsize=0x5649ac kernelversion=4.4.167-14-rockchip-g53bdb2fa44e8 initrdimg=initrd.img-4.4.167-14-rockchip-g53bdb2fa44e8 kernelimg=vmlinuz-4.4.167-14-rockchip-g53bdb2fa44e8
Modify the macro PWM in /home/rock/mraa-examples/pwm.c as follows to test PWM0. To test PWM2, change #define PWM 11 to #define PWM 13.
/* PWM declaration */ #define PWM 11
Save and compile pwm.c
rock@rockpin10:~/mraa-examples$ gcc -o pwm pwm.c -lmraa rock@rockpin10:~/mraa-examples$ sudo ./pwm PWM value is 0.010015 PWM value is 0.019985 PWM value is 0.030000 PWM value is 0.040014 PWM value is 0.049984 PWM value is 0.059999
I2C test
ROCK Pi N10 V1.1 provides 3 I2C,There are I2C2 ,I2C6, I2C7. If you need the I2C function, you can turn on one or more of them.
Firstly, modify the /boot/uEnv.txt file to add the following
root@rockpin10:~# cat /boot/uEnv.txt verbosity=7 overlay_prefix=rockchip rootfstype=ext4 fdtfile=rockchip/rk3399pro-rockpi-n10-linux.dtb rk3399pro-i2c2 rk3399pro-i2c6 rk3399pro-i2c7 overlays=rk3399pro-console-on-uart2 rootuuid=11958cc2-af5a-4e6f-9ad4-2b3baef3eebe initrdsize=0x5649ac kernelversion=4.4.167-14-rockchip-g53bdb2fa44e8 initrdimg=initrd.img-4.4.167-14-rockchip-g53bdb2fa44e8 kernelimg=vmlinuz-4.4.167-14-rockchip-g53bdb2fa44e8
We use MPU6050 tets i2c. Modify the macro MPU6050 in /home/rock/mraa-examples/i2c_mpu6050.c ,as follows to test I2C2. To test I2C6 and I2C7, change #define I2C_BUS 0 to #define I2C_BUS 1 or#define I2C_BUS 2 .
/* mraa header */ #include "mraa/i2c.h" #define I2C_BUS 0
Save and compile pwm.c
rock@rockpis:~/mraa-examples$ gcc -o i2c_mpu6050 i2c_mpu6050.c -lmraa rock@rockpis:~/mraa-examples$ sudo ./i2c_mpu6050 accel: x:0 y:0 z:0 gyro: x:-10 y:1 z:7 accel: x:0 y:0 z:0 gyro: x:107 y:-12 z:69 accel: x:0 y:0 z:0 gyro: x:6 y:136 z:-50
UART test
ROCK Pi n10 V1.1 provides 2 UART,There are uart2 ,uart4. If you need the uatr function, you can turn on one or more of them.
Firstly, Disable console and enable uart2 for uart test.modify the /boot/uEnv.txt file to add the following
root@rockpin10:~# cat /boot/uEnv.txt verbosity=7 overlay_prefix=rockchip rootfstype=ext4 fdtfile=rockchip/rk3399pro-rockpi-n10-linux.dtb console= overlays=rk3399pro-uart2 rk3399pro-uart4 rootuuid=11958cc2-af5a-4e6f-9ad4-2b3baef3eebe initrdsize=0x5649ac kernelversion=4.4.167-14-rockchip-g53bdb2fa44e8 initrdimg=initrd.img-4.4.167-14-rockchip-g53bdb2fa44e8 kernelimg=vmlinuz-4.4.167-14-rockchip-g53bdb2fa44e8
Here set console to NULL value.
then reboot . use ssh connect to rockpin10,we must have 2 windows,one is for send data,anothor is for receive.
send command follow:
rock@rockpin10:~$ sudo mraa-uart dev 0 baud 1500000 send radxa
Another window sets the baud 1500000 and receives the data
radxa
receive command follow:
rock@rockpin10:~$ sudo mraa-uart dev 0 baud 1500000 recv 1000 radxa
SPI test
Firstly, modify the /boot/uEnv.txt file.
root@rockpin10:~# cat /boot/uEnv.txt verbosity=7 overlay_prefix=rockchip rootfstype=ext4 fdtfile=rockchip/rk3399pro-rockpi-n10-linux.dtb rk3399pro-spi-spidev overlays=rk3399pro-console-on-uart2 rootuuid=11958cc2-af5a-4e6f-9ad4-2b3baef3eebe initrdsize=0x5649ac kernelversion=4.4.167-14-rockchip-g53bdb2fa44e8 initrdimg=initrd.img-4.4.167-14-rockchip-g53bdb2fa44e8 kernelimg=vmlinuz-4.4.167-14-rockchip-g53bdb2fa44e8
then execute command, sync, followed by reboot.
Create file test-spi.c and add the following contents.
rock@rockpin10:~/mraa-examples$ cat test-spi.c #include <signal.h> #include <stdlib.h> #include <unistd.h> /* mraa header */ #include "mraa/spi.h" /* SPI declaration */ #define SPI_BUS 2 /* SPI frequency in Hz */ #define SPI_FREQ 10000000 int main(int argc, char** argv) { mraa_result_t status = MRAA_SUCCESS; mraa_spi_context spi; int i, j; /* initialize mraa for the platform (not needed most of the times) */ mraa_init(); //! [Interesting] /* initialize SPI bus */ spi = mraa_spi_init(SPI_BUS); if (spi == NULL) { fprintf(stderr, "Failed to initialize SPI\n"); mraa_deinit(); return EXIT_FAILURE; } /* set SPI frequency */ status = mraa_spi_frequency(spi, SPI_FREQ); if (status != MRAA_SUCCESS) goto err_exit; /* set big endian mode */ status = mraa_spi_lsbmode(spi, 0); if (status != MRAA_SUCCESS) { goto err_exit; } j = 10; while(j) { j--; printf("0x%x\n",mraa_spi_write(spi, 0xaa)); } err_exit: mraa_result_print(status); /* stop spi */ mraa_spi_stop(spi); /* deinitialize mraa for the platform (not needed most of the times) */ mraa_deinit(); return EXIT_FAILURE; } rock@rockpin10:~/mraa-examples$ gcc -o test-spi test-spi.c -lmraa
Then short pin19 and pin21 and run test-spi
rock@rockpin10:~/mraa-examples$ sudo ./test-spi 0xaa 0xaa 0xaa 0xaa 0xaa 0xaa 0xaa 0xaa 0xaa 0xaa MRAA: SUCCESS
ADC test
connect signal you want to measure, and then compile mraa example c file, aio.c, to test.
rock@rockpin10:~/mraa-examples$ gcc -o aio aio.c -lmraa rock@rockpin10:~/mraa-examples$ sudo ./aio ADC A0 read 1A5 - 421 ADC A0 read float - 0.41153 ADC A0 read 1A5 - 421 ADC A0 read float - 0.41153 ADC A0 read 1A5 - 421 ADC A0 read float - 0.41153 ADC A0 read 1A5 - 421 ADC A0 read float - 0.41153
Development
More introduction of libmraa can look at libmraa official website.
Troubleshooting
- Post your issue on the forum: https://forum.radxa.com/c/rockpiN10