📄 74hc165_button.c
字号:
/********************************************************************
* Filename: 74HC165_button.c
********************************************************************/
#include<linux/module.h>
#include<linux/config.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/timer.h>
#include<linux/ioctl.h>
#include<linux/io.h>
#include<linux/types.h>
#include<linux/kernel.h>
#include<linux/ioport.h>
#include<linux/errno.h>
#include<asm/uaccess.h>
#include "74HC165_button.h"
#define BUF_MAXSIZE 16
#define TIMER_DELAY 5
static unsigned int BReadBuf = 0;
//static unsigned char chip_number = 1;
static unsigned char chip_number = 2;
static int delay(int time){
while(--time);
return 0;
}
static void button_read(void){
int time;
int val = 0,reg;
//printk("button_read\n");
/* CLK =0 */
SOC3210_74HC165_READ(reg, SOC3210_74HC165_CLK);
SOC3210_74HC165_WRITE(SOC3210_74HC165_CLK, reg & (~(1 << 0)));
/* PL = 0 */
SOC3210_74HC165_READ(reg, SOC3210_74HC165_PL);
SOC3210_74HC165_WRITE(SOC3210_74HC165_PL, reg & ~(1 << 2));
/* delay 100 */
delay(10000);
/* PL = 1 */
SOC3210_74HC165_READ(reg, SOC3210_74HC165_PL);
SOC3210_74HC165_WRITE(SOC3210_74HC165_PL, reg | (1 << 2));
delay(10);
SOC3210_74HC165_READ(reg, SOC3210_74HC165_DATA);
val = ((~reg) & (1 << 1));
BReadBuf = val >> 1;
/* scanf keyboard */
for(time = 1; time < 16; time ++){
/* delay */
delay(100);
/* CLK = 1 */
SOC3210_74HC165_READ(reg, SOC3210_74HC165_CLK);
SOC3210_74HC165_WRITE(SOC3210_74HC165_CLK, reg | (1 << 0));
delay(100);
SOC3210_74HC165_READ(reg, SOC3210_74HC165_DATA);
val = ((~reg) & (1 << 1));
val = val >> 1;
BReadBuf |= val << time;
/* CLK = 0 */
SOC3210_74HC165_READ(reg, SOC3210_74HC165_CLK);
SOC3210_74HC165_WRITE(SOC3210_74HC165_CLK, reg & (~(1 << 0)));
}
if(BReadBuf){
printk("\nBReadBuf value is 0x%08x\n",BReadBuf);
}
}
static int SOC3210_74HC165_button_open(struct inode *inode, struct file *filp){
int reg;
printk("Welcome to use 74HC165 driver\n");
outb(0x3f,SOC3210_LPB_MISC_BASE + REG_LPB_MISC_CTL);
outb(0x05,SOC3210_LPB_MISC_BASE + REG_LPB_GPIO_OE);
printk("init 74HC165 is done\n");
SOC3210_74HC165_READ(reg, SOC3210_74HC165_PL);
printk("74HC165_PL value is 0x%04X\n",reg);
SOC3210_74HC165_READ(reg, SOC3210_74HC165_CLK);
printk("74HC165_CLK value is 0x%04X\n",reg);
return 0;
}
static ssize_t SOC3210_74HC165_button_read(struct file *filp, char __user *buf, size_t count, loff_t *oppos){
int i;
button_read();
if(BReadBuf){
copy_to_user(buf,&BReadBuf,count);
BReadBuf = 0;
return count;
}
return count;
}
static int SOC3210_74HC165_button_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg){
return 0;
}
static struct file_operations SOC3210_74HC165_button_fops = {
.open = SOC3210_74HC165_button_open,
.read = SOC3210_74HC165_button_read,
.ioctl = SOC3210_74HC165_button_ioctl,
};
static int __init SOC3210_74HC165_button_init(void){
int ret;
printk("======================button init=========================\n");
ret = register_chrdev(0,"SOC3210_74HC165_button",&SOC3210_74HC165_button_fops);
if(ret < 0){
printk("74HC165_button can't get major number !\n");
return ret;
}
#ifdef CONFIG_DEVFS_FS
devfs_button_dir = devfs_mk_dir(NULL,"SOC3210_74HC165_button",NULL);
devfs_buttonraw = devfs_register(devfs_kbd_dir,"0raw",DEVFS_FL_DEFAULT,kbdMajor,KBDRAW_MINOR,S_IFCHR|S_IRUSR|S_IWUSR,&SOC3210_74HC165_button_fops,NULL);
#endif
return 0;
}
static void __exit SOC3210_74HC165_button_exit(void){
}
module_init(SOC3210_74HC165_button_init);
module_exit(SOC3210_74HC165_button_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -