GPIO API Reference
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
对GPI涉及到的关键代码进行注解
一、gpio-func
结构
gpio-func <pinnum> <inputmode> <pullup> <pulldown>
功能
配置管脚
参数
pinnum -端口号地址
inputmode- io模式
pullup- 上拉
pulldown-下拉
示例
static void cmd_gpio_func(char *buf, int len, int argc, char **argv)
{
/*
ionum:端口编号
inputmode:输入模式
pullup:上拉
pulldown:下拉
*/
int ionum = -1, inputmode = -1, pullup = -1, pulldown = -1;
if (5 != argc) {
printf("Usage: %s 24 1 1 0\r\n set GPIO24 to input with pullup\r\n",
argv[0]
);
return;
}
ionum = atoi(argv[1]);
inputmode = atoi(argv[2]);
pullup = atoi(argv[3]);
pulldown = atoi(argv[4]);
if (ionum < 0 || inputmode < 0 || pullup < 0 || pulldown < 0) {
puts("Illegal arg\r\n");
return;
}
printf("GPIO%d is set %s with %s pullup %s pulldown\r\n",
ionum,
inputmode ? "input" : "output",
pullup ? "Active" : "null",
pulldown ? "Active" : "null"
);
if (inputmode) {
bl_gpio_enable_input(ionum, pullup ? 1 : 0, pulldown ? 1 : 0);
} else {
bl_gpio_enable_output(ionum, pullup ? 1 : 0, pulldown ? 1 : 0);
}
}
二、gpio_set
结构
gpio_set(uint8 ionum, uint8 val)
功能
获取命令行传入的信息,并作为参数,设置对应gpio口的电平。
参数
ionum -端口号地址
val -电平值
示例
static void cmd_gpio_set(char *buf, int len, int argc, char **argv)
{
int ionum = -1, val = -1;
if (3 != argc) {
printf("Usage: %s 24 1\r\n set GPIO24 output to high\r\n",
argv[0]
);
return;
}
ionum = atoi(argv[1]);
val = atoi(argv[2]);
if (ionum < 0 || val < 0) {
puts("Illegal arg\r\n");
return;
}
printf("GPIO%d is set to %s\r\n",
ionum,
val ? "high" : "lo"
);
bl_gpio_output_set(ionum, val ? 1 : 0);
}
三、gpio_get
结构
gpio_get(uint8 ionum, uint8 val)
功能
获取命令行传入的信息,并作为参数,获取对应gpio口的电平。
参数
ionum -端口号地址
val -电平值
示例
static void uart_echo_task(void *arg)
{
int length = 0, total = 0;
uint8_t buf_recv[128];
uint8_t *pbuf = buf_recv;
const char *name = arg;
const char *send_recv_log = "1234567890abcdefg";
int fd = aos_open(name, 0);
log_info("%s-> fd = %d\r\n", name, fd);
if (fd < 0) {
return;
}
memset(buf_recv, 0, sizeof(buf_recv));
log_step(ci_table_step_init);
aos_write(fd, send_recv_log, strlen(send_recv_log));
log_step(ci_table_step_send);
vTaskDelay(1000);
length = 0;
while (1) {
length = aos_read(fd, pbuf + total, strlen(send_recv_log));
total += length;
if (total != strlen(send_recv_log)) {
continue;
}
if (memcmp(buf_recv, send_recv_log, strlen(send_recv_log)) == 0) {
printf("recvbuff:%s\r\n", send_recv_log);
log_step(ci_table_step_recv);
break;
}
vTaskDelay(10);
}
aos_close(fd);
log_step(ci_table_step_end);
}
总结
以上api实际上使用很灵活,可以结合cmd,结合具体的功能接口