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

Difference between revisions of "Rock3/dev/libmraa"

< Rock3‎ | dev
Line 5: Line 5:
 
     [[rock3 | ROCK 3]] > [[rock3/dev | Development]] > [[rock3/dev/libmraa  | Install Libmraa ]]
 
     [[rock3 | ROCK 3]] > [[rock3/dev | Development]] > [[rock3/dev/libmraa  | Install Libmraa ]]
  
=== Libmraa on ROCK 3 ===
+
=== Libmraa on ROCK 3A ===
  
This guide describes how to use libmraa on ROCK 3.
+
This guide describes how to use libmraa on ROCK 3 to access GPIO, SPI, and other peripherals.
 +
 
 +
=== Requirement ===
 +
* ROCK 3A
 +
* You need our latest [https://github.com/radxa/rock-3-images-released/releases official image]
  
 
=== Install essential packages ===
 
=== Install essential packages ===
Edit your /etc/apt/sources.list.d/apt-radxa-com.list as following:
+
Prebuilt libmraa is located in our Radxa APT Focal and Buster repositories.
  
* '''For Debian Buster'''
+
Update your <code>/etc/apt/sources.list.d/apt-radxa-com.list</code> accordingly to enable Radxa APT repository.
    deb http://apt.radxa.com/buster-stable/ buster main
+
    deb http://apt.radxa.com/buster-testing/ buster main
+
* '''For Ubuntu Focal'''
+
    deb http://apt.radxa.com/focal-stable/ focal main
+
    deb http://apt.radxa.com/focal-testing/ focal main
+
 
+
Get the pub key
+
  
 
* '''For Debian Buster'''
 
* '''For Debian Buster'''
    wget -O - apt.radxa.com/buster-testing/public.key | sudo apt-key add -
+
  deb http://apt.radxa.com/buster-stable/ buster main
 +
deb http://apt.radxa.com/buster-testing/ buster main
 
* '''For Ubuntu Focal'''
 
* '''For Ubuntu Focal'''
    wget -O - apt.radxa.com/focal-testing/public.key | sudo apt-key add -
+
  deb http://apt.radxa.com/focal-stable/ focal main
 
+
  deb http://apt.radxa.com/focal-testing/ focal main
Update  
+
    sudo apt-get update
+
  
Install essential packages:
+
Update and install libmraa
    sudo apt-get install -y rockchip-overlay && sudo apt-get install -y linux-4.19-rock-3-latest
+
sudo apt-get update
 +
sudo apt-get install libmraa
  
 
==== Enable interface ====
 
==== Enable interface ====
Line 109: Line 106:
 
     PIN37      ADC_IN5          //the measure voltage must lower than 1.8v
 
     PIN37      ADC_IN5          //the measure voltage must lower than 1.8v
  
When using, you need to pay attention to the pin's multiplexing function to see if there is a conflict.
+
Since many peripherals are reusing the same physical pin, all but one has to be disabled to allow proper functionality. While in a normal PC user can easily disable or enable peripherals in their UEFI firmware, currently there is no such universal equivalent in ARM world. Instead, Device tree blobs (dtb) and Device tree overlays (dtbo) are used to inform Linux kernel the existence of external devices. Please refer to [[Device-tree-overlays#Part_One:_Rockchip | this guide]] on available dtbo for ROCK 3A. Please read those carefully or you may get an unbootable system.
==== 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.
+
==== Run libmraa tests ====
 +
 
 +
After set correct dtbo to use, reboot Radxa Zero for change to take effect. You can now try some examples supplied by libmraa. They can be used to test SPI, UART, I2C, GPIO, and so on.
 +
 
 +
In this example, we will use the sample files located under /usr/local/share/mraa/examples/c.
  
 
And we copy them to home directory.
 
And we copy them to home directory.
Line 225: Line 225:
  
 
===== UART test =====
 
===== UART test =====
ROCK 3A V1.3 provides 6 UART,There are  '''uart0''' ,'''uart3''', '''uart5''', '''uart7''', '''uart8''', '''uart9''' .
+
ROCK 3A V1.3 provides 6 UART,There are  '''uart0''' ,'''uart3''', '''uart5''', '''uart7''', '''uart8''', '''uart9'''.
 
If you need the uart function, you can turn on one or more of them.
 
If you need the uart function, you can turn on one or more of them.
  
Line 255: Line 255:
 
===== SPI test =====
 
===== SPI test =====
  
Firstly, modify the '''/boot/uEnv.txt''' file.
+
Change overlays in /boot/uEnv.txt and reboot
  
 
  root@rock3a:~# cat /boot/uEnv.txt  
 
  root@rock3a:~# cat /boot/uEnv.txt  
Line 351: Line 351:
 
===== PWM test =====
 
===== PWM test =====
  
ROCK 3A V1.3 provides 7 PWM, '''but only one PWM function can be used at a time currently'''.  
+
ROCK 3A V1.3 provides 7 PWM, there are '''pwm1-m0''', '''pwm1-m1''', '''pwm2-m0''', '''pwm2-m1''', '''pwm9-m0''', '''pwm12-m1''', '''pwm13-m1''', '''pwm14-m0''', '''pwm14-m1''', '''pwm15-m0''', '''pwm15-m1'''.
 +
 
 +
'''but only one PWM function can be used at a time currently'''.  
  
 
Firstly, modify the /boot/uEnv.txt file to add the following
 
Firstly, modify the /boot/uEnv.txt file to add the following

Revision as of 08:15, 9 December 2021

    ROCK 3 >  Development >  Install Libmraa 

Libmraa on ROCK 3A

This guide describes how to use libmraa on ROCK 3 to access GPIO, SPI, and other peripherals.

Requirement

Install essential packages

Prebuilt libmraa is located in our Radxa APT Focal and Buster repositories.

Update your /etc/apt/sources.list.d/apt-radxa-com.list accordingly to enable Radxa APT repository.

  • For Debian Buster
deb http://apt.radxa.com/buster-stable/ buster main
deb http://apt.radxa.com/buster-testing/ buster main
  • For Ubuntu Focal
deb http://apt.radxa.com/focal-stable/ focal main
deb http://apt.radxa.com/focal-testing/ focal main

Update and install libmraa

sudo apt-get update
sudo apt-get install libmraa

Enable interface

See ROCK 3A GPIO pintout. ROCK 3A has a 40 pins colorful expansion header. Each pin is distinguished by color. mraa define is shown below:

Hardware V1.3

25 GPIO:

   PIN03      GPIO1_A0
   PIN05      GPIO1_A1
   PIN07      GPIO0_B5
   PIN11      GPIO3_C4
   PIN12      GPIO3_A3
   PIN13      GPIO3_C5
   PIN15      GPIO0_C0
   PIN16      GPIO0_B6
   PIN18      GPIO3_B2
   PIN19      GPIO4_C3
   PIN21      GPIO4_C5
   PIN22      GPIO0_C1
   PIN23      GPIO4_C2
   PIN24      GPIO4_C6
   PIN26      GPIO4_D1
   PIN27      GPIO0_B4
   PIN28      GPIO0_B3
   PIN29      GPIO2_D7
   PIN31      GPIO3_A0
   PIN32      GPIO3_C2
   PIN33      GPIO3_C3
   PIN35      GPIO3_A4
   PIN36      GPIO3_A2
   PIN38      GPIO3_A6
   PIN40      GPIO3_A5

3 I2C:

   PIN27      I2C1_SDA
   PIN28      I2C1_SCL
   PIN7       I2C2_SCL_M0
   PIN16      I2C2_SDA_M0
   PIN3       I2C3_SDA_M0
   PIN5       I2C3_SCL_M0
  

1 SPI:

   PIN19      SPI3_MOSI_M1
   PIN21      SPI3_MISO_M1
   PIN23      SPI3_CLK_M1
   PIN24      SPI3_CS0_M1

6 UART:

   PIN22      UART0_TX
   PIN15      UART0_RX
   PIN5       UART3_TX_M0
   PIN3       UART3_RX_M0
   PIN32      UART5_TX_M1
   PIN33      UART5_RX_M1
   PIN11      UART7_TX_M1
   PIN13      UART7_RX_M1
   PIN29      UART8_TX_M1
   PIN31      UART8_RX_M1
   PIN21      UART9_TX_M1
   PIN24      UART9_RX_M1

7 PWM: (Currently only one PWM function can be used at a time)

   PIN15      PWM1_M0
   PIN7       PWM1_M1
   PIN22      PWM2_M0
   PIN16      PWM2_M1
   PIN18      PWM9_M0
   PIN21      PWM12_M1
   PIN24      PWM13_M1
   PIN11      PWM14_M0
   PIN23      PWM14_M1
   PIN13      PWM15_M0
   PIN19      PWM15_M1

1 ADC:

   PIN37      ADC_IN5          //the measure voltage must lower than 1.8v

Since many peripherals are reusing the same physical pin, all but one has to be disabled to allow proper functionality. While in a normal PC user can easily disable or enable peripherals in their UEFI firmware, currently there is no such universal equivalent in ARM world. Instead, Device tree blobs (dtb) and Device tree overlays (dtbo) are used to inform Linux kernel the existence of external devices. Please refer to this guide on available dtbo for ROCK 3A. Please read those carefully or you may get an unbootable system.

Run libmraa tests

After set correct dtbo to use, reboot Radxa Zero for change to take effect. You can now try some examples supplied by libmraa. They can be used to test SPI, UART, I2C, GPIO, and so on.

In this example, we will use the sample files located under /usr/local/share/mraa/examples/c.

And we copy them to home directory.

rock@rock3a:~/mraa-examples$ sudo 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

rock@rock3a:~$ mraa-gpio list
01         3V3: 
02          5V: 
03 I2C3_SDA_M0: GPIO I2C  UART 
04          5V: 
05 I2C3_SCL_M0: GPIO I2C  UART 
06         GND: 
07 I2C2_SCL_M0: GPIO I2C  PWM  
08   UART2_TXD: GPIO UART 
09         GND: 
10   UART2_RXD: GPIO UART 
11 PWM14_M0,UA: GPIO PWM  UART 
12    GPIO3_A3: GPIO 
13 PWM15_IR_M0: GPIO PWM  UART 
14         GND: 
15 PWM1_M0,UAR: GPIO PWM  UART 
16 I2C2_SDA_M0: GPIO I2C  PWM  
17         3V3: 
18     PWM9_M0: GPIO PWM  
19 PWM15_IR_M1: GPIO SPI  PWM  
20         GND: 
21 PWM12_M1,SP: GPIO SPI  PWM  UART 
22 PWM2_M0,UAR: GPIO PWM  UART 
23 PWM14_M1,SP: GPIO SPI  PWM  
24 PWM13_M1,SP: GPIO SPI  PWM  UART 
25         GND: 
26 SPI3_CS1_M1: GPIO SPI  
27    I2C1_SDA: GPIO I2C  
28    I2C1_SCL: GPIO I2C  
29 UART8_TX_M1: GPIO UART 
30         GND: 
31 UART8_RX_M1: GPIO UART 
32 UART5_TX_M1: GPIO UART 
33 UART5_RX_M1: GPIO UART 
34         GND: 
35    GPIO3_A4: GPIO 
36    GPIO3_A2: GPIO 
37 SARADC_VIN5: AIO  
38    GPIO3_A6: GPIO 
39         GND: 
40    GPIO3_A5: GPIO 

root@rock3a:~$ mraa-gpio set 40 1           #pin40 pull high
root@rock3a:~# mraa-gpio set 40 0           #pin40 pull low
I2C test

ROCK 3A V1.3 provides 3 I2C,There are I2C1 , I2C2m0, I2C3m0. 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@rock3a:~# cat /boot/uEnv.txt 
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3568-rock-3-a.dtb
overlays=rk3568-pwm8-m0-fan rk3568-i2c3-m0
rootuuid=ee2ce23a-e0b1-4625-a54b-6ce12c167e97
initrdsize=0x81bfa9
kernelversion=4.19.193-16-rockchip-g0cca7bdf9cdb
initrdimg=initrd.img-4.19.193-16-rockchip-g0cca7bdf9cdb
kernelimg=vmlinuz-4.19.193-16-rockchip-g0cca7bdf9cdb

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

root@rock3a:~# mraa-i2c list
Bus   0: id=01 type=linux  default
Bus   1: id=02 type=linux 
Bus   2: id=03 type=linux 

root@rock3a:~# mraa-i2c detect 2
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  

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

  /* mraa header */
  #include "mraa/i2c.h"
  #define I2C_BUS 2
  
rock@rock3a:~/mraa-examples$ gcc -o i2c_mpu6050 i2c_mpu6050.c  -lmraa
rock@rock3a:~/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
UART test

ROCK 3A V1.3 provides 6 UART,There are uart0 ,uart3, uart5, uart7, uart8, uart9. 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

root@rock3a:~# cat /boot/uEnv.txt 
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3568-rock-3-a.dtb
overlays=rk3568-pwm8-m0-fan rk3568-uart0
rootuuid=ee2ce23a-e0b1-4625-a54b-6ce12c167e97
initrdsize=0x81bfa9
kernelversion=4.19.193-16-rockchip-g0cca7bdf9cdb
initrdimg=initrd.img-4.19.193-16-rockchip-g0cca7bdf9cdb
kernelimg=vmlinuz-4.19.193-16-rockchip-g0cca7bdf9cdb

Here set console to NULL value.

then reboot . use ssh connect to rock3a in a new window, to send data.

send command follow:

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

Another window sets the baud 1500000 and receives the data by serial console

radxaradxa
SPI test

Change overlays in /boot/uEnv.txt and reboot

root@rock3a:~# cat /boot/uEnv.txt 
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3568-rock-3-a.dtb
overlays=rk3568-pwm8-m0-fan rk3568-spi3-m1-cs0-spidev
rootuuid=ee2ce23a-e0b1-4625-a54b-6ce12c167e97
initrdsize=0x81bfa9
kernelversion=4.19.193-16-rockchip-g0cca7bdf9cdb
initrdimg=initrd.img-4.19.193-16-rockchip-g0cca7bdf9cdb
kernelimg=vmlinuz-4.19.193-16-rockchip-g0cca7bdf9cdb

then execute command, sync, followed by reboot.

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

rock@rock3a:~/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 3

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

Then short pin19 and pin21 and run test-spi

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

ROCK 3A V1.3 provides 7 PWM, there are pwm1-m0, pwm1-m1, pwm2-m0, pwm2-m1, pwm9-m0, pwm12-m1, pwm13-m1, pwm14-m0, pwm14-m1, pwm15-m0, pwm15-m1.

but only one PWM function can be used at a time currently. 

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

root@rock3a:~# cat /boot/uEnv.txt 
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3568-rock-3-a.dtb
overlays=rk3568-pwm8-m0-fan rk3568-pwm1-m0
rootuuid=ee2ce23a-e0b1-4625-a54b-6ce12c167e97
initrdsize=0x81bfa9
kernelversion=4.19.193-16-rockchip-g0cca7bdf9cdb
initrdimg=initrd.img-4.19.193-16-rockchip-g0cca7bdf9cdb
kernelimg=vmlinuz-4.19.193-16-rockchip-g0cca7bdf9cdb

Modify the macro PWM in /home/rock/mraa-examples/pwm.c as follows to test PWM1_M0.

/* PWM declaration */
#define PWM 15        //15 means its pin number

Save and compile pwm.c

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

connect signal you want to measure, and then compile mraa example c file, aio.c, to test.

rock@rock3a:~/mraa-examples$ gcc -o aio aio.c -lmraa
rock@rock3a:~/mraa-examples$ sudo ./aio 
ADC A0 read 20F - 527
ADC A0 read float - 0.51808
ADC A0 read 212 - 530
ADC A0 read float - 0.51906
ADC A0 read 213 - 531
ADC A0 read float - 0.51711

Development

More introduction of libmraa can look at libmraa official website.

Troubleshooting

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