基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)
作者
彭東林
pengdonglin137@163.com
平臺
tiny4412 ADK
Linux-4.4.4
u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uImage做了稍許改動
概述
這篇博客以一個簡單的按鍵中斷來演示一下有了設備樹後的中斷的使用,其中涉及到新版kernel的pinctrl和gpio子系統。
正文
在tiny4412的底板上有四個key,如下:
上圖中,在沒有按鍵的時候,對應的GPIO是被拉高的,當按下鍵的時候,對應的GPIO被拉低,從而產生一個下降沿中斷。
有了上面的準備,首先我們需要修改設備樹,添加相應的節點和相關的屬性:
- diff --git a/arch/arm/boot/dts/exynos4412-tiny4412.dts b/arch/arm/boot/dts/exynos4412-tiny4412.dts
- index 610202a..a130047
- --- a/arch/arm/boot/dts/exynos4412-tiny4412.dts
- +++ b/arch/arm/boot/dts/exynos4412-tiny4412.dts
- @@ -, +, @@
- };
- };
- #endif
- +
- + interrupt_demo: interrupt_demo {
- + compatible = "tiny4412,interrupt_demo";
- + tiny4412,int_gpio1 = <&gpx3 GPIO_ACTIVE_HIGH>;
- + tiny4412,int_gpio2 = <&gpx3 GPIO_ACTIVE_HIGH>;
- + tiny4412,int_gpio3 = <&gpx3 GPIO_ACTIVE_HIGH>;
- + tiny4412,int_gpio4 = <&gpx3 GPIO_ACTIVE_HIGH>;
- + };
- };
上面的代碼中,我們添加了四個屬性,對應的就是那四個key對應的gpio,參考gpx3的實現(exynos4x12-pinctrl.dtsi):
- gpx3: gpx3 {
- gpio-controller;
- #gpio-cells = <>;
- interrupt-controller;
- #interrupt-cells = <>;
- };
可以看到,gpx3含有gpio-controller和interrupt-controller屬性,表示它是一個gpio控制器和中斷控制器,它的gpio-cell爲2,意味着應該給這個gpx3傳遞兩個參數,以
tiny4412,int_gpio1 = <&gpx3 2 GPIO_ACTIVE_HIGH>
爲例,這兩個參數就是2和GPIO_ACTIVE_HIGH,如果參數不對的話,比如只傳了一個參數2,那麼在調用of_get_named_gpio會出現如下錯誤:
- [ 31.133799] /interrupt_demo: arguments longer than property
- [ 31.133935] interrupt_demo interrupt_demo: Looking up tiny4412,int_gpio4 property in node /interrupt_demo failed -
- [ 31.144562] interrupt_demo: probe of interrupt_demo failed with error -
使用 make dtbs 編譯完設備樹。
在Samsung的pinctrl驅動中加一些log:
- @@ -, +, @@ static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
- struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
- unsigned int virq;
- + printk("%s enter.\n", __func__);
- +
- if (!bank->irq_domain)
- return -ENXIO;
然後我們編寫對應的驅動程序:
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/gpio.h>
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- #include <linux/interrupt.h>
- typedef struct
- {
- int gpio;
- int irq;
- char name[];
- }int_demo_data_t;
- static irqreturn_t int_demo_isr(int irq, void *dev_id)
- {
- int_demo_data_t *data = dev_id;
- printk("%s enter, %s: gpio:%d, irq: %d\n", __func__, data->name, data->gpio, data->irq);
- return IRQ_HANDLED;
- }
- static int int_demo_probe(struct platform_device *pdev) {
- struct device *dev = &pdev->dev;
- int irq_gpio = -;
- int irq = -;
- int ret = ;
- int i = ;
- int_demo_data_t *data = NULL;
- printk("%s enter.\n", __func__);
- if (!dev->of_node) {
- dev_err(dev, "no platform data.\n");
- goto err1;
- }
- data = devm_kmalloc(dev, sizeof(*data)*, GFP_KERNEL);
- if (!data) {
- dev_err(dev, "no memory.\n");
- goto err0;
- }
- #if 0
- for (i = ; i >= ; i--) {
- sprintf(data[i].name, "tiny4412,int_gpio%d", i+);
- #else
- for (i = ; i < ; i++) {
- #endif
- irq_gpio = of_get_named_gpio(dev->of_node,
- data[i].name, );
- if (irq_gpio < ) {
- dev_err(dev, "Looking up %s property in node %s failed %d\n",
- data[i].name, dev->of_node->full_name, irq_gpio);
- goto err1;
- }
- data[i].gpio = irq_gpio;
- irq = gpio_to_irq(irq_gpio);
- if (irq < ) {
- dev_err(dev,
- "Unable to get irq number for GPIO %d, error %d\n",
- irq_gpio, irq);
- goto err1;
- }
- data[i].irq = irq;
- printk("%s: gpio: %d ---> irq (%d)\n", __func__, irq_gpio, irq);
- ret = devm_request_any_context_irq(dev, irq,
- int_demo_isr, IRQF_TRIGGER_FALLING, data[i].name, data+i);
- if (ret < ) {
- dev_err(dev, "Unable to claim irq %d; error %d\n",
- irq, ret);
- goto err1;
- }
- }
- return ;
- err1:
- devm_kfree(dev, data);
- err0:
- return -EINVAL;
- }
- static int int_demo_remove(struct platform_device *pdev) {
- printk("%s enter.\n", __func__);
- return ;
- }
- static const struct of_device_id int_demo_dt_ids[] = {
- { .compatible = "tiny4412,interrupt_demo", },
- {},
- };
- MODULE_DEVICE_TABLE(of, int_demo_dt_ids);
- static struct platform_driver int_demo_driver = {
- .driver = {
- .name = "interrupt_demo",
- .of_match_table = of_match_ptr(int_demo_dt_ids),
- },
- .probe = int_demo_probe,
- .remove = int_demo_remove,
- };
- static int __init int_demo_init(void)
- {
- int ret;
- ret = platform_driver_register(&int_demo_driver);
- if (ret)
- printk(KERN_ERR "int demo: probe failed: %d\n", ret);
- return ret;
- }
- module_init(int_demo_init);
- static void __exit int_demo_exit(void)
- {
- platform_driver_unregister(&int_demo_driver);
- }
- module_exit(int_demo_exit);
- MODULE_LICENSE("GPL");
編譯驅動後,將ko文件拷貝到nfs目錄下,然後在開發板上執行 mount -t nfs -o nolock 192.168.2.6:/nfsroot /mnt 將共享目錄從PC上掛載到開發板上,然後insmod這個驅動:
- [root@tiny4412 ]# mount -t nfs -o nolock 192.168.2.6:/nfsroot /mnt
- [root@tiny4412 ]# cd /mnt
- [root@tiny4412 mnt]# ls
- fdt interrupt_demo.ko tiny4412.dts
- [root@tiny4412 mnt]# insmod interrupt_demo.ko
- [ 1655.872546] int_demo_probe enter.
[ 1655.872841] samsung_gpio_to_irq enter.
[ 1655.873061] int_demo_probe: gpio: 238 ---> irq (105)
[ 1655.873716] samsung_gpio_to_irq enter.
[ 1655.873906] int_demo_probe: gpio: 239 ---> irq (106)
[ 1655.874424] samsung_gpio_to_irq enter.
[ 1655.874773] int_demo_probe: gpio: 240 ---> irq (107)
[ 1655.879981] samsung_gpio_to_irq enter.
[ 1655.883485] int_demo_probe: gpio: 241 ---> irq (108)- [root@tiny4412 mnt]#
// 然後我們嘗試按底板上的按鍵,會看到相應的中斷log- [root@tiny4412 mnt]# [ 33.462207] int_demo_isr enter, tiny4412,int_gpio1: gpio:, irq:
- [ 33.657304] int_demo_isr enter, tiny4412,int_gpio1: gpio:, irq:
- [ 35.769955] int_demo_isr enter, tiny4412,int_gpio3: gpio:, irq:
- [ 35.951373] int_demo_isr enter, tiny4412,int_gpio3: gpio:, irq:
- [ 36.525804] int_demo_isr enter, tiny4412,int_gpio4: gpio:, irq:
- [ 36.698501] int_demo_isr enter, tiny4412,int_gpio4: gpio:, irq:
- [ 41.710481] int_demo_isr enter, tiny4412,int_gpio2: gpio:, irq:
- [ 41.857190] int_demo_isr enter, tiny4412,int_gpio2: gpio:, irq:
- [root@tiny4412 mnt]# cat /proc/interrupts
- CPU0 CPU1 CPU2 CPU3
- : GIC Edge mct_comp_irq
- : GIC Edge MCT
- : GIC Edge mmc0
- : GIC Edge .hsotg, .hsotg, dwc2_hsotg:usb1
- : GIC Edge ehci_hcd:usb2, ohci_hcd:usb3
- : GIC Edge .serial
- : GIC Edge .i2c
- : GIC Edge .pdma
- : GIC Edge .pdma
- : GIC Edge .mdma
- : GIC Edge .pinctrl
- : GIC Edge .pinctrl
- : COMBINER Edge .pinctrl
- : GIC Edge 106e0000.pinctrl
- : GIC Edge dw-mci
- : exynos4210_wkup_irq_chip Edge mma7660
- : exynos_gpio_irq_chip Edge .sdhci cd
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio1
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio2
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio3
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio4
- IPI0: CPU wakeup interrupts
- IPI1: Timer broadcast interrupts
- IPI2: Rescheduling interrupts
- IPI3: Function call interrupts
- IPI4: Single function call interrupts
- IPI5: CPU stop interrupts
- IPI6: IRQ work interrupts
- IPI7: completion interrupts
- Err:
如果我們修改驅動驅動,改變一下中斷的申請順序,然後重啓系統(測試發現,如果不重啓系統,而是重新加載新的驅動,gpio跟irq的對應關系沒有發生改變)
- #if 1
- for (i = ; i >= ; i--) {
- sprintf(data[i].name, "tiny4412,int_gpio%d", i+);
- #else
- for (i = ; i < ; i++) {
- #endif
此時,會看到如下的log:
- [root@tiny4412 mnt]# insmod interrupt_demo.ko
- [ 119.735726] int_demo_probe enter.
[ 119.736295] samsung_gpio_to_irq enter.
[ 119.736417] int_demo_probe: gpio: 241 ---> irq (105)
[ 119.736852] samsung_gpio_to_irq enter.
[ 119.736985] int_demo_probe: gpio: 240 ---> irq (106)
[ 119.737254] samsung_gpio_to_irq enter.
[ 119.737893] int_demo_probe: gpio: 239 ---> irq (107)
[ 119.743029] samsung_gpio_to_irq enter.
[ 119.746631] int_demo_probe: gpio: 238 ---> irq (108)- [root@tiny4412 mnt]# cat /proc/interrupts
- CPU0 CPU1 CPU2 CPU3
- : GIC Edge mct_comp_irq
- : GIC Edge MCT
- ... ...
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio4
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio3
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio2
- : exynos4210_wkup_irq_chip Edge tiny4412,int_gpio1
- ... ...
未完待續... ...
基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)的更多相关文章
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(2)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- 基於tiny4412的Linux內核移植 --- 实例学习中断背后的知识(2)
作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台 tiny4412 ADK Linux-4.9 概述 前面一篇博文基於tiny4412的Linux內核移 ...
- 基於tiny4412的Linux內核移植 --- 实例学习中断背后的知识(1)
作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台 tiny4412 ADK Linux-4.9 概述 前面几篇博文列举了在有设备树的时候,gpio中断的 ...
- Linux內核中常用的一些延時方法
Linux內核中常用的一些延時方法 這些方法在以下路徑下定義:kernel/include/linux/delay.h #ifndef _LINUX_DELAY_H #define _LINUX_DE ...
- 【转】Linux內核驅動之GPIO子系統(一)GPIO的使用 _蝸牛
原文网址:http://tc.chinawin.net/it/os/article-2512b.html 一 概述 Linux內核中gpio是最簡單,最常用的資源(和interrupt ,dma,ti ...
- 無心插柳的Linux學習者代言人——蔡德明
誰是「蔡德明」恐怕沒有多少人知道,不過提到「鳥哥」這個稱號,在臺灣的Linux社群幾乎是無人不知無人不曉,蔡德明正是鳥哥的本名.鳥哥究竟多有名? 如果你是有意學習Linux的初學者,卻不知如何下手,1 ...
- 第一章 Linux內核簡介
1. Linux是類Unix系統,但他不是Unix. 儘管Linux借鑑了Unix的許多設計並且實現了Unix的API(由Posix標準和其他Single Unix Specification定義的) ...
- 基于tiny4412的Linux内核移植 ---- 調試方法
作者信息 彭東林 郵箱: pengdonglin137@163.com 平臺 Linux-4.4.4 uboot使用的是友善自帶的(爲了支持uImage和設備樹做了稍許修改) 概述 這篇博客主要用於匯 ...
随机推荐
- 探究javascript对象和数组的异同,及函数变量缓存技巧
javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...
- Cassandra简介
在前面的一篇文章<图形数据库Neo4J简介>中,我们介绍了一种非常流行的图形数据库Neo4J的使用方法.而在本文中,我们将对另外一种类型的NoSQL数据库——Cassandra进行简单地介 ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- ExtJS 4.2 Grid组件的单元格合并
ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- vue.js学习笔记
有了孩子之后,元旦就哪也去不了了(孩子太小),刚好利用一些时间,来公司充充电补补课,学习学习新技术,在这里做一个整理和总结.(选择的东西,既然热爱就把他做好吧!). 下来进入咱们的学习环节: 一.从H ...
- 一个诡异的COOKIE问题
今天下午,发现本地的测试环境突然跑不动了,thinkphp直接跑到异常页面,按照正常的排错思路,直接看thinkphp的log 有一条 [ error ] [2]setcookie() expects ...
- 总结iOS开发中的断点续传那些事儿
前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...
- 【代码笔记】iOS-获得当前的月的天数
一,代码. #import "ViewController.h" @interface ViewController () @end @implementation ViewCon ...
- 监控 SQL Server (2005/2008) 的运行状况
Microsoft SQL Server 2005 提供了一些工具来监控数据库.方法之一是动态管理视图.动态管理视图 (DMV) 和动态管理函数 (DMF) 返回的服务器状态信息可用于监控服务器实例的 ...