linuxマルチキー駆動最適化

2910 ワード

  • マルチキーサポート
  • キーアクセスアプリケーション設計
  • 関連コード

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define GPFCON 0x56000050
    #define GPFDAT 0x56000054
    
    struct work_struct *work1;
    
    struct timer_list key_timer ;
    unsigned int * gpio_data;
    unsigned int key_num;
    
    void work1_func(struct work_struct *work)
    {
        //printk("key down 
    "); // //jiffies linux , ( ) mod_timer(&key_timer,jiffies + HZ/10);//HZ 1s, 100ms } void key_timer_func(unsigned long delta) { unsigned short data ; data = readw(gpio_data)&0x01 ; if( data == 0 ){ printk("key4 down
    "); key_num = 4; } data = readw(gpio_data)&0x04 ; if( data == 0 ){ printk("key3 down
    "); key_num = 3; } } irqreturn_t key_int(int irq , void * dev_id) { // schedule_work(work1); return 0; } void key_hw_init() { unsigned int * gpio_config; unsigned short data; gpio_config = ioremap(GPFCON,4); data = readw(gpio_config); data &= ~0b110011; data |= 0b100010; writew(data,gpio_config); gpio_data = ioremap(GPFDAT,4); } void key_open(struct inode *node, struct file *filp) { return 0 ; } ssize_t key_read(struct file *filp,char __user *buf,size_t size,loff_t *pos) { copy_to_user(buf,key_num,4); } struct file_operations key_fops={ .open = key_open, .read = key_read, }; struct miscdevice key_miscdev = { .minor = 200, .name = "key", .fops = &key_fops, }; static int button_init() { misc_register(&key_miscdev); // request_irq(IRQ_EINT0,key_init,IRQF_TRIGGER_FALLING,"key",0); //K4 request_irq(IRQ_EINT2,key_init,IRQF_TRIGGER_FALLING,"key",0); //K3 // key_hw_init(); // work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL); INIT_WORK(work1,work1_func); // init_timer(&key_timer); //key_timer.expires = 10 ; key_timer.function = key_timer_func ; // add_timer(&key_timer); return 0 ; } static void button_exit() { misc_deregister(&key_miscdev); } module_init(button_init); module_exit(button_exit);

    三、キーアクセスアプリケーション

    #include 
    #include 
    
    int main()
    {
        int fd,key_num;
        //1.    
        fd = open("/dev/2440key",0);
        if(fd < 0){
            printf("open device fail!
    "); } //2. read(fd,&key_num,4); printf("key is %d
    ",key_num); //3. close(fd); }

    四、設備ファイルの作成

    mknod /dev/2440key c 10 200 //     10(    ),    200, c     。