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

Rockpi4/dev/libmraa

< Rockpi4‎ | dev
Revision as of 02:14, 20 August 2021 by Ken (Talk | contribs)

       ROCK Pi 4 >  Development >  Install Libmraa 


Libmraa-rockpi4

Below is the instructions of how to install libmraa.

Install essential packages

Edit your /etc/apt/sources.list and add the following:

For Debian Stretch

   deb http://apt.radxa.com/stretch/ stretch main

For Ubuntu bionic

   deb http://apt.radxa.com/bionic/ bionic main

Get the pub key

   wget -O -  apt.radxa.com/stretch/public.key | sudo apt-key add -

Update

   sudo apt-get update && sudo apt-get upgrade

Install essential packages:

   sudo apt-get install -y rockchip-overlay rockchip-fstab && sudo apt-get install -y rockpi4-dtbo

Install the latest u-boot and kernel

   apt-get install rockpi4b-rk-u-boot-latest

Manually run the u-boot flash script

   /usr/local/sbin/rockpi4b_upgrade_bootloader.sh


Type YES to perform the uboot.img flashing.

then install kernel

   apt-get install linux-base
   apt-get install linux-4.4-latest


After installation, check file /boot/extlinux/extlinux.conf. It lists all kernel version installed in ROCK Pi. The kernel corresponding to the first label is the newest version and the first boot option.

Enable interface

See rockpi4 GPIO pinout,ROCK Pi 4 has a 40-pin expansion header. Each pin is distinguished by color. mraa define follow:

27 GPIO:

   PIN03    GPIO2_A2
   PIN05    GPIO2_B0
   PIN07    GPIO2_B3
   PIN08    GPIO4_C4
   PIN10    GPIO4_C3
   PIN11    GPIO4_C2
   PIN12    GPIO4_A3
   PIN13    GPIO4_C6
   PIN15    GPIO4_C5
   PIN16    GPIO4_D2
   PIN18    GPIO4_D4
   PIN19    GPIO1_B0
   PIN21    GPIO1_A7
   PIN22    GPIO4_D5
   PIN23    GPIO1_B1
   PIN24    GPIO1_B2
   PIN27    GPIO2_A0
   PIN28    GPIO2_A1
   PIN29    GPIO2_B2
   PIN31    GPIO2_B1
   PIN32    GPIO3_C0
   PIN33    GPIO2_B4
   PIN35    GPIO4_A5
   PIN36    GPIO4_A4
   PIN37    GPIO4_D6
   PIN38    GPIO4_A6
   PIN40    GPIO4_A7

2 I2C:

   PIN3       I2C7_SDA
   PIN5       I2C7_SCL
   PIN27      I2C2_SCL
   PIN28      I2C2_SCL
   PIN29      I2C6_SCL
   PIN31      I2C6_SDA

2 SPI:

   PIN19      SPI1TX                    //must disable uart4
   PIN21      SPI1RX                    //must disable uart4
   PIN23      SPI1_CLK
   PIN24      SPI1_CS
   PIN29      SPI2TX                   //must disable i2c6
   PIN31      SPI2RX                   //must disable i2c6
   PIN7       SPI2_CLK
   PIN33      SPI2_CS  

1 UART:

   PIN8       UART2_TX
   PIN10      UART2_RX
   PIN19      UART4_TX
   PIN21      UART4_RX

2 PWM:

   PIN11      PWM0
   PIN13      PWM1

1 ADC:

   PIN13      ADC0       //the measure voltage must lower than 1.8v

modify /boot/hw_intfc.conf to enable pwm0,pwm1,uart2,uart4,i2c2,i2c6,i2c7 for test

   root@localhost:/# cat boot/hw_intfc.conf 
   
   # Hardware Interface Config
   
   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
   
   # Set "on" to enable the optional hardware interfaces while set "off" to disable.
   
   intfc:pwm0=on
   intfc:pwm1=on
   intfc:uart2=on
   intfc:uart4=on
   intfc:spi1=off
   intfc:spi2=off
   intfc:i2c2=on
   intfc:i2c6=on
   intfc:i2c7=on
   
   # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
   
   #intfc:dtoverlay=at24c02
   #intfc:dtoverlay=two-color-led
   #intfc:dtoverlay=console-on-ttyS2
   #intfc:dtoverlay=console-on-ttyS4
   #intfc:dtoverlay=devspi1
   #intfc:dtoverlay=devspi2
   root@localhost:/# 

then reboot

Install libmraa-rockpi4

   apt-get install libmraa-rockpi4

Test

test gpio

Use mraa-gpio tool to test

   rock@localhost:~/mraa/build$ mraa-gpio list 
   01         3V3: 
   02          5V: 
   03        SDA7: GPIO I2C  
   04          5V: 
   05        SCL7: GPIO I2C  
   06         GND: 
   07    SPI2_CLK: GPIO SPI  
   08        TXD2: GPIO UART 
   09         GND: 
   10        RXD2: GPIO UART 
   11        PWM0: GPIO PWM  
   12    GPIO4_A3: GPIO 
   13        PWM1: GPIO PWM  
   14         GND: 
   15    GPIO4_C5: GPIO 
   16    GPIO4_D2: GPIO 
   17         3V3: 
   18    GPIO4_D4: GPIO 
   19 SPI1TX,TXD4: GPIO SPI  UART 
   20         GND: 
   21 SPI1RX,RXD4: GPIO SPI  UART 
   22    GPIO4_D5: 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_C0: GPIO 
   33      SPI2CS: GPIO SPI  
   34         GND: 
   35    GPIO4_A5: GPIO 
   36    GPIO4_A4: GPIO 
   37    GPIO4_D6: GPIO 
   38    GPIO4_A6: GPIO 
   39         GND: 
   40    GPIO4_A7: GPIO 
   rock@localhost:~/mraa/build$ 
   rock@localhost:~/mraa/build$  sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                      
   rock@localhost:~/mraa/build$  sudo mraa-gpio set 40 0  //pin40 pull low
   root@localhost:~#

test i2c

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

   rock@localhost:~/mraa/build$ mraa-i2c list
   Bus   0: id=07 type=linux  default
   Bus   1: id=02 type=linux 
   Bus   2: id=06 type=linux 
   rock@localhost:~/mraa/build$ sudo mraa-i2c detect 0       //device on bus 0  //i2c7
   00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- -- 
   60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
   rock@localhost:~/mraa/build$ sudo mraa-i2c set 0 0x50 0x10 0x55   //write                                                                                                                          
   Device 50, Register = 10, Value = 55
   rock@localhost:~/mraa/build$ sudo mraa-i2c get 0 0x50 0x10              //read                                                                                              
   Register 0X10 = 0X55

test spi

fristly,modiy /boot/hw_intfc.conf,if use spi1,must disable uart4,if use spi2,disable i2c6.follow:

   rock@localhost:~/mraa/build$ cat /boot/hw_intfc.conf 
   
   # Hardware Interface Config
   
   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
   
   # Set "on" to enable the optional hardware interfaces while set "off" to disable.
   
   intfc:pwm0=on
   intfc:pwm1=on
   intfc:uart2=on
   intfc:uart4=off
   intfc:spi1=on
   intfc:spi2=on
   intfc:i2c2=on
   intfc:i2c6=off
   intfc:i2c7=on
   
   # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
   
   #intfc:dtoverlay=at24c02
   #intfc:dtoverlay=two-color-led
   #intfc:dtoverlay=console-on-ttyS2
   #intfc:dtoverlay=console-on-ttyS4
   intfc:dtoverlay=devspi1           #spi1
   intfc:dtoverlay=devspi2        #spi2

If need debug console, you could uncomment intfc:dtoverlay=console-on-ttyS2

use c to test spi,compile spi.c,if use spi1,must disable

   root@localhost:~# 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@localhost:~# gcc spi.c -lmraa

then short pin19 and pin21 and run a.out

   root@localhost:~# ./a.out 
   0xaa
   0xaa
   ......

test pwm

for pwm python test,should install python and libpython2.7

   apt-get install libpython2.7 python

use python script follow:

   root@localhost:~# cat pwm.py 
   #!/usr/bin/env python
   # Example Usage: Generates PWM at a step rate of 0.01 continuously.
   import mraa
   import time
   
   x = mraa.Pwm(11) # pin11 pwm0
   # x = mraa.Pwm(13) # pin13 pwm1
   x.period_us(700)
   x.enable(True)
   
   value= 0.0
   
   while True:
       x.write(value)
   
       time.sleep(0.05)
   
       value = value + 0.01
       if value >= 1:
           value = 0.0
   root@localhost:~# 

then run pwm.py

   root@localhost:~# ls
   pwm.py
   root@localhost:~# chmod +x pwm.py 
   root@localhost:~# ./pwm.py

test uart

Disable console and enable uart2 and uart4 for uart test

   root@localhost:/# cat boot/hw_intfc.conf 
   
   # Hardware Interface Config
   
   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
   
   # Set "on" to enable the optional hardware interfaces while set "off" to disable.
   
   intfc:pwm0=on
   intfc:pwm1=on
   intfc:uart2=on
   intfc:uart4=off
   intfc:spi1=on
   intfc:spi2=off
   intfc:i2c2=on
   intfc:i2c6=off
   intfc:i2c7=off
   
   # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
   
   #intfc:dtoverlay=at24c02
   #intfc:dtoverlay=two-color-led
   #intfc:dtoverlay=console-on-ttyS2
   #intfc:dtoverlay=console-on-ttyS4
   root@localhost:/# 

then reboot. Connect your serial line to the corresponding serial port. use ssh connect to rockpi4,we must have 2 windows(ssh and serial console ),one is for send data,anothor is for receive.

send command follow:

   sudo mraa-uart dev 0 baud 1500000 send  data
   or
   sudo mraa-uart dev 1baud 1500000 send  data

receive command follow:

   sudo mraa-uart dev 0 baud 1500000 recv  1000
   or
   sudo mraa-uart dev 1 baud 1500000 recv  1000

the receive window will show the data

test adc

connect signal you want to measure,and then compile mraa examplec file to test.

   rock@localhost:~/mraa$ ls examples/c/aio.c 
   examples/c/aio.c
   gcc examples/c/aio.c  -lmraa

then run a.out

More examples

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

/usr/local/share/mraa/examples/