日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 單片機 > 單片機
[導讀]#include #include #include #include #include #include #include #include #include #include #include #include ccess.h>#include #include #include #include static struct timer_list buttons_timer;static st

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include ccess.h>
#include
#include
#include
#include


static struct timer_list buttons_timer;


static struct class *sixthdrv_class;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct fasync_struct *button_async;
//static atomic_t canopen = ATOMIC_INIT(1); //定義原子變量并初始化為1
static DECLARE_MUTEX(button_lock); //定義互斥鎖


/* 中斷事件標志, 中斷服務(wù)程序?qū)⑺?,buttons_timer_read將它清0 */
static volatile int ev_press = 0;//等于0的話就會一直等待
static unsigned char key_val;


struct pin_desc {
unsigned int pin;
unsigned int val;
};
struct pin_desc pins [] = {
{S3C2410_GPG(0),1},
{S3C2410_GPG(3),2},
{S3C2410_GPG(5),3},
{S3C2410_GPG(6),4}
};

static struct pin_desc *irq_pd;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 10ms后啟動定時器 */
/*
原理當觸發(fā)中斷的時候,進入此函數(shù),然后修改定時器的值10ms后
去執(zhí)行定時器指定的函數(shù),讀取鍵值
*/
irq_pd = (struct pin_desc *)dev_id;//這里為參數(shù)傳遞,因為
//buttons_timer_function里沒法得到傳入引腳值
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer_function(unsigned long data)
{
//printk("irqno = %dn" , irqno);
struct pin_desc *pings = irq_pd;

if (pings==NULL)
return -EFAULT;

if(s3c2410_gpio_getpin(pings->pin))//傳入pin值
{
/* 松開 */
key_val = 0x80 | pings->val;
}
else
{
/* 按下 */
key_val = pings->val;
}
ev_press = 1; /* 表示中斷發(fā)生了 */
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */
/*
通過系統(tǒng)函數(shù)kill_fasync向進程發(fā)送異步IO信號
POLL_IN表示數(shù)據(jù)可讀
*/
kill_fasync (&button_async, SIGIO, POLL_IN);
//return IRQ_RETVAL(IRQ_HANDLED);//這里是一個宏,里面是一個條件判斷,最終返回1
}

static int buttons_timer_open(struct inode *inode, struct file *file)
{//S3C2410_GPG(0)

/*
* K1,K2,K3,K4對應(yīng)GPG0,GPG3,GPG5,GPG6
*/
#if 0
if (!atomic_dec_and_test(&canopen))//自減并判斷是否為0,為0返回true,不然返回false
{
atomic_inc(&canopen);//自增
return -EBUSY;
}
#endif

if (file->f_flags & O_NONBLOCK)//以非阻塞方式打開,不成功還是返回
{

//printk(KERN_EMERG "open failedn");
//down_trylock獲取信號量不成功不會睡眠而是正常退出
if (down_trylock(&button_lock))//申請信號量,如果成功申請則返回0,不然返回非0
return -EBUSY;//不會導致睡眠
//return 100;
}
else
{
/* 獲取信號量 */
down(&button_lock);//如果獲取不到信號量,將進入不可中斷的睡眠
// down_killable(&button_lock)//如果獲取不到信號量,可以殺
// down_interruptible(&button_lock)//如果獲取不到信號量,可以中斷方式喚醒
}

request_irq(IRQ_EINT8, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1", &pins[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2", &pins[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3", &pins[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4", &pins[3]);

return 0;
}

ssize_t buttons_timer_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{

if (size != 8)//格式控制,如果用戶程序傳過來的不是1長度,則直接返回
return -EINVAL;
if (file->f_flags & O_NONBLOCK)//非阻塞方式讀,沒有鍵值,返回,不會等待
{
if (!ev_press)
return -EAGAIN;
}
else
{
/* 如果沒有按鍵動作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}

/* 如果沒有按鍵動作, 休眠 在此就使應(yīng)用程序休眠,直到有中斷來喚醒*/
//wait_event_interruptible(button_waitq, ev_press);//ev_press = 0時休眠
//執(zhí)行到這,說明已被喚醒,ev_press = 1,所以要清零
ev_press = 0;
copy_to_user(buf, &key_val, 1);

return 1;
}
int buttons_timer_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT8, &pins[0]);
free_irq(IRQ_EINT11, &pins[1]);
free_irq(IRQ_EINT13, &pins[2]);
free_irq(IRQ_EINT14, &pins[3]);
//atomic_inc(&canopen);
up(&button_lock);
return 0;
}
static unsigned buttons_timer_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0; //為0則poll_wait加入隊列后進入休眠
poll_wait(file, &button_waitq, wait); // 先加入隊列,不會立即休眠

if (ev_press)
mask |= POLLIN | POLLRDNORM;

return mask;//返回非零直接喚醒進程
}
static int buttons_timer_fasync (int fd, struct file *filp, int on)
{
printk("driver: buttons_timer_fasyncn");
return fasync_helper (fd, filp, on, &button_async);//此函數(shù)會初始化button_async結(jié)構(gòu)體
}


static struct file_operations buttons_timer_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量 */
.open = buttons_timer_open,
.read=buttons_timer_read,
.release = buttons_timer_close,
.poll = buttons_timer_poll,
.fasync = buttons_timer_fasync,//注冊異步處理函數(shù)
};//當應(yīng)用程序一打開異步模式,就會調(diào)用此函數(shù)


int major;
static int buttons_timer_init(void)
{

init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
//buttons_timer.expires = 0;
add_timer(&buttons_timer);

major = register_chrdev(0, "buttons_timer", &buttons_timer_fops);

sixthdrv_class = class_create(THIS_MODULE, "buttons_timer");

device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons_timer"); /* /dev/buttons */

return 0;
}

static void buttons_timer_exit(void)
{
unregister_chrdev(major, "buttons_timer");
device_destroy(sixthdrv_class,MKDEV(major, 0));
class_destroy(sixthdrv_class);

}


module_init(buttons_timer_init);

module_exit(buttons_timer_exit);

MODULE_LICENSE("GPL");

這里有一點需要注意,就是關(guān)于空指針的問題如:

這里定義static struct pin_desc *irq_pd;指針,但是沒給它賦值的話,雖然定時器函數(shù)中irq_pd = (struct pin_desc *)dev_id;好像賦了值,但是在struct pin_desc *pings = irq_pd;中再次使用的時候,也會有空指針問題熱報錯 所以要加入

if (pings==NULL)
return -EFAULT;


本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

Sept. 8, 2025 ---- 根據(jù)TrendForce集邦咨詢最新調(diào)查,2025年第二季NVIDIA(英偉達) Blackwell平臺規(guī)?;鲐?,以及北美CSP業(yè)者持續(xù)擴大布局General Server(通用型...

關(guān)鍵字: SSD DDR4 服務(wù)器

Sept. 4, 2025 ---- Apple(蘋果)即將發(fā)布iPhone 17、iPhone 17 Air(暫名)、iPhone 17 Pro及Pro Max四款旗艦新機,除了外觀辨識度升級,處理器性能、散熱和拍攝功...

關(guān)鍵字: iPhone 16 A19處理器 折疊機

Sept. 3, 2025 ---- 根據(jù)TrendForce集邦咨詢最新發(fā)布的《2025近眼顯示市場趨勢與技術(shù)分析》報告,2025年隨著國際品牌陸續(xù)推出AR眼鏡原型,以及Meta預計在近期發(fā)布AR眼鏡Celeste,市...

關(guān)鍵字: AR眼鏡 OLED

Sept. 2, 2025 ---- TrendForce集邦咨詢表示,2025年第二季DRAM產(chǎn)業(yè)因一般型DRAM (Conventional DRAM)合約價上漲、出貨量顯著增長,加上HBM出貨規(guī)模擴張,整體營收為3...

關(guān)鍵字: DRAM 智能手機 ASP

Sept. 1, 2025 ---- 根據(jù)TrendForce集邦咨詢最新調(diào)查,2025年第二季因中國市場消費補貼引發(fā)的提前備貨效應(yīng),以及下半年智能手機、筆電/PC、Server新品所需帶動,整體晶圓代工產(chǎn)能利用率與出貨...

關(guān)鍵字: 晶圓代工 智能手機 筆電

Aug. 28, 2025 ---- 根據(jù)TrendForce集邦咨詢最新調(diào)查,2025年第二季NAND Flash產(chǎn)業(yè)雖面臨平均銷售價格(ASP)小幅下滑,所幸原廠減產(chǎn)策略緩解供需失衡,疊加中、美兩大市場政策推動,整體...

關(guān)鍵字: NAND Flash SSD AI

Aug. 26, 2025 ---- NVIDIA(英偉達)近日推出的Jetson Thor被視為機器人的物理智慧核心,以Blackwell GPU、128 GB記憶體堆疊出2070 FP4 TFLOPS AI算力,是前...

關(guān)鍵字: 機器人 大型語言模型 AI算力

Aug. 21, 2025 ---- 根據(jù)TrendForce集邦咨詢最新液冷產(chǎn)業(yè)研究,隨著NVIDIA GB200 NVL72機柜式服務(wù)器于2025年放量出貨,云端業(yè)者加速升級AI數(shù)據(jù)中心架構(gòu),促使液冷技術(shù)從早期試點邁...

關(guān)鍵字: AI 數(shù)據(jù)中心 服務(wù)器

除了充電電路外,鋰電池的放電過程也需要保護。鋰電池的放電電壓不能低于3.0V,否則電池壽命會大幅縮短。為了實現(xiàn)這一保護,工程師們設(shè)計了DW01芯片與8205 MOS管的電路組合。DW01芯片能夠監(jiān)控鋰電池的放電電壓和電流...

關(guān)鍵字: 鋰電池 電池

在PCB設(shè)計的宏偉藍圖中,布局與布線規(guī)則猶如精密樂章中的指揮棒,是鑄就電路板卓越性能、堅不可摧的可靠性及經(jīng)濟高效的制造成本的靈魂所在。恰如一位巧手的園藝師,合理的布局藝術(shù)性地編排著每一寸空間,既削減了布線交織的繁復迷宮,...

關(guān)鍵字: PCB 電路板
關(guān)閉