|
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
- 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
- <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_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:- 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 测试测试 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 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:~[ 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=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
复制代码测试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();
-
- //! [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
复制代码然后短接 pin19 和 pin21 并运行 a.out- root@rockpi4b:~# ./a.out
- 0xaa
- 0xaa
- ......
复制代码
测试PWM 对于 pwm c 测试,应该安装 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 send data
- or
- sudo mraa-uart dev 1 baud 1500000 send data
复制代码 接收命令如下:- sudo mraa-uart dev 0 baud 1500000 recv 1000
- or
- sudo mraa-uart dev 1 baud 1500000 recv 1000
复制代码接收窗口将显示数据
测试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
复制代码
|
|