硬件:at91rm9200 软件:linux 目标:9200的6个gpio口接6个按键,编写了查询方式的驱动,驱动编译通过,加载也成功,但是用测试程序测试的时候没反应。我把源码附上,请各位高手给个建议。 typedef char S8; typedef unsigned char U8; typedef int S16; typedef unsigned int U16; typedef long S32; typedef unsigned long U32;
U8 set_cnt;
#define TIME_KEY 200
#define KK0 ((unsigned int)0x1 << 12) //pb12 DOWN #define KK1 ((unsigned int)0x1 << 13) //pb13 Confirm #define KK2 ((unsigned int)0x1 << 14) //pb14 Right #define KK3 ((unsigned int)0x1 << 15) //pb15,left #define KK4 ((unsigned int)0x1 << 16) //pb16,cancle #define KK5 ((unsigned int)0x1 << 17) //pb17 up
U8 KEY_FLAG = 0x00;
#define key_PageDown_** (0x1<<0) #define confirm_press_** (0x1<<1) #define key_NumAdd_** (0x1<< 2) #define key_NumSubb_** (0x1<<3) #define cancel_press_** (0x1<<4) #define key_PageUp_** (0x1<<5)
void kdb_gpio_init(void); //IO端口初始化 static int major = 0; static char dev_name[] = "kdb_dev";
U8 ReadKey( void);
void Delay(U16 t) { while(t--); }
//void Opreat_IO();
/************************************************************************* name : void key_close(struct inode * inode, struct file * file) **************************************************************************/ static int kbd_close (struct inode * inode, struct file * file) { printk("key is closed
"); MOD_DEC_USE_COUNT; return 0; }
/************************************************************************** name : static int key_open(struct inode * inode, struct file * file) ****************************************************************************/ static int kbd_open(struct inode * inode, struct file * file) { printk("key is open
"); MOD_INC_USE_COUNT; return 0; }
/***************************************************************************8
*************************************************************************/
static U8 kbd_read(struct file *file,char *buffer,size_t count,loff_t *ppos) { U8 temp; temp = ReadKey(); copy_to_user(buffer,&temp,sizeof(U8)); return (sizeof(U8)); }
/********************************************************************************** name: atic int kdb_Ioctl(struct inode *inode, struct file * file, unsigned int cmd, unsigned long arg) function: kdb io ioput control ***********************************************************************************/ static int kbd_Ioctl(struct inode *inode,struct file * file,unsigned long cmd,unsigned long arg) { return 0; }
/******************************************************************** name : struct file_operations kdb_dev_fops function: 文件结构 ********************************************************************/ static struct file_operations kdb_dev_fops = { owner: THIS_MODULE, ioctl: kbd_Ioctl, open: kbd_open, release: kbd_close, read: kbd_read, };
/********************************************************** name:int __int_module(void) function:初始化模块 **********************************************************/ static devfs_handle_t devfs_handle; static int __init kdb_init_module(void) { int ret; ret = devfs_register_chrdev(major,dev_name,&kdb_dev_fops); if(ret < 0) { printk("embedder linux:init_module failed with %d
",ret); return ret; } else printk("KERN_INFO:Embedded linux chardevice register successful!
"); if(major == 0) major = ret; devfs_handle = devfs_register(NULL,dev_name,DEVFS_FL_DEFAULT,major,0,S_IFCHR|S_IWUSR,&kdb_dev_fops,NULL); AT91_SYS->PIOB_PER |= KK0 | KK1 | KK2 |KK3 |KK4 | KK5; AT91_SYS->PIOB_ODR |= KK0 | KK1 | KK2 |KK3 |KK4 | KK5; AT91_SYS->PMC_PCER |= (0x1 << AT91C_ID_PIOB); //enable the mck clock; return 0; }
/******************************************************* name: __exit s_cleanup_module(void) function:注销gpio_lcd模块 ********************************************************/ static void __exit kdb_cleanup_module(void) { int retv; retv = devfs_unregister_chrdev(major,dev_name); if(retv < 0) { printk("gpio_kdb unregister failed
"); return; } AT91_SYS->PIOB_PDR |= KK0 | KK1 | KK2 |KK3 |KK4 | KK5; AT91_SYS->PIOB_IFDR |= KK0 | KK1 | KK2 |KK3 |KK4 | KK5; AT91_SYS->PMC_PCDR = 0x1 << AT91C_ID_PIOB; //enable the mck clock; printk("kdb goodbye
"); }
module_init(kdb_init_module); module_exit(kdb_cleanup_module); MODULE_AUTHOR("MZ"); MODULE_LICENSE("GPL");
U8 ReadKey( ) { U8 KK; KEY_FLAG = 0x00; KK = (AT91_SYS->PIOB_PDSR & 0x3f000); if((KK & KK0) == 0 ) { Delay(TIME_KEY); if((KK & KK0) == 0 ) KEY_FLAG |= key_PageDown_**; } if((KK & KK1) == 0 ) { Delay(TIME_KEY); if((KK & KK1) == 0 ) KEY_FLAG |= confirm_press_**; } if((KK & KK2) == 0 ) { Delay(TIME_KEY); if((KK & KK2) == 0 ) KEY_FLAG |= key_NumAdd_** ; } if((KK & KK3) == 0 ) { Delay(TIME_KEY); if((KK & KK3) == 0 ) KEY_FLAG |= key_NumSubb_**; } if((KK & KK4) == 0 ) { Delay(TIME_KEY); if((KK & KK4) == 0 ) KEY_FLAG |= cancel_press_**; } if((KK & KK5) == 0 ) { Delay(TIME_KEY); if((KK & KK5) == 0 ) KEY_FLAG |= key_PageUp_**; }
return KEY_FLAG; }
测试程序:
U8 KEY_FLAG = 0x00; #define key_PageDown_** KEY_FLAG & (0x1<<0) #define confirm_press_** KEY_FLAG & (0x1<<1) #define key_NumAdd_** KEY_FLAG & (0x1<< 2) #define key_NumSubb_** KEY_FLAG & (0x1<<3) #define cancel_press_** KEY_FLAG & (0x1<<4) #define key_PageUp_** KEY_FLAG & (0x1<<5)
void Delayms(int t) { short ms=0x1147; short i,j; for(j=0;j<t;++j) for(i=0;i<ms;++i); } void Delay(int t) { while(--t); }
int main(void) { int fd; int count; if((fd = open("/dev/kdb_dev",0)) < 0) { printf("can not open /dev/kdb_dev
");
exit(0); } printf("kbd fd is %d
",fd); count = read(fd,&KEY_FLAG,sizeof(U8)); printf("key_flag is %x
",KEY_FLAG); if(count != sizeof(U8)) { printf("read ts error!
"); return 0; } while(1) { Delay(1000); if(confirm_press_**) printf("confirm key
"); else if(cancel_press_** ) printf("cancle key
"); else if( key_PageDown_**) printf("page down
"); else if(key_PageUp_**) printf("page up
"); else if(key_NumAdd_**) printf("confirm key
"); else if(key_NumSubb_**) printf("confirm key
"); else ; }
测试程序循环执行,但是终端只显示confirm key ,各位高手看下是什么原因。
|
|