【GPIO】linux中GPIO相关函数介绍
1、设置GPIO口方向
1 2 | int gpiod_direction_input( struct gpio_desc *desc) int gpiod_direction_output( struct gpio_ desc *desc, int value) |
2、获取GPIO口方向
1 | int gpiod_get_direction( const struct gpio_desc *desc)huoqu |
- 函数返回GPIOF_DIR_ IN或者GPIOF_DIR_OUT
3、读取GPIO口电平
访问分为以下两种
(1)一种是通过储存器读写实现的,这种操作属于原子操作,不需要等待,所以可以在中断处理程序中使用:.
1 2 | int gpiod_get_value( const struct gpio_ _desc *desc); void gpiod_set_value( struct gpio_ desc *desc, int value); |
(2)还有一种访问必须通过消息总线比如I2C或者SPI,这种访问需要在总线访问队列中等待,所以可能进入睡眠,此类访问不能出现在IRQ handler。 可以使用如下函数分辨这些设备:
1 | int gpiod_cansleep( const struct gpio_desc *desc) |
使用如下函数读写:
1 2 | int gpiod_ get_value_cansleep( const struct gpio_desc *desc) void gpiod_set_value_cansleep( struct gpio_desc *desc, int value) |
4、#active-low和raw-value
active-low & raw value有些设备采用低电平有效的方式输出逻辑信号。此时低电平输出1,高电平输出0。此时可以通过访问raw_ value 的方式来访问,实际电路上的值,与逻辑处理无关:假设我们在DTS里面这样设置
1 | reset-gpios = <&gpio3 RK_PA3 GPIO_ACTIVE_LOW>; |
然后我们这样调用
1 | gpiod_set_value_ cansleep( gc5025->reset_gpio, 1); |
因为DTS里面的active 状态是GPI0_ ACTIVE_ LOW, 所以这个代码输出的是低电平
1 | gpiod_set_value_cansleep( gc5025->reset_gpio, 0); |
输出的是高电平
这几个函数如下:
1 2 3 4 5 | int gpiod_ get_ raw_value( const struct gpio_desc *desc) void gpiod_set_ raw_value( struct gpio_desc *desc, int value) int gpiod_get_raw_value_cansleep( const struct gpio_desc *desc) void gpiod_set_ raw_value_cansleep( struct gpio_desc *desc, int value) int gpiod_direction_ output_raw( struct gpio_desc *desc, int value) |
1 2 3 4 5 6 7 8 9 10 11 | raw- value的意思就是不在乎DTS里面的ACTIVE,我set高电平,就是高电平。 逻辑关系汇总如下: Function (example) active-low property physical line gpiod_set_raw_value(desc, 0); //don 't care low gpiod_ set_ raw_ value(desc, 1); //don't care high gpiod_ _set_ value(desc, 0); //default (active -high) low gpiod_ set_ value(desc, 1); //default (active-high) high gpiod_ _set_ value(desc, 0); //active-low high gpiod_ set_ _value(desc, 1); //active-low low //可以使用如下函数判断一个设备是否是低电平有效的设备。 int gpiod_is_active_low( const struct gpio_desc *desc) |
5、設置多个输出
这个没使用过使用如下函数设置一组设备的输出值
1 2 3 4 5 6 7 8 9 10 11 12 | void gpiod_set_array_value(unsigned int array_size, struct gpio_desc **desc_array, int *value_array) void gpiod_set_raw_array_value(unsigned int array_size, struct gpio_desc **desc_array, int *value_array) void gpiod_set_array_value_cansleep(unsigned int array_size, struct gpio_desc **desc_array, int *value_array) void gpiod_set_raw_array_value_cansleep(unsigned int array_size, struct gpio_desc **desc_array, int *value_array) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)