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
(Enable interface)
(Blanked the page)
 
(61 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{rockpi4_header}}
 
  
{{Languages|rockpi4/dev/libmraa}}
 
 
      [[rockpi4 | ROCK Pi 4]] > [[rockpi4/dev | Development]] > [[rockpi4/dev/libmraa  | Install Libmraa ]]
 
 
 
= Libmraa-rockpi4 =
 
 
Below is the instructions of how to install  '''libmraa'''.
 
 
== Install essential packages ==
 
 
Edit your /etc/apt/sources.list and add the following:
 
 
'''For Debian Stretch'''
 
 
    deb http://apt.radxa.com/stretch/ stretch main
 
 
'''For Ubuntu bionic'''
 
 
    deb http://apt.radxa.com/bionic/ bionic main
 
 
Get the pub key 
 
    wget -O -  apt.radxa.com/stretch/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 rockchip-fstab && sudo apt-get install -y rockpi4-dtbo
 
 
== Install the latest u-boot and kernel==
 
    apt-get install rockpi4b-rk-u-boot-lastest
 
 
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-lastest
 
 
 
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 [[Rockpi4/hardware/rockpi4#gpio | GPIO pinout]],ROCK Pi 4 has a 40-pin expansion header. Each pin is distinguished by color. mraa define follow: 
 
 
15 GPIO:
 
    PIN07    GPIO2_B3
 
    PIN12    GPIO4_A3
 
    PIN15    GPIO4_C5
 
    PIN16    GPIO4_D2
 
    PIN18    GPIO4_D4
 
    PIN22    GPIO4_D5
 
    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
 
1 SPI:
 
    PIN19      SPI'''1'''_MOSI
 
    PIN21      SPI'''1'''_MISO
 
    PIN23      SPI'''1'''_CLK
 
    PIN24      SPI'''1'''_CS0
 
1 UART:
 
    PIN8      UART'''2'''_TX
 
    PIN10      UART'''2'''_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,spi1,i2c2,i2c7''' for test
 
    root@localhost:/# cat boot/hw_intfc.conf
 
   
 
    # Hardware Interface Config
 
   
 
    # For more details, check file /usr/local/share/doc/rockpi4-dtbo/README
 
   
 
    # 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:pwm1=on'''
 
    intfc:uart2=off
 
    intfc:uart4=off
 
    '''intfc:spi1=on'''
 
    intfc:spi2=off
 
    '''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=two-color-led
 
    #intfc:dtoverlay=console-disabled
 
    #intfc:dtoverlay=console-on-uart4
 
    root@localhost:/#
 
then '''reboot'''
 
 
==Install libmraa-rockpi4==
 
 
    apt-get install libmraa-rockpi4
 
 
==Test==
 
===test gpio===
 
 
Use '''mraa-gpio''' tool to test 
 
 
    root@localhost:~# mraa-gpio list
 
    07    GPIO2_B3: GPIO
 
    12    GPIO4_A3: GPIO
 
    15    GPIO4_C5: GPIO
 
    16    GPIO4_D2: GPIO
 
    18    GPIO4_D4: GPIO
 
    22    GPIO4_D5: GPIO
 
    29    GPIO2_B2: GPIO
 
    31    GPIO2_B1: GPIO
 
    32    GPIO3_C0: GPIO
 
    33    GPIO2_B4: GPIO
 
    35    GPIO4_A5: GPIO
 
    36    GPIO4_A4: GPIO
 
    37    GPIO4_D6: GPIO
 
    38    GPIO4_A6: GPIO
 
    40    GPIO4_A7: GPIO
 
    root@localhost:~# mraa-gpio set 40 1  //pin40 pull high                                                                                                                     
 
    root@localhost:~# mraa-gpio set 40 0  //pin40 pull low
 
    root@localhost:~#
 
===test i2c===
 
Use '''mraa-i2c''' tool to test,we must have a i2c slave device,for example,we use I2C EEPROM:
 
    root@localhost:~# mraa-i2c list   
 
    Bus  0: id=07 type=linux  default
 
    Bus  1: id=02 type=linux
 
    root@localhost:~# mraa-i2c detect 0      //device on bus 0
 
    00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- --
 
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    root@localhost:~# mraa-i2c detect 1    //no device on bus 1
 
    00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
    root@localhost:~# mraa-i2c set 0 0x50 0x10 0x55  //write                                                                                                                         
 
    Device 50, Register = 10, Value = 55
 
    root@localhost:~# mraa-i2c get 0 0x50 0x10              //read                                                                                             
 
    Register 0X10 = 0X55
 
    root@localhost:~#   
 
 
===test spi===
 
use '''c''' to test spi,compile spi.c
 
    root@localhost:~# 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@localhost:~# gcc spi.c -lmraa
 
then short '''pin19''' and '''pin21''' and run a.out
 
    root@localhost:~# ./a.out
 
    0xaa
 
    0xaa
 
    ......
 
 
===test pwm===
 
 
for pwm python test,should install python and libpython2.7 
 
 
    apt-get install libpython2.7 python
 
 
use python script follow: 
 
 
    root@localhost:~# cat pwm.py
 
    #!/usr/bin/env python
 
    # Example Usage: Generates PWM at a step rate of 0.01 continuously.
 
    import mraa
 
    import time
 
   
 
    x = mraa.Pwm(11) # pin11 pwm0
 
    # x = mraa.Pwm(13) # pin13 pwm1
 
    x.period_us(700)
 
    x.enable(True)
 
   
 
    value= 0.0
 
   
 
    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 
 
 
    root@localhost:~# ls
 
    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
 
   
 
    # For more details, check file /usr/local/share/doc/rockpi4-dtbo/README
 
   
 
    # 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:pwm1=on
 
    '''intfc:uart2=on'''
 
    intfc:uart4=off
 
    intfc:spi1=on
 
    intfc:spi2=off
 
    intfc:i2c2=on
 
    intfc:i2c6=off
 
    intfc:i2c7=off
 
   
 
    # DTS Overlay Config
 
    # 1. check the name.dtbo in /boot/overlays
 
    # 2. add intfc:dtoverlay=name as below
 
   
 
    #intfc:dtoverlay=at24c02
 
    #intfc:dtoverlay=two-color-led
 
    '''intfc:dtoverlay=console-disabled'''
 
    #intfc:dtoverlay=console-on-uart4
 
    root@localhost:/#
 
then '''reboot''' and short '''pin8''' and '''pin10'''.
 
use ssh connect to rockpi4,we must have 2 windows,one is for send data,anothor is for receive. 
 
 
send command follow:
 
    sudo mraa-uart dev 0 baud 1500000 send  data
 
receive command follow:
 
    sudo mraa-uart dev 0 baud 1500000 recv  1000
 
 
the receive window will show the data
 

Latest revision as of 01:53, 12 July 2023