- UID
- 1029342
- 性别
- 男
|
我的板子用的AT24C08,首先写出驱动的框架:定义 file_operations结构函数static
struct file_operations test_iic =
{
.open = open_iic,
.write = write_iic,
.read = read_iic,
};
加载驱动时的初始化函数和结束时的函数
static
int __init init_iic(void)
{
int ret,result;
ret = register_chrdev(154,"iic0",&test_iic);
if(ret <
0)
{
printk("fail!\n");
return ENODEV;
}
else
{printk("iic is success!!!\n");
result=request_irq(IRQ_IIC,iic_interrupt,IRQF_SHARED,"iic0",iic_interrupt);
if(result <
0)
{
printk("the interrupt is requested fail\n");
return ENODEV;
}
else
{
printk("requested success!\n");
return result;
}
return ret;
}
}
static
void __exit exit_iic(void)
{
unregister_chrdev(154,"iic0");
}
驱动怎么知道调用哪一个呢,要在下面说明一下 :
module_init(init_iic);
module_exit(exit_iic);
MODULE_LICENSE("GPL");
然后分别实现open,write,read
open函数的实现:
static
int open_iic(struct inode * inode,struct file * file)
{
int cmmd;
s3c2410_gpio_cfgpin(S3C2410_GPE14,S3C2410_GPE14_IICSCL);
s3c2410_gpio_cfgpin(S3C2410_GPE15,S3C2410_GPE15_IICSDA);
IIC_IICCON = ioremap(IICCON,4)
;
IIC_IICSTAT = ioremap(IICSTAT,4);
IIC_IICDS = ioremap(IICDS,4);
IIC_IICADD = ioremap(IICADD,4);
IIC_IICLC = ioremap(IICLC,4);
IIC_CLKCON = ioremap(CLKCON,4);
/*打开iic时钟使能*/
*IIC_CLKCON |=(1
<<
16);
cmmd =
((S3C2410_IICCON_ACKEN)|(S3C2410_IICCON_TXDIV_16)|(S3C2410_IICCON_IRQEN)|(0x0f));
writel(cmmd,IIC_IICCON);
writel(0x10,IIC_IICSTAT);
writel(0x10,IIC_IICADD);
return
0;
}
write函数的实现:
static
int write_iic(struct file*file,const
char __user *userbuf,size_t count,loff_t
*off)
{
int i;
ack =
0;
if(copy_from_user(wr_data,userbuf,count)!=0)
{
printk("write is error!!\n");
return EFAULT;
};
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;
for(i=0;i<5;i++)
{
writel(wr_data[i],IIC_IICDS);
printk("wr_data=%x\n",wr_data[i]);
writel(0xaf,IIC_IICCON);
udelay(100);
while(ack ==
0);
ack =
0;
}
writel(0xd0,IIC_IICSTAT);
writel(0xaf,IIC_IICCON);
udelay(100);
return count;
} |
|