//編譯驅動? (注意Makefile的編譯到移植到開發(fā)板的內核)
? ? ? ? make arch=arm
//清除編譯生成文件
? ? ? ? make clean
******************************************
//安裝驅動
? ? ? ? insmod mycdev.ko
//卸載驅動
? ? ? ? rmmod mycdev
?
需要在<內核路徑>/arch/arm/boot/dts/?
修改 stm32mp157a-fsmp1a-dts 文件
***************************
添加以下內容
leds{ led1-gpios=<&gpioe 10 0>;//10表示引腳編號 0表示默認 led2-gpios=<&gpiof 10 0>; led3-gpios=<&gpioe 8 0>; }; myirq{ interrupt-parent=<&gpiof>;//引用中斷父節(jié)點 interrupts=<9 0>,<7 0>,<8 0>;//聲明和中斷父節(jié)點的關系 9表示索引號,0表示默認設置 };
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
struct device_node *dev;
/* leds{
led1-gpios=<&gpioe 10 0>;//10表示引腳編號 0表示默認
led2-gpios=<&gpiof 10 0>;
led3-gpios=<&gpioe 8 0>;
};*/
struct gpio_desc *gpiono1; // led1
struct gpio_desc *gpiono2; // led2
struct gpio_desc *gpiono3; // led3
/*
myirq{
interrupt-parent=<&gpiof>;//引用中斷父節(jié)點
interrupts=<9 0>,<7 0>,<8 0>;//聲明和中斷父節(jié)點的關系 9表示索引號,0表示默認設置
};
*/
unsigned int irqno1; // key1
unsigned int irqno2; // key2
unsigned int irqno3; // key3
// 定時器處理函數(shù)
void ctl_led(struct gpio_desc *gpiono)
{
// led狀態(tài)取反
gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
}
// 中斷處理函數(shù)
irqreturn_t myirq_handler(int irq, void *dev)
{
if (irq == irqno1)
{
printk("KEY1_INTERRUPT\n");
ctl_led(gpiono3);
}
else if (irq == irqno2)
{
printk("KEY2_INTERRUPT\n");
ctl_led(gpiono2);
}
else if (irq == irqno3)
{
printk("KEY3_INTERRUPT\n");
ctl_led(gpiono1);
}
return IRQ_HANDLED;
}
static int myled_to_init(int a)
{
// 根據(jù)設備樹節(jié)點的路徑解析設備樹信息
dev = of_find_node_by_path("/leds");
if (dev == NULL)
{
printk("解析設備樹節(jié)點失敗\n");
return -EFAULT;
}
printk("解析設備樹節(jié)點成功\n");
// 申請gpio_desc對象并設置輸出為低電平
gpiono1 = gpiod_get_from_of_node(dev, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("申請gpio對象失敗\n");
return -PTR_ERR(gpiono1);
}
printk("申請gpio對象成功\n");
gpiono2 = gpiod_get_from_of_node(dev, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("申請gpio對象失敗\n");
return -PTR_ERR(gpiono2);
}
printk("申請gpio對象成功\n");
gpiono3 = gpiod_get_from_of_node(dev, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("申請gpio對象失敗\n");
return -PTR_ERR(gpiono3);
}
printk("申請gpio對象成功\n");
return 0;
}
static void myled_to_exit(int a)
{
// 滅燈
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 釋放gpio編號
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
printk("led注銷成功");
}
static int __init mycdev_init(void)
{
int ret;
myled_to_init(1);
// 解析按鍵的設備樹節(jié)點
dev = of_find_node_by_path("/myirq");
if (dev == NULL)
{
printk("解析設備樹節(jié)點失敗\n");
return -EFAULT;
}
printk("解析設備樹節(jié)點成功\n");
// 根據(jù)設備樹節(jié)點解析出軟中斷號
irqno1 = irq_of_parse_and_map(dev, 0); // 按鍵1索引號為0
if (!irqno1)
{
printk("解析軟中斷號1失敗\n");
return -ENXIO;
}
printk("解析軟中斷號1成功 irqno=%d\n", irqno1);
irqno2 = irq_of_parse_and_map(dev, 1); // 按鍵2索引號為1
if (!irqno2)
{
printk("解析軟中斷號2失敗\n");
return -ENXIO;
}
printk("解析軟中斷號2成功 irqno=%d\n", irqno2);
irqno3 = irq_of_parse_and_map(dev, 2); // 按鍵3索引號為2
if (!irqno3)
{
printk("解析軟中斷號3失敗\n");
return -ENXIO;
}
printk("解析軟中斷號3成功 irqno=%d\n", irqno3);
// 注冊中斷
ret = request_irq(irqno1, myirq_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
if (ret)
{
printk("注冊中斷1失敗\n");
return ret;
}
printk("注冊中斷1成功\n");
ret = request_irq(irqno2, myirq_handler, IRQF_TRIGGER_FALLING, "key2", NULL);
if (ret)
{
printk("注冊中斷2失敗\n");
return ret;
}
printk("注冊中斷2成功\n");
ret = request_irq(irqno3, myirq_handler, IRQF_TRIGGER_FALLING, "key3", NULL);
if (ret)
{
printk("注冊中斷3失敗\n");
return ret;
}
printk("注冊中斷3成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
myled_to_exit(1);
// 注銷中斷
free_irq(irqno1, NULL);
free_irq(irqno2, NULL);
free_irq(irqno3, NULL);
printk("irq注銷成功\n");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
Makefile?文章來源:http://www.zghlxwxcb.cn/news/detail-622642.html
KERNELDIR 賦予的路徑可能有所不同文章來源地址http://www.zghlxwxcb.cn/news/detail-622642.html
modname ?= mycdev
arch ?= arm
ifeq ($(arch),arm)
KERNELDIR := /home/ubuntu/13_UBOOT/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
distclean:
make -C $(KERNELDIR) M=$(PWD) clean
obj-m := $(modname).o
到了這里,關于驅動開發(fā) day8 (設備樹驅動,按鍵中斷實現(xiàn)led亮滅)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!