外设与接口
硬件资源图
序号 | 接口 | 序号 | 接口 |
---|---|---|---|
1 | RTC 电池接口 | 10 | Type-C 电源接口 |
2 | Micro USB (UART 调试) | 11 | PWR 按键 |
3 | TurboX C6490P SOM | 12 | EDL 按键 |
4 | 3.5mm 耳机接口 | 13 | 摄像头接口 2 |
5 | USB Type-C with DP (USB 3.1) | 14 | 摄像头接口 1 |
6 | USB Type-A (USB 2.0) | 15 | Wi-Fi/蓝牙模块 |
7 | 2 x USB Type-A (USB 3.0) | 16 | 风扇接口 |
8 | 1000M 以太网 | 17 | 40-pin 连接器 |
9 | HDMI OUT | 18 | M.2 Key M 接口 |
40 pin 连接器
GPIO
RUBIK Pi 3 适配了 WiringRP(基于高性能 GPIO 编程库 WiringPi),推荐使用 WiringRP 来控制 GPIO,和对 GPIO 编程。关于 WiringRP 详细信息可访问 https://github.com/rubikpi-ai/WiringRP 查看。
引脚分布
下图是 RUBIK Pi 3 40-pin 连接器的引脚默认功能,其中大部分引脚和树梅派 40-pin 连接器引脚的默认功能兼容。
下表是 40-pin连接器支持的所有功能,图中蓝色字体表明默认功能。
使用 shell 命令控制
在 RUBIK Pi 3 中执行下面的步骤控制 GPIO。
下面命令需要使用 root 权限:
执行
su root
命令,换到 root 用户
- 帐号: root
- 密码: root
或在普通用户下,在命令前加入
sudo
,如sudo gpio readall
,根据提示输入用户的登录密码。
-
使用 WiringRP 相关命令
-
查看 GPIO 状态
gpio readall
-
设置 GPIO 模式
gpio mode 15 in # 将15号引脚模式置为输入
gpio pins # 查看更改之后的状态
gpio mode 15 out # 将15号引脚模式置为输出
gpio pins # 查看更改之后的状态 -
设置引脚电平
gpio write 15 1 # 将15号引脚置为高电平
gpio read 15 # 读取更改后引脚状态
gpio write 15 0 # 将15号引脚置为低电平
gpio read 15 # 读取更改后引脚状态
-
-
操作 /sys/class/gpio 下相关节点
GPIO 子系统的编号如下表。
- 进入 /sys/class/gpio 目录:
cd /sys/class/gpio
- 将要控制的 GPIO 导出,如控制 13 号引脚 GPIO_24:
echo 559 > export
- 进入到 gpio559 目录设置 GPIO 属性:
cd gpio559
ls
-
direction(方向):
- 输入:in
- 输出:out
-
value(值):
- 低电平:0
- 高电平:1
-
edge (中断边沿):
- 上升沿触发:rising
- 下降沿触发:falling
- 双边沿触发:both
- 禁用中断:none
如设置 13 号引脚输出高电平:
echo out > direction
echo 1 > value取消导出 13 号引脚到用户空间:
echo 559 > unexport
使用 WiringRP (C) 控制
WiringRP 库中提供了一系列的 API 函数,用更少的逻辑实现控制。
- 以下代码示例,代码将 13 号引脚设置为输出, 15 号引脚设置为输入,循环检测 15 号引脚的电平状态:
#include <stdio.h>
#include <wiringPi.h>
int main (void)
{
wiringPiSetup () ;
pinMode (13, OUTPUT) ;
pinMode (15, INPUT) ;
for (;;)
{
digitalWrite (13, HIGH) ; // On
printf("%d\n", digitalRead (15)); // On
delay (1000) ; // mS
digitalWrite (13, LOW) ; // Off
printf("%d\n", digitalRead (15)); // On
delay (1900) ;
}
return 0 ;
}
-
编译程序
- 在 RUBIK Pi 3 中编译
adb push gpio.c /opt
adb shell
su
cd /opt
gcc gpio.c -o gpio -lwiringPi备注若无 gcc 命令,可执行
apt install gcc
命令进行安装。 -
将 13 和 15 号引脚使用杜邦线短接,测试 GPIO 电平控制和电平读取情况,如下图所示:
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
./gpio程序运行结果如下:
使用 WiringRP-Python 控制
WiringRP 库中提供了一系列的 API 函数,用更少的逻辑实现控制。
-
下方截取代码是使用 WiringRP 库操作 GPIO 的示例,其中将 13 号引脚设置为输出,15 号引脚设置为输入,循环检测 15 号引脚的电平状态。
import wiringpi
import time
wiringpi.wiringPiSetup()
wiringpi.pinMode(13, 1)
wiringpi.pinMode(15, 0)
wiringpi.digitalRead(15)
while True:
wiringpi.digitalWrite(13,1)
pin_level = wiringpi.digitalRead(15)
print(f"in_gpio level: {pin_level}")
time.sleep(1)
wiringpi.digitalWrite(13,0)
pin_level = wiringpi.digitalRead(15)
print(f"in_gpio level: {pin_level}")
time.sleep(1) -
将 gpio.py 传输到 RUBIK Pi 3 中,如使用 ADB 传输。
adb push gpio.py /opt
-
将 13 和 15 号引脚使用杜邦线短接,测试 GPIO 电平控制和电平读取情况,如下图所示
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
python3 gpio.py程序运行结果如下:
使用 Python 程序控制
-
可使用 Python 的 periphery 库控制 GPIO,可在 RUBIK Pi 3 中使用下面命令进行安装:
apt install python3-periphery
-
下方截取代码是使用 periphery 库操作 GPIO 的示例,其中将 13 号引脚设置 为输出,15 号引脚设置为输入,循环检测 15 号引脚的电平状态。
from periphery import GPIO
import time
out_gpio = GPIO(559, "out")
in_gpio = GPIO(560, "in")
try:
while True:
try:
out_gpio.write(True)
pin_level = in_gpio.read()
print(f"in_gpio level: {pin_level}")
out_gpio.write(False)
pin_level = in_gpio.read()
print(f"in_gpio level: {pin_level}")
time.sleep(1)
except KeyboardInterrupt:
out_gpio.write(False)
break
except IOError:
print("Error")
finally:
out_gpio.close()
in_gpio.close() -
将 gpio.py 传输到 RUBIK Pi 3 中,如使用 ADB 传输。
adb push gpio.py /opt
-
将 13 和 15 号引脚使用杜邦线短接测试 GPIO 电平控制和电平读取情况,如下图所示:
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
python3 gpio.py程序运行结果如下:
使用 C 语言程序控制
-
以下代码示例,代码将 13 号引脚设置为输出, 15 号引脚设置为输入,循环检测 15 号引脚的电平状态:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int out_gpio = 559;
int in_gpio = 560;
int main() {
char export_path[50] = {};
char export_command[100] = {};
snprintf(export_path, sizeof(export_path), "/sys/class/gpio/export");
snprintf(export_command, sizeof(export_command), "echo %d > %s ", out_gpio, export_path);
system(export_command);
snprintf(export_command, sizeof(export_command), "echo %d > %s ", in_gpio, export_path);
system(export_command);
char direction_path[50] = {};
snprintf(direction_path, sizeof(direction_path), "/sys/class/gpio/gpio%d/direction", out_gpio);
FILE *direction_file = fopen(direction_path, "w");
if (direction_file == NULL) {
perror("Failed to open GPIO direction file");
return -1;
}
fprintf(direction_file, "out");
fclose(direction_file);
snprintf(direction_path, sizeof(direction_path), "/sys/class/gpio/gpio%d/direction", in_gpio);
direction_file = fopen(direction_path, "w");
if (direction_file == NULL) {
perror("Failed to open GPIO direction file");
return -1;
}
fprintf(direction_file, "in");
fclose(direction_file);
char value_in_path[50] = {};
char value_out_path[50] = {};
char cat_command[100] = {};
snprintf(value_out_path, sizeof(value_out_path), "/sys/class/gpio/gpio%d/value", out_gpio);
snprintf(value_in_path, sizeof(value_in_path), "/sys/class/gpio/gpio%d/value", in_gpio);
snprintf(cat_command, sizeof(cat_command), "cat %s", value_in_path);
FILE *value_out_file = fopen(value_out_path, "w");
if (value_out_file == NULL) {
perror("Failed to open GPIO value file");
return -1;
}
for (int i = 0; i < 5; i++) {
fprintf(value_out_file, "1");
fflush(value_out_file);
system(cat_command);
sleep(1);
fprintf(value_out_file, "0");
fflush(value_out_file);
system(cat_command);
sleep(1);
}
fclose(value_out_file);
char unexport_path[50] = {};
char unexport_command[100] = {};
snprintf(unexport_path, sizeof(unexport_path), "/sys/class/gpio/unexport");
snprintf(unexport_command, sizeof(unexport_command), "echo %d > %s ", out_gpio, unexport_path);
system(unexport_command);
snprintf(unexport_command, sizeof(unexport_command), "echo %d > %s ", in_gpio, unexport_path);
system(unexport_command);
return 0;
} -
编译程序:
-
交叉编译。
aarch64-qcom-linux-gcc gpio.c -o gpio --sysroot=/home/zhy/qcom_sdk_meta/sysroots/armv8-2a-qcom-linux/
-
在 RUBIK Pi 3 中编译:
adb push gpio.c /opt
adb shell
su
cd /opt
gcc gpio.c -o gpio备注若无 gcc 命令,可执行
apt install gcc
命令进行安装。若使用了交叉编译,需将 gpio 传输到 RUBIK Pi 3 中,如使用 ADB 传输:
adb push gpio /opt
-
-
将 13 和 15 号引脚使用杜邦线短接,测试 GPIO 电平控制和电平读取情况,如下图所示:
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
./gpio程序运行结果如下:
I2C
I2C 是飞利浦公司在 20 世纪 80 年代开发的一种双向 2 线制总线,用于实现高效的 IC 间控制总线。总线上的每个设备都有其唯一的地址(由飞利浦公司领导的 I2C 总机构注册)。I2C 核心支持多控制器模式,以及 10 位目标地址和 10 位可扩展地址。关于 I2C 的更多信息,请参阅 https://www.i2c-bus.org/fileadmin/ftp/i2c_bus_specification_1995.pdf
RUBIK Pi 3 适配了 WiringRP(基于高性能 GPIO 编程库 WiringPi ) ,推荐使用 WiringRP 来控制 I2C,和对 I2C 编程。关于 WiringRP 详细信息可访问 https://github.com/rubikpi-ai/WiringRP 查看。
引脚分布
下图是 RUBIK Pi 3 40-pin 连接器的引脚默认功能,其中大部分引脚和树梅派 40-pin 连接器引脚的默认功能兼容。
3 号引脚和 5 号引脚默认已设置配为 I2C1。
下表是 40-pin 连接器支持的所有功能,图中蓝色字体表明默认功能。
使用 shell 命令测试
在 RUBIK Pi 3 中执行下面步骤控制 I2C 总线。
-
使用 WiringRP 相关命令:
./gpio -x ads1115:100:10 aread 100 #通过 I2C 总线读取 ADS1115 设备的模拟信号值
-
使用 i2cdetect 工具
-
查看 I2C1 接口上的设备:
i2cdetect -a -y -r 1
-
读取地址为 0x38 设备的全部寄存器:
i2cdump -f -y 1 0x38
-
向地址为 0x38 设备的 0x01 寄存器地址写入 0xaa:
i2cset -f -y 1 0x38 0x01 0xaa
-
读取地址为 0x38 的设备,寄存器地址为0x01处的数值:
i2cget -f -y 1 0x38 0x01
-
若无 i2cdetect
等命令,可执行 apt install i2c-tools
命令进行安装。
使用 WiringRP (C) I2C 通信
WiringRP 库中提供了一系列的 API函数,用更少的逻辑实现控制。
-
以下代码示例,I2C1总线和地址为0x38的 设备进行通信,向设备0x01地址处写入0xaa:
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define I2C_ADDRESS 0x38
int main(void)
{
int fd;
if (wiringPiSetup() == -1) {
exit(1);
}
fd = wiringPiI2CSetup(1, I2C_ADDRESS);
if (fd == -1) {
exit(1);
}
unsigned char data[2];
if (read(fd, data, 2) != 2) {
exit(1);
}
wiringPiI2CWriteReg8(fd, 0x01, 0xaa) ;
close(fd);
return 0;
} -
编译程序
-
在 RUBIK Pi 3 中编译
adb push gpio.c /opt
adb shell
su
cd /opt
gcc i2c.c -o i2c -lwiringPi备注若无 gcc 命令,可执行
apt install gcc
命令进行安装。
-
-
将 3 和 5 号引脚连接 I2C 传感器,验证 I2C 总线通信,如下图所示
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令运行程序:
cd /opt
./i2c
使用 WiringRP-Python I2C 通信
WiringRP 库中提供了一系列的 API 函数,用更少的逻辑实现控制。
-
以下代 码示例,使用 I2C1 总线和地址为 0x38 的设备进行通信,向设备 0x01 地址处写入 0xaa:
import wiringpi as wpi
wpi.wiringPiSetup()
fd=wpi.wiringPiI2CSetup(0x38, 1)
wpi.wiringPiI2CWriteReg8 (fd, 0x01, 0xaa) -
将 i2c.py 传输到 RUBIK Pi 3 中,如使用 ADB 传输。
adb push i2c.py /opt
-
将 3 和 5 号引脚连接 I2C 传感器,验证 I2C 总线通信,如下图所示:
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
python3 i2c.py
使用 Python 程序 I2C 通信
-
可使用 Python 的 smbus 库控制 I2C,可在 RUBIK Pi 3 中使用下面命令进行安装:
apt install python3-smbus
-
以下代码示例,使用 I2C1 总线和地址为 0x38 的设备进行通信,向设备 0x01 地址处写入 0xaa:
import smbus
def main():
data = [0x01, 0xaa]
try:
i2c_bus = smbus.SMBus(1)
print("i2cdetect addr : ", end="")
for address in range(0x7F):
try:
i2c_bus.write_i2c_block_data(address, 0, data)
print("0x{:02X},".format(address), end="")
except OSError:
pass
print()
except Exception as e:
print(f"An error occurred: {e}")
finally:
if i2c_bus:
i2c_bus.close()
if __name__ == "__main__":
main() -
将 i2c.py 传输到 RUBIK Pi 3中,如果使用 ADB 传输,命令如下:
adb push i2c.py /opt
-
将 3 和 5 号引脚连接 I2C 传感器,验证 I2C 总线通信,如下图所示:
注意注意引脚顺序,请勿将电源和地引脚短接,否则可能会造成板子损坏。
运行如下命令:
cd /opt
python3 i2c.py程序运行结果如下:
使用 C 语言程序 I2C 通信
-
以下代码示例,I2C1 总线和地址为 0x38 的设备进行通信,向设备 0x01 地址处写入 0xaa:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#define I2C_DEVICE_PATH "/dev/i2c-1"
int main() {
uint8_t data[2] = {0x01,0xaa};
const char *i2c_device = I2C_DEVICE_PATH;
int i2c_file;
if ((i2c_file = open(i2c_device, O_RDWR)) < 0) {
perror("Failed to open I2C device");
return -1;
}
ioctl(i2c_file, I2C_TENBIT, 0);
ioctl(i2c_file, I2C_RETRIES, 5);
printf("i2cdetect addr : ");
for (int x = 0; x < 0x7f; x++)
{
if (ioctl(i2c_file, I2C_SLAVE, x) < 0) {
perror("Failed to set I2C slave address");
close(i2c_file);
return -1;
}
if (write(i2c_file, data, 2) == 2)
{
printf("0x%x,", x);
}
}
close(i2c_file);
printf("\r\n");
return 0;
} -
编译程序:
-
交叉编译。
aarch64-qcom-linux-gcc i2c.c -o i2c --sysroot=/home/zhy/qcom_sdk_meta/sysroots/armv8-2a-qcom-linux/
-