Linux 下驱动GPIO 其实不用自己写,最近项目要用到开发板上的GPIO,搜索了一番,发现linux 下实现GPIO 驱动实在是太方便了,有现成的framework 可用,基本上要做的事情就是定义一下
Linux 下通过框架实现LED 大致过程如下
1、实现框架要求的几个函数
int gpio_request(unsigned gpio, const char *label)
void gpio_free(unsigned gpio)
int gpio_direction_input(unsigned gpio)
int gpio_direction_output(unsigned gpio, int value)
int gpio_get_value(unsigned gpio)
void gpio_set_value(unsigned gpio, int value)
int gpio_to_irq(unsigned gpio)
int irq_to_gpio(unsigned irq)
这几个函数是硬件相关的,具体实现就跟硬件管脚有关了
2、在结构 gpio_led 中定义gpio 的名字,并跟gpio 管脚关联起来,然后在初始时用函数platform_add_devices加到系统中
3、使用时对 /sys/class/leds 下的文件接口设置相应的值即可
举个例子,例如有1个LED 灯,需要实现如下功能
亮,灭,慢闪,快闪
首先实现
int gpio_request(unsigned gpio, const char *label)
void gpio_free(unsigned gpio)
int gpio_direction_input(unsigned gpio)
int gpio_direction_output(unsigned gpio, int value)
int gpio_get_value(unsigned gpio)
void gpio_set_value(unsigned gpio, int value)
int gpio_to_irq(unsigned gpio)
int irq_to_gpio(unsigned irq)
然后定义
/* LEDS */
static struct gpio_led default_leds[] = {
{ .name = "myboard:led0", .gpio = 4,.default_trigger = "timer" ,},
};
static struct gpio_led_platform_data myboard_led_data = {
.num_leds = ARRAY_SIZE(default_leds),
.leds = default_leds,
};
static struct platform_device myboard_leds = {
.name = "leds-gpio",
.id = -1,
.dev = {
.platform_data = &myboard_led_data,
}
};
static struct platform_device *myboard_devs[] = {
&myboard_leds,
};
static int __init my_board_setup(void)
{
return platform_add_devices(myboard_devs, ARRAY_SIZE(myboard_devs));
}
arch_initcall(my_board_setup);
要常亮 echo 1 > /sys/class/leds/myboard:led0/brightness
要熄灭 echo 0 > /sys/class/leds/myboard:led0/brightness
要快闪
echo 125 > /sys/class/leds/myboard:led0/delay_on
echo 375 > /sys/class/leds/myboard:led0/delay_off
要慢闪
echo 250 > /sys/class/leds/myboard:led0/delay_on
echo 750 > /sys/class/leds/myboard:led0/delay_off
阅读(1879) | 评论(0) | 转发(0) |