i2c驱动app

查看i2c源代码

24cXX.c

/***************************************************************************
    copyright            : (C) by 2003-2004 Stefano Barbato
    email                : stefano@codesink.org

    $Id: 24cXX.h,v 1.6 2004/02/29 11:05:28 tat Exp $

 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include "24cXX.h"


static inline __s32 i2c_smbus_access(int file, 
				char read_write, 
				__u8 command,                                  
				int size, 
				union i2c_smbus_data *data)
{
	struct i2c_smbus_ioctl_data args;

	args.read_write = read_write;
	args.command = command;
	args.size = size;
	args.data = data;
	return ioctl(file,I2C_SMBUS,&args);
}


static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
	return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
}
	
static inline __s32 i2c_smbus_read_byte(int file)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
		return -1;
	else
		return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
	                        I2C_SMBUS_BYTE,NULL);
}

static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_BYTE_DATA,&data))
		return -1;
	else
		return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, 
                                              __u8 value)
{
	union i2c_smbus_data data;
	data.byte = value;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_BYTE_DATA, &data);
}

static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_WORD_DATA,&data))
		return -1;
	else
		return 0x0FFFF & data.word;
}

static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, 
                                              __u16 value)
{
	union i2c_smbus_data data;
	data.word = value;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_WORD_DATA, &data);
}

static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
	union i2c_smbus_data data;
	data.word = value;
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                     I2C_SMBUS_PROC_CALL,&data))
		return -1;
	else
		return 0x0FFFF & data.word;
}


/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, 
                                              __u8 *values)
{
	union i2c_smbus_data data;
	int i;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_BLOCK_DATA,&data))
		return -1;
	else {
		for (i = 1; i <= data.block[0]; i++)
			values[i-1] = data.block[i];
		return data.block[0];
	}
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, 
                                               __u8 length, __u8 *values)
{
	union i2c_smbus_data data;
	int order;
	if (length > 32)
		length = 32;
	for (order = 1; order <= length; order++)
		data.block[order] = values[order-1];
	data.block[0] = length;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_BLOCK_DATA, &data);
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
                                                  __u8 *values)
{
	union i2c_smbus_data data;
	int order;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                      I2C_SMBUS_I2C_BLOCK_DATA,&data))
		return -1;
	else {
		for (order = 1; order <= data.block[0]; order++)
			values[order-1] = data.block[order];
		return data.block[0];
	}
}

static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
                                               __u8 length, __u8 *values)
{
	union i2c_smbus_data data;
	int order;
	if (length > 32)
		length = 32;
	for (order = 1; order <= length; order++)
		data.block[order] = values[order-1];
	data.block[0] = length;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_I2C_BLOCK_DATA, &data);
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
                                                 __u8 length, __u8 *values)
{
	union i2c_smbus_data data;
	int order;
	if (length > 32)
		length = 32;
	for (order = 1; order <= length; order++)
		data.block[order] = values[order-1];
	data.block[0] = length;
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                     I2C_SMBUS_BLOCK_PROC_CALL,&data))
		return -1;
	else {
		for (order = 1; order <= data.block[0]; order++)
			values[order-1] = data.block[order];
		return data.block[0];
	}
}

static int i2c_write_1b(struct eeprom *eeprom_device_struct, __u8 buf)
{
	int return_value;
	// we must simulate a plain I2C byte write with SMBus functions
	return_value = i2c_smbus_write_byte(eeprom_device_struct->fd, buf);
	if(return_value < 0)
		fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));
	usleep(10);
	return return_value;
}

static int i2c_write_2b(struct eeprom *eeprom_device_struct, __u8 buf[2])
{
	int return_value;
	// we must simulate a plain I2C byte write with SMBus functions
	return_value = i2c_smbus_write_byte_data(eeprom_device_struct->fd, buf[0], buf[1]);
	if(return_value < 0)
		fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
	usleep(10);
	return return_value;
}

static int i2c_write_3b(struct eeprom *eeprom_device_struct, __u8 buf[3])
{
	int return_value;
	// we must simulate a plain I2C byte write with SMBus functions
	// the __u16 data field will be byte swapped by the SMBus protocol
	return_value = i2c_smbus_write_word_data(eeprom_device_struct->fd, buf[0], buf[2] << 8 | buf[1]);
	if(return_value < 0)
		fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
	usleep(10);
	return return_value;
}


#define CHECK_I2C_FUNC( var, label ) \
	do { 	if(0 == (var & label)) { \
		fprintf(stderr, "\nError: " \
			#label " function is required. Program halted.\n\n"); \
		exit(1); } \
	} while(0);

int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* eeprom_device_struct)
{
	int funcs, fd, return_value;
	eeprom_device_struct->fd = eeprom_device_struct->addr = 0;
	eeprom_device_struct->dev = 0;
	
	fd = open(dev_fqn, O_RDWR);
	if(fd <= 0)
	{
		fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
		return -1;
	}

	// get funcs list
	if((return_value = ioctl(fd, I2C_FUNCS, &funcs) < 0))
	{
		fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
		return -1;
	}

	
	// check for req funcs
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );

	// set working device
	if( ( return_value = ioctl(fd, I2C_SLAVE, addr)) < 0)
	{
		fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
		return -1;
	}
	eeprom_device_struct->fd = fd;
	eeprom_device_struct->addr = addr;
	eeprom_device_struct->dev = dev_fqn;
	eeprom_device_struct->type = type;
	return 0;
}

int eeprom_close(struct eeprom *eeprom_device_struct)
{
	close(eeprom_device_struct->fd);
	eeprom_device_struct->fd = -1;
	eeprom_device_struct->dev = 0;
	eeprom_device_struct->type = EEPROM_TYPE_UNKNOWN;
	return 0;
}

#if 0
int eeprom_24c32_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
{
	__u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
	return i2c_write_3b(e, buf);
}


int eeprom_24c32_read_current_byte(struct eeprom* e)
{
	ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
	return i2c_smbus_read_byte(e->fd);
}

int eeprom_24c32_read_byte(struct eeprom* e, __u16 mem_addr)
{
	int r;
	ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
	__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
	r = i2c_write_2b(e, buf);
	if (r < 0)
		return r;
	r = i2c_smbus_read_byte(e->fd);
	return r;
}
#endif


int eeprom_read_current_byte(struct eeprom* eeprom_device_struct)
{
	ioctl(eeprom_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
	return i2c_smbus_read_byte(eeprom_device_struct->fd);
}

int eeprom_read_byte(struct eeprom* eeprom_device_struct, __u16 mem_addr)
{
	int return_value;
	ioctl(eeprom_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
	if(eeprom_device_struct->type == EEPROM_TYPE_8BIT_ADDR)
	{
		__u8 buf =  mem_addr & 0x0ff;
		return_value = i2c_write_1b(eeprom_device_struct, buf);
	} else if(eeprom_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
		__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
		return_value = i2c_write_2b(eeprom_device_struct, buf);
	} else {
		fprintf(stderr, "ERR: unknown eeprom type\n");
		return -1;
	}
	if (return_value < 0)
		return return_value;
	return_value = i2c_smbus_read_byte(eeprom_device_struct->fd);
	return return_value;
}

int eeprom_write_byte(struct eeprom *eeprom_device_struct, __u16 mem_addr, __u8 data)
{
	if(eeprom_device_struct->type == EEPROM_TYPE_8BIT_ADDR) {
		__u8 buf[2] = { mem_addr & 0x00ff, data };
		return i2c_write_2b(eeprom_device_struct, buf);
	} else if(eeprom_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
		__u8 buf[3] = 
			{ (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
		return i2c_write_3b(eeprom_device_struct, buf);
	} 
	fprintf(stderr, "ERR: unknown eeprom type\n");
	return -1;
}

24cXX.h

/***************************************************************************
    copyright            : (C) by 2003-2004 Stefano Barbato
    email                : stefano@codesink.org

    $Id: 24cXX.h,v 1.6 2004/02/29 11:05:28 tat Exp $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef _24CXX_H_
#define _24CXX_H_
#include <linux/i2c-dev.h>
#include <linux/i2c.h>

#define EEPROM_TYPE_UNKNOWN	0
#define EEPROM_TYPE_8BIT_ADDR	1
#define EEPROM_TYPE_16BIT_ADDR 	2

struct eeprom
{
	char *dev; 	// device file i.e. /dev/i2c-N
	int addr;	// i2c address
	int fd;		// file descriptor
	int type; 	// eeprom type
};

/*
 * opens the eeprom device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the eeprom_24c32 [e]
 */
int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*);
/*
 * closees the eeprom device [e] 
 */
int eeprom_close(struct eeprom *eeprom_device_struct);
/*
 * read and returns the eeprom byte at memory address [mem_addr] 
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 */
int eeprom_read_byte(struct eeprom* eeprom_device_struct, __u16 mem_addr);
/*
 * read the current byte
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 */
int eeprom_read_current_byte(struct eeprom *eeprom_device_struct);
/*
 * writes [data] at memory address [mem_addr] 
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 */
int eeprom_write_byte(struct eeprom *eeprom_device_struct, __u16 mem_addr, __u8 data);

#endif

eeprog.c

/***************************************************************************
    copyright            : (C) by 2009 Guangzhou FriendlyaRM, in China
    email                : capbily@163.com
    website		         : arm9.net
 ***************************************************************************/

#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "24cXX.h"

#define DEVICE_FILE_STRING "/dev/i2c-0"
#define DEVICE_ADDRESS 0x50

#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);
void do_usage_if(int b, int line) {
	const static char *eeprog_usage = 
		"I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"
		"FriendlyARM Computer Tech. 2009\n";
	if(!b)
		return;
	fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);
	exit(1);
}


#define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);
void do_die_if(int b, char* msg, int line) {
	if(!b)
		return;
	fprintf(stderr, "Error at line %d: %s\n", line, msg);
	fprintf(stderr, "	sysmsg: %s\n", strerror(errno));
	exit(1);
}


static int read_from_eeprom(struct eeprom *eeprom_device_struct, int addr, int size) {
	int ch, index;
	for(index = 0; index < size; ++index, ++addr) {
		die_if((ch = eeprom_read_byte(eeprom_device_struct, addr)) < 0, "read error");
		if( (index % 16) == 0 ) 
			printf("\n %.4x|  ", addr);
		else if( (index % 8) == 0 ) 
			printf("  ");
		printf("%.2x ", ch);
		fflush(stdout);
	}
	fprintf(stderr, "\n\n");
	return 0;
}

static int write_to_eeprom(struct eeprom *eeprom_device_struct, int addr) {
	int index;
	for(index=0, addr=0; index<256; index++, addr++) {
		if( (index % 16) == 0 ) 
			printf("\n %.4x|  ", addr);
		else if( (index % 8) == 0 ) 
			printf("  ");
		printf("%.2x ", index);
		fflush(stdout);
		die_if(eeprom_write_byte(eeprom_device_struct, addr, index), "write error");
	}
	fprintf(stderr, "\n\n");
	return 0;
}

int main(int argc, char** argv) {
	struct eeprom eeprom_device_struct;
	int op;

	op = 0;

	usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0');
	op = argv[1][1];
	
	//TODO: 将数字改为自己的学号。
	write(STDOUT_FILENO, "APP for 201930310035 ...\n", strlen("APP for 201930310035 ...\n"));
	fprintf(stderr, "Open %s with 8bit mode\n", DEVICE_FILE_STRING);
	die_if(eeprom_open(DEVICE_FILE_STRING, DEVICE_ADDRESS, EEPROM_TYPE_8BIT_ADDR, &eeprom_device_struct) < 0,
			"unable to open eeprom device file "
			"(check that the file exists and that it's readable)");
	switch(op) {
	case 'r':
		fprintf(stderr, "  Reading 256 bytes from 0x0\n");
		read_from_eeprom(&eeprom_device_struct, 0, 256);
		break;
	case 'w':
		fprintf(stderr, "  Writing 0x00-0xff into 24C08 \n");
		write_to_eeprom(&eeprom_device_struct, 0);
		break;
	default:
		usage_if(1);
		exit(1);
	}
	eeprom_close(&eeprom_device_struct);

	return 0;
}

编译并在Ubuntu运行

cd i2c-wdx-template
make

运行结果
在这里插入图片描述

提高项

使用 ${CROSS_COMPILE}gcc 编译各个程序

请添加图片描述

在开发板内 mount nfs,并运行各个程序

在主机创建要共享的文件夹,将 i2c 文件夹放入其中,以便能在开发板上访问 i2c 文件夹及运行里面的程序

在主机根目录建立 /nfs 并更改权限请添加图片描述请添加图片描述
添加目录到共享

sudo vim /etc/exports
/nfs *    (rw,async,wdelay,nohide,insecure,no_root_squash,no_subtree_check,sec=sys,rw,insecure,no_root_squash,no_all_squash)

请添加图片描述
启动nfs服务

/usr/sbin/exportfs -a
service nfs-kernel-server restart
service nfs-kernel-server status
登陆开发板
~/ubuntu-18.04_imx6ul_qemu_system/qemu-imx6ull-nogui.sh
mount nfs

将主机已经设定好的共享目录 /nfs 挂载(mount)到开发板的 /root/mnt 目录下

mount -t nfs -o nolock 10.0.2.2:/nfs /root/mnt

请添加图片描述

运行i2c程序
./i2c -w
./i2c -r

请添加图片描述
请添加图片描述

思考题

I2C总线的优点是什么?

I2C总线主要的优点是其简单性和有效性。由于接口直接在组件之上,因此I2C总线占用的空间非常小,减少了电路板的空间和芯片管脚的数量,降低了互联成本。总线的长度可高达25英尺,并且能够以10Kbps的传输速率支持40个组件。I2C总线的另一个优点是,它支持多主控, 其中任何能够进行发送和接收的设备都可以成为主总线。一个主控能够控制信号的传输和时钟频率。当然,在任何时间点上只能有一个主控。

I2C总线的启动信号和结束信号有什么特点

开始信号:SCL为高电平时,SDA由高电平向低电平跳变,开始传送数据。
结束信号:SCL为高电平时,SDA由低电平向高电平跳变,结束传送数据。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值