⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cat1025.c

📁 这是pxa270的I2c驱动程序
💻 C
字号:
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <fcntl.h>            /* 文件操作 */#include <sys/ioctl.h>        /* ioctl()函数 *///#include "/pxa270/kernel-test/linux-2.6.18/include/linux/i2c.h" /* I2C总线控制器驱动命令 */#include "i2c.h"#define	 I2C_ADDR 	0xA0     /* CAT1025 从机地址 */int main(){    int fd, ret, i, num;    unsigned int suba;    unsigned char buf[257] = {0xFF,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,			           0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x00};    //char s;    fd = open("/dev/i2c-0", O_RDWR);              /* 打开设备 */    if(fd == -1)    {        printf("\nFailed to open i2c-0!\n");        exit(-1);    }     printf("********************************\n");    printf("* ==== CAT1025 Information === *\n");    printf("* SLAVE ADDRESS : 0xA0         *\n");    printf("* TOTAL BYTES   : 512 Bytes    *\n");    printf("* PAGE SIZE     : 16  Bytes    *\n");    printf("********************************\n");    ret = ioctl(fd, I2C_TENBIT, 0);               /* 指定从机地址为7bit */    if (ret != 0)    {    	printf("\nCan't set I2C address bit number.\n");    	close(fd);    	exit(-1);    }     ret = ioctl(fd, I2C_SLAVE, I2C_ADDR >> 1); /* 设置从机地址,7位地址,须右移1位*/    if (ret != 0)    {    	printf("\nCan't set I2C slave device address.\n");    	close(fd);    	exit(-1);    }            printf("\n ==== Start Write CAT1025 ====\n");     printf("Please Input the SUB-ADDRESS you will write(Hex):");    scanf("%x", &suba);    printf("SUB-ADDRESS suba = 0x%02x\n", suba);    buf[0] = suba;    printf("Please Input the Bytes Numbers you want to write(Hex):");    scanf("%x", &num);    printf("Bytes Numbers num  = 0x%02x\n", num);    printf("Writing 0x%02x Bytes to CAT1025 from 0x%02x\n", num, suba);    for (i = 1; i < num+1; i++)       printf("0x%02x ", buf[i]);      /* Start write CAT1025 */    ret = write(fd, buf, num+1);  /* 写num个字节到CAT1025,第1字节为子地址 */    if (ret != num+1)    {    	printf("Write CAT1025 failed.\n");    	close(fd);    	exit(-1);    }        usleep(10000);                      /* 等待10ms写入完成 */    //    printf("\n\n ==== Read All Data in CAT1025 ====\n");	//    suba = 0;    ret = write(fd, &suba, 1);		  /* 发送从机子地址 */    if (ret != 1)    {    	printf("\nSend slave address failed.\n");    	close(fd);    	exit(-1);    }   	        printf("Read 0x%02x Bytes from 0x%02x in CAT1025.\n", num, suba);    ret = read(fd, &buf, num);  	      if (ret == num)    {       for (i = 0; i < num; i++)           printf("0x%02x ", buf[i]);    }    else        {    	    printf("\nRead CAT1025 failed.\n");    	    close(fd);    	    exit(-1);   		   	}    printf("\nPlease Verify the data.\n");    close(fd);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -