📄 keydrv.c
字号:
/***************************************************************
* Green Automation
* File Name: keydrv.c
* Description: Light leds on h9261 board
* Author: Holland
***************************************************************/
#include <linux/module.h>
#include <linux/version.h>#define MOD_INC_USE_COUNT#define MOD_DEC_USE_COUNT
//#define CONFIG_ARCH_AT91SAM9261
//#define __KERNEL__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/fcntl.h>
#include <linux/at91sam9261.h>
#include <asm/hardware.h>
static int key_open(struct inode *inode, struct file *file);
static int key_release(struct inode *inode, struct file *filp);
static int key_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long param);
static void Delay(int);
static unsigned int major = 249;
static struct file_operations key_fops =
{
ioctl: (void(*))key_ioctl,
open: (void(*))key_open,
release: (void(*))key_release,
};
char key_name[]="keydrv";
int keydrv_init_module(void)
{
unsigned long num;
int result;
result = register_chrdev(major, key_name, &key_fops);
if(result < 0)
{
printk("<1>Register Fail!\n");
return result;
}
if(major==0)
major=result;
printk("<1>Key device OK!\n");
AT91PS_SYS sys = (AT91PS_SYS) AT91_VA_BASE_SYS;
sys->PIOA_PPUDR=(unsigned int)(1<<24); // Pull-up Disable
sys->PIOA_PER=(unsigned int)(1<<24); // PIO Enable
sys->PIOA_ODR=(unsigned int)(1<<24); // Input Enable
sys->PMC_PCER==(unsigned int)(1<<AT91C_ID_PIOA); //Enable PIOA MCK
sys->PIOA_CODR=(unsigned int)(1<<14); // Clear Output Data,the lights is on
Delay(1000);
sys->PIOA_SODR=(unsigned int)(1<<14); // Set Output Data,the lights is off
// num = sys->PIOA_PDSR ; // Read Pin Data
// while(num&(unsigned int)(1<<25)) //key-5
// {Delay(100);
// printk("get 1\n");
// num = sys->PIOA_PDSR ; }
// printk("get 0\n");
return 0;
}
void keydrv_cleanup(void)
{
AT91PS_SYS sys = (AT91PS_SYS) AT91_VA_BASE_SYS;
sys->PIOA_SODR=(unsigned int)(1<<14); // Set Output Data,the lights is off
result=unregister_chrdev(major,key_name);
if(result<0)
{
printk("<1>UnRegister Fail!\n");
return;
}
printk("<1>KEYDRV:GOOD-bye!\n");
}
static int key_ioctl(struct inode * inode, struct file * filp, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case 1:
AT91PS_SYS sys = (AT91PS_SYS) AT91_VA_BASE_SYS;
num = sys->PIOA_PDSR ; // Read Pin Data
while(num&(unsigned int)(1<<25)) //key-5 when key-5 is off ,this test will go on
{
if(num&(unsigned int)(1<<26)) //key-4
sys->PIOA_SODR=(unsigned int)(1<<14); // when key-4 off ,the lights is off
else
sys->PIOA_CODR=(unsigned int)(1<<14); // when key-4 on ,the lights is on Delay(100);
Delay(100);
num = sys->PIOA_PDSR ;
}
break;
default:
printk("IOctl error!\n");
break;
}
return 1;
}
static int key_open(struct inode * inode, struct file * file)
{
MOD_INC_USE_COUNT;
return 0;
}
static int key_release(struct inode * inode, struct file * filp)
{
MOD_DEC_USE_COUNT;
return 0;
}
static void Delay(int x)
{
int i,j,k;
for(i=0;i<x;i++)
for(j=0;j<0xff;j++)
for(k=0;k<0xff;k++);
}
module_init(keydrv_init_module);
module_exit(keydrv_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Holland");
MODULE_DESCRIPTION("key driver for H9261 board");
/*end of keydrv.c*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -