S5PV210(TQ210)學(xué)習(xí)筆記——按鍵驅(qū)動(dòng)程序
一 按鍵驅(qū)動(dòng)程序的簡(jiǎn)單實(shí)現(xiàn)
本文引用地址:http://m.butianyuan.cn/article/201611/322807.htm下面是基于中斷和消息的按鍵驅(qū)動(dòng)程序,其工作原理是:當(dāng)應(yīng)用程序讀取鍵值時(shí),會(huì)調(diào)用按鍵驅(qū)動(dòng)程序的read函數(shù),而我們實(shí)現(xiàn)的read函數(shù)檢測(cè)完讀取長(zhǎng)度后沒有直接讀取鍵值而是等待按鍵消息,如果沒有按鍵,程序會(huì)進(jìn)入休眠狀態(tài),這樣可以節(jié)省大量的CPU,而當(dāng)我們按鍵時(shí)硬件會(huì)產(chǎn)生中斷,程序自動(dòng)進(jìn)入中斷處理函數(shù),在中斷處理函數(shù)中,驅(qū)動(dòng)程序讀取鍵值存入全局變量并激活read函數(shù)中等待的消息,應(yīng)用程序被迅速喚醒并通過(guò)read函數(shù)讀取鍵值,如此,完成了獲取鍵值的工作。下面是源碼,比較簡(jiǎn)單,也就不多說(shuō)了。
源碼:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- staticdev_tdevno;
- staticstructcdevcdev;
- staticstructclass*buttons_class;
- staticstructdevice*buttons_device;
- staticwait_queue_head_tbutton_waitq;
- staticvolatileintpressed=0;
- staticunsignedcharkey_val;
- structkey_desc{
- unsignedintpin;
- unsignedcharvalue;
- };
- staticstructkey_desckey_descs[8]={
- [0]={
- .pin=S5PV210_GPH0(0),
- .value=0x00,
- },
- [1]={
- .pin=S5PV210_GPH0(1),
- .value=0x01,
- },
- [2]={
- .pin=S5PV210_GPH0(2),
- .value=0x02,
- },
- [3]={
- .pin=S5PV210_GPH0(3),
- .value=0x03,
- },
- [4]={
- .pin=S5PV210_GPH0(4),
- .value=0x04,
- },
- [5]={
- .pin=S5PV210_GPH0(5),
- .value=0x05,
- },
- [6]={
- .pin=S5PV210_GPH2(6),
- .value=0x06,
- },
- [7]={
- .pin=S5PV210_GPH2(7),
- .value=0x07,
- },
- };
- staticirqreturn_tbuttons_irq(intirq,void*dev_id){
- volatilestructkey_desc*key=(volatilestructkey_desc*)dev_id;
- if(gpio_get_value(key->pin)){
- key_val=key->value|0x80;
- }
- else{
- key_val=key->value;
- }
- pressed=1;
- wake_up_interruptible(&button_waitq);
- returnIRQ_RETVAL(IRQ_HANDLED);
- }
- staticintbuttons_open(structinode*inode,structfile*file){
- intret;
- ret=request_irq(IRQ_EINT(0),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key1",&key_descs[0]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(1),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key2",&key_descs[1]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(2),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key3",&key_descs[2]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(3),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key4",&key_descs[3]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(4),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key5",&key_descs[4]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(5),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key6",&key_descs[5]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(22),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key7",&key_descs[6]);
- if(ret)
- returnret;
- ret=request_irq(IRQ_EINT(23),buttons_irq,IRQ_TYPE_EDGE_BOTH,"key8",&key_descs[7]);
- if(ret)
- returnret;
- return0;
- }
- staticssize_tbuttons_read(structfile*file,char__user*data,size_tcount,loff_t*loff){
- if(count!=1){
- printk(KERN_ERR"Thedrivercanonlygiveonekeyvalueonce!");
- return-ENOMEM;
- }
- wait_event_interruptible(button_waitq,pressed);
- pressed=0;
- if(copy_to_user(data,&key_val,1)){
- printk(KERN_ERR"Thedrivercannotcopythedatatouserarea!");
- return-ENOMEM;
- }
- return0;
- }
- staticintbuttons_close(structinode*inode,structfile*file){
- free_irq(IRQ_EINT(0),&key_descs[0]);
- free_irq(IRQ_EINT(1),&key_descs[1]);
- free_irq(IRQ_EINT(2),&key_descs[2]);
- free_irq(IRQ_EINT(3),&key_descs[3]);
- free_irq(IRQ_EINT(4),&key_descs[4]);
- free_irq(IRQ_EINT(5),&key_descs[5]);
- free_irq(IRQ_EINT(22),&key_descs[6]);
- free_irq(IRQ_EINT(23),&key_descs[7]);
- return0;
- }
- structfile_operationsbuttons_ops={
- .open=buttons_open,
- .read=buttons_read,
- .release=buttons_close,
- };
- intbuttons_init(void){
- intret;
- cdev_init(&cdev,&buttons_ops);
- cdev.owner=THIS_MODULE;
- ret=alloc_chrdev_region(&devno,0,1,"buttons");
- if(ret){
- printk(KERN_ERR"allocchardeviceregionfaild!");
- returnret;
- }
- ret=cdev_add(&cdev,devno,1);
- if(ret){
- printk(KERN_ERR"addchardevicefaild!");
- gotoadd_error;
- }
- buttons_class=class_create(THIS_MODULE,"buttonsdrv");
- if(IS_ERR(buttons_class)){
- printk(KERN_ERR"createclasserror!");
- gotoclass_error;
- }
- buttons_device=device_create(buttons_class,NULL,devno,NULL,"buttons");
- if(IS_ERR(buttons_device)){
- printk(KERN_ERR"createbuttonsdeviceerror!");
- gotodevice_error;
- }
關(guān)鍵詞:
S5PV210按鍵驅(qū)
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評(píng)論