hpaluch / ch341-i2c-24c01c

How to access I2C EEPROM 24C01C from LC CH341A USB Adapter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to access I2C EEPROM 24C01C from LC CH341 USB board

Here is example how to access I2C EEPROM 24C01C: from CH341A USB to UART/IIC/SPI/TTL/ISP adapter EPP/MEM Parallel converter using I2C mode.

This is nice introductory example - it just uses stock functions CH341WriteEEPROM() and CH341ReadEEPROM() already provided by CH341DLL.DLL library from CH341 driver. So the code is fairly trivial as you can see from ch341_i2c_24c01c.cpp listing.

WARNING!

This program will overwrite all contents of your 24C01C EEPROM!

The example program does following:

  1. Write sample string to EEPROM
  2. Read back all EEPROM contents and compare sample string
  3. Write erase pattern to whole EEPROM
  4. Read back whole EEPROM and verify contents

Circuit schematic is below:

Schematic of I2C w 24C01C

Hardware:

!!!IMPORTANT WARNING!!!

Do NOT use original Atmel 24C01! It uses different protocol (the 1st byte which should be use for slave type/address is (mis)used for EEPROM data address. The official CH341A library routines will work with 24C01A or later revisions only!!!

Setup

The CH341A adapter must be setup following way:

  • jumper set to I2C/SPI mode
  • voltage set to 5V TTL logic - the only voltage supported by 24C01C

LC CH341A USB adapter setup

Software setup:

  • Download and install CH341PAR.ZIP - USB driver for CH341 chip in Parallel mode (EPP, MEM). This driver is valid also for I2C mode and SPI mode (yes - even when it is marked parallel).
  • install VisualSutdio 2010

Create environment variable CH341_SDK that should point to extracted CH341PAR.ZIP header and library files. For example if you have extracted file:

C:\CH341_DRIVER\LIB\C\CH341DLL.H 

Then your CH341_SDK should be set to C:\CH341_DRIVER\LIB\C.

Open and rebuild solution VS2010/ch341_i2c_24c01c/ch341_i2c_24c01c.sln in VisualStudio 2010. There should be no errors.

Connect your CH341A USB module to target circuit. Following pins are used:

PIN Name Direction Description
GND N/A Common ground
VCC N/A 5V supply
SDA Open Drain I2C Data
SCL Open Drain I2C Clock

Connect your CH341 USB module to your PC. There should be permanently lighting red LED on USB module.

Now you can run sample program - you should see following output:

ch341_i2c_24c01c.exe


CH341 library version: 33
Opening device# 0
CH341 driver  version: 34
Storing string 'Hello!' (including '\0') at EEPROM address 0x5...
Fetching whole memory content...
Dumping EEPROM contents...
Dump of buffer at 0x00417140,  bytes 128
0x0000 ff ff ff ff ff 48 65 6c 6c 6f 21 00 ff ff ff ff .....Hello!.....
0x0010 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0020 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0030 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0040 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0050 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0060 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0070 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................

Cleaning EEPROM contents with 0xff...
Fetching whole memory content...
Dumping EEPROM contents...
Dump of buffer at 0x00417140,  bytes 128
0x0000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0010 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0020 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0030 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0040 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0050 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0060 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
0x0070 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................

TIP:

If you are curious how library functions work, you can found them in Linux driver (there is no guarantee that they 100% match Windows version though):

  1. Download and extract Linux driver: CH341PAR_LINUX.ZIP
  2. Look at CH341PAR_LINUX\lib\ch34x_lib.c

For example in CH34xWriteEEPROM() function is very clever trick how to found number of bytes till the end of page boundary:

mLen = iEepromID >= ID_24C04 ? 16 - ( iAddr & 15 ) : 8 - (iAddr & 7 );
if( mLen > iLength )
  			mLen = iLength;

In case of our EEPROM (ID_24C01) the second expression will apply:

mLen = 8 - (iAddr & 7 );
...

NOTE

Alternate driver source is from board vendor page CH341A USB to UART/IIC/SPI/TTL/ISP adapter EPP/MEM Parallel converter:

  1. Get and extract LC-TTL-CH341A_EN.zip (uhhh, the link seems to lost after recent site revamp)
  2. Look into LC-TTL-CH341A\Drivers\Other platform drivers\CH341PAR_LINUX.ZIP

Logic Analyzer output

Here are Logic Analyzer outputs using Sigrok PulseView :

Page Write of Hello!\0 string to EEPROM:

PulseView I2C EEPROM 24C01 Page Write

There can be easily seen few challenges:

  • All 24C01A/B/C supports at least 8-byte page write (some of them, including 24C01C supports even 16-byte page write, but it is not supported directly with library)
  • page write may not cross page boundary. Because starting address is 0x5 and page boundary is 0x8 there are only 3 bytes to be written in one page write...

And here is full sequential read (reading whole EEPROM):

PulseView I2C EEPROM 24C01 Sequential Read

Please note that sequential read has no page boundary limitation. Whole EEPROM can be read using one command...

About

How to access I2C EEPROM 24C01C from LC CH341A USB Adapter


Languages

Language:C++ 86.5%Language:C 13.5%