Hi, I'm trying to use the EZUSB I2C library to access an external I2C EEPROM. The library provides the following functions to access the I2C bus
EZUSB_ReadI2C() EZUSB_WriteI2C()
I've tried to use both functions to read from the FX2LP dev board's buttons as well as write to the 7-segment LED with success. However I'm having trouble accessing an I2C EEPROM.
As I understand, in order to read a byte from an I2C EEPROM, I have to first write the word address, then then read it back. So I wrote the following function
void EEPROMRead(WORD addr, BYTE length, BYTE xdata *buf) { BYTE i = 1; BOOL rdOK = FALSE; rdOK = EZUSB_WriteI2C(EEPROM_ADDR, i, &addr); rdOK = EZUSB_ReadI2C(EEPROM_ADDR, length, buf); }
However, this didn't work, and buf isn't touched at all, retaining its previous value.
I tried hooking up a logic analyzer to see what the exact signals look like, and strangely the signals don't correspond to what I'm telling the I2C functions! For example when I put a device address of 0xA0 for the EEPROM, what I see on the logic analyzer isn't 0xA0 but something else. Same deal when I try to access the on-board buttons and LED, even though they work!
This got me confused, so I tried manually setting the I2CS and I2DAT to send a write command to the address 0xA0, and this time, I see 0xA0 on the logic analyzer.
So, is there something wrong with the EZUSB I2C library functions, or am I missing something?
You already have a thread on this very subject:
http://www.keil.com/forum/docs/thread10833.asp
Stick to that thread - don't make a duplicate!
You are missing the source code of the library. You'll find it in this folder, C:\Cypress\USB\Target\Lib\LP i2c_rw.c and i2c.c
The first parameter, addr, of these functions is shifted left and put to the bus.
I2DAT = addr << 1; - EZUSB_WriteI2C_ or I2DAT = addr << 1 | 0x01; - EZUSB_ReadI2C_
Then, you have to provide a 7-bit slave address (0x50), instead of 8-bit (0xA0).
For the on-board button and LEDs, you may have applied the defined values on dev_io.c, which are 7-bit address. Then, they worked without any error.
Surely, the EZ-USB manual doesn't explain about these functions well. But the source code tells everything.
Tsuneo
Thanks tsuneo, that was exactly it. It took me a while googling for the source code to figure out that it was left shifting the address.