全志A33 lichee 开发板 Linux中断编程原理说明

image

开发平台

*  芯灵思SinlinxA33开发板

淘宝店铺: https://sinlinx.taobao.com/

image

嵌入式linux 开发板交流 QQ:641395230

本节实验目标实现按键触发中断终端显示按键松开或按下
实验平台 芯灵思Sinlinx A33 开发板

image

step1 查看原理图,三个按键都连接到LRADC0引脚,通过判断电压大小来确定是按的哪个键。
step2 内核关于 CPU 的中断号linux 中断注册函数中的 irq 中断号并不是芯片物理上的编号,而是由芯片商在移植 Linux 系统时定在构架相
关的头文件中定义好的, 在内核源码中,名字一般是 irqs.h。
打开vim /root/work/sinlinx/a33/lichee/linux-3.4/arch/ARM/mach-sunxi/include/mach/irqs.h

image

这里全志A33 是#include "sun8i/irqs-sun8iw5p1.h"
打开vim /root/work/sinlinx/a33/lichee/linux-3.4/arch/arm/mach-sunxi/include/mach/sun8i/irqs-sun8iw5p1.h

image

不知道开发板用的哪个平台,直接在.config中找

image

由此找到芯片在内核中的中断号
step 3 简要介绍中断驱动要用到的函数
查看 irq.h 文件 里面有关于中断的函数结构体声明 /root/work/sinlinx/a33/lichee/linux-3.4/include/linux/irq.h

* struct irq_data - per irq and irq chip data passed down to chip functions
* @irq:                interrupt number
* @hwirq:              hardware interrupt number, local to the interrupt domain
* @node:               node index useful for balancing
* @state_use_accessors: status information for irq chip functions.
*                      Use accessor functions to deal with it
* @chip:               low level interrupt hardware access
* @domain:             Interrupt translation domain; responsible for mapping
*                      between hwirq number and linux irq number.
* @handler_data:       per-IRQ data for the irq_chip methods
* @chip_data:          platform-specific per-chip private data for the chip
*                      methods, to allow shared chip implementations
* @msi_desc:           MSI descriptor
* @affinity:           IRQ affinity on SMP
*
* The fields here need to overlay the ones in irq_desc until we
* cleaned up the direct references and switched everything over to
* irq_data.
*/
struct irq_data {
        unsigned int            irq;
        unsigned long           hwirq;
        unsigned int            node;
        unsigned int            state_use_accessors;
        struct irq_chip         *chip;
        struct irq_domain       *domain;
        void                    *handler_data;
        void                    *chip_data;
        struct msi_desc         *msi_desc;
#ifdef CONFIG_SMP
        cpumask_var_t           affinity;
#endif
};

struct irqaction 结构体在 /root/work/sinlinx/a33/lichee/linux-3.4/include/linux/interrupt.h

/**
* struct irqaction - per interrupt action descriptor
* @handler:    interrupt handler function
* @flags:      flags (see IRQF_* above)
* @name:       name of the device
* @dev_id:     cookie to identify the device
* @percpu_dev_id:      cookie to identify the device
* @next:       pointer to the next irqaction for shared interrupts
* @irq:        interrupt number
* @dir:        pointer to the proc/irq/NN/name entry
* @thread_fn:  interrupt handler function for threaded interrupts
* @thread:     thread pointer for threaded interrupts
* @thread_flags:       flags related to @thread
* @thread_mask:        bitmask for keeping track of @thread activity
*/
struct irqaction {
        irq_handler_t           handler;      中断服务函数 handler
        unsigned long           flags;
        void                    *dev_id;
        void __percpu           *percpu_dev_id;
        struct irqaction        *next;
        int                     irq;
        irq_handler_t           thread_fn;
        struct task_struct      *thread;
        unsigned long           thread_flags;
        unsigned long           thread_mask;
        const char              *name;
        struct proc_dir_entry   *dir;

} ____cacheline_internodealigned_in_smp;

在 interrupt.h 中有许多和中断相干的函数
exmple:

request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, const char *name, void *dev)
功能 向内核注册一个中断服务函数,当发生中断号为 irq 的中断时候,会执行 handler 指针函数。

void free_irq(unsigned int irq, void *dev_id)

功能 从内核中断链表上删除一个中断结构
void disable_irq(unsigned int irq)

功能 关闭指定的中断,并等待中断服务函数运行结束后才会返回, 在中断函数外调用,
不能在中断服务程序中调用。
void disable_irq_nosync(unsigned int irq)

功能 关闭指定的中断,不等待中断服务函数结束,调用完这个函数立即返回。 可以中断服务函数
中调用。
void enable_irq(unsigned int irq)

功能 使能指定的中断
local_save_flags(flags)

功能 禁止本 CPU 全部中断,并保存 CPU 状态信息。
local_irq_disable()

功能 禁止本 CPU 全部中断

Linux 内核和 GPIO 口相关的内核 API
exmple:

static inline int gpio_get_value(unsigned int gpio)

功能 获取指定 IO 口的电平状态
返回 IO 电平状态,非 0:表示高电平 , 0 表示低电平

static inline void gpio_set_value(unsigned int gpio, int value)

功能 设置 gpio 口的电平状态为 value
返回 IO 电平状态,非 0:表示高电平 , 0 表示低电平

static inline int gpio_to_irq(unsigned int gpio)

功能 通过 gpio 口编号获得出现这个 IO 上的外部中断编号
返回 这个 IO 上对应的外部中断编号

step 4关于 Linux 中断共享
共享中断是指多个设备共享一根中断线的情况, 在中断到来时,会遍历共享此中断的所有中断处理程序, 直

到某一个中断服务函数时返回 IRQ_HANDLED

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容