本帖最后由 damiaa 于 2024-2-27 21:29 编辑
【Luckfox幸狐 RV1106 Linux 开发板测评】04 应用程序交叉编译与gpio
一、RV1106和电脑交互的方式有多种
串口登录和控制
Ssh登陆和文件拷贝(scp)
adb登录和文件传送
samba传输文件 这里推荐用它
二、编译程序
如果是用python就不需要编译,直接编写好拷贝进去就可以运行,或者用板子上带的vi编辑。
C语言应用程序的编译是使用的交叉编译工具,工具的地址在
/home/z@z-virtual-machine:~/rv-xxxx/rv1106-spinand/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin$
中
vi ~/.bashrc (但我检查了一下好像加进去了!)
我们只需要在home/z/.bashrc文件后面添加
export =PATH=home/z/rv-xxxx/rv1106-spinand/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin:$PATH 保存后重启,环境变量就加进去了。这样在任何地方都可以调用上图路径的gcc编译应用程序了。
三、GPIO的操作
GPIO 的命名方式为 GPIO{bank}_{group}{X},如下所示:
GPIO0_A0 ~ A7
GPIO0_B0 ~ B7
GPIO0_C0 ~ C7
GPIO0_D0 ~ D7
GPIO1_A0 ~ A7
....
GPIO1_D0 ~ D7
....
GPIO4_D0 ~ D7
GPIO 编号计算方法如下:
GPIO 引脚编号计算公式:pin = bank * 32 + number
GPIO 组内编号计算公式:number = group * 8 + X
综上:pin = bank * 32 + (group * 8 + X)
以 GPIO1_C7_d 为例说明,其中:
- bank :1
- group :2 (A=0, B=1, C=2, D=3)
- X :7
所以 GPIO1_C7_d 的引脚编号为:1 x 32 + ( 2 x 8 + 7 ) = 55
设备目录
ls /sys/class/gpio/
查看设备属性
cd /sys/class/gpio/gpio55
ls
把GPIO1_C7_d GPIO1_C6_d跨接
然后到
echo 55 > /sys/class/gpio/export
echo 55 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio54/direction
echo in > /sys/class/gpio/gpio55/direction
echo 1 > /sys/class/gpio/gpio54/value
cat /sys/class/gpio/gpio55/value
四、python控制gpio就把程序直接推送到板子运行程序 python3 gpio.py
from periphery import GPIO
import time
Write_Pin = 55
Read_Pin = 54
Write_GPIO = GPIO(Write_Pin, "out")
Read_GPIO = GPIO(Read_Pin, "in")
try:
while True:
try:
Write_GPIO.write(True)
pin_state = Read_GPIO.read()
print(f"Pin state: {pin_state}")
Write_GPIO.write(False)
pin_state = Read_GPIO.read()
print(f"Pin state: {pin_state}")
time.sleep(1)
except KeyboardInterrupt:
Write_GPIO.write(False)
break
except IOError:
print("Error")
finally:
Write_GPIO.close()
Read_GPIO.close()
五、c语言控制gpio
编辑好的程序交叉编译
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int gpio_pin;
printf("Please enter the GPIO pin number: ");
scanf("%d", &gpio_pin);
FILE *export_file = fopen("/sys/class/gpio/export", "w");
if (export_file == NULL) {
perror("Failed to open GPIO export file");
return -1;
}
fprintf(export_file, "%d", gpio_pin);
fclose(export_file);
char direction_path[50];
snprintf(direction_path, sizeof(direction_path), "/sys/class/gpio/gpio%d/direction", gpio_pin);
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);
char value_path[50];
char cat_command[100];
snprintf(value_path, sizeof(value_path), "/sys/class/gpio/gpio%d/value", gpio_pin);
snprintf(cat_command, sizeof(cat_command), "cat %s", value_path);
FILE *value_file = fopen(value_path, "w");
if (value_file == NULL) {
perror("Failed to open GPIO value file");
return -1;
}
for (int i = 0; i < 3; i++) {
fprintf(value_file, "1");
fflush(value_file);
system(cat_command);
sleep(1);
fprintf(value_file, "0");
fflush(value_file);
system(cat_command);
sleep(1);
}
fclose(value_file);
FILE *unexport_file = fopen("/sys/class/gpio/unexport", "w");
if (unexport_file == NULL) {
perror("Failed to open GPIO unexport file");
return -1;
}
fprintf(unexport_file, "%d", gpio_pin);
fclose(unexport_file);
return 0;
}
然后拷贝到 板子
修改执行属性
运行查看结果
好实验先到这里,谢谢。