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

Difference between revisions of "Rockpi4/dev/libmraa"

< Rockpi4‎ | dev
(test spi)
(test pwm)
(45 intermediate revisions by 4 users not shown)
Line 5: Line 5:
 
       [[rockpi4 | ROCK Pi 4]] > [[rockpi4/dev | Development]] > [[rockpi4/dev/libmraa  | Install Libmraa ]]
 
       [[rockpi4 | ROCK Pi 4]] > [[rockpi4/dev | Development]] > [[rockpi4/dev/libmraa  | Install Libmraa ]]
  
 
+
== Install Libmraa ==
= Libmraa-rockpi4 =
+
 
+
 
Below is the instructions of how to install  '''libmraa'''.
 
Below is the instructions of how to install  '''libmraa'''.
  
== Install essential packages ==
+
=== Install essential packages ===
 
+
 
Edit your /etc/apt/sources.list and add the following:
 
Edit your /etc/apt/sources.list and add the following:
  
'''For Debian Stretch'''
+
* '''For Debian Buster'''
 +
    deb http://apt.radxa.com/buster-testing/ buster main
 +
* '''For Ubuntu Focal'''
 +
    deb http://apt.radxa.com/focal-testing/ focal main
  
    deb http://apt.radxa.com/stretch/ stretch main
+
Get the pub key
  
'''For Ubuntu bionic'''
+
* '''For Debian Buster'''
 
+
     wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -
     deb http://apt.radxa.com/bionic/ bionic main
+
* '''For Ubuntu Focal'''
 
+
     wget -O -  apt.radxa.com/focal-testing/public.key | sudo apt-key add -
Get the pub key
+
     wget -O -  apt.radxa.com/stretch/public.key | sudo apt-key add -
+
  
 
Update   
 
Update   
Line 29: Line 27:
  
 
Install essential packages:
 
Install essential packages:
     sudo apt-get install -y rockchip-overlay rockchip-fstab && sudo apt-get install -y rockpi4-dtbo
+
     sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtbo
  
== Install the latest u-boot and kernel==  
+
=== Install the latest u-boot and kernel (optional) ===  
     apt-get install rockpi4b-rk-u-boot-lastest
+
     apt-get install rockpi4b-rk-u-boot-latest
  
 
Manually run the u-boot flash script
 
Manually run the u-boot flash script
 
     /usr/local/sbin/rockpi4b_upgrade_bootloader.sh
 
     /usr/local/sbin/rockpi4b_upgrade_bootloader.sh
 
  
 
Type '''YES''' to perform the uboot.img flashing.
 
Type '''YES''' to perform the uboot.img flashing.
 
 
then install kernel   
 
then install kernel   
 
     apt-get install linux-base
 
     apt-get install linux-base
     apt-get install linux-4.4-lastest
+
     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.
 
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 ==  
+
=== Enable interface ===  
 
See rockpi4 [[Rockpi4/hardware/rockpi4#gpio | GPIO pinout]],ROCK Pi 4 has a 40-pin expansion header. Each pin is distinguished by color. mraa define follow:   
 
See rockpi4 [[Rockpi4/hardware/rockpi4#gpio | GPIO pinout]],ROCK Pi 4 has a 40-pin expansion header. Each pin is distinguished by color. mraa define follow:   
  
Line 108: Line 103:
 
     root@localhost:/# cat boot/hw_intfc.conf  
 
     root@localhost:/# cat boot/hw_intfc.conf  
 
      
 
      
     # Hardware Interface Config
+
     # 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.'''
 
      
 
      
    # For more details, check file /usr/local/share/doc/rockpi4-dtbo/README
+
     '''intfc:pwm0=off'''
   
+
     '''intfc:pwm1=off'''
    # Uncomment configuration line if you use it. Take i2c6 as an example,
+
     '''intfc:uart2=off'''
    # setting off disables i2c6 bus and setting on enables i2c6 bus.
+
     '''intfc:uart4=off'''
   
+
     '''intfc:spi1=off'''
     '''intfc:pwm0=on'''
+
     '''intfc:spi2=off'''
     '''intfc:pwm1=on'''
+
     '''intfc:i2c2=off'''
     '''intfc:uart2=on'''
+
     '''intfc:i2c6=off'''
     '''intfc:uart4=on'''
+
     '''intfc:i2c7=off'''
     intfc:spi1=off
+
     intfc:spi2=off
+
     '''intfc:i2c2=on'''
+
     '''intfc:i2c6=on'''
+
     '''intfc:i2c7=on'''
+
 
      
 
      
     # DTS Overlay Config
+
     # '''Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.'''
    # 1. check the name.dtbo in /boot/overlays
+
    # 2. add intfc:dtoverlay=name as below
+
 
      
 
      
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=two-color-led
 
     #intfc:dtoverlay=two-color-led
     '''intfc:dtoverlay=console-disabled'''
+
     #intfc:dtoverlay=console-on-ttyS2
     #intfc:dtoverlay=console-on-uart4
+
     #intfc:dtoverlay=console-on-ttyS4
     #intfc:dtoverlay=devspi
+
     #intfc:dtoverlay=devspi1
 
     #intfc:dtoverlay=devspi2
 
     #intfc:dtoverlay=devspi2
 
     root@localhost:/#  
 
     root@localhost:/#  
then '''reboot'''
+
then '''reboot''' to make them effective
  
==Install libmraa-rockpi4==  
+
=== Install libmraa ===  
  
     apt-get install libmraa-rockpi4
+
     apt-get install libmraa
  
==Test==
+
=== Test ===
===test gpio===
+
====test gpio====
  
 
Use '''mraa-gpio''' tool to test   
 
Use '''mraa-gpio''' tool to test   
  
     rock@localhost:~/mraa/build$ mraa-gpio list  
+
     rock@rockpi4b:~$ mraa-gpio list  
 
     01        3V3:  
 
     01        3V3:  
 
     02          5V:  
 
     02          5V:  
Line 188: Line 178:
 
     39        GND:  
 
     39        GND:  
 
     40    GPIO4_A7: GPIO  
 
     40    GPIO4_A7: GPIO  
     rock@localhost:~/mraa/build$  
+
     rock@rockpi4b:~$  
     rock@localhost:~/mraa/build$  sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                       
+
     rock@rockpi4b:~$  sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                       
     rock@localhost:~/mraa/build$  sudo mraa-gpio set 40 0  //pin40 pull low
+
     rock@rockpi4b:~$  sudo mraa-gpio set 40 0  //pin40 pull low
    root@localhost:~#
+
  
===test i2c===
+
==== test i2c ====
Use '''mraa-i2c''' tool to test,we must have a i2c slave device,for example,we use I2C EEPROM:
+
Firstly, modify the /boot/hw_intfc.conf file as following and then reboot to enable i2c2 i2c6 i2c7.
     rock@localhost:~/mraa/build$ mraa-i2c list
+
    # 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=off
 +
    intfc:pwm1=off
 +
    intfc:uart2=off
 +
    intfc:uart4=off
 +
    intfc:spi1=off
 +
    intfc:spi2=off
 +
    intfc:i2c2=on
 +
    intfc:i2c6=on
 +
    intfc:i2c7=on
 +
 
 +
 
 +
Then, use '''mraa-i2c''' tool to test,we must have a i2c slave device,for example,we use I2C EEPROM:
 +
     rock@rockpi4b:~$ mraa-i2c list
 
     Bus  0: id=07 type=linux  default
 
     Bus  0: id=07 type=linux  default
 
     Bus  1: id=02 type=linux  
 
     Bus  1: id=02 type=linux  
 
     Bus  2: id=06 type=linux  
 
     Bus  2: id=06 type=linux  
     rock@localhost:~/mraa/build$ sudo mraa-i2c detect 0      //device on bus 0  //i2c7
+
     rock@rockpi4b:~$ sudo mraa-i2c detect 0      //device on bus 0  //i2c7
 
     00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 
     00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 
     10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 
     10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
Line 208: Line 212:
 
     60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 
     60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 
     70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
     70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
     rock@localhost:~/mraa/build$ sudo mraa-i2c set 0 0x50 0x10 0x55  //write                                                                                                                           
+
     rock@rockpi4b:~$ sudo mraa-i2c set 0 0x50 0x10 0x55  //write                                                                                                                           
 
     Device 50, Register = 10, Value = 55
 
     Device 50, Register = 10, Value = 55
     rock@localhost:~/mraa/build$ sudo mraa-i2c get 0 0x50 0x10              //read                                                                                               
+
     rock@rockpi4b:~$ sudo mraa-i2c get 0 0x50 0x10              //read                                                                                               
 
     Register 0X10 = 0X55
 
     Register 0X10 = 0X55
  
===test spi===
+
==== test spi====
 
fristly,modiy '''/boot/hw_intfc.conf''',if  use spi1,must disable uart4,if use spi2,disable i2c6.follow:
 
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  
+
     rock@rockpi4b:~$ cat /boot/hw_intfc.conf  
 
      
 
      
 
     # Hardware Interface Config
 
     # 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.
 
      
 
      
     # For more details, check file /usr/local/share/doc/rockpi4-dtbo/README
+
     intfc:pwm0=off
 +
    intfc:pwm1=off
 +
    intfc:uart2=off
 +
    intfc:uart4=off
 +
    '''intfc:spi1=on'''
 +
    '''intfc:spi2=on'''
 +
    intfc:i2c2=off
 +
    intfc:i2c6=off
 +
    intfc:i2c7=off
 
      
 
      
     # Uncomment configuration line if you use it. Take i2c6 as an example,
+
     # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
    # setting off disables i2c6 bus and setting on enables i2c6 bus.
+
   
+
    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
+
   
+
    # DTS Overlay Config
+
    # 1. check the name.dtbo in /boot/overlays
+
    # 2. add intfc:dtoverlay=name as below
+
 
      
 
      
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=two-color-led
 
     #intfc:dtoverlay=two-color-led
     intfc:dtoverlay=console-disabled
+
     #intfc:dtoverlay=console-on-ttyS2
     #intfc:dtoverlay=console-on-uart4
+
     #intfc:dtoverlay=console-on-ttyS4
     '''intfc:dtoverlay=devspi'''          #spi1
+
     '''intfc:dtoverlay=devspi1'''          #spi1
     '''intfc:dtoverlay=devspi2'''       #spi2
+
     '''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  
 
use '''c''' to test spi,compile spi.c,if use spi1,must disable  
     root@localhost:~# cat spi.c
+
     root@rockpi4b:~# cat spi.c
 
     #include <signal.h>
 
     #include <signal.h>
 
     #include <stdlib.h>
 
     #include <stdlib.h>
Line 302: Line 303:
 
         return EXIT_FAILURE;
 
         return EXIT_FAILURE;
 
     }
 
     }
     root@localhost:~# gcc spi.c -lmraa
+
     root@rockpi4b:~# gcc spi.c -lmraa
 
then short '''pin19''' and '''pin21''' and run a.out
 
then short '''pin19''' and '''pin21''' and run a.out
     root@localhost:~# ./a.out  
+
     root@rockpi4b:~# ./a.out  
 
     0xaa
 
     0xaa
 
     0xaa
 
     0xaa
 
     ......
 
     ......
 +
==== test pwm ====
  
===test pwm===
+
for pwm c test,should install gcc 
 +
apt-get install gcc
  
for pwm python test,should install python and libpython2.7  
+
modiy /boot/hw_intfc.conf,if use spi1,must disable uart4,if use spi2,disable i2c6.follow:
 +
  # 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 disablee
 +
   
 +
  '''intfc:pwm0=on'''
 +
  '''intfc:pwm1=on'''
 +
  intfc:uart2=off
 +
  intfc:uart4=off
 +
  intfc:spi1=off
 +
  intfc:spi2=off
 +
  intfc:i2c2=off
 +
  intfc:i2c6=off
 +
  intfc:i2c7=off
  
    apt-get install libpython2.7 python
+
Use c program in /usr/local/share/mraa/examples/c/pwm.c to test: 
  
use python script follow: 
+
Modify the macro PWM in /usr/local/share/mraa/examples/c/pwm.c as follows to test PWM0. To test PWM1, change #define PWM 11 to #define PWM 13.
  
    root@localhost:~# cat pwm.py
+
'''/* PWM declaration */'''
    #!/usr/bin/env python
+
'''#define PWM 11'''
    # Example Usage: Generates PWM at a step rate of 0.01 continuously.
+
save and compile pwm.c
    import mraa
+
  root@rockpi4b:/usr/local/share/mraa/examples/c# gcc -o pwm pwm.c -lmraa
    import time
+
  root@rockpi4b:/usr/local/share/mraa/examples/c# ./pwm
   
+
  PWM value is 0.010045
    x = mraa.Pwm(11) # pin11 pwm0
+
  PWM value is 0.019985
    # x = mraa.Pwm(13) # pin13 pwm1
+
  PWM value is 0.030030
    x.period_us(700)
+
  PWM value is 0.039971
    x.enable(True)
+
  PWM value is 0.050016
   
+
  PWM value is 0.059956
    value= 0.0
+
  PWM value is 0.070001
   
+
  PWM value is 0.080046
    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 
+
==== test uart ====
 
+
Disable console and enable uart2 and uart4 for uart test
    root@localhost:~# ls
+
     root@rockpi4b:~# cat /boot/hw_intfc.conf  
    pwm.py
+
    root@localhost:~# chmod +x pwm.py
+
    root@localhost:~# ./pwm.py
+
 
+
===test uart===
+
Disable console and enable uart2 for uart test
+
     root@localhost:/# cat boot/hw_intfc.conf  
+
 
      
 
      
 
     # Hardware Interface Config
 
     # Hardware Interface Config
   
+
     # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
     # For more details, check file /usr/local/share/doc/rockpi4-dtbo/README
+
     # Set "on" to enable the optional hardware interfaces while set "off" to disable.
   
+
    # Uncomment configuration line if you use it. Take i2c6 as an example,
+
     # setting off disables i2c6 bus and setting on enables i2c6 bus.
+
 
      
 
      
 
     intfc:pwm0=on
 
     intfc:pwm0=on
 
     intfc:pwm1=on
 
     intfc:pwm1=on
 
     '''intfc:uart2=on'''
 
     '''intfc:uart2=on'''
     intfc:uart4=off
+
     '''intfc:uart4=on'''
     intfc:spi1=on
+
     intfc:spi1=off
 
     intfc:spi2=off
 
     intfc:spi2=off
     intfc:i2c2=on
+
     intfc:i2c2=off
 
     intfc:i2c6=off
 
     intfc:i2c6=off
 
     intfc:i2c7=off
 
     intfc:i2c7=off
 
      
 
      
     # DTS Overlay Config
+
     # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
    # 1. check the name.dtbo in /boot/overlays
+
    # 2. add intfc:dtoverlay=name as below
+
 
      
 
      
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=at24c02
 
     #intfc:dtoverlay=two-color-led
 
     #intfc:dtoverlay=two-color-led
     '''intfc:dtoverlay=console-disabled'''
+
     #intfc:dtoverlay=console-on-ttyS2
     #intfc:dtoverlay=console-on-uart4
+
     #intfc:dtoverlay=console-on-ttyS4
     root@localhost:/#  
+
     root@rockpi4b:~#  
then '''reboot''' and short '''pin8''' and '''pin10'''.
+
then '''reboot'''. Connect your serial line to the corresponding serial port.
use ssh connect to rockpi4,we must have 2 windows,one is for send data,anothor is for receive.   
+
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:
 
send command follow:
     sudo mraa-uart dev 0 baud 1500000 send  data
+
     sudo mraa-uart dev 0 baud 1500000 send  data 
 +
    or
 +
    sudo mraa-uart dev 1 baud 1500000 send  data
 
receive command follow:
 
receive command follow:
 
     sudo mraa-uart dev 0 baud 1500000 recv  1000
 
     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
 
the receive window will show the data
 +
 +
==== test adc ====
 +
connect signal you want to measure,and then compile mraa examplec file to test.
 +
    root@rockpi4b:/usr/local/share/mraa/examples/c# '''gcc -o aio aio.c -lmraa'''
 +
    root@rockpi4b:/usr/local/share/mraa/examples/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 3FF - 1023
 +
    ADC A0 read float - 1.00000
 +
    ADC A0 read 3FF - 1023
 +
    ADC A0 read float - 1.00000
 +
    ADC A0 read 3FF - 1023
 +
    ADC A0 read float - 1.00000
 +
 +
===More examples===
 +
More examples (including examples written in python and java) can be found  at:
 +
 +
/usr/local/share/mraa/examples/

Revision as of 02:36, 23 September 2021

       ROCK Pi 4 >  Development >  Install Libmraa 

Install Libmraa

Below is the instructions of how to install libmraa.

Install essential packages

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

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

Get the pub key

  • For Debian Buster
   wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -
  • For Ubuntu Focal
   wget -O -  apt.radxa.com/focal-testing/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 && sudo apt-get install -y rockpi4-dtbo

Install the latest u-boot and kernel (optional)

   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=off
   intfc:pwm1=off
   intfc:uart2=off
   intfc:uart4=off
   intfc:spi1=off
   intfc:spi2=off
   intfc:i2c2=off
   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
   #intfc:dtoverlay=devspi1
   #intfc:dtoverlay=devspi2
   root@localhost:/# 

then reboot to make them effective

Install libmraa

   apt-get install libmraa

Test

test gpio

Use mraa-gpio tool to test

   rock@rockpi4b:~$ 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@rockpi4b:~$ 
   rock@rockpi4b:~$  sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                      
   rock@rockpi4b:~$  sudo mraa-gpio set 40 0  //pin40 pull low

test i2c

Firstly, modify the /boot/hw_intfc.conf file as following and then reboot to enable i2c2 i2c6 i2c7.

   # 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=off
   intfc:pwm1=off
   intfc:uart2=off
   intfc:uart4=off
   intfc:spi1=off
   intfc:spi2=off
   intfc:i2c2=on
   intfc:i2c6=on
   intfc:i2c7=on


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

   rock@rockpi4b:~$ mraa-i2c list
   Bus   0: id=07 type=linux  default
   Bus   1: id=02 type=linux 
   Bus   2: id=06 type=linux 
   rock@rockpi4b:~$ 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@rockpi4b:~$ sudo mraa-i2c set 0 0x50 0x10 0x55   //write                                                                                                                          
   Device 50, Register = 10, Value = 55
   rock@rockpi4b:~$ 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@rockpi4b:~$ 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=off
   intfc:pwm1=off
   intfc:uart2=off
   intfc:uart4=off
   intfc:spi1=on
   intfc:spi2=on
   intfc:i2c2=off
   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
   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@rockpi4b:~# 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@rockpi4b:~# gcc spi.c -lmraa

then short pin19 and pin21 and run a.out

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

test pwm

for pwm c test,should install gcc

apt-get install gcc

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

  # 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 disablee

  intfc:pwm0=on
  intfc:pwm1=on
  intfc:uart2=off
  intfc:uart4=off
  intfc:spi1=off
  intfc:spi2=off
  intfc:i2c2=off
  intfc:i2c6=off
  intfc:i2c7=off

Use c program in /usr/local/share/mraa/examples/c/pwm.c to test:

Modify the macro PWM in /usr/local/share/mraa/examples/c/pwm.c as follows to test PWM0. To test PWM1, change #define PWM 11 to #define PWM 13.

/* PWM declaration */ 
#define PWM 11

save and compile pwm.c

  root@rockpi4b:/usr/local/share/mraa/examples/c# gcc -o pwm pwm.c -lmraa
  root@rockpi4b:/usr/local/share/mraa/examples/c# ./pwm
  PWM value is 0.010045
  PWM value is 0.019985
  PWM value is 0.030030 
  PWM value is 0.039971
  PWM value is 0.050016
  PWM value is 0.059956
  PWM value is 0.070001
  PWM value is 0.080046

test uart

Disable console and enable uart2 and uart4 for uart test

   root@rockpi4b:~# 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=off
   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@rockpi4b:~# 

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 1 baud 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.

   root@rockpi4b:/usr/local/share/mraa/examples/c# gcc -o aio aio.c -lmraa 
   root@rockpi4b:/usr/local/share/mraa/examples/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 3FF - 1023
   ADC A0 read float - 1.00000
   ADC A0 read 3FF - 1023
   ADC A0 read float - 1.00000
   ADC A0 read 3FF - 1023
   ADC A0 read float - 1.00000

More examples

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

/usr/local/share/mraa/examples/