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
(SPI Test)
(Blanked the page)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{rockpi4_header}}
 
  
{{Languages|rockpi4/dev/libmraa}}
 
 
      [[rockpi4 | ROCK Pi 4]] > [[rockpi4/dev | Development]] > [[rockpi4/dev/libmraa  | Install Libmraa ]]
 
 
== Install Libmraa ==
 
Below is the instructions of how to install  '''libmraa'''.
 
 
=== Install essential packages ===
 
Edit file /etc/apt/sources.list and add the following:
 
 
* '''For Debian Buster'''
 
    deb http://apt.radxa.com/buster-testing/ buster main
 
* '''For Debian Bullseye'''
 
    deb http://apt.radxa.com/bullseye-testing/ bullseye 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 Debian Bullseye'''
 
    wget -O -  apt.radxa.com/bullseye-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
 
 
Install essential packages:
 
    sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtbo
 
 
=== Enable interface ===
 
See ROCK 4 [[Rockpi4/hardware/rockpi4#gpio | GPIO pinout]]. ROCK 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      I2C'''7'''_SDA
 
    PIN5      I2C'''7'''_SCL
 
    PIN27      I2C'''2'''_SCL
 
    PIN28      I2C'''2'''_SCL
 
    PIN29      I2C'''6'''_SCL
 
    PIN31      I2C'''6'''_SDA
 
2 SPI:
 
    PIN19      SPI'''1'''TX                    //'''must disable uart4'''
 
    PIN21      SPI'''1'''RX                    //'''must disable uart4'''
 
    PIN23      SPI'''1'''_CLK
 
    PIN24      SPI'''1'''_CS
 
    PIN29      SPI'''2'''TX                  //'''must disable i2c6'''
 
    PIN31      SPI'''2'''RX                  //'''must disable i2c6'''
 
    PIN7      SPI'''2'''_CLK
 
    PIN33      SPI'''2'''_CS 
 
1 UART:
 
    PIN8      UART'''2'''_TX
 
    PIN10      UART'''2'''_RX
 
    PIN19      UART'''4'''_TX
 
    PIN21      UART'''4'''_RX
 
2 PWM:
 
    PIN11      PWM'''0'''
 
    PIN13      PWM'''1'''
 
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
 
    rock@rockpi-4b:~$ 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
 
    rock@rockpi-4b:~$
 
 
then '''reboot''' to make them effective.
 
 
=== Install libmraa ===
 
 
    rock@rockpi-4b:~$ sudo apt-get install libmraa
 
 
=== Test ===
 
==== GPIO Test ====
 
 
Use '''mraa-gpio''' tool to test 
 
 
    rock@rockpi-4b:~$ 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@rockpi-4b:~$
 
    rock@rockpi-4b:~$  sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                     
 
    rock@rockpi-4b:~$  sudo mraa-gpio set 40 0  //pin40 pull low
 
 
==== I2C Test ====
 
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@rockpi-4b:~$ mraa-i2c list
 
    Bus  0: id=07 type=linux  default
 
    Bus  1: id=02 type=linux
 
    Bus  2: id=06 type=linux
 
    rock@rockpi-4b:~$ 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@rockpi-4b:~$ sudo mraa-i2c set 0 0x50 0x10 0x55  //write                                                                                                                         
 
    Device 50, Register = 10, Value = 55
 
    rock@rockpi-4b:~$ sudo mraa-i2c get 0 0x50 0x10              //read                                                                                             
 
    Register 0X10 = 0X55
 
 
==== SPI Test ====
 
Firstly modify '''/boot/hw_intfc.conf'''. When SPI1 is used, UART4 and spi1-flash dtbo should be disabled. When SPI2 is used, UART4 and I2C-6 should be disabled.
 
    rock@rockpi-4b:~$ 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=spi1-flash'''
 
    '''intfc:dtoverlay=devspi1'''          #spi1
 
    '''intfc:dtoverlay=devspi2'''          #spi2
 
 
If need debug console, you could uncomment '''intfc:dtoverlay=console-on-ttyS2'''.
 
 
When SPI-1 and SPI-2 are enabled, and devspi1, devspi2 dtbo are added. The following two devices are created.
 
 
    ls /dev/spidev*
 
    /dev/spidev1.0 /dev/spidev2.0
 
 
Example for SPI-1 bus test.
 
 
    root@rockpi-4b:~# 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@rockpi-4b:~# gcc spi.c -lmraa
 
then short '''pin19''' and '''pin21''' and run a.out
 
    root@rockpi-4b:~# ./a.out
 
    0xaa
 
    0xaa
 
    ......
 
 
==== PWM test ====
 
 
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@rockpi-4b:/usr/local/share/mraa/examples/c# gcc -o pwm pwm.c -lmraa
 
  root@rockpi-4b:/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
 
 
==== UART Test ====
 
Disable console and enable uart2 and uart4 for uart test
 
    root@rockpi-4b:~# 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@rockpi-4b:~#
 
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
 
 
==== ADC Test ====
 
connect signal you want to measure,and then compile mraa examplec file to test.
 
    root@rockpi-4b:/usr/local/share/mraa/examples/c# '''gcc -o aio aio.c -lmraa'''
 
    root@rockpi-4b:/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/
 

Latest revision as of 01:53, 12 July 2023