i2c驱动设备的应用
- I2C总线原理
I2C即Inter IC,由Philips公司开发,是当今电子设计中应用非常广泛的串行总线之一,主要用于电压、温度监控,EEPROM(电可擦可编程只读存储器)数据的读写,光模块的管理等。I2C总线只有两根线,SCL和SDA,SCL即Serial Clock,串行时钟,SDA即Serial Data,串行数据。
- I2C总线的速率
标准模式下:100Kbps
快速模式下:400Kbps
高速模式下:3.4Mbps
- I2C总线结构如下图所示:
1.阅读理解源代码
查看源代码
进行修改,重构,远程调试等
$ vim 24cXX.c
$ vim eeprog.c
- 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 *p_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* p_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 *p_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 *p_device_struct, __u16 mem_addr, __u8 data);
#endif
- 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 i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i - 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 i;
if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
I2C_SMBUS_I2C_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_i2c_block_data(int file, __u8 command,
__u8 length, __u8* values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i - 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 i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i - 1];
data.block[0] = length;
if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BLOCK_PROC_CALL, &data))
return -1;
else {
for (i = 1; i <= data.block[0]; i++)
values[i - 1] = data.block[i];
return data.block[0];
}
}
static int i2c_write_1b(struct eeprom* p_device_struct, __u8 buf)
{
int byte;
// we must simulate a plain I2C byte write with SMBus functions
byte = i2c_smbus_write_byte(p_device_struct->fd, buf);
if (byte < 0)
fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));
usleep(10);
return byte;
}
static int i2c_write_2b(struct eeprom* p_device_struct, __u8 buf[2])
{
int byte;
// we must simulate a plain I2C byte write with SMBus functions
byte = i2c_smbus_write_byte_data(p_device_struct->fd, buf[0], buf[1]);
if (byte < 0)
fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
usleep(10);
return byte;
}
static int i2c_write_3b(struct eeprom* p_device_struct, __u8 buf[3])
{
int byte;
// we must simulate a plain I2C byte write with SMBus functions
// the __u16 data field will be byte swapped by the SMBus protocol
byte = i2c_smbus_write_word_data(p_device_struct->fd, buf[0], buf[2] << 8 | buf[1]);
if (byte < 0)
fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
usleep(10);
return byte;
}
#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* p_device_struct)
{
int funcs, fd, byte;
p_device_struct->fd = p_device_struct->addr = 0;
p_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 ((byte = 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 ((byte = ioctl(fd, I2C_SLAVE, addr)) < 0)
{
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
return -1;
}
p_device_struct->fd = fd;
p_device_struct->addr = addr;
p_device_struct->dev = dev_fqn;
p_device_struct->type = type;
return 0;
}
int eeprom_close(struct eeprom* p_device_struct)
{
close(p_device_struct->fd);
p_device_struct->fd = -1;
p_device_struct->dev = 0;
p_device_struct->type = EEPROM_TYPE_UNKNOWN;
return 0;
}
#if 0
int eeprom_24c32_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
{
__u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(p_device_struct, buf);
}
int eeprom_24c32_read_current_byte(struct eeprom* p_device_struct)
{
ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
return i2c_smbus_read_byte(p_device_struct->fd);
}
int eeprom_24c32_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
{
int byte;
ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
byte = i2c_write_2b(p_device_struct, buf);
if (byte < 0)
return byte;
byte = i2c_smbus_read_byte(p_device_struct->fd);
return byte;
}
#endif
int eeprom_read_current_byte(struct eeprom* p_device_struct)
{
ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
return i2c_smbus_read_byte(p_device_struct->fd);
}
int eeprom_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
{
int byte;
ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR)
{
__u8 buf = mem_addr & 0x0ff;
byte = i2c_write_1b(p_device_struct, buf);
}
else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
byte = i2c_write_2b(p_device_struct, buf);
}
else {
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
}
if (byte < 0)
return byte;
byte = i2c_smbus_read_byte(p_device_struct->fd);
return byte;
}
int eeprom_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
{
if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR) {
__u8 buf[2] = { mem_addr & 0x00ff, data };
return i2c_write_2b(p_device_struct, buf);
}
else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[3] =
{ (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(p_device_struct, buf);
}
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
}
- 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(flag) do { do_usage_if( flag , __LINE__); } while(0);
void do_usage_if(int error_code, int line) {
const static char* eeprog_usage =
"I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"
"FriendlyARM Computer Tech. 2009\n";
if (!error_code)
return;
fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);
exit(1);
}
#define die_if(flag, msg) do { do_die_if( flag , msg, __LINE__); } while(0);
void do_die_if(int error_code, char* msg, int line) {
if (!error_code)
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* e, int addr, int size) {
int ch, i;
for (i = 0; i < size; ++i, ++addr) {
die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error");
if ((i % 16) == 0)
printf("\n %.4x| ", addr);
else if ((i % 8) == 0)
printf(" ");
printf("%.2x ", ch);
fflush(stdout);
}
fprintf(stderr, "\n\n");
return 0;
}
static int write_to_eeprom(struct eeprom* e, int addr) {
int i;
for (i = 0, addr = 0; i < 256; i++, addr++) {
if ((i % 16) == 0)
printf("\n %.4x| ", addr);
else if ((i % 8) == 0)
printf(" ");
printf("%.2x ", i);
fflush(stdout);
die_if(eeprom_write_byte(e, addr, i), "write error");
}
fprintf(stderr, "\n\n");
return 0;
}
int main(int argc, char** argv) {
struct eeprom e;
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 201930310071 ...\n", strlen("APP for 201930310071 ...\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, &e) < 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(&e, 0, 256);
break;
case 'w':
fprintf(stderr, " Writing 0x00-0xff into 24C08 \n");
write_to_eeprom(&e, 0);
break;
default:
usage_if(1);
exit(1);
}
eeprom_close(&e);
return 0;
}
2.编译应用程序
(1)使用Makefile文件直接编译
- Makefile文件
vim Makefile
#C=gcc
C=${CROSS_COMPILE}gcc
CFLAGS= -Wall -O2
OUTS=i2c
O_OUTS=eeprog.o 24cXX.o
i2c: ${O_OUTS}
$(C) $(CFLAGS) -o $@ $^
%.o: %.c
$(C) -c -o $@ $^
clean:
@rm -vf *.o *~ ${OUTS}
- 编译
make
arm-linux-gnueabihf-gcc -c -o eeprog.o eeprog.c
arm-linux-gnueabihf-gcc -c -o 24cXX.o 24cXX.c
arm-linux-gnueabihf-gcc -Wall -O2 -o i2c eeprog.o 24cXX.o
(2)使用NFS mount开发板主机
[root@ubuntu-qemu root]#
[/mnt/yaffs] mount -t nfs -o nolock 192.168.253.128:/nfs /mnt/nfs
[/mnt/yaffs]cd /mnt/nfs/i2c
[/mnt/nfs/i2c /]./i2c –w
Open /dev/i2c/0 with 8bit mode
Writing 0x00-0xff into ...
0000| 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
0010| 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
0020| 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
0030| 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
0040| 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
0050| 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
0060| 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
0070| 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
0080| 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
0090| 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
00a0| a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
00b0| b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
00c0| c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
00d0| d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
00e0| e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
00f0| f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
[/mnt/nfs/i2c /]./i2c –r
Open /dev/i2c/0 with 8bit mode
Reading 256 bytes from 0x00
0000| 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
0010| 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
0020| 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
0030| 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
0040| 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
0050| 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
0060| 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
0070| 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
0080| 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
0090| 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
00a0| a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
00b0| b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
00c0| c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
00d0| d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
00e0| e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
00f0| f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
3.思考题
(1)I2C总线的优点是什么?
1、I2C总线只需要一根数据线和一根时钟线两根线,总线接口已
经集成在芯片内部,优化主板空间和成本。
2、无论总线上有多少设备,都只使用两条线,保持低引脚/信号
数。
3、真正的支持多主机设备,但是同一时刻只允许一台主机。
4、I2C总线具有低功耗、抗干扰强的优点,传输距离长的特点。
5、连接到相同总线的IC 数量只受到总线的最大电容400pF 限
制。
6、串行的8 位双向数据传输位速率在标准模式下可达
100kbit/s,快速模式下可达400kbit/s,高速模式下可达3.4Mbit/s。
(2)I2C总线的启动信号和结束信号有什么特点。
启动信号:SCL为高电平时,SDA由高电平向低电平跳变,开始传送数据。
结束信号:SCL为高电平时,SDA由低电平向高电平跳变,结束传送数据。