Linux学习之i-mx287学习:ARM9与GD32F407的I2C通信(ioctl,write,read)

        最近想试一下ARM9下的I2C设备与外挂MCU通信,外挂mcu用的是GD32F407,在把GD32F4的I2C0初始化成从中断接收模式后,ARM9的i2c读写遇到了一点问题,mcu始终没有进接收中断,在搜索问题解决方法时了解到linux下的I2C设备操作的一些经验,在这里记录一下。

一、linux下I2C设备的设置

i2c通信无非是模式设置、设备地址设置、速率设置,linux下的i2c设备设置操作给出了标准,需要使用ioctl这个函数进行设置,下面就I-MAX287A的ARM板中给的I2C读写例程来进行分析。

/*i2c_test.c*/

#include     <stdio.h>
#include     <stdlib.h> 
#include     <unistd.h>  
#include     <sys/types.h>
#include     <sys/stat.h>
#include     <fcntl.h> 
#include     <termios.h>
#include     <errno.h>  

#include "iic.h"

#define I2C_ADDR  0x30 //从设备地址 如果单片机的i2c从地址是0x30
#define DATA_LEN  15

#define DEV_NANE "/dev/i2c-1"

int main(void)
{
	unsigned int uiRet;
	int i;

	unsigned char tx_buf[DATA_LEN];
	unsigned char rx_buf[DATA_LEN];
	unsigned char addr[2] ;

	addr[0] = 0x00;
	    
	GiFd = open(DEV_NANE, O_RDWR|O_NONBLOCK);   
	if(GiFd < 0)
    	perror("open i2c-1\n");
	printf("i2c1 open ok\r\n");
	
	//设置i2c的地址位宽,0:7bit addr;1:10bit addr.
	uiRet = ioctl(GiFd, I2C_TENBIT, 0);
	if (uiRet < 0) {
	printf("setenv addr bit faile ret: %x \n", uiRet);
	return -1;
 	}	
	
	//设置通信从设备地址
	uiRet = ioctl(GiFd, I2C_SLAVE, I2C_ADDR >> 1);
	if (uiRet < 0) {
	printf("setenv address faile ret: %x \n", uiRet);
	return -1;
 	}
	 
	 
	for (i = 0; i < DATA_LEN; i++) //写将要发送的数据到 发送buf
    	tx_buf[i] = i+1; 
	
	for (i = 0; i < DATA_LEN; i++){ //写要发送的数据到 24c02
		addr[0] = 0x30; //当前地址
		write(GiFd, addr, 1);  //写地址
		write(GiFd, &tx_buf[i], 1); //写数据
	}
	
	// addr[0] = 0x00; //当前地址
	// write(GiFd, addr, 1);  //写地址
	// read(GiFd, rx_buf, DATA_LEN); //读数据

	// printf("read from eeprom:");
	// for(i = 0; i < DATA_LEN; i++) {
    //  	printf(" %x", rx_buf[i]);
	// }

	printf("\n");

	return 0;
}
/*iic.h*/

#ifndef __I2C_H_
#define __I2C_H_

#define I2C_SLAVE	0x0703
#define I2C_TENBIT	0x0704

int GiFd;

int GiIndex;

int  iicOpen();

int waitIRQ();
void waitset();

void setdriverTo500smod();
void setdriverToCOMMmod();

#endif

(1) ioctl函数的使用:
原型:struct ioctl(struct file *file,unsigned int cmd,unsigned long arg);
 cmd有I2C_SLAVE,I2C_SLAVE_FORCE,I2C_TENBIT,I2C_SET_SPEED几个选项;
    I2C_SLAVE:对应的arg取值为I2C从机地址,用来设定I2C从机地址;

    I2C_SLAVE_FORCE:对应的arg取值为I2C从机地址,用来修改I2C从机地址;

    I2C_TENBIT:对应的arg取值为0:从机地址为7 bit;对应的arg取值为1:从机地址为10bit。用来指定I2C从机地址的位数;  

    I2C_SET_SPEED:对应的arg取值为I2C总线控制器分频值。用来设置I2C总线控制器时钟频率;


    常用设置设置I2c从机地址为0xA0,如果选用at24c08设备,那么从机是7 bit地址,所以要右移1位,指定从机地址为7 bit,

ioctl(fd,I2C_TENBIT,0)。
ioctl(fd,I2C_SLAVE,0xA0>>1);

上面贴的代码中,没有I2C_SLAVE_FORCE和I2C_SET_SPEED设置项,这个应该是示例中没有给出的,I2C_SLAVE 的值为何是0x0703,暂时没有找到依据,猜测是驱动里面给这个值过去后,会将I2C设备的某个寄存器进行相应的设置,I2C_TENBIT也同理。

二、linux下i2c设备的读写操作

read()与write()函数的使用
假设子地址为48,向有子地址的器件写进7个字节:

unsigned char buf[8]={48,'a','b','c','d','e','f','g');/*第1个字节48为子地址*/

write(fd,buf,9);/*写进7个字节,第1个字节为子地址*/

从有子地址的I2C器件读取7个字节:
unsigned char suba=0,recbuf[20];
write(fd,buf,1);/*发送子地址0*/
read(fd,recbuf,7);/*从子地址48开始读取7个字节*/

三、linux下i2c的结束标志位
linux下I2C做主设备读写数据时,结束标志都由主设备给出,如果发现MCU设备没有退出I2C中断,需检查MCU的I2C中断配置是否有问题。

四、通信效果


 

猜你喜欢

转载自blog.csdn.net/qq_28643619/article/details/102184839