I2C驱动App

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(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);
}

/*从eeprom存储器的地址为int_address_for_service_eeprom的存储单元中读取字节数据*/
static int read_from_eeprom(struct eeprom *p_eeprom_device_struct, int int_address_for_service_eeprom, int size) {
	int char_read_byte_from_eeprom, index_for_loops;
	for(index_for_loops = 0; index_for_loops < size; ++index_for_loops, ++int_address_for_service_eeprom) {
		die_if((char_read_byte_from_eeprom = eeprom_read_byte(p_eeprom_device_struct, int_address_for_service_eeprom)) < 0, "read error"); 
		if( (index_for_loops % 16) == 0 )
			printf("\n %.4x|  ", int_address_for_service_eeprom);
		else if( (index_for_loops % 8) == 0 ) 
			printf("  ");
		printf("%.2x ", char_read_byte_from_eeprom);
		fflush(stdout);
	}
	fprintf(stderr, "\n\n");
	return 0;
}
/*把字节数据写入eeprom地址为int_address_for_service_eeprom的存储单元*/
static int write_to_eeprom(struct eeprom *p_eeprom_device_struct, int int_address_for_service_eeprom) {
	int index_for_loops;
	for(index_for_loops=0, int_address_for_service_eeprom=0; index_for_loops<256; index_for_loops++, int_address_for_service_eeprom++) {
		if( (index_for_loops % 16) == 0 ) 
			printf("\n %.4x|  ", int_address_for_service_eeprom);
		else if( (index_for_loops % 8) == 0 ) 
			printf("  ");
		printf("%.2x ", index_for_loops);
		fflush(stdout);
		die_if(eeprom_write_byte(p_eeprom_device_struct, int_address_for_service_eeprom, index_for_loops), "write error"); //写入
	}
	fprintf(stderr, "\n\n");
	return 0;
}

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

	option_read_or_write = 0;

	usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0');
	option_read_or_write = argv[1][1];
	
	//TODO: 将数字改为自己的学号。
	write(STDOUT_FILENO, "APP for 201930310005 ...\n", strlen("APP for 201930310005 ...\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(option_read_or_write) {
	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;
}

2.在ubuntu下编译

 执行命令后图片,#此i2c不可以在开发板内执行

 

 

3. 交叉编译(在ubuntu下编译)

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 -02 -o i2c eeprog.o 24cXX.o 

在此用make方式编译

 

 

执行I2C文件

i2c-w

 

 i2c-r

 执行成功

 

4.思考题

1)I2C总线的优点是什么?

(1)只使用两根电线

(2)支持多个主服务器和多个从服务器ACK / NACK位确认每个帧都已成功传输

(3)硬件简单

(4)协议使用广泛

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

动条件:在SCL线路从高电平切换到低电平之前,SDA线路从高电平切换到低电平。

停止条件:SCL线路从低电平切换到高电平后,SDA线路从低电平切换到高电平。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值