写了个IIC键盘驱动有些错误,大牛们过来看看能否解决,同时功能上是否能实现!
附件里面我放了源代码,同时也将源代码帖到下面:
/*IIC键盘程序:CPU是S3C2440,嵌入式操作系统是linux2.6.13,IIC键盘芯片是ZLG7290(周立功的片子);硬件中断连接方法,
将ZLG7290的INT按键中断引脚接GPF2(将GPF2配置成中断功能EINT2),当有按键按下时,ZLG7290的INT引脚产生中断,CPU接受中断
通过IIC读取ZLG7290的01H寄存器,01H寄存器存放键值。
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <asm/arch/irqs.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/signal.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/regs-irq.h>
#include <asm/dma.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/ctype.h>
#include <linux/pagemap.h>
#ifndef __ZLG7290_H__
#define __ZLG7290_H__
#define WRDATA (1)
#define POLLACK (2)
#define RDDATA (3)
#define SETRDADDR (4)
#define IICBUFSIZE 0x20
//ZLG7290 SlaveAddress
#define ZLG7290_SlaveADDR 0x70 //IIC从设备地址
//ZLG7290 Register
#define ZLG7290_SystemReg 0x00 //系统寄存器
#define ZLG7290_Key 0x01 //获取键值寄存器地址,复位为00H
#define ZLG7290_RepeatCnt 0x02 //按键重复按的次数
#endif
#define DEVICE "IIC_ZLG7290"
#define BIT_IIC (0x1<<27)
int major = 233;
int minor = 0;
void *R_GPECON,*R_GPEUP,*R_GPFCON,*R_GPFUP,*R_EXTINT0,*R_IICCON,*R_IICSTAT,*R_IICADD,*R_IICDS;
#define U8 unsigned char
#define U32 unsigned int
static unsigned char _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;
static int flag = 0;
struct work_struct Iic_wq; //工作队列
void Iic_do_work(unsigned long);//中断处理底半部函数
void _Rd2410Iic(U32 slvAddr,U32 addr,U32 *rdata);
void Run_IicPoll(void);
void IicPoll(void);
static U8 Getkey_zlg7290(void);
int IIC_open(struct inode *, struct file *);
int IIC_release(struct inode *, struct file *);
ssize_t IIC_read(struct file *file, char* buf, size_t count, loff_t *f_pos);
unsigned int IIC_poll(struct file* file, poll_table* wait);
irqreturn_t interrupt_handle( int irq, void* dev_id, struct pt_regs* regs );
static void address_map(void)
{
#define GPIO_BASE (0x56000000)
#define IIC_GPECON ( GPIO_BASE + 0x40 )
#define IIC_GPEUP ( GPIO_BASE + 0x48 )
#define IIC_GPFCON ( GPIO_BASE + 0x50 )
#define IIC_GPFUP ( GPIO_BASE + 0x58 )
#define IIC_EXTINT0 ( GPIO_BASE + 0x88 )
#define IIC_BASE (0x54000000)
#define IIC_CON ( IIC_BASE + 0x0 )
#define IIC_STAT ( IIC_BASE + 0x4 )
#define IIC_ADDR ( IIC_BASE + 0x8 )
#define IIC_DS ( IIC_BASE + 0xC )
R_GPECON = ioremap(IIC_GPECON,4);
R_GPEUP = ioremap(IIC_GPEUP ,4);
R_GPFCON = ioremap(IIC_GPFCON,4);
R_GPFUP = ioremap(IIC_GPFUP ,4);
R_EXTINT0 = ioremap(IIC_EXTINT0 ,4);
R_IICCON = ioremap(IIC_CON ,4);
R_IICSTAT = ioremap(IIC_STAT ,4);
R_IICADD = ioremap(IIC_ADDR ,4);
R_IICDS = ioremap(IIC_DS ,4);
}
static void address_unmap(void)
{
iounmap( R_GPECON );
iounmap( R_GPEUP );
iounmap( R_GPFCON );
iounmap( R_GPFUP );
iounmap( R_EXTINT0 );
iounmap( R_IICCON );
iounmap( R_IICSTAT );
iounmap( R_IICADD );
iounmap( R_IICDS );
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = IIC_read,
.open = IIC_open,
.release = IIC_release,
.poll = IIC_poll,
};
struct IIC_dev{
wait_queue_head_t rq; // 读取等待队列
uint8_t key; //用于存放键值
struct semaphore sem;
struct cdev cdev;
};
struct IIC_dev *zlg7290_dev;
//*********************[ Delay ]*********************************
void Delay(int times)
{
udelay(1000);
}
//************************[ _Rd2410Iic ]********************************
void _Rd2410Iic(U32 slvAddr,U32 addr,U32 *rdata)
{
PDEBUG("read---->0\n");
_iicMode = SETRDADDR;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicDataCount = 1;
R_IICDS = slvAddr&0xfe;
R_IICSTAT = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = RDDATA;
_iicPt = 0;
_iicDataCount = 1;
R_IICDS = slvAddr|0x01;
R_IICSTAT = 0xb0; //Master Rx,Start
R_IICCON = 0xaf; //Resumes IIC operation.
while(_iicDataCount!=-1)
Run_IicPoll();
*rdata = _iicData[1];
}
//**********************[ Run_IicPoll ]*********************************
void Run_IicPoll(void)
{
if(R_IICCON & 0x10) //第183行,第一个错误在这里,好像说是&这个符号有问题
IicPoll();
}
//**********************[IicPoll ]**************************************
void IicPoll(void)
{
U32 iicSt,i;
iicSt = R_IICSTAT;
if(iicSt & 0x8){
PDEBUG("bus arbitration is failed\n");
} //When bus arbitration is failed.
if(iicSt & 0x4){
PDEBUG("matched\n");
} //When a slave address is matched with IICADD
if(iicSt & 0x2){
PDEBUG("slave address 0000000b\n");
} //When a slave address is 0000000b
if(iicSt & 0x1){
PDEBUG("Ack isn't received\n");
} //When ACK isn't received
switch(_iicMode)
{
case POLLACK:
PDEBUG("poll--ACK\n");
_iicStatus = iicSt;
break;
case RDDATA:
PDEBUG("read--ACK\n");
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = R_IICDS;
R_IICSTAT = 0x90; //Stop MasRx condition
R_IICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
_iicData[_iicPt++] = R_IICDS;
//The last data has to be read with no ack.
if((_iicDataCount)==0)
R_IICCON = 0x2f; //Resumes IIC operation with NOACK.
else
R_IICCON = 0xaf; //Resumes IIC operation with ACK
break;
case SETRDADDR:
PDEBUG("SETRDADDR\n");
if((_iicDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
R_IICDS = _iicData[_iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
R_IICCON = 0xaf; //resumes IIC operation.
break;
default:
break;
}
}
static U8 Getkey_zlg7290(void)
{
U8 keypress,repeatcount;
static U32 rdata;
keypress=_Rd2410Iic(ZLG7290_SlaveADDR, ZLG7290_Key,&(rdata));//第254行,第二个错误在这里
// repeatcount=_Rd2410Iic(ZLG7290_SlaveADDR, ZLG7290_RepeatCnt,&(rdata));//这个没有用到
return keypress;
}