如何使用mpc8xxx_gpio.c设备驱动
我是设备驱动程序开发的新手。我正在尝试访问 MPC837xERDB 评估板的 GPIO。
我已将其内核升级到 linux-2.6.28.9
并启用对 mpc8xxx_gpio.c
的支持。启动时,它成功检测到两个 gpio
控制器。
现在我的问题是如何使用它与 gpio
引脚进行通信?我是否必须修改 mpc8xxx_gpio.c
文件中的代码才能执行我想要使用 gpios
执行的任何操作,或者我可以使用标准 gpio
API在内核中提供(gpio_request()
/gpio_free()
)。我也尝试了标准内核 API,但失败了。这是我的代码:
#include <linux/module.h>
#include <linux/errno.h> /* error codes */
#include <linux/gpio.h>
static __init int sample_module_init(void)
{
int ret;
int i;
for (i=1; i<32; i++) {
ret = gpio_request(i, "Sample Driver");
if (ret) {
printk(KERN_WARNING "sample_driver: unable to request GPIO_PG%d\n", i);
//return ret;
}
}
return 0;
}
static __exit void sample_module_exit(void)
{
gpio_free(9);
}
MODULE_LICENSE("GPL");
module_init(sample_module_init);
module_exit(sample_module_exit);
它给出了以下 O/P:
[ 617.075329] sample_driver: unable to request GPIO_PG1
[ 617.080418] sample_driver: unable to request GPIO_PG2
[ 617.085470] sample_driver: unable to request GPIO_PG3
[ 617.090522] sample_driver: unable to request GPIO_PG4
[ 617.095574] sample_driver: unable to request GPIO_PG5
[ 617.100625] sample_driver: unable to request GPIO_PG6
[ 617.105676] sample_driver: unable to request GPIO_PG7
[ 617.110727] sample_driver: unable to request GPIO_PG8
[ 617.115779] sample_driver: unable to request GPIO_PG9
[ 617.120830] sample_driver: unable to request GPIO_PG10
[ 617.125968] sample_driver: unable to request GPIO_PG11
[ 617.131106] sample_driver: unable to request GPIO_PG12
[ 617.136245] sample_driver: unable to request GPIO_PG13
[ 617.141383] sample_driver: unable to request GPIO_PG14
[ 617.146521] sample_driver: unable to request GPIO_PG15
[ 617.151660] sample_driver: unable to request GPIO_PG16
[ 617.156798] sample_driver: unable to request GPIO_PG17
[ 617.161936] sample_driver: unable to request GPIO_PG18
[ 617.167074] sample_driver: unable to request GPIO_PG19
[ 617.172213] sample_driver: unable to request GPIO_PG20
[ 617.177351] sample_driver: unable to request GPIO_PG21
[ 617.182489] sample_driver: unable to request GPIO_PG22
[ 617.187628] sample_driver: unable to request GPIO_PG23
[ 617.192767] sample_driver: unable to request GPIO_PG24
[ 617.197905] sample_driver: unable to request GPIO_PG25
[ 617.203042] sample_driver: unable to request GPIO_PG26
[ 617.208182] sample_driver: unable to request GPIO_PG27
[ 617.213319] sample_driver: unable to request GPIO_PG28
[ 617.218458] sample_driver: unable to request GPIO_PG29
[ 617.223597] sample_driver: unable to request GPIO_PG30
[ 617.228735] sample_driver: unable to request GPIO_PG31
[ 617.233873] sample_driver: unable to request GPIO_PG32
有人可以给我提供示例代码或其他东西吗?实际上我正在尝试设置 GPIO 引脚号。 9 为低电平有效,因为它连接到板上的 LED。
I am new to device driver development. I am trying to access the GPIO of MPC837xERDB eval board.
I have upgraded its kernel to linux-2.6.28.9
and enable support for mpc8xxx_gpio.c
. On boot up, it successfully detect two gpio
controllers.
Now my question is how I am going to use it to communicate with the gpio
pins? Do I have to modify the code in mpc8xxx_gpio.c
file to do whatever I want to do with gpios
or I can use the standard gpio
API provided in kernel ( gpio_request()
/gpio_free()
). I also tries the standard kernel API, but it fails. Here is my code :
#include <linux/module.h>
#include <linux/errno.h> /* error codes */
#include <linux/gpio.h>
static __init int sample_module_init(void)
{
int ret;
int i;
for (i=1; i<32; i++) {
ret = gpio_request(i, "Sample Driver");
if (ret) {
printk(KERN_WARNING "sample_driver: unable to request GPIO_PG%d\n", i);
//return ret;
}
}
return 0;
}
static __exit void sample_module_exit(void)
{
gpio_free(9);
}
MODULE_LICENSE("GPL");
module_init(sample_module_init);
module_exit(sample_module_exit);
It give the following O/P:
[ 617.075329] sample_driver: unable to request GPIO_PG1
[ 617.080418] sample_driver: unable to request GPIO_PG2
[ 617.085470] sample_driver: unable to request GPIO_PG3
[ 617.090522] sample_driver: unable to request GPIO_PG4
[ 617.095574] sample_driver: unable to request GPIO_PG5
[ 617.100625] sample_driver: unable to request GPIO_PG6
[ 617.105676] sample_driver: unable to request GPIO_PG7
[ 617.110727] sample_driver: unable to request GPIO_PG8
[ 617.115779] sample_driver: unable to request GPIO_PG9
[ 617.120830] sample_driver: unable to request GPIO_PG10
[ 617.125968] sample_driver: unable to request GPIO_PG11
[ 617.131106] sample_driver: unable to request GPIO_PG12
[ 617.136245] sample_driver: unable to request GPIO_PG13
[ 617.141383] sample_driver: unable to request GPIO_PG14
[ 617.146521] sample_driver: unable to request GPIO_PG15
[ 617.151660] sample_driver: unable to request GPIO_PG16
[ 617.156798] sample_driver: unable to request GPIO_PG17
[ 617.161936] sample_driver: unable to request GPIO_PG18
[ 617.167074] sample_driver: unable to request GPIO_PG19
[ 617.172213] sample_driver: unable to request GPIO_PG20
[ 617.177351] sample_driver: unable to request GPIO_PG21
[ 617.182489] sample_driver: unable to request GPIO_PG22
[ 617.187628] sample_driver: unable to request GPIO_PG23
[ 617.192767] sample_driver: unable to request GPIO_PG24
[ 617.197905] sample_driver: unable to request GPIO_PG25
[ 617.203042] sample_driver: unable to request GPIO_PG26
[ 617.208182] sample_driver: unable to request GPIO_PG27
[ 617.213319] sample_driver: unable to request GPIO_PG28
[ 617.218458] sample_driver: unable to request GPIO_PG29
[ 617.223597] sample_driver: unable to request GPIO_PG30
[ 617.228735] sample_driver: unable to request GPIO_PG31
[ 617.233873] sample_driver: unable to request GPIO_PG32
Can someone provide me a sample code or something else. Actually I am trying to set the GPIO pin no. 9 to active low as it is connected to a LED on board.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否查看过
Documentation/gpio.txt
在你的内核源代码中?Have you looked into
Documentation/gpio.txt
in your kernel source?根据您的其他问题:
您有两块 GPIO 引脚。一个块从编号 192 开始,另一个块从编号 224 开始。在上面的代码中,您尝试请求 GPIO 0-31,但您的平台上不存在该编号。
您可以通过查看每个
gpiochip*
目录中的ngpio
文件来了解每个块中有多少个 GPIO。From your other question:
You have two blocks of GPIO pins. One block starts at number 192 and the other at 224. In your code above, you are trying to request GPIO 0-31, which don't exist on your platform.
You can find out how many GPIOs are in each block by looking at the
ngpio
file in eachgpiochip*
directory.