Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "RockpiE/dev/libmraa"

< RockpiE‎ | dev
Line 103: Line 103:
 
  rootuuid=6bdd02ee-ae60-404d-99c9-913f3f1bffb1
 
  rootuuid=6bdd02ee-ae60-404d-99c9-913f3f1bffb1
 
then '''reboot'''
 
then '''reboot'''
 
  
 
==== Test ====
 
==== Test ====
Line 391: Line 390:
  
 
  /usr/local/share/mraa/examples/
 
  /usr/local/share/mraa/examples/
 
  
 
==== Development ====
 
==== Development ====

Revision as of 03:01, 9 January 2021

    ROCK Pi E >  Development >  libmraa

Libmraa on ROCK Pi E

This guide describes how to use libmraa on ROCK Pi E.

Install essential packages

Package libmraa is in the Radxa APT bionic-testing, bionic-stable, buster-testing and buster-stable repositories.

Execute the following commands to add Radxa APT

rock@rockpie:~$ export DISTRO=bionic-stable      # for Bionic
rock@rockpie:~$ export DISTRO=bionic-testing      # for Bionic

or

rock@rockpie:~$ export DISTRO=buster-stable      # for Buster
rock@rockpie:~$ export DISTRO=buster-testing      # for Buster
rock@rockpie:~$ 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@rockpie:~$  wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -

Install essential packages:

rock@rockpie:~$ sudo apt-get update
rock@rockpie:~$ sudo apt-get install -y rockchip-overlay libmraa # libmraa version: >=1.0
rock@rockpie:~$ sudo apt-get install -y linux-4.4-rock-pi-e-latest # ROCK Pi E kernel version: >= 4.4.194-17

Install compiler:

rock@rockpie:~$ sudo apt install -y build-essential

Enable interface

See ROCK Pi E GPIO pintout. ROCK Pi E has one 40-pin expansion header. Each pin is distinguished by color.

Hardware V1.0/V1.1

25 GPIO:

PIN3   GPIO2_A4
PIN5   GPIO2_A5
PIN7   GPIO3_A4
PIN8   GPIO2_A0
PIN10 GPIO2_A1
PIN11 GPIO3_A0
PIN12 GPIO2_D2
PIN13 GPIO3_B0
PIN15 GPIO3_A2
PIN19 GPIO0_D3
PIN21 GPIO2_C3
PIN22 GPIO2_B4
PIN23 GPIO1_D4
PIN24 GPIO2_A3
PIN26 GPIO2_C7
PIN27 GPIO3_A1
PIN28 GPIO2_C1
PIN29 GPIO2_C0
PIN31 GPIO2_B7
PIN32 GPIO3_A6
PIN33 GPIO2_C2
PIN36 GPIO2_C5
PIN37 GPIO2_A2
PIN38 GPIO2_C4
PIN40 GPIO2_C6

I2C: x1, I2C1

PIN3 I2C1_SDA
PIN5 I2C1_SCL

SPI: x1, SPI0(Pin#11/13/15/27)

PIN11 SPI_CLK_M2
PIN13 SPI_CSN0_M2
PIN15 SPI_RXD_M2
PIN27 SPI_TXD_M2

UART: x1, UART1 and UART2

PIN7    UART1_TX
PIN32  UART1_RX
PIN8    UART2_TX_M1
PIN10  UART2_RX_M1

ADC_IN1: x1, ADC_IN1

PIN35  ADC_IN1

Modify /boot/uEnv.txt to enable uart1 and spi0 for test,as follow :

verbosity=7
fdtfile=rockchip/rk3328-rock-pi-e.dtb
console=ttyS2,1500000n8
overlays=rk3328-uart2 rk3328-spi-spidev rk3328-uart1
param_spidev_spi_bus=0
param_spidev_spi_cs=0
param_spidev_max_freq=10000000
initrdsize=0x655f2d
kernelversion=4.4.194-12-rockchip-g615ae7431150
initrdimg=initrd.img-4.4.194-12-rockchip-g615ae7431150
kernelimg=vmlinuz-4.4.194-12-rockchip-g615ae7431150
rootuuid=6bdd02ee-ae60-404d-99c9-913f3f1bffb1

then reboot

Test

test gpio

Use mraa-gpio to test

rock@rockpie:~$ mraa-gpio list
01         3V3: 
02          5V: 
03        SDA1: GPIO I2C  
04          5V: 
05        SCL1: GPIO I2C  
06         GND: 
07        TXD1: GPIO UART 
08        TXD2: GPIO UART 
09         GND: 
10        RXD2: GPIO UART 
11  SPI_CLK_M2: GPIO SPI  
12    GPIO2_D2: GPIO 
13 SPI_CSN0_M2: GPIO SPI  
14         GND: 
15  SPI_RXD_M2: GPIO SPI  
16     USB20DM: 
17         3V3: 
18     USB20DP: 
19    GPIO0_D3: GPIO 
20         GND: 
21    GPIO2_C3: GPIO 
22    GPIO2_B4: GPIO 
23    GPIO1_D4: GPIO 
24    GPIO2_A3: GPIO 
25         GND: 
26    GPIO2_C7: 
27  SPI_TXD_M2: GPIO SPI  
28    GPIO2_C1: GPIO 
29    GPIO2_C0: GPIO 
30         GND: 
31    GPIO2_B7: GPIO 
32    UART1_RX: GPIO UART 
33    GPIO2_C2: GPIO 
34         GND: 
35     ADC_IN1: AIO  
36    GPIO2_C5: GPIO 
37    GPIO2_A2: GPIO 
38    GPIO2_C4: GPIO 
39         GND: 
40    GPIO2_C6: GPIO 
rock@rockpie:~$ 
rock@rockpie:~$ sudo mraa-gpio set 40 1   //pin40 pull high  
rock@rockpie:~$ sudo mraa-gpio set 40 0   //pin40 pull low
test i2c

Use mraa-i2c to test, we must have a i2c slave device , for example, we use I2C EEPROM:

rock@rockpie:~$ mraa-i2c list
Bus   0: id=01 type=linux  default
rock@rockpie:~$ sudo mraa-i2c detect 0                                                                        
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
rock@rockpie:~$ sudo mraa-i2c set 0 0x50 0x10 0x55
Device 50, Register = 10, Value = 55
rock@rockpie:~$ sudo mraa-i2c get 0 0x50 0x10
Register 0X10 = 0X55
rock@rockpie:~$ 
test spi

fristly, modify /boot/uEnv.txt to enable the spi0, The changes are as follows :

rock@rockpie:~$ cat /boot/uEnv.txt 
verbosity=7
fdtfile=rockchip/rk3328-rock-pi-e.dtb
console=ttyS2,1500000n8
overlays=rk3328-uart2 rk3328-spi-spidev
param_spidev_spi_bus=0
param_spidev_spi_cs=0
param_spidev_max_freq=10000000
initrdsize=0x655f2d
kernelversion=4.4.194-12-rockchip-g615ae7431150
initrdimg=initrd.img-4.4.194-12-rockchip-g615ae7431150
kernelimg=vmlinuz-4.4.194-12-rockchip-g615ae7431150
rootuuid=6bdd02ee-ae60-404d-99c9-913f3f1bffb1
rock@rockpie:~$ 

After modification, you need to reboot

use c to test spi,compile spi.c.

  root@rockpie:~# cat spi.c
  #include <signal.h>
  #include <stdlib.h>
  #include <unistd.h>
  
  /* mraa header */
  #include "mraa/spi.h"
  
  /* SPI declaration */
  #define SPI_BUS 0
  
  /* SPI frequency in Hz */
  #define SPI_FREQ 400000
  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;
      }
      while(1) {
          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;
  }
  root@rockpie:~# gcc spi.c -lmraa


then short pin15 and pin27 and run a.out

  root@rockpie:~# ./a.out 
  0xaa
  0xaa
  ......
test Uart

Enable Uart1 for uart test, The changes are as follows :

rock@rockpie:~$ cat /boot/uEnv.txt 
verbosity=7
fdtfile=rockchip/rk3328-rock-pi-e.dtb
console=ttyS2,1500000n8
overlays=rk3328-uart2 rk3328-uart1
initrdsize=0x655f2d
kernelversion=4.4.194-12-rockchip-g615ae7431150
initrdimg=initrd.img-4.4.194-12-rockchip-g615ae7431150
kernelimg=vmlinuz-4.4.194-12-rockchip-g615ae7431150
rootuuid=6bdd02ee-ae60-404d-99c9-913f3f1bffb1
rock@rockpie:~$ 

then reboot and short pin7 and pin32. use ssh connect to rockpie,we must have 2 windows,one is for send data,anothor is for receive.

send command follow:

sudo mraa-uart dev 0 baud 1500000 send  data

receive command follow:

 sudo mraa-uart dev 0 baud 1500000 recv  1000

the receive window will show the data

test adc

Firstly, short pin23 and pin35, Compile mraa example file to test, as follow:

rock@rockpie:~$ cp /usr/local/share/mraa . -r
rock@rockpie:~$ cd mraa/examples/
rock@rockpie:~/mraa/examples$ ls 
CMakeLists.txt  c  c++  python
rock@rockpie:~/mraa/examples$ mkdir build
rock@rockpie:~/mraa/examples$ cd build
rock@rockpie:~/mraa/examples/build$ sudo apt-get install cmake
rock@rockpie:~/mraa/examples/build$ sudo apt-get install g++
rock@rockpie:~/mraa/examples/build$ cmake ..
rock@rockpie:~/mraa/examples/build$ cd c; make
rock@rockpie:~/mraa/examples/build/c$ sudo su
[sudo] password for rock:							#password:rock
root@rockpie:/home/rock/mraa/examples/build/c# ./gpio &
[1] 1250
root@rockpie:/home/rock/mraa/examples/build/c# ./aio
ADC A0 read 3FF - 1023
ADC A0 read float - 1.00000
ADC A0 read 3FF - 1023
ADC A0 read float - 1.00000
...
ADC A0 read 0 - 0
ADC A0 read float - 0.00000
ADC A0 read 0 - 0
ADC A0 read float - 0.00000


Compilation example

Install compiler and project builder

Install g++ compiler and cmake project builder

rock@rockpie:~$ sudo apt-get install cmake g++

Configure Java environment variables. Because the libmraa-rockpie package has been installed openjdk-11, there is no need to install JDK, but openjdk-11 does not contain JRE, so you need to generate JRE manually and configure the Java environment variables.

rock@rockpie:~$ sudo su     #password: rock
root@rockpie:/home/rock# cd /usr/lib/jvm/java-1.11.0-openjdk-arm64
root@rockpie:/usr/lib/jvm/java-1.11.0-openjdk-arm64# jlink --module-path jmods --add-modules java.desktop --output jre
root@rockpie:/usr/lib/jvm/java-1.11.0-openjdk-arm64# cd 
root@rockpie:~# vim ./.bashrc
  ...
  export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-arm64
  export JRE_HOME=${JAVA_HOME}/jre
  export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
  export PATH=${JAVA_HOME}/bin:$PATH
  
  export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

root@rockpie:~# source ./.bashrc
root@rockpie:~# cp /usr/local/share/mraa . -r
root@rockpie:~# cd mraa/examples/
root@rockpie:~/mraa/examples# mkdir build 
root@rockpie:~/mraa/examples# cd build
root@rockpie:~/mraa/examples/build# cmake ..; make
...
[ 97%] Building CXX object c++/CMakeFiles/i2c_cpp.dir/i2c.o
[100%] Linking CXX executable i2c_cpp
[100%] Built target i2c_cpp
root@rockpie:~/mraa/examples/build# ls 
CMakeCache.txt  CMakeFiles  Makefile  c  c++  cmake_install.cmake  java

After compiling, you can see that C, C + + and Java are generated. Let's take a look at the example of using Java.

Example of running Java

Go to the Java example directory and use the Java command to run the .jar package.
Let's short 35pin and 23pin, and then run the .jar package, and watch the output.

root@rockpie:~/mraa/examples/build# cd java/
root@rockpie:~/mraa/examples/build/java# ls 
AioA0         CMakeFiles  GpioMmapped  Isr         UartExample
BlinkIO       CyclePwm3   GpioRead6    Makefile    cmake_install.cmake
BlinkOnboard  Example     HelloEdison  SpiMAX7219
Bmp85         FTDITest    I2cCompass   SpiMCP4261
root@rockpie:~/mraa/examples/build/java# java -jar GpioMmapped/GpioMmapped.jar &
root@rockpie:~/mraa/examples/build/java# hello mraa
Version: v2.1.0-6-gb915502
Platform: ROCK Pi E                  #Enter the Enter key on the keyboard

root@rockpie:~/mraa/examples/build/java# java -jar AioA0/AioA0.jar 
ADC A0 read 0 - 0
ADC A0 read 0.00000
ADC A0 read 0 - 0
ADC A0 read 0.00000
...
ADC A0 read 3FF - 1023
ADC A0 read 1.00000
ADC A0 read 3FF - 1023
ADC A0 read 1.00000

Look, you have successfully run it. Next, you can try to write a Java demo yourself. Viewing these .hpp files can help you write your own demo

root@rockpie:/usr/local/include/mraa# ls 
aio.h       gpio.h    iio.hpp               led.hpp  types.h    uart_ow.hpp
aio.hpp     gpio.hpp  iio_kernel_headers.h  pwm.h    types.hpp
common.h    i2c.h     initio.h              pwm.hpp  uart.h
common.hpp  i2c.hpp   initio.hpp            spi.h    uart.hpp
firmata.h   iio.h     led.h                 spi.hpp  uart_ow.h
root@rockpie:/usr/local/include/mraa#
More examples

More examples (including examples written in python and c++) can be found at:

/usr/local/share/mraa/examples/

Development

More introduction of libmraa can look at libmraa official website.

Troubleshooting