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

Difference between revisions of "RockpiS/dev/libmraa"

< RockpiS‎ | dev
(test pwm)
Line 11: Line 11:
 
==== Install essential packages ====
 
==== Install essential packages ====
  
Package libmraa-rockpis is in the Radxa APT bionic-testing and buster-testing repositories.
+
Package libmraa is in the Radxa APT bionic-testing and buster-testing repositories.
  
 
Execute the following commands to add Radxa APT
 
Execute the following commands to add Radxa APT
  
    export DISTRO=bionic-testing      # for Bionic
+
  rock@rockpis:~$ export DISTRO=bionic-testing      # for Bionic
    # or
+
    export DISTRO=buster-testing      # for Buster
+
  
    echo "deb http://apt.radxa.com/$DISTRO/ ${DISTRO%-*} main" | sudo tee -a /etc/apt/sources.list.d/apt-radxa-com.list
+
or
  
Get the pub key
+
  rock@rockpis:~$ export DISTRO=buster-testing      # for Buster
    wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -
+
  rock@rockpis:~$ 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@rockpis:~$  wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -
  
 
Install essential packages:
 
Install essential packages:
    sudo apt-get update
+
 
    sudo apt-get install -y rockchip-overlay rockpis-dtbo libmraa
+
  rock@rockpis:~$ sudo apt-get update
 +
  rock@rockpis:~$ sudo apt-get install -y rockchip-overlay rockpis-dtbo libmraa
 +
 
 
==== Enable interface ====
 
==== Enable interface ====
See ROCK Pi S [[rockpiS/hardware/gpio | GPIO pintout]]. ROCK Pi S has a 26-pin colorful expansion header. Each pin is distinguished by color. mraa define follow:   
+
 
 +
See ROCK Pi S [[rockpiS/hardware/gpio | GPIO pintout]]. ROCK Pi S has a 26-pin colorful expansion header. Each pin is distinguished by color. mraa define is shown below:   
  
 
===== Hardware V12 =====
 
===== Hardware V12 =====
Line 58: Line 63:
 
     PIN15      I2C'''3'''_SCL
 
     PIN15      I2C'''3'''_SCL
 
    
 
    
 
 
1 SPI:
 
1 SPI:
 
     PIN19      SPI'''2'''TX        //must disable i2c0
 
     PIN19      SPI'''2'''TX        //must disable i2c0
Line 84: Line 88:
 
For those ROCK Pi S system images released before March 1st, 2020, modify '''/boot/uEnv.txt''' to enable '''pwm2,pwm3,uart1,uart2,i2c0,i2c1,i2c3''' for test.
 
For those ROCK Pi S system images released before March 1st, 2020, modify '''/boot/uEnv.txt''' to enable '''pwm2,pwm3,uart1,uart2,i2c0,i2c1,i2c3''' for test.
  
  rock@rockpis:/boot$ cat uEnv.txt
+
<pre>
  verbosity=7
+
rock@rockpis:~$ cat /boot/uEnv.txt
  overlay_prefix=rockchip
+
verbosity=7
  rootfstype=ext4
+
overlay_prefix=rockchip
  fdtfile=rockchip/rk3308-rock-pi-s.dtb
+
rootfstype=ext4
  overlays=rk3308-console-on-uart0 rk3308-i2c0 rk3308-i2c1 rk3308-i2c3
+
fdtfile=rockchip/rk3308-rock-pi-s.dtb
  initrdsize=0x62c6be
+
overlays=rk3308-console-on-uart0 rk3308-i2c0 rk3308-i2c1 rk3308-i2c3
  kernelversion=4.4.143-39-rockchip-gdaf243b9655a
+
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
  initrdimg=initrd.img-4.4.143-39-rockchip-gdaf243b9655a
+
initrdsize=0x64d1d4
  kernelimg=vmlinuz-4.4.143-39-rockchip-gdaf24
+
kernelversion=4.4.143-47-rockchip-g495ff574976c
 +
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
 +
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c
 +
</pre>
  
 
==== Test ====
 
==== 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.
 +
 +
  root@rockpis:~# 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.
  
 
===== test gpio =====
 
===== test gpio =====
Line 101: Line 116:
 
Use '''mraa-gpio tool''' to test
 
Use '''mraa-gpio tool''' to test
  
     root@rockpis:/# mraa-gpio list
+
     root@rockpis:~# mraa-gpio list
 
     01        3V3:  
 
     01        3V3:  
 
     02          5V:  
 
     02          5V:  
Line 128: Line 143:
 
     25        GND:  
 
     25        GND:  
 
     26    ADC_IN0: AIO  
 
     26    ADC_IN0: AIO  
     root@rockpis:/# mraa-gpio set 15 1          #pin15 pull high
+
     root@rockpis:~# mraa-gpio set 15 1          #pin15 pull high
     root@rockpis:/# mraa-gpio set 15 0          #pin15 pull high
+
     root@rockpis:~# mraa-gpio set 15 0          #pin15 pull high
    root@rockpis:/#
+
  
===== test pwm =====
+
===== PWM test =====
First, modify the / boot/uEnv.txt file to add the following
+
 
  rock@rockpis:~/mraa/src/arm$ cat /boot/uEnv.txt
+
ROCK Pi S V1.2 provides 2 PWM, PWM2 and PWM3. It is necessary to enable PWM1, PWM2 and PWM3 at the same time if you need PWM function.
  verbosity=7
+
 
  overlay_prefix=rockchip
+
Firstly, modify the /boot/uEnv.txt file to add the following
  rootfstype=ext4
+
 
  fdtfile=rockchip/rk3308-rock-pi-s.dtb
+
<pre>
  overlays=rk3308-console-on-uart0 '''rk3308-pwm1'''  
+
root@rockpis:~# cat /boot/uEnv.txt
  initrdsize=0x62c6be
+
verbosity=7
  kernelversion=4.4.143-39-rockchip-gdaf243b9655a
+
overlay_prefix=rockchip
  initrdimg=initrd.img-4.4.143-39-rockchip-gdaf243b9655a
+
rootfstype=ext4
  kernelimg=vmlinuz-4.4.143-39-rockchip-gdaf243b9655a
+
fdtfile=rockchip/rk3308-rock-pi-s.dtb
  rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
+
overlays=rk3308-console-on-uart0 '''rk3308-pwm1 rk3308-pwm2 rk3308-pwm3'''  
 +
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
 +
initrdsize=0x64d1d4
 +
kernelversion=4.4.143-47-rockchip-g495ff574976c
 +
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
 +
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c
 +
</pre>
 +
 
 +
Modify the macro PWM in /home/rock/mraa-examples/pwm.c as follows to test PWM2. To test PWM3, change '''#define PWM 11''' to '''#define PWM 13'''.
 +
 
 +
  '''/* PWM declaration */'''
 +
  '''#define PWM 11'''
  
Folder '''/mraa/examples/c/pwm.c'''  has examples of testing PWM,Modify the PWM pin in pwm.c as follows
 
  '''/* PWM declaration */'''
 
  '''#define PWM 11'''
 
 
Save and compile pwm.c
 
Save and compile pwm.c
  rock@rockpis:~/mraa/examples/c$ gcc -o pwm pwm.c -lmraa
+
  rock@rockpis:~/mraa-examples$ gcc -o pwm pwm.c -lmraa
  rock@rockpis:~/mraa/examples/c$ sudo ./pwm  
+
  rock@rockpis:~/mraa-examples$ sudo ./pwm  
  PWM value is 0.010015
+
  PWM value is 0.010015
  PWM value is 0.019985
+
  PWM value is 0.019985
  PWM value is 0.030000
+
  PWM value is 0.030000
  PWM value is 0.040014
+
  PWM value is 0.040014
  PWM value is 0.049984
+
  PWM value is 0.049984
  PWM value is 0.059999
+
  PWM value is 0.059999
 +
 
 +
===== I2C test =====
  
===== test i2c =====
+
Use mraa-i2c tool to test. Here we use I2C MPU6050
Use mraa-i2c tool to test,here we use I2C MPU6050
+
     rrock@rockpis:~/mraa-examples$ cat i2c_mpu6050.c  
     root@rockpis:/home/rock/c# cat i2c_mpu6050.c  
+
 
     /* standard headers */
 
     /* standard headers */
 
     #include <endian.h>
 
     #include <endian.h>
Line 172: Line 195:
 
     #include "mraa/i2c.h"
 
     #include "mraa/i2c.h"
 
      
 
      
     #define I2C_BUS 1
+
     '''#define I2C_BUS 1'''
 
      
 
      
 
     /* register definitions */
 
     /* register definitions */
Line 306: Line 329:
 
     }
 
     }
 
      
 
      
     root@rockpis:/home/rock/c# gcc i2c_mpu6050.c -lmraa
+
     rock@rockpis:~/mraa-examples$ gcc i2c_mpu6050 i2c_mpu6050.c -lmraa
    root@rockpis:/home/rock/c#
+
  
 
Run the compiled file
 
Run the compiled file
    root@rockpis:/home/rock/c# ./a.out
+
    rock@rockpis:~/mraa-examples$ sudo ./i2c_mpu6050
 
     accel: x:0 y:0 z:0
 
     accel: x:0 y:0 z:0
 
     gyro: x:-10 y:1 z:7
 
     gyro: x:-10 y:1 z:7
Line 326: Line 348:
 
     gyro: x:3 y:3 z:5
 
     gyro: x:3 y:3 z:5
  
===== test uart =====
+
===== UART test =====
enable uart1 for uart test
+
    root@rockpis:/home/rock# cat /boot/hw_intfc.conf
+
   
+
    # Hardware Interface Config
+
   
+
    # Set "on" to enable the optional hardware interfaces while set "off" to disable.
+
   
+
    intfc:i2c0=off
+
    intfc:i2c1=off
+
    intfc:i2c2=off
+
    intfc:i2c3=off
+
   
+
    intfc:pwm1=off
+
    intfc:pwm2=off
+
    intfc:pwm3=off
+
   
+
    intfc:spi0=off
+
    intfc:spi2=off
+
   
+
    intfc:uart0=off
+
    intfc:uart1=on
+
    intfc:uart2=on
+
   
+
    # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
+
   
+
    # Serial console on UART0
+
    intfc:dtoverlay=console-on-ttyS0
+
   
+
    # waveshare 3.5inch lcd (B v2) on SPI2. Need set:
+
    # intfc:uart1=off intfc:uart2=off intfc:i2c0=off intfc:spi2=on
+
    #intfc:dtoverlay=spi2-waveshare35b-v2
+
   
+
    # waveshare 3.5inch lcd (C) on SPI2. Need set:
+
    # intfc:uart1=off intfc:uart2=off intfc:i2c0=off intfc:spi2=on
+
    #intfc:dtoverlay=spi2-waveshare35c
+
   
+
    # Dummy spi device on SPI0 for test. Need set: intfc:spi0=on
+
    #intfc:dtoverlay=devspi0
+
   
+
    # Dummy spi device on SPI2 for test. Need set: intfc:spi2=on
+
    #intfc:dtoverlay=devspi2
+
   
+
    # Use /dev/ttyS0. Need set: intfc:uart0=on
+
    #intfc:dtoverlay=rk3308-uart0
+
   
+
    # Use /dev/ttyS1. Need set: intfc:uart1=on
+
    intfc:dtoverlay=rk3308-uart1
+
   
+
    # Use /dev/ttyS2. Need set: intfc:uart2=on
+
    intfc:dtoverlay=rk3308-uart2
+
 
+
then reboot and use serial port PIN23 and PIN24
+
 
+
send command follow:
+
    sudo mraa-uart dev 0 baud 1500000 send  data
+
  
receive command follow:
+
===== SPI test =====
    sudo mraa-uart dev 0 baud 1500000 recv  1000
+
  
the receive window will show
+
Firstly, modify the '''/ boot/uEnv.txt''' file.
    date
+
  
===== test spi =====
+
<pre>
First, modify the '''/ boot/uEnv.txt''' file to add the following
+
root@rockpis:~# cat /boot/uEnv.txt
  rock@rockpis:/boot$ cat uEnv.txt
+
verbosity=7
  verbosity=7
+
overlay_prefix=rockchip
  overlay_prefix=rockchip
+
rootfstype=ext4
  rootfstype=ext4
+
fdtfile=rockchip/rk3308-rock-pi-s.dtb
  fdtfile=rockchip/rk3308-rock-pi-s.dtb
+
overlays=rk3308-console-on-uart0 '''rk3308-spi-spidev'''
  overlays=rk3308-console-on-uart0 '''rk3308-spi-spidev'''
+
'''param_spidev_spi_bus=2'''
  '''param_spidev_spi_bus=2'''
+
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
  initrdsize=0x62c6be
+
initrdsize=0x64d1d4
  kernelversion=4.4.143-39-rockchip-gdaf243b9655a
+
kernelversion=4.4.143-47-rockchip-g495ff574976c
  initrdimg=initrd.img-4.4.143-39-rockchip-gdaf243b9655a
+
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
  kernelimg=vmlinuz-4.4.143-39-rockchip-gdaf243b9655a
+
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c
  rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
+
</pre>
  
then '''reboot'''
+
then execute command, sync, followed by '''reboot'''.
  
 
use c to test spi,compile spi.c,if use spi1,must disable
 
use c to test spi,compile spi.c,if use spi1,must disable
Line 483: Line 448:
 
   MRAA: SUCCESS
 
   MRAA: SUCCESS
  
 +
===== ADC test =====
  
===== test adc =====
 
 
connect signal you want to measure,and then compile mraa examplec file to test.
 
connect signal you want to measure,and then compile mraa examplec file to test.
  
Line 499: Line 464:
 
   ADC A0 read float - 0.41153
 
   ADC A0 read float - 0.41153
  
====Development====
+
==== Development ====
  
 
More introduction of libmraa can look at [http://iotdk.intel.com/docs/master/mraa/index.html libmraa official website].
 
More introduction of libmraa can look at [http://iotdk.intel.com/docs/master/mraa/index.html libmraa official website].
 +
 +
==== Troubleshooting ====
 +
 +
* Refer [[RockpiS/getting_started/troubleshooting | Troubleshooting page]]
 +
* Post your issue on the forum: https://forum.radxa.com/c/rockpiS

Revision as of 12:05, 21 April 2020

       ROCK Pi S >  Development >  Install Libmraa 

Libmraa on ROCK Pi S

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

Install essential packages

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

Execute the following commands to add Radxa APT

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

or

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

Install essential packages:

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

Enable interface

See ROCK Pi S GPIO pintout. ROCK Pi S has a 26-pin colorful expansion header. Each pin is distinguished by color. mraa define is shown below:

Hardware V12

16 GPIO:

   PIN03      GPIO2_A2
   PIN05      GPIO2_B0
   PIN07      GPIO2_B3
   PIN09      GPIO4_C4
   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
   PIN26      GPIO1_B2

3 I2C:

   PIN23      I2C0_SDA
   PIN24      I2C0_SCL
   PIN3       I2C1_SCL
   PIN5       I2C1_SDA
   PIN13      I2C3_SDA
   PIN15      I2C3_SCL
  

1 SPI:

   PIN19      SPI2TX        //must disable i2c0
   PIN21      SPI2RX        //must disable i2c0
   PIN23      SPI2CLK
   PIN24      SPI2CSN

3 UART:

   PIN8       UART0_TX
   PIN10      UART0_RX
   PIN23      UART1_TX
   PIN24      UART1_RX
   PIN19      UART2_RX
   PIN21      UART2_TX

2 PWM:

   PIN11      PWM2
   PIN13      PWM3

1 ADC:

   PIN26      ADC_IN0          //the measure voltage must lower than 1.8v

For those ROCK Pi S system images released after March 1st, 2020, to enable pwm2,pwm3,uart1,uart2,i2c0,i2c1,i2c3 and so overlays=, need to follow Device tree overlays.

For those ROCK Pi S system images released before March 1st, 2020, modify /boot/uEnv.txt to enable pwm2,pwm3,uart1,uart2,i2c0,i2c1,i2c3 for test.

rock@rockpis:~$ cat /boot/uEnv.txt
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3308-rock-pi-s.dtb
overlays=rk3308-console-on-uart0 rk3308-i2c0 rk3308-i2c1 rk3308-i2c3
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
initrdsize=0x64d1d4
kernelversion=4.4.143-47-rockchip-g495ff574976c
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c

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.

 root@rockpis:~# 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.

test gpio

Use mraa-gpio tool to test

   root@rockpis:~# mraa-gpio list
   01         3V3: 
   02          5V: 
   03     I2C_SDA: GPIO I2C  
   04          5V: 
   05     I2C_SCL: GPIO I2C  
   06         GND: 
   07 I2S0_8CH_MC: GPIO 
   08    UART0_TX: GPIO UART 
   09         GND: 
   10    UART0_RX: GPIO UART 
   11 PWM2,I2C3_S: GPIO I2C  PWM  
   12 I2S0_8CH_SC: GPIO 
   13 PWM3,I2C3_S: GPIO I2C  PWM  
   14         GND: 
   15    SPDIF_TX: GPIO 
   16 I2S0_8CH_SD: GPIO 
   17         3V3: 
   18 I2S0_8CH_SD: GPIO 
   19 UART1_RTSN,: GPIO SPI  UART 
   20         GND: 
   21 UART1_CTSN,: GPIO SPI  UART 
   22 I2S0_8CH_LR: GPIO 
   23 UART1_RX,I2: GPIO I2C  SPI  UART 
   24 UART1_TX,I2: GPIO I2C  SPI  UART 
   25         GND: 
   26     ADC_IN0: AIO 
   root@rockpis:~# mraa-gpio set 15 1           #pin15 pull high
   root@rockpis:~# mraa-gpio set 15 0           #pin15 pull high
PWM test

ROCK Pi S V1.2 provides 2 PWM, PWM2 and PWM3. It is necessary to enable PWM1, PWM2 and PWM3 at the same time if you need PWM function.

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

root@rockpis:~# cat /boot/uEnv.txt
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3308-rock-pi-s.dtb
overlays=rk3308-console-on-uart0 '''rk3308-pwm1 rk3308-pwm2 rk3308-pwm3''' 
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
initrdsize=0x64d1d4
kernelversion=4.4.143-47-rockchip-g495ff574976c
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c

Modify the macro PWM in /home/rock/mraa-examples/pwm.c as follows to test PWM2. To test PWM3, change #define PWM 11 to #define PWM 13.

 /* PWM declaration */
 #define PWM 11

Save and compile pwm.c

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

Use mraa-i2c tool to test. Here we use I2C MPU6050

   rrock@rockpis:~/mraa-examples$ cat i2c_mpu6050.c 
   /* standard headers */
   #include <endian.h>
   #include <signal.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   
   /* mraa header */
   #include "mraa/i2c.h"
   
   #define I2C_BUS 1
   
   /* register definitions */
   #define MPU6050_ADDR 0x68
   #define MPU6050_REG_PWR_MGMT_1 0x6b
   #define MPU6050_REG_RAW_ACCEL_X 0x3b
   #define MPU6050_REG_RAW_ACCEL_Y 0x3d
   #define MPU6050_REG_RAW_ACCEL_Z 0x3f
   #define MPU6050_REG_RAW_GYRO_X 0x43
   #define MPU6050_REG_RAW_GYRO_Y 0x45
   #define MPU6050_REG_RAW_GYRO_Z 0x47
   
   /* bit definitions */
   #define MPU6050_RESET 0x80
   #define MPU6050_SLEEP (1 << 6)
   #define MPU6050_PLL_GYRO_X (1 << 1)
   
   /* accelerometer scale factor for (+/-)2g */
   #define MPU6050_ACCEL_SCALE 16384.0
   
   /* gyroscope scale factor for (+/-)250/s */
   #define MPU6050_GYRO_SCALE 131.0
   
   volatile sig_atomic_t flag = 1;
   
   void
   sig_handler(int signum)
   {
       if (signum == SIGINT) {
           fprintf(stdout, "Exiting...\n");
           flag = 0;
       }
   }
   
   int16_t
   i2c_read_word(mraa_i2c_context dev, uint8_t command)
   {
       return be16toh(mraa_i2c_read_word_data(dev, command));
   }
       
   int
   main(void)
   {
       mraa_result_t status = MRAA_SUCCESS;
       mraa_i2c_context i2c;
       uint8_t data;
       int16_t accel_data[3];
       int16_t gyro_data[3];
       int ret;
   
       /* install signal handler */
       signal(SIGINT, sig_handler);
            
       /* initialize mraa for the platform (not needed most of the times) */
       mraa_init();
   
       //! [Interesting]
       /* initialize I2C bus */
       i2c = mraa_i2c_init(I2C_BUS);
       if (i2c == NULL) {
           fprintf(stderr, "Failed to initialize I2C\n");
           mraa_deinit();
           return EXIT_FAILURE;
       }
   
       /* set slave address */
       status = mraa_i2c_address(i2c, MPU6050_ADDR);
       if (status != MRAA_SUCCESS) {
           goto err_exit;
       }
   
       /* reset the sensor */
       status = mraa_i2c_write_byte_data(i2c, MPU6050_RESET, MPU6050_REG_PWR_MGMT_1);
       if (status != MRAA_SUCCESS) {
           goto err_exit;
       }
   
       sleep(5);
    
       /* configure power management register */
       ret = mraa_i2c_read_byte_data(i2c, MPU6050_REG_PWR_MGMT_1);
       if (ret == -1) {
           return EXIT_FAILURE;
       }
   
       data = ret;
       data |= MPU6050_PLL_GYRO_X;
       data &= ~(MPU6050_SLEEP);
   
       status = mraa_i2c_write_byte_data(i2c, data, MPU6050_REG_PWR_MGMT_1);
       if (status != MRAA_SUCCESS) {
       goto err_exit;
   }
   
       sleep(5);
   
       while (flag) {
           /* read raw accel data */
           accel_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_X) / MPU6050_ACCEL_SCALE;
           accel_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Y) / MPU6050_ACCEL_SCALE;
           accel_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Z) / MPU6050_ACCEL_SCALE;
   
           /* read raw gyro data */
           gyro_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_X) / MPU6050_GYRO_SCALE;
           gyro_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Y) / MPU6050_GYRO_SCALE;
           gyro_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Z) / MPU6050_GYRO_SCALE;
   
           fprintf(stdout, "accel: x:%d y:%d z:%d\n", accel_data[0], accel_data[1], accel_data[2]);
           fprintf(stdout, "gyro: x:%d y:%d z:%d\n\n", gyro_data[0], gyro_data[1], gyro_data[2]);
   
           sleep(2);
       }
       
       /* stop i2c */
       mraa_i2c_stop(i2c);
   
       //! [Interesting]
       /* deinitialize mraa for the platform (not needed most of the times) */
       mraa_deinit();
    
       return EXIT_SUCCESS;
     
   err_exit:
       mraa_result_print(status);
    
       /* stop i2c */
       mraa_i2c_stop(i2c);
    
       /* deinitialize mraa for the platform (not needed most of the times) */
       mraa_deinit();
   
       return EXIT_FAILURE;
   }
   
   rock@rockpis:~/mraa-examples$ gcc i2c_mpu6050 i2c_mpu6050.c -lmraa

Run the compiled file

   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
   
   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
SPI test

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

root@rockpis:~# cat /boot/uEnv.txt
verbosity=7
overlay_prefix=rockchip
rootfstype=ext4
fdtfile=rockchip/rk3308-rock-pi-s.dtb
overlays=rk3308-console-on-uart0 '''rk3308-spi-spidev'''
'''param_spidev_spi_bus=2'''
rootuuid=37055840-4ec4-444f-979b-9e47ee4bd848
initrdsize=0x64d1d4
kernelversion=4.4.143-47-rockchip-g495ff574976c
initrdimg=initrd.img-4.4.143-47-rockchip-g495ff574976c
kernelimg=vmlinuz-4.4.143-47-rockchip-g495ff574976c

then execute command, sync, followed by reboot.

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

  rock@rockpis:~$ 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@rockpis:~$ gcc -o test-spi test-spi.c -lmraa

then short pin19 and pin21 and run a.out

  rock@rockpis:~$ 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 examplec file to test.

Folder /mraa/examples/c/aio.c has examples of testing ADC,compiling it and executing

  rock@rockpis:~/mraa/examples/c$ gcc aio.c -lmraa
  rock@rockpis:~/mraa/examples/c$ ./a.out
  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