📄 matrix_button.c
字号:
#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/ioctl.h>#include <linux/types.h>#include <linux/config.h>#include <linux/init.h>//#include <linux/io.h>#include "matrix_button.h"static struct timer_list GC3210_74LS164_timer;static u8 col=0;//for output. //used in the GC3210_74LS164_timer() and matrix_button_read() static int delay(int time){ while(--time); return 0;}static void button_read(u8 * row){ //printk("================button read\n"); int time; u8 val=0,reg=0; * row = 0; printk("button_read\n"); //CLK=0 reg = * BUTTON_74HC165_CLK_O; * BUTTON_74HC165_CLK_O = reg & ~(1<<0); //PL=0 reg = * BUTTON_74HC165_PL_O; * BUTTON_74HC165_PL_O = reg & ~(1<<2); //delay 10000 delay(10); //PL=1 reg= * BUTTON_74HC165_PL_O; * BUTTON_74HC165_PL_O = reg | (1<<2); //scanf keyboard for(time=0;time<8;time++) { reg = * BUTTON_74HC165_DATA_I; val = (reg & (1<<1)); val =val>>1; * row |= val << time; //CLK=1 reg = * BUTTON_74HC165_CLK_O; * BUTTON_74HC165_CLK_O = reg | (1<<0);/* if( ~reg & (1<<1)) { * row = (* row << 1) | (1<<0); } else { * row = (* row << 1) & ~(1<<0); }*/ //delay(10); //CLK=0 reg = * BUTTON_74HC165_CLK_O; * BUTTON_74HC165_CLK_O = reg & ~(1<<0); } }static void sendData(u8 data){//send a byte int time; u8 val=0,reg=0; for(time=0;time<8;time++) { //SCL = 0 reg = * BUTTON_74LS164_SCL_O; * BUTTON_74LS164_SCL_O = reg & ~(1<<4); //send a bit if(data & (1<<time)) { reg = * BUTTON_74LS164_SDA_O; * BUTTON_74LS164_SDA_O = reg | (1<<3); } else { reg = * BUTTON_74LS164_SDA_O; * BUTTON_74LS164_SDA_O = reg & ~(1<<3); } delay(10); //SCL = 1 reg = * BUTTON_74LS164_SCL_O; * BUTTON_74LS164_SCL_O = reg | (1<<4); delay(10); } }static void GC3210_74LS164_timer_handler(void){ static int time=0; time = ++time % 8; col=1<<time; sendData( col ); printk("col=%x\n",col); GC3210_74LS164_timer.expires=jiffies + TIMER_DELAY; add_timer(&GC3210_74LS164_timer);}static int matrix_button_open(struct inode *inode,struct file *file){ u8 reg; printk("open matrix button \n"); //* BUTTON_LPB_MISC_CFG = ENABLE_LPB_MISC_CFG; //* BUTTON_GPIO_OE = 0x05; //reset * BUTTON_LPB_MISC_CFG &=0x0; * BUTTON_GPIO_OE &= 0x0; //enable LPB MISC CFG * BUTTON_LPB_MISC_CFG |= ENABLE_LPB_MISC_CFG; //enable CLK output * BUTTON_GPIO_OE |= ENABLE_GPIO_OE_CLK; //enable PL output * BUTTON_GPIO_OE |= ENABLE_GPIO_OE_PL; //enable DATA input * BUTTON_GPIO_OE &= ENABLE_GPIO_OE_DATA; //enable LCD SDA output * BUTTON_GPIO_OE |= ENABLE_GPIO_OE_LCD_DATA; //enable LCD CLK output * BUTTON_GPIO_OE |= ENABLE_GPIO_OE_LCD_CLK; #if Y reg = * BUTTON_74HC165_PL_O; printk("74HC165 PL is %x\n",reg); reg = * BUTTON_74HC165_CLK_O; printk("74HC165 CLK is %x\n",reg); reg = * BUTTON_74LS164_SDA_O; printk("74HC165 CLK is %x\n",reg); reg = * BUTTON_74LS164_SCL_O; printk("74HC165 CLK is %x\n",reg);#endif //initialize the timer for reading key data init_timer(&GC3210_74LS164_timer); GC3210_74LS164_timer.expires=jiffies + TIMER_DELAY; GC3210_74LS164_timer.function=(void *)(GC3210_74LS164_timer_handler); GC3210_74LS164_timer.data=0; //add_timer(&GC3210_74LS164_timer); //return nonseekable_open(inode,file); return 0;}static ssize_t matrix_button_read(struct file *filp,char __user *buf,size_t count,loff_t *oppos){ u8 row=0; u8 position[2]={0}; //printk(" matrix button read\n"); button_read(&row); if(1) { position[0]=row; position[1]=col; } copy_to_user(buf,position,count); return count;}static int matrix_button_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg){ printk("matrix button ioctl\n"); return 0;}static struct file_operations matrix_button_fops={ .open=matrix_button_open, .ioctl=matrix_button_ioctl, .read=matrix_button_read,};static int __init matrix_button_init(void){ int ret; printk("=================Matrix button init================\n"); ret=register_chrdev(0,"matrix_button",&matrix_button_fops); if(ret<0) { printk("matrix button can not get magor number : initial error\n"); return ret; } return 0;}static void __exit matrix_button_exit(void){ printk("=================Matrix button exit=====================\n");}module_init(matrix_button_init);module_exit(matrix_button_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -