为了增加大家 DIY 的乐趣,XiaomaGee今天为大家只做了一篇使用iTool2内置的USB转I2C来读写EEPROM的方法和代码。
iTool2简介
iTool2为银杏公司面向电子类研发工程师推出的一款多功能调试工具箱,其具有使用方便、功能强大等诸多优点。本文利用iTool2内置的USB转 I2C功能,基于MinGW32平台完成对24XX系列的EEPROM的读写。
MinGW平台
平台简介
MinGW是Minimalist GNU for Windows的缩写。它是一个可自由使用和自由发布的Windows特定头文件和使用GNU工具集导入库的集合,允许你在GNU/Linux和Windows平台生成本地的Windows程序而不需要第三方C运行时(C Runtime)库。(摘自于百度百科)。
下载与安装
MinGW可以通过以太网自由获取,其官方网址如下:
可以通过官网的Download 标签下载MinGW最新的发行版的安装工具。下面地址不保证长期有效:
http://nchc.dl.sourceforge.net/project/mingw/Installer/mingw-get-setup.exe
下载后运行可执行文件,它便会自行下载并安装MinGW32 工具集,默认文件夹为C盘根目录下。
添加系统路径
由于MinGW工具为命令行运行,并且没有IDE,为了方便使用,一般需要把工具集加入到系统路径里面。具体方法为在“系统属性”里的“环境变量”功能里的 Path 变量下加入如下路径。(根据安装目录决定)
c:MinGWin;
源文件组成
代码基于MinGW开发平台的,包含以下源文件:
1、 ico.rc 可执行程序图标资源描述
2、 itool2.ico 可执行程序图标文件
3、 main.c 主程序
4、 usb.c usb转i2c功能底层代码
5、 usb.h usb转 i2c 功能头文件
6、 makefile makefile文件
构建可执行文件
若MinGW平台安装成功且路径配置好,通过命令提示符进入源文件文件夹后,可执行下面命令完成程序编译。
出现上图消息即说明工程编译构建成功。同时也会在本文件夹内产生一个名字为iTool2_eeprom.exe的可执行文件,如下图所示。
硬件连接
由于没有定制化的硬件测试平台,所以本技术笔记硬件需要自行搭建,且只适合有一定动手能力的朋友,硬件连接如下图所示。
图中8脚芯片实为24LC04B,与iTool2连接共有四根线,分别为I2C的SDA / SCL信号线及3.3V / GND 供电线。
软件流程图
为了更好的对EEPROM 进行读写测试,本软件按照如下流程图进行测试。
首先产生100个字节随机数(以时间为种子);然后把100个字节按顺序写入EEPROM中,写入完成后再把100个字节读出来并与写入的数值进行匹配,然后显示匹配结果。
执行结果
相关资源及连接
1、iTool2简易手册:
http://files.cnblogs.com/files/xiaomagee/iTool2%E6%89%8B%E5%86%8CV1.0.pdf
2、iTool2 购买地址:
https://item.taobao.com/item.htm?id=19090288084
3、代码包下载:
http://pan.baidu.com/s/1mgixgDU
4、本文PDF 下载:
http://pan.baidu.com/s/1kTvqxBD
附录:核心代码
usb.c:
1 /* 2 * USB.C 3 * 4 * CH341H Driver. 5 * Designed By XiaomaGee 2015.11.05 6 * 7 * 8 */ 9 10 //--------------include files----------------// 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include "usb.h" 14 15 //--------------function prototype----------------// 16 17 FP1_FT ch341_open,ch341_get_descr,ch341_close,ch341_set_stream,ch341_write_eeprom,ch341_read_eeprom; 18 FP2_FT ch341_get_driver_ver; 19 FP3_FT ch341_get_device_name; 20 21 static int initialize_ch341(void); 22 static int open(unsigned long int); 23 static int close(unsigned long int); 24 static int set_stream(unsigned long int /* device_id */,unsigned long int /* mode */); 25 static int write_eeprom(unsigned long int /* device_id */,unsigned long int /* eeprom type */,unsigned long int /* address */,unsigned long int /* length */, unsigned char * /* buffer*/); 26 27 static int read_eeprom(unsigned long int /* device_id */,unsigned long int /* eeprom type */,unsigned long int /* address */,unsigned long int /* length */, unsigned char * /* buffer*/); 28 29 static unsigned long int get_driver_ver(unsigned long int); 30 static void * get_device_name(unsigned long int); 31 32 //--------------varitable-----------------// 33 HMODULE ch341_handle; 34 35 USB_T usb={ 36 .initialize=initialize_ch341, 37 .open = open, 38 .close = close, 39 .set_stream = set_stream, 40 .get_driver_ver = get_driver_ver, 41 .get_device_name = get_device_name, 42 .write_eeprom = write_eeprom, 43 .read_eeprom = read_eeprom 44 }; 45 46 //---------------function------------------// 47 /* 48 * write_eeprom 49 * 50 * 51 * 52 */ 53 static int write_eeprom(unsigned long int device_id ,unsigned long int type,unsigned long int address,unsigned long int length, unsigned char * buffer) 54 { 55 return ch341_write_eeprom(device_id,type,address,length,buffer); 56 } 57 /* 58 * read_eeprom 59 * 60 * 61 * 62 */ 63 static int read_eeprom(unsigned long int device_id ,unsigned long int type,unsigned long int address,unsigned long int length, unsigned char * buffer) 64 { 65 return ch341_read_eeprom(device_id,type,address,length,buffer); 66 } 67 68 /* 69 * open 70 * 71 * 72 * 73 */ 74 static int open(unsigned long int id) 75 { 76 return ch341_open(id); 77 } 78 79 /* 80 * close 81 * 82 * 83 * 84 */ 85 static int close(unsigned long int id) 86 { 87 int i; 88 89 i=ch341_close(id); 90 FreeLibrary(ch341_handle); 91 92 return i; 93 94 } 95 /* 96 * 97 * set_stream 98 * 99 * 100 * 101 */ 102 static int set_stream(unsigned long int id,unsigned long mode) 103 { 104 return ch341_set_stream(id,mode); 105 } 106 /* 107 * 108 * get_driver_ver 109 * 110 * 111 * 112 */ 113 114 static unsigned long int get_driver_ver(unsigned long int id) 115 { 116 return ch341_get_driver_ver(id); 117 } 118 /* 119 * get_device_name 120 * 121 * 122 * 123 */ 124 125 static void * get_device_name(unsigned long int id) 126 { 127 return ch341_get_device_name(id); 128 } 129 130 131 /* 132 * initialize 133 * 134 * 135 * 136 */ 137 138 139 static int initialize_ch341(void) 140 { 141 142 ch341_handle=LoadLibrary("CH341DLL.DLL"); 143 144 ch341_open=GetProcAddress(ch341_handle,"CH341OpenDevice"); 145 ch341_close=GetProcAddress(ch341_handle,"CH341CloseDevice"); 146 ch341_get_descr=GetProcAddress(ch341_handle,"CH341GetDeviceDescr"); 147 ch341_get_device_name=(FP3_FT)GetProcAddress(ch341_handle,"CH341GetDeviceName"); 148 ch341_get_driver_ver=(FP2_FT)GetProcAddress(ch341_handle,"CH341GetDrvVersion"); 149 150 ch341_set_stream=GetProcAddress(ch341_handle,"CH341SetStream"); 151 152 ch341_read_eeprom = GetProcAddress(ch341_handle,"CH341ReadEEPROM"); 153 ch341_write_eeprom = GetProcAddress(ch341_handle,"CH341WriteEEPROM"); 154 155 return 0; 156 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <windows.h> 4 #include <math.h> 5 #include <time.h> 6 #include "usb.h" 7 8 9 #define REV "0.1" 10 11 12 13 /* 14 * 15 * initialize 16 * 17 * 18 */ 19 20 21 int initialize(void) 22 { 23 int len=1000,ver=0; 24 char *p; 25 int i; 26 unsigned long int t; 27 unsigned char buf[2]; 28 29 usb.initialize(); 30 31 system("cls"); 32 printf("╔════════════════╗ "); 33 printf("║ ║ "); 34 printf("║ iTool2 I2C 功能测试程序 V%s ║ ",REV); 35 printf("║ ║ "); 36 printf("╠════════════════╣ "); 37 printf("║ Gingko Technology Co.,Ltd. ║ "); 38 printf("╚════════════════╝ "); 39 40 if(usb.open(0) == -1){ 41 printf("# 错误! 未连接 iTool2! "); 42 return -1; 43 }else { 44 printf("# 连接 iTool2 ............ 成功! "); 45 } 46 47 usb.set_stream(0,0x80); 48 49 return 0; 50 } 51 52 /* 53 * 54 * I2C test 55 * 56 */ 57 int i2c_test(void) 58 { 59 unsigned char write_buffer[100]; 60 unsigned char read_buffer[100]; 61 int i,j; 62 63 printf("# 产生 100 字节随机数...... "); 64 srand(time(NULL)); 65 for(i = 0 ; i < 100; i ++)write_buffer[i] = rand()%255; 66 67 //打印随机数到屏幕 68 for(i = 0; i < 10; i ++){ 69 for(j = 0; j < 10; j ++)printf(" %02X",write_buffer[i*10 + j]); 70 printf(" "); 71 } 72 printf(" # 把随机数写入eeprom...... "); 73 usb.write_eeprom(0,2,0,100,write_buffer); 74 75 printf("# 从eeprom中读取数据...... "); 76 memset(read_buffer,0,100); 77 78 usb.read_eeprom(0,2,0,100,read_buffer); 79 80 //打印随机数到屏幕 81 for(i = 0; i < 10; i ++){ 82 for(j = 0; j < 10; j ++)printf(" %02X",read_buffer[i*10 + j]); 83 printf(" "); 84 } 85 86 printf(" # 测试 I2C 接口 .......... "); 87 88 if(memcmp(write_buffer,read_buffer,100) ==0)printf("成功!"); 89 else printf("失败!"); 90 91 92 return 0; 93 } 94 95 96 /* 97 * 98 * 99 * main 100 * 101 * 102 */ 103 int main(int argc, char *argv[]) 104 { 105 if(initialize() == -1)goto end; 106 107 i2c_test(); 108 109 usb.close(0); 110 111 end: printf(" "); 112 system("Pause"); 113 114 115 return 0; 116 }
makefile:
itool2_eeprom:main.o usb.o ico.o gcc -o itool2_eeprom main.o usb.o ico.o main.o:main.c gcc -c main.c usb.o:usb.c usb.h gcc -c usb.c ico.o:itool2.ico ico.rc windres -i ico.rc -O coff -o ico.o clean: del *.o itool2_eeprom.exe *.*~