设为首页 加入收藏

TOP

Linux IIC驱动程序(二)
2014-11-24 07:34:53 来源: 作者: 【 】 浏览:3
Tags:Linux IIC 驱动程序
tion.

//mdelay(100);
while(f_nGetACK == 0);// Wait ACK
f_nGetACK = 0;

//Send control byte
rIICDS = unSlaveAddr; // 0xa0
rIICSTAT = 0xb0; // Master Rx,Start
rIICCON = 0xaf; // Resumes IIC operation.
mdelay(100);
while(f_nGetACK == 0);// Wait ACK
f_nGetACK = 0;

//Get data
// cRecvByte = rIICDS;
rIICCON = 0x2f;
mdelay(1);

// Get data
cRecvByte = rIICDS;

// End receive
rIICSTAT = 0x90; // Stop Master Rx condition
rIICCON = 0xaf; // Resumes IIC operation.
mdelay(10); // Wait until stop condtion is in effect.

*pData = cRecvByte;
}



ssize_t I2C_read (struct file *filp, char *buff, size_t count, loff_t *offp)
{
ssize_t result = 0;
int i;


for(i=0; i data[i]=0;
// Read 16 byte from 24C04
for(i=0; i {
iic_read_24c040(0xa0, i, &(data[i])); //第一个参数是slave设备的地址,最后有详细解释
}
data[count]='\0';
// printk("rev=%s\n",data);

if (copy_to_user (buff, data, count))
result = -EFAULT;
//else
//printk (KERN_INFO "wrote %d bytes\n", count);

result=count;

return result;
}




ssize_t I2C_write (struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
int i;
ssize_t ret = 0;
//printk ("Writing %d bytes\n", count);
if (count>127) return -ENOMEM;
if (count<0) return -EINVAL;
if (copy_from_user (data, buf, count))
{
ret = -EFAULT;
}
else {
data[127]='\0';
//printk ("Received: %s\n", data);


// Write 0 - 16 to 24C04
for(i=0; i {
iic_write_24c040(0xa0, i, data[i]);
//mdelay(100);
}
//printk("write end\n");
ret = count;
}
return ret;
}



static int I2C_open(struct inode *inode ,struct file *file)
{
int result;
// Initialize iic
rIICADD = 0x10; // S3C2410X slave address //这个地址当S3C2410X用作IIC的slave设备时用的
rIICCON = 0xaf; // Enable ACK, interrupt, SET IICCLK=MCLK/16
rIICSTAT = 0x10; // Enable TX/RX

rGPECON =(rGPECON&((~0xf)<<28))+(0xa<<28);
//printk("rGPECON=%x\n",rGPECON);

result = request_irq (IRQ_IIC, iic_int_24c04, SA_INTERRUPT, DEVICE_NAME, NULL);
if (result) {
printk(KERN_INFO "I2C: can't get assigned irq\n");
}

//printk(KERN_NOTICE"open the I2C now!\n");
return 0;
}


static int I2C_release(struct inode *inode,struct file *file)
{
free_irq(IRQ_IIC, NULL);//释放中断资源
//printk("I2C closed\n");
return 0;
}

static int I2C_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)

{
return 0;
}
//将设备注册到系统之中
static void I2C_setup_dev(struct cdev *dev,int minor,struct file_operations *fops)
{
int err;
int devno=MKDEV(I2C_major,minor);
cdev_init(dev,fops);
dev->owner=THIS_MODULE;
dev->ops=fops;
err=cdev_add(dev,devno,1);
if(err)
printk(KERN_INFO"Error %d adding I2C %d\n",err,minor);
}

static struct file_operations I2C_remap_ops={
.owner=THIS_MODULE,
.open=I2C_open,
.write = I2C_write,
.read = I2C_read,
.release=I2C_release,
.ioctl=I2C_ioctl,
};

//注册设备驱动程序,主要完成主设备号的注册
static int __init s3c2410_I2C_init(void)
{
int result;

dev_t dev = MKDEV(I2C_major,0);

if(I2C_major)
result = register_chrdev_region(dev,1,DEVICE_NAME);
else
{
result = alloc_chrdev_region(&dev,0,1,DEVICE_NA
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++虚基类解决二义性问题及执行结.. 下一篇x86-qtopia应用程序编写及到arm板..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)