买了树莓派5才知道,Wiringpi不支持树莓派5了, Wiringpi官方好像不更新了。那用什么来替代呢?
通过资料查询,libgpiod 支持io控制;
https://github.com/brgl/libgpiod //gpio库
gpiodetect #检测支持的io
可以看到,果然检测出来了RP1芯片信息。接下来就需要去看help看看怎么用起来了。
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#ifndef CONSUMER
#define CONSUMER "Consumer"
#endif
int main(int argc, char **argv)
{
char *chipname = "gpiochip0"; //GPIO1
char *chipname1 = "gpiochip4"; //GPIO5
unsigned int line_num_10 = 10; // 偏锟斤拷GPIO 10 GPIO1_10
unsigned int line_num_14 = 14; // 偏锟斤拷GPIO 136 GPIO5_8
int val;
struct gpiod_chip *chip;
struct gpiod_chip *chip_rp1;
struct gpiod_line *line10, *line14;
int i, ret;
//find gpiochip
printf("start my test gpio\n");
chip_rp1 = gpiod_chip_open_by_name(chipname1);
if (!chip_rp1) {
printf("Open chip by name failed. name: %s\n", chipname1);
}
//find gpio line
line14 = gpiod_chip_get_line(chip_rp1, line_num_14);
if (!line14) {
printf("Get line failed. line_num: %u\n", line_num_14);
}
//set to output
ret = gpiod_line_request_output(line14, CONSUMER,GPIOD_LINE_ACTIVE_STATE_HIGH);
if (ret < 0) {
printf("Request line18 as output failed\n");
}
/* Blink 20 times */
val = 1;
for (i = 0; i <= 20; i++) {
ret = gpiod_line_set_value(line14, val);
if (ret < 0) {
printf("Set line14 output failed. val: %u\n", val);
}
printf("Times %d\n", i);
usleep(500000);
val = !val;
}
gpiod_line_release(line14);
gpiod_chip_close(chip_rp1);
return 0;
}
然后,用gcc myapp.c –lgpiod编译
./a.out 执行,就可以了;