全志A33 lichee 開發板 Linux中斷編程原理說明

全志A33 lichee 開發板 Linux中斷編程原理說明

開發平臺

*  芯靈思SinlinxA33開發板

淘寶店鋪: https://sinlinx.taobao.com/

image

嵌入式linux 開發板交流 QQ:641395230

本節實驗目標實現按鍵觸發中斷終端顯示按鍵鬆開或按下
實驗平臺 芯靈思Sinlinx A33 開發板

全志A33 lichee 開發板 Linux中斷編程原理說明

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

全志A33 lichee 開發板 Linux中斷編程原理說明

這裏全志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

全志A33 lichee 開發板 Linux中斷編程原理說明

不知道開發板用的哪個平臺,直接在.config中找

全志A33 lichee 開發板 Linux中斷編程原理說明

由此找到芯片在內核中的中斷號
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章