安装 libmraa(rock pi 4A/B gpio 操作)
rock pi 4 A/B 卡片电脑 GPIO操作https://item.taobao.com/item.htm?&id=599897768013
https://gd1.alicdn.com/imgextra/i3/56843193/O1CN01Xl4pbV1ZSRFiSExT0_!!56843193.jpg
Install Libmraa
下面是如何安装 libmraa 的说明。
Install essential packages
编辑您的 /etc/apt/sources.list 并添加以下内容:
For Debian Buster
deb http://apt.radxa.com/buster-testing/ buster mainFor Ubuntu Focal
deb http://apt.radxa.com/focal-testing/ focal mainGet 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
<span style="background-color: rgb(255, 255, 255);">sudo apt-get update && sudo apt-get upgrade</span>Install essential packages:
sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtboI安装最新的 u-boot 和内核(可选)apt-get install rockpi4b-rk-u-boot-latest手动运行 u-boot flash 脚本
/usr/local/sbin/rockpi4b_upgrade_bootloader.sh键入 YES 执行 uboot.img 烧录。 然后安装内核
apt-get install linux-base
apt-get install linux-4.4-latest安装后,检查文件/boot/extlinux/extlinux.conf。 它列出了 ROCK Pi 中安装的所有内核版本。 第一个标签对应的内核是最新版本和第一个引导选项。
启用接口
参见rockpi4 GPIO pinout,ROCK Pi 4有一个40-pin的扩展接头。 每个引脚都以颜色区分。 mraa 定义如下: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_A72 I2C:PIN3 I2C7_SDA
PIN5 I2C7_SCL
PIN27 I2C2_SCL
PIN28 I2C2_SCL
PIN29 I2C6_SCL
PIN31 I2C6_SDA2 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_CS1 UART: PIN8 UART2_TX
PIN10 UART2_RX
PIN19 UART4_TX
PIN21 UART4_RX2 PWM:PIN11 PWM0
PIN13 PWM11 ADC:PIN13 ADC0 //the measure voltage must lower than 1.8v修改 /boot/hw_intfc.conf 启用 pwm0,pwm1,uart2,uart4,i2c2,i2c6,i2c7 进行测试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:/# 然后重新启动以使它们生效
真正安装libmraa apt-get install libmraa测试测试 GPIO使用mraa-gpio工具进行测试 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 SPIUART
20 GND:
21 SPI1RX,RXD4: GPIO SPIUART
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 I2CSPI
30 GND:
31 SDA6,SPI2TX: GPIO I2CSPI
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:~[ DISCUZ_CODE_113 ]nbsp; sudo mraa-gpio set 40 1//pin40 pull high
rock@rockpi4b:~[ DISCUZ_CODE_113 ]nbsp; sudo mraa-gpio set 40 0//pin40 pull low测试 i2c首先修改 /boot/hw_intfc.conf 文件如下,然后重启以启用 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然后,使用mraa-i2c工具进行测试,我们必须有一个i2c从设备,例如我们使用I2C EEPROM: rock@rockpi4b:~$ mraa-i2c list
Bus 0: id=07 type=linuxdefault
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测试spi首先,修改/boot/hw_intfc.conf,如果使用spi1,必须禁用uart4,如果使用spi2,禁用i2c6.如下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如果需要调试控制台,您可以取消注释 intfc:dtoverlay=console-on-ttyS2
如果使用c测试spi,编译spi.c,如果使用spi1,必须禁用 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();
//!
/* 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然后短接 pin19 和 pin21 并运行 a.outroot@rockpi4b:~# ./a.out
0xaa
0xaa
......
测试PWM对于 pwm c 测试,应该安装 gccapt-get install gcc修改/boot/hw_intfc.conf,如果使用spi1,必须禁用uart4,如果使用spi2,禁用i2c6.如下:# 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使用/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。/* PWM declaration */
#define PWM 11保存并编译 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测试串口禁用控制台并启用 uart2 和 uart4 进行 uart 测试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:~# 然后重新启动。 将串行线连接到相应的串行端口。 使用 ssh 连接到 Rockpi4,我们必须有 2 个窗口(ssh 和串行控制台),一个用于发送数据,另一个用于接收。
发送命令如下: sudo mraa-uart dev 0 baud 1500000 senddata
or
sudo mraa-uart dev 1 baud 1500000 senddata接收命令如下:sudo mraa-uart dev 0 baud 1500000 recv1000
or
sudo mraa-uart dev 1 baud 1500000 recv1000接收窗口将显示数据
测试ADC连接要测量的信号,然后编译mraa examplec文件进行测试。 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
sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtboI
这条指令最后那个字母I是没有的?我看官方给的指令是sudo apt-get install -y rockpi4-dtbo
安装之后在ubuntu20系统下重启失败,有遇到过吗?
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 这该怎么解决
页:
[1]