嵌入式Linux驅動學習 按鍵定時器防抖

2021-10-24 12:16:44 字數 2912 閱讀 1945

(1)全域性變數

jiffies:記錄了系統啟動以來,經過了多少tick,初始值為0。乙個tick代表多長時間,在核心的config_hz中定義。比如config_hz=200,則乙個jiffies對應5ms時間。

hz:系統定時器每秒產生中斷的頻率,/arch/arm/include/asm/param.h中定義。

(2)函式

//include/linux/timer.h

struct timer_list

;void

init_timer

(struct timer_list *timer )

;//初始化

add_timer

(struct timer_list*

)//新增定時器

mod_timer

(struct timer_list *

,unsigned

long jiffier_timerout)

//新的定時值

del_timer

(struct timer_list*

)//刪除定時器

驅動程式,在中斷裡,修改定時器,在定時器處理函式中,喚醒佇列。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

static

struct class *buttons_class;

static

struct class_device *buttons_class_dev;

static

struct fasync_struct *button_async;

static

declare_wait_queue_head

(button_waitq)

;/* 中斷事件標誌, 中斷服務程式將它置1,forth_drv_read將它清0 */

static

volatile

int ev_press =0;

struct pin_desc

;static

struct timer_list buttons_timer;

static

struct pin_desc *irq_dev_id;

/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */

/* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 */

static

unsigned

char key_val;

/* * k1,k2,k3,k4對應gpg0,gpg3,gpg5,gpg6

*/struct pin_desc pins_desc[4]

=,,,

,};/*

* 確定按鍵值

*/static irqreturn_t buttons_irq

(int irq,

void

*dev_id)

static

intbuttons_drv_open

(struct inode *inode,

struct file *file)

ssize_t buttons_drv_read

(struct file *file,

char __user *buf, size_t size, loff_t *ppos)

intbuttons_drv_close

(struct inode *inode,

struct file *file)

static

unsigned

buttons_drv_poll

(struct file *file, poll_table *wait)

static

intbuttons_fasync

(int fd,

struct file *file,

int on)

static

struct file_operations buttons_drv_fops =

;static

void

buttons_timer_fun

(long data)

pinval =

s3c2410_gpio_getpin

(irq_dev_id->pin);if

(pinval)

else

ev_press =1;

/* 表示中斷發生了 */

wake_up_interruptible

(&button_waitq)

;/* 喚醒休眠的程序 */

kill_fasync

(&button_async, sigio, poll_in);}

int major;

static

intbuttons_drv_init

(void

)static

void

buttons_drv_exit

(void

)module_init

(buttons_drv_init)

;module_exit

(buttons_drv_exit)

;module_license

("gpl"

);

嵌入式linux之按鍵驅動程式

1.寫出框架 2.硬體操作 操作虛擬位址ioremap 1.重要函式 trap init init irq asm do irq中斷總入口 分辨中斷,處理中斷,清中斷。讀一下wait event interruptible 的原始碼,不難發現這個函式先將 當前程序的狀態設定成task interru...

Linux嵌入式pinctrl驅動學習

dt device tree fdt flattened device tree of open firmware dts device tree source dtsi device tree source include dtb device tree blob dtc device tree ...

嵌入式Linux驅動學習之路 二 u boot體驗

現在的u boot支援powerpc arm x86 mips體系結構的上百種開發板,已經稱為功能最多 靈活性最強,並且開發最積極的開源bootloader。目前由denx的wolfgangdenk維護。u boot郵件列表 denx的u boot主頁 1.安裝好toolchain並設定好環境變數。...