标题:
s3c2440之iic驱动程序编写(2)
[打印本页]
作者:
yuyang911220
时间:
2015-2-26 15:12
标题:
s3c2440之iic驱动程序编写(2)
read函数的实现:static
int read_iic(struct file*file,const
char __user *userbuf,size_t count,loff_t
*off)
{
int i;
ack =
0;
writel(0xa0,IIC_IICDS);
writel(0xf0,IIC_IICSTAT);
udelay(100);
while(ack ==
0);
ack =
0;
writel(0x00,IIC_IICDS);
writel(0xaf,IIC_IICCON);
udelay(100);
while(ack ==
0);
ack =
0;
writel(0xa1,IIC_IICDS);
writel(0xb0,IIC_IICSTAT);
writel(0xaf,IIC_IICCON);
udelay(100);
for(i=0;i<5;i++)
{
while(ack ==
0);
ack =
0;
writel(0xaf,IIC_IICCON);
udelay(100);
ra_data
=readl(IIC_IICDS);
printk("ra_data=%x\n",ra_data
);
}
writel(0x90,IIC_IICSTAT);
writel(0xaf,IIC_IICCON);
udelay(100);
if(copy_to_user(userbuf,ra_data,count)!=0)
{
printk("read is error!!\n");
return EFAULT;
};
return count;
}
中断的实现
/**********************中断实现**************************/
static
irqreturn_t iic_interrupt(int irq,void
*dev_id)
{
ack =
1;
return IRQ_HANDLED;
}
/*********************************************************/
这时,驱动基本完成,再加上必要的头文件和寄存器地址的定义
#include<linux/delay.h>
#include<linux/pci.h>
#include<linux/poll.h>
#include<linux/interrupt.h>
#include<mach/regs-gpio.h>
#include<plat/regs-iic.h>
#define IICCON 0x54000000
#define IICSTAT 0x54000004
#define IICDS 0x5400000c
#define IICADD 0x54000008
#define IICLC 0x54000010
#define CLKCON 0x4c00000c
static
unsigned
long
*IIC_IICCON ;
static
unsigned
long
*IIC_IICSTAT;
static
unsigned
long
*IIC_IICDS ;
static
unsigned
long
*IIC_IICADD;
static
unsigned
long
*IIC_IICLC ;
static
unsigned
long
*IIC_CLKCON;
static
int wr_data[5];
static
int ra_data[5];
static
int ack =
0;
完成。下面编写应用程序进行测试
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
int main()
{
int fd;
int wr_data[5]={0,1,2,3,4};
int ra_data[5];
fd = open("dev/iic0",O_RDWR);
write(fd,wr_data,sizeof(wr_data));
sleep(2);
read(fd,ra_data,sizeof(ra_data));
}
首先写入5个数字,再将其读出。
测试正常。
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0