查看: 10526|回复: 2

安装 libmraa(rock pi 4A/B gpio 操作)

[复制链接]

404

主题

246

回帖

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
20407
发表于 2021-12-7 10:26:29 | 显示全部楼层 |阅读模式
rock pi 4 A/B 卡片电脑 GPIO操作
https://item.taobao.com/item.htm?&id=599897768013


Install Libmraa

下面是如何安装 libmraa 的说明。
Install essential packages
编辑您的 /etc/apt/sources.list 并添加以下内容:
For Debian Buster
  1.   deb http://apt.radxa.com/buster-testing/ buster main
复制代码
For Ubuntu Focal
  1. deb http://apt.radxa.com/focal-testing/ focal main
复制代码
Get the pub key
For Debian Buster
  1. wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -
复制代码
For Ubuntu Focal
  1. wget -O -  apt.radxa.com/focal-testing/public.key | sudo apt-key add -
复制代码
Update
  1.    <span style="background-color: rgb(255, 255, 255);">sudo apt-get update && sudo apt-get upgrade</span>
复制代码
Install essential packages:
  1. sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtboI
复制代码
安装最新的 u-boot 和内核(可选)
  1. apt-get install rockpi4b-rk-u-boot-latest
复制代码
手动运行 u-boot flash 脚本
  1. /usr/local/sbin/rockpi4b_upgrade_bootloader.sh
复制代码
键入 YES 执行 uboot.img 烧录。 然后安装内核
  1. apt-get install linux-base
  2.    apt-get install linux-4.4-latest
复制代码
安装后,检查文件/boot/extlinux/extlinux.conf。 它列出了 ROCK Pi 中安装的所有内核版本。 第一个标签对应的内核是最新版本和第一个引导选项。

启用接口
参见rockpi4 GPIO pinout,ROCK Pi 4有一个40-pin的扩展接头。 每个引脚都以颜色区分。 mraa 定义如下:
27 GPIO:
  1. PIN03    GPIO2_A2
  2.    PIN05    GPIO2_B0
  3.    PIN07    GPIO2_B3
  4.    PIN08    GPIO4_C4
  5.    PIN10    GPIO4_C3
  6.    PIN11    GPIO4_C2
  7.    PIN12    GPIO4_A3
  8.    PIN13    GPIO4_C6
  9.    PIN15    GPIO4_C5
  10.    PIN16    GPIO4_D2
  11.    PIN18    GPIO4_D4
  12.    PIN19    GPIO1_B0
  13.    PIN21    GPIO1_A7
  14.    PIN22    GPIO4_D5
  15.    PIN23    GPIO1_B1
  16.    PIN24    GPIO1_B2
  17.    PIN27    GPIO2_A0
  18.    PIN28    GPIO2_A1
  19.    PIN29    GPIO2_B2
  20.    PIN31    GPIO2_B1
  21.    PIN32    GPIO3_C0
  22.    PIN33    GPIO2_B4
  23.    PIN35    GPIO4_A5
  24.    PIN36    GPIO4_A4
  25.    PIN37    GPIO4_D6
  26.    PIN38    GPIO4_A6
  27.    PIN40    GPIO4_A7
复制代码
2 I2C:
  1. PIN3       I2C7_SDA
  2.    PIN5       I2C7_SCL
  3.    PIN27      I2C2_SCL
  4.    PIN28      I2C2_SCL
  5.    PIN29      I2C6_SCL
  6.    PIN31      I2C6_SDA
复制代码
2 SPI:
  1. PIN19      SPI1TX                    //must disable uart4
  2.    PIN21      SPI1RX                    //must disable uart4
  3.    PIN23      SPI1_CLK
  4.    PIN24      SPI1_CS
  5.    PIN29      SPI2TX                   //must disable i2c6
  6.    PIN31      SPI2RX                   //must disable i2c6
  7.    PIN7       SPI2_CLK
  8.    PIN33      SPI2_CS  
复制代码
1 UART:
  1. PIN8       UART2_TX
  2.    PIN10      UART2_RX
  3.    PIN19      UART4_TX
  4.    PIN21      UART4_RX
复制代码
2 PWM:
  1.   PIN11      PWM0
  2.    PIN13      PWM1
复制代码
1 ADC:
  1. PIN13      ADC0       //the measure voltage must lower than 1.8v
复制代码
修改 /boot/hw_intfc.conf 启用 pwm0,pwm1,uart2,uart4,i2c2,i2c6,i2c7 进行测试
  1.   root@localhost:/# cat boot/hw_intfc.conf
  2.    
  3.    # Hardware Interface Config   
  4.    # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.  
  5.    # Set "on" to enable the optional hardware interfaces while set "off" to disable.
  6.    
  7.    intfc:pwm0=off
  8.    intfc:pwm1=off
  9.    intfc:uart2=off
  10.    intfc:uart4=off
  11.    intfc:spi1=off
  12.    intfc:spi2=off
  13.    intfc:i2c2=off
  14.    intfc:i2c6=off
  15.    intfc:i2c7=off
  16.    
  17.    # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
  18.    
  19.    #intfc:dtoverlay=at24c02
  20.    #intfc:dtoverlay=two-color-led
  21.    #intfc:dtoverlay=console-on-ttyS2
  22.    #intfc:dtoverlay=console-on-ttyS4
  23.    #intfc:dtoverlay=devspi1
  24.    #intfc:dtoverlay=devspi2
  25.    root@localhost:/#
复制代码
然后重新启动以使它们生效

真正安装libmraa
  1. apt-get install libmraa
复制代码
测试
测试 GPIO
使用mraa-gpio工具进行测试
  1. rock@rockpi4b:~$ mraa-gpio list
  2.    01         3V3:
  3.    02          5V:
  4.    03        SDA7: GPIO I2C  
  5.    04          5V:
  6.    05        SCL7: GPIO I2C  
  7.    06         GND:
  8.    07    SPI2_CLK: GPIO SPI  
  9.    08        TXD2: GPIO UART
  10.    09         GND:
  11.    10        RXD2: GPIO UART
  12.    11        PWM0: GPIO PWM  
  13.    12    GPIO4_A3: GPIO
  14.    13        PWM1: GPIO PWM  
  15.    14         GND:
  16.    15    GPIO4_C5: GPIO
  17.    16    GPIO4_D2: GPIO
  18.    17         3V3:
  19.    18    GPIO4_D4: GPIO
  20.    19 SPI1TX,TXD4: GPIO SPI  UART
  21.    20         GND:
  22.    21 SPI1RX,RXD4: GPIO SPI  UART
  23.    22    GPIO4_D5: GPIO
  24.    23     SPI1CLK: GPIO SPI  
  25.    24      SPI1CS: GPIO SPI  
  26.    25         GND:
  27.    26     ADC_IN0: AIO  
  28.    27        SDA2: GPIO I2C  
  29.    28        SCL2: GPIO I2C  
  30.    29 SCL6,SPI2RX: GPIO I2C  SPI  
  31.    30         GND:
  32.    31 SDA6,SPI2TX: GPIO I2C  SPI  
  33.    32    GPIO3_C0: GPIO
  34.    33      SPI2CS: GPIO SPI  
  35.    34         GND:
  36.    35    GPIO4_A5: GPIO
  37.    36    GPIO4_A4: GPIO
  38.    37    GPIO4_D6: GPIO
  39.    38    GPIO4_A6: GPIO
  40.    39         GND:
  41.    40    GPIO4_A7: GPIO
  42.    rock@rockpi4b:~$
  43.    rock@rockpi4b:~[        DISCUZ_CODE_113        ]nbsp; sudo mraa-gpio set 40 1  //pin40 pull high                                                                                                                     
  44.    rock@rockpi4b:~[        DISCUZ_CODE_113        ]nbsp; sudo mraa-gpio set 40 0  //pin40 pull low
复制代码
测试 i2c
首先修改 /boot/hw_intfc.conf 文件如下,然后重启以启用 i2c2 i2c6 i2c7。
  1.    # Hardware Interface Config
  2.    # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
  3.    # Set "on" to enable the optional hardware interfaces while set "off" to disable
  4.    intfc:pwm0=off
  5.    intfc:pwm1=off
  6.    intfc:uart2=off
  7.    intfc:uart4=off
  8.    intfc:spi1=off
  9.    intfc:spi2=off
  10.    intfc:i2c2=on
  11.    intfc:i2c6=on
  12.    intfc:i2c7=on
复制代码
然后,使用mraa-i2c工具进行测试,我们必须有一个i2c从设备,例如我们使用I2C EEPROM:
  1.    rock@rockpi4b:~$ mraa-i2c list
  2.    Bus   0: id=07 type=linux  default
  3.    Bus   1: id=02 type=linux
  4.    Bus   2: id=06 type=linux
  5.    rock@rockpi4b:~$ sudo mraa-i2c detect 0       //device on bus 0  //i2c7
  6.    00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  7.    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  8.    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  9.    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  10.    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  11.    50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- --
  12.    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  13.    70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  14.    rock@rockpi4b:~$ sudo mraa-i2c set 0 0x50 0x10 0x55   //write                                                                                                                          
  15.    Device 50, Register = 10, Value = 55
  16.    rock@rockpi4b:~$ sudo mraa-i2c get 0 0x50 0x10              //read                                                                                             
  17.    Register 0X10 = 0X55
复制代码
测试spi
首先,修改/boot/hw_intfc.conf,如果使用spi1,必须禁用uart4,如果使用spi2,禁用i2c6.
如下
  1. rock@rockpi4b:~$ cat /boot/hw_intfc.conf
  2.    
  3.    # Hardware Interface Config
  4.    # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
  5.    # Set "on" to enable the optional hardware interfaces while set "off" to disable.
  6.    
  7.    intfc:pwm0=off
  8.    intfc:pwm1=off
  9.    intfc:uart2=off
  10.    intfc:uart4=off
  11.    intfc:spi1=on
  12.    intfc:spi2=on
  13.    intfc:i2c2=off
  14.    intfc:i2c6=off
  15.    intfc:i2c7=off
  16.    
  17.    # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
  18.    
  19.    #intfc:dtoverlay=at24c02
  20.    #intfc:dtoverlay=two-color-led
  21.    #intfc:dtoverlay=console-on-ttyS2
  22.    #intfc:dtoverlay=console-on-ttyS4
  23.    intfc:dtoverlay=devspi1           #spi1
  24.    intfc:dtoverlay=devspi2           #spi2
复制代码
如果需要调试控制台,您可以取消注释 intfc:dtoverlay=console-on-ttyS2

如果使用c测试spi,编译spi.c,如果使用spi1,必须禁用
  1. root@rockpi4b:~# cat spi.c
  2.    #include <signal.h>
  3.    #include <stdlib.h>
  4.    #include <unistd.h>
  5.    
  6.    /* mraa header */
  7.    #include "mraa/spi.h"
  8.    
  9.    /* SPI declaration */
  10.    #define SPI_BUS 0
  11.    
  12.    /* SPI frequency in Hz */
  13.    #define SPI_FREQ 400000
  14.    int
  15.    main(int argc, char** argv)
  16.    {
  17.        mraa_result_t status = MRAA_SUCCESS;
  18.        mraa_spi_context spi;
  19.        int i, j;
  20.    
  21.        /* initialize mraa for the platform (not needed most of the times) */
  22.        mraa_init();
  23.    
  24.        //! [Interesting]
  25.        /* initialize SPI bus */
  26.        spi = mraa_spi_init(SPI_BUS);
  27.        if (spi == NULL) {
  28.            fprintf(stderr, "Failed to initialize SPI\n");
  29.            mraa_deinit();
  30.            return EXIT_FAILURE;
  31.        }
  32.    
  33.        /* set SPI frequency */
  34.        status = mraa_spi_frequency(spi, SPI_FREQ);
  35.        if (status != MRAA_SUCCESS)
  36.            goto err_exit;
  37.    
  38.        /* set big endian mode */
  39.        status = mraa_spi_lsbmode(spi, 0);
  40.        if (status != MRAA_SUCCESS) {
  41.            goto err_exit;
  42.        }
  43.        while(1) {
  44.            printf("0x%x\n",mraa_spi_write(spi, 0xaa));
  45.        }
  46.    err_exit:
  47.        mraa_result_print(status);
  48.    
  49.        /* stop spi */
  50.        mraa_spi_stop(spi);
  51.    
  52.        /* deinitialize mraa for the platform (not needed most of the times) */
  53.        mraa_deinit();
  54.    
  55.        return EXIT_FAILURE;
  56.    }
  57.    root@rockpi4b:~# gcc spi.c -lmraa
复制代码
然后短接 pin19 pin21 并运行 a.out
  1. root@rockpi4b:~# ./a.out
  2.    0xaa
  3.    0xaa
  4.    ......
复制代码

测试PWM
对于 pwm c 测试,应该安装 gcc
  1. apt-get install gcc
复制代码
修改/boot/hw_intfc.conf,如果使用spi1,必须禁用uart4,如果使用spi2,禁用i2c6.
如下:
  1. # Hardware Interface Config
  2.   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
  3.   # Set "on" to enable the optional hardware interfaces while set "off" to disablee

  4.   intfc:pwm0=on
  5.   intfc:pwm1=on
  6.   intfc:uart2=off
  7.   intfc:uart4=off
  8.   intfc:spi1=off
  9.   intfc:spi2=off
  10.   intfc:i2c2=off
  11.   intfc:i2c6=off
  12.   intfc:i2c7=off
复制代码
使用/usr/local/share/mraa/examples/c/pwm.c中的c程序进行测试:

修改/usr/local/share/mraa/examples/c/pwm.c中的宏PWM如下,测试PWM0。
要测试 PWM1,请将 #define PWM 11 更改为 #define PWM 13。
  1. /* PWM declaration */
  2. #define PWM 11
复制代码
保存并编译 pwm.c
  1. root@rockpi4b:/usr/local/share/mraa/examples/c# gcc -o pwm pwm.c -lmraa
  2.   root@rockpi4b:/usr/local/share/mraa/examples/c# ./pwm
  3.   PWM value is 0.010045
  4.   PWM value is 0.019985
  5.   PWM value is 0.030030
  6.   PWM value is 0.039971
  7.   PWM value is 0.050016
  8.   PWM value is 0.059956
  9.   PWM value is 0.070001
  10.   PWM value is 0.080046
复制代码
测试串口
禁用控制台并启用 uart2 和 uart4 进行 uart 测试
  1. root@rockpi4b:~# cat /boot/hw_intfc.conf
  2.    
  3.    # Hardware Interface Config
  4.    # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.
  5.    # Set "on" to enable the optional hardware interfaces while set "off" to disable.
  6.    
  7.    intfc:pwm0=on
  8.    intfc:pwm1=on
  9.    intfc:uart2=on
  10.    intfc:uart4=on
  11.    intfc:spi1=off
  12.    intfc:spi2=off
  13.    intfc:i2c2=off
  14.    intfc:i2c6=off
  15.    intfc:i2c7=off
  16.    
  17.    # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
  18.    
  19.    #intfc:dtoverlay=at24c02
  20.    #intfc:dtoverlay=two-color-led
  21.    #intfc:dtoverlay=console-on-ttyS2
  22.    #intfc:dtoverlay=console-on-ttyS4
  23.    root@rockpi4b:~#
复制代码
然后重新启动。 将串行线连接到相应的串行端口。 使用 ssh 连接到 Rockpi4,我们必须有 2 个窗口(ssh 和串行控制台),一个用于发送数据,另一个用于接收。

发送命令如下:
  1. sudo mraa-uart dev 0 baud 1500000 send  data   
  2.    or
  3.    sudo mraa-uart dev 1 baud 1500000 send  data
复制代码
接收命令如下:
  1. sudo mraa-uart dev 0 baud 1500000 recv  1000
  2.    or
  3.    sudo mraa-uart dev 1 baud 1500000 recv  1000
复制代码
接收窗口将显示数据

测试ADC
连接要测量的信号,然后编译mraa examplec文件进行测试。
  1. root@rockpi4b:/usr/local/share/mraa/examples/c# gcc -o aio aio.c -lmraa
  2.    root@rockpi4b:/usr/local/share/mraa/examples/c#./aio
  3.    ADC A0 read 3FF - 1023
  4.    ADC A0 read float - 1.00000
  5.    ADC A0 read 3FF - 1023
  6.    ADC A0 read float - 1.00000
  7.    ADC A0 read 3FF - 1023
  8.    ADC A0 read float - 1.00000
  9.    ADC A0 read 3FF - 1023
  10.    ADC A0 read float - 1.00000
  11.    ADC A0 read 3FF - 1023
  12.    ADC A0 read float - 1.00000
复制代码





风火轮微信公众号
回复

使用道具 举报

1

主题

2

回帖

15

积分

新手上路

Rank: 1

积分
15
发表于 2022-7-5 11:42:31 | 显示全部楼层
sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtboI
这条指令最后那个字母I是没有的?我看官方给的指令是sudo apt-get install -y rockpi4-dtbo
安装之后在ubuntu20系统下重启失败,有遇到过吗?
回复 支持 反对

使用道具 举报

0

主题

1

回帖

8

积分

新手上路

Rank: 1

积分
8
发表于 2023-2-16 14:21:50 | 显示全部楼层

sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtboI
这条指令最后那个字母I是没有的?我看官方给的指令是sudo apt-get install -y rockpi4-dtbo
还有就是我按着前面的步骤在配置文件添加了 deb http://apt.radxa.com/buster-testing/ buster main
后面也更新源列表了,结果sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtbo时显示E: Unable to locate package rockpi4-dtbo 这该怎么解决
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|风火轮WIKI|手机版|小黑屋|深圳风火轮团队 ( 粤ICP备17095099号 )

GMT+8, 2024-4-19 16:08 , Processed in 0.050825 second(s), 19 queries .

快速回复 返回顶部 返回列表
 
【客服1】 商务合作 15289193
【客服2】 业务洽谈 13257599
【客服3】 售前咨询 510313198
【邮箱】
smartfire@smartfire.cn