网上买了一块二手板子Firefly-RK3288 Reload板子,放了一段时间没用.....下班时不时的玩一下.....不玩下载编译都忘记了。。。
以下是步骤:
1.看下主板原理图
DIY-LED,是核心板输出的gpio, 设置成低电平就能亮了。
2.看下引脚定义,原理图上写的不好,DIY_LED变为了POWER_LED,这里是从60来判断是GPIO8_A2的。
3.看下dtsi , 文件名是firefly-rk3288-reload.dts
这里首先看到的是:
1.驱动要匹配compatible"gpio-leds"
2. GPIO_A2 gpio8 是user的设备节点
所以我们软件要申请的是leds的子节点(power) ----> 的兄弟节点user的gpio
(备注: 这里代码写的是yellow, 实际硬件是个蓝灯啊..............!! )
4.写代码
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#endif
#define GPIO_LOW 0
#define GPIO_HIGH 1
static int firefly_led_probe(struct platform_device *pdev)
{
int ret = -1;
int i;
int gpio;
enum of_gpio_flags flag;
struct device_node *led_node = pdev->dev.of_node;
printk("qyc, led_node->name= %s\n", led_node->name);
printk("qyc, led_node->child->sibling->name= %s\n", led_node->child->sibling->name);
gpio = of_get_named_gpio_flags(led_node->child->sibling, "gpios", 0, &flag);
if(!gpio_is_valid(gpio)) {
printk(KERN_ALERT "qyc, invalid gpio: %d\n", gpio);
return -1;
}
ret = gpio_request(gpio, "user");
if (ret != 0) {
gpio_free(gpio);
printk(KERN_ALERT "qyc, request gpio failed !");
return -EIO;
}
gpio_direction_output(gpio, GPIO_LOW);
msleep(500);
gpio_direction_output(gpio, GPIO_HIGH);
msleep(500);
gpio_direction_output(gpio, GPIO_LOW);
return 0;
}
static int firefly_led_remove(struct platform_device *pdev)
{
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id of_firefly_led_match[] = {
{.compatible = "gpio-leds"},
};
#endif
static struct platform_driver firefly_led_driver = {
.probe = firefly_led_probe,
.remove = firefly_led_remove,
.driver = {
.name = "firefly_led",
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = of_firefly_led_match,
#endif
},
};
static int __init hello_init(void)
{
printk(KERN_INFO "qyc, at %s...\n", __FUNCTION__);
return platform_driver_register(&firefly_led_driver);
}
static void __exit hello_exit(void)
{
platform_driver_unregister(&firefly_led_driver);
}
subsys_initcall(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
附开机log:
5.现象是蓝灯闪, 开机之后常亮. OVER !!