将一个组中的写入到AT24C02中,然后再读出来!
以下为DOME程序!
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define DEV_NAME "/dev/2402_RW"
int main()
{ int fd,ret,i;
int wr_data[5]={0x00,0x10,0x30,0x70,0xf0};
int ra_data[5];
fd=open(DEV_NAME,O_RDWR);
if (fd<0)
{ printf ("the derive is opened fail !!\n");
return fd;
}
else
{ printf ("the derive is opened success !!\n");
write(fd,wr_data,sizeof(wr_data));
for(i=0;i<5;i++)
{ printf ("write-----data=%x\n",wr_data[i]);
}
sleep(2);
read(fd,ra_data,20);
for (i=0;i<5;i++)
{ printf ("read------data=%x\n",ra_data[i]);
}
}
ret=close(fd);
printf ("the ret=%d\n",ret);
}
以下为源码!
#include <linux/kernel.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <asm-generic/errno-base.h>
#include <asm/arch-s3c2410/regs-gpio.h>
#include <asm/arch-s3c2410/regs-iic.h>
#include <asm/arch-s3c2410/irqs.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define DEV_ID 107
#define DEV_NAME "2402_RW"
#define IICCON 0x54000000
#define IICSTAT 0x54000004
#define IICDS 0x5400000c
#define IICADD 0x54000008
#define IICLC 0x54000010
#define CLKCON 0x4c00000c
static void *S3C2402_IICSTAT;
static void *S3C2402_IICCON;
static void *S3C2402_IICDS;
static void *S3C2402_IICADD;
static void *S3C2402_IICLC;
static void *S3C2440_CLKCON;
static int wr_data[5];
static int ra_data[5];
static int ack=0;
void delay(int t)
{ int i;
for(;t>0;t--)
{ for(i=0;i<20000;i++);
}
}
static int open_2402(struct inode * inode, struct file * file)
{ int cmmd;
writel(0xc0000,S3C2410_GPEUP);
s3c2410_gpio_cfgpin(S3C2410_GPE14,S3C2410_GPE14_IICSCL);
s3c2410_gpio_cfgpin(S3C2410_GPE15,S3C2410_GPE15_IICSDA);
S3C2402_IICCON=ioremap(IICCON,0x00000004);
S3C2402_IICSTAT=ioremap(IICSTAT,0x00000004);
S3C2402_IICDS=ioremap(IICDS,0x00000004);
S3C2402_IICADD=ioremap(IICADD,0x00000004);
S3C2402_IICLC=ioremap(IICLC,0X00000004);
S3C2440_CLKCON=ioremap(CLKCON,0x00000004);
writel(0xf3eb70,S3C2440_CLKCON);
cmmd=((S3C2410_IICCON_ACKEN)|(S3C2410_IICCON_TXDIV_16)|(S3C2410_IICCON_IRQEN)|(0x0f));
writel(cmmd,S3C2402_IICCON);
writel(0x10,S3C2402_IICADD);
writel(0x10,S3C2402_IICSTAT);
writel((S3C2410_IICLC_FILTER_ON|S3C2410_IICLC_SDA_DELAY5),S3C2402_IICLC);
return 0;
}
static int write_2402(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");