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

Zero/dev/libmraa

< Zero‎ | dev
Revision as of 12:01, 28 October 2021 by Ken (Talk | contribs)

 Radxa Zero >  Development Guide >  Install Libmraa

Libmraa on Radxa Zero

This guide describes how to use libmraa on Radxa Zero.

Requirement

Install essential packages

Package libmraa is in the Radxa APT focal-stable and buster-stable repositories.

Execute the following commands to add Radxa APT

rock@zero:~$ export DISTRO=focal-testing      # for Focal

or

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

Install essential packages:

rock@zero:~$ sudo apt-get update
rock@zero:~$ sudo apt-get install -y rockchip-overlay libmraa 

Install compiler:

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

Enable interface

See Radxa Zero GPIO pintout. Radxa Zero has a 40-pin colorful expansion header. Each pin is distinguished by color. mraa define is shown below: 23 GPIO:

   PIN03      GPIOA_14
   PIN05      GPIOA_15
   PIN07      GPIOAO_3
   PIN08      GPIOAO_0
   PIN10      GPIOAO_1
   PIN11      GPIOAO_2
   PIN12      GPIOX_9
   PIN13      GPIOX_11
   PIN16      GPIOX_10
   PIN18      GPIOX_8
   PIN19      GPIOH_4
   PIN21      GPIOH_5
   PIN22      GPIOC_7      //Not supported gpio funtion now
   PIN23      GPIOH_7
   PIN24      GPIOH_6
   PIN27      GPIOAO_3
   PIN28      GPIOAO_2
   PIN32      GPIOAO_4
   PIN35      GPIOAO_8
   PIN36      GPIOH_8
   PIN37      GPIOAO_9
   PIN38      GPIOAO_10
   PIN40      GPIOAO_11

3 I2C:

   PIN23      I2C1_SCL
   PIN24      I2C1_SDA
   PIN13      I2C1_SCL
   PIN16      I2C1_SDA    //these are two i2c-1, only one can be opened when using
   PIN3       I2C3_SDA
   PIN5       I2C3_SCL
   PIN7       I2C4_SDA
   PIN11      I2C4_SCL
   PIN27      I2C4_SDA
   PIN28      I2C4_SCL   ///these are two i2c-4, only one can be opened when using
  

2 SPI: pwm_c

   PIN12      SPI0RX        //must disable i2c1 uart1 pwm_c
   PIN13      SPI0CLK
   PIN16      SPI0CSN
   PIN18      SPI0TX
   PIN19      SPI1TX        //must disable i2c1 uart4
   PIN21      SPI1RX        
   PIN23      SPI1CLK
   PIN24      SPI1CSN

3 UART:

   PIN8       UART0_TX    
   PIN10      UART0_RX
   PIN7       UART1_RX    //must disable i2c4
   PIN11      UART1_TX
   PIN27      UART1_RX
   PIN28      UART1_TX    
   PIN23      UART4_TX    //must disable i2c1 spi1
   PIN24      UART4_RX

2 PWM:

   PIN18      PWM18
   PIN40      PWM40

For those Radxa Zero system images released, the configuration file is /boot/uEnv.txt. For more details, follow this guide, Device tree overlays.

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@zero:~$ 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

Use mraa-gpio tool to test

root@rockpis:~# mraa-gpio list
01         3V3:  
02          5V: 
03 I2C_EE_M3_S: GPIO I2C  
04          5V: 
05 I2C_EE_M3_S: GPIO I2C  
06         GND: 
07 I2C_AO_M0_S: GPIO I2C  UART 
08 UART_AO_A_T: GPIO UART 
09         GND: 
10 UART_AO_A_R: GPIO UART 
11 I2C_AO_M0_S: GPIO I2C  UART 
12  SPI_A_MISO: GPIO SPI  
13 I2C_EE_M1_S: GPIO I2C  SPI  
14         GND: 
15  SARADC_CH1: 
16 I2C_EE_M1_S: GPIO I2C  SPI  
17         3V3: 
18 SPI_A_MOSI,: GPIO SPI  PWM  
19  SPI_B_MOSI: GPIO SPI  
20         GND: 
21  SPI_B_MISO: GPIO SPI  
22     GPIOC_7: GPIO 
23 I2C_EE_M1_S: GPIO I2C  SPI  UART 
24 UART_EE_C_R: GPIO I2C  SPI  UART 
25         GND: 
26  SARADC_CH2: 
27 I2C_AO_M0_S: GPIO I2C  UART 
28 I2C_AO_M0_S: GPIO I2C  UART 
29          NC: 
30         GND: 
31          NC: 
32    GPIOAO_4: GPIO 
33          NC: 
34         GND: 
35 UART_AO_B_T: GPIO UART 
36     GPIOH_8: GPIO 
37 UART_AO_B_R: GPIO UART 
38   GPIOAO_10: GPIO 
39         GND: 
40     PWMAO_A: GPIO PWM  
root@rockpis:~# mraa-gpio set 40 1           #pin40 pull high
root@rockpis:~# mraa-gpio set 40 0           #pin40 pull low
PWM test

Radxa Zero provides 2 PWM, PWM_C and PWMAO_A. It is necessary to enable then at the same time if you need PWM function.

Firstly, modify the /boot/uEnv.txt file to add the following.

rock@zero:~$ cat /boot/uEnv.txt 
verbosity=7
console=ttyAML0,115200
overlay_prefix=meson
rootfstype=ext4
fdtfile=amlogic/meson-g12a-radxa-zero.dtb
overlays=meson-g12a-uart-ao-a-on-gpioao-0-gpioao-1 meson-g12a-pwm-c-on-gpiox-8 meson-g12a-pwmao-a-on-gpioao-11
rootuuid=bbe0631f-bbed-4033-8f75-fa5930deceed
initrdsize=0xf1ee3e
kernelversion=5.10.69-5-amlogic-gfeede6ac474e
initrdimg=initrd.img-5.10.69-5-amlogic-gfeede6ac474e
kernelimg=vmlinuz-5.10.69-5-amlogic-gfeede6ac474e

Modify the macro PWM in /home/rock/mraa-examples/pwm.c as follows to test PWM_C. To test PWMAO_A, change #define PWM 18 to #define PWM 40.

/* PWM declaration */
#define PWM 18

Save and compile pwm.c

rock@zero:~/mraa-examples$ gcc -o pwm pwm.c -lmraa
rock@zero:~/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

Radxa Zero provides 3 I2C,There are I2C1 ,I2C3, I2C4. 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

rock@zero:~$ cat /boot/uEnv.txt 
verbosity=7
console=ttyAML0,115200
overlay_prefix=meson
rootfstype=ext4
fdtfile=amlogic/meson-g12a-radxa-zero.dtb
overlays=meson-g12a-uart-ao-a-on-gpioao-0-gpioao-1 meson-g12a-i2c-ee-m1-gpioh-6-gpioh-7
rootuuid=bbe0631f-bbed-4033-8f75-fa5930deceed
initrdsize=0xf1ee3e
kernelversion=5.10.69-5-amlogic-gfeede6ac474e
initrdimg=initrd.img-5.10.69-5-amlogic-gfeede6ac474e
kernelimg=vmlinuz-5.10.69-5-amlogic-gfeede6ac474e

We use MPU6050 test i2c. Modify the macro MPU6050 in /home/rock/mraa-examples/i2c_mpu6050.c ,as follows to test I2C1.

  /* mraa header */
  #include "mraa/i2c.h"
  #define I2C_BUS 0
  
rock@zero:~/mraa-examples$ gcc -o i2c_mpu6050 i2c_mpu6050.c  -lmraa
rock@zero:~/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
   
accel: x:0 y:0 z:0
gyro: x:-5 y:48 z:-103
     
accel: x:0 y:0 z:0
gyro: x:3 y:3 z:5
UART test

Radxa Zero provides 3 UART,There are uart0 ,uart1, uart4. If you need the uart function, you can turn on one or more of them.

Firstly, Disable console and enable uart0 for uart test.modify the /boot/uEnv.txt file to add the following

rock@zero:~$ cat /boot/uEnv.txt 
verbosity=7
console=ttyAML0,115200
overlay_prefix=meson
rootfstype=ext4
fdtfile=amlogic/meson-g12a-radxa-zero.dtb
overlays=meson-g12a-uart-ao-a-on-gpioao-0-gpioao-1 meson-g12a-uart-ao-b-on-gpioao-2-gpioao-3
console=
rootuuid=bbe0631f-bbed-4033-8f75-fa5930deceed
initrdsize=0xf1ee3e
kernelversion=5.10.69-5-amlogic-gfeede6ac474e
initrdimg=initrd.img-5.10.69-5-amlogic-gfeede6ac474e
kernelimg=vmlinuz-5.10.69-5-amlogic-gfeede6ac474e

Here set console to NULL value. then reboot . use ssh connect to zero,we must have 2 windows,one is for send data,anothor is receiver by serial com.

send command follow:

rock@zero:~$ sudo mraa-uart dev 0 baud 1500000 send  radxa

Another window sets the baud 1500000 and receives the data

radxa

receive command follow:

rock@zero:~$ sudo mraa-uart dev 0 baud 1500000 recv  1000
radxa
SPI test

Firstly, modify the /boot/uEnv.txt file.

rock@zero:~$ cat /boot/uEnv.txt 
verbosity=7
console=ttyAML0,115200
overlay_prefix=meson
rootfstype=ext4
fdtfile=amlogic/meson-g12a-radxa-zero.dtb
overlays=meson-g12a-uart-ao-a-on-gpioao-0-gpioao-1 meson-g12a-spi-spidev 
param_spidev_spi_bus=0 
param_spidev_max_freq=10000000
rootuuid=bbe0631f-bbed-4033-8f75-fa5930deceed
initrdsize=0xf1ee3e
kernelversion=5.10.69-5-amlogic-gfeede6ac474e
initrdimg=initrd.img-5.10.69-5-amlogic-gfeede6ac474e
kernelimg=vmlinuz-5.10.69-5-amlogic-gfeede6ac474e

then execute command, sync, followed by reboot.

Create file test-spi.c and add the following contents.

rock@zero:~/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 0

/* 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@zero:~/mraa-examples$ gcc -o test-spi test-spi.c -lmraa

Then short pin12 and pin18 and run test-spi

rock@zero:~/mraa-examples$ sudo ./test-spi 
0xaa
0xaa
0xaa
0xaa
0xaa
0xaa
0xaa
0xaa
0xaa
0xaa
MRAA: SUCCESS

Development

More introduction of libmraa can look at libmraa official website.

Troubleshooting

Post your issue on the forum: https://forum.radxa.com/c/zero