I’m trying to write and read from a Microchip 24AA01 EEPROM over the I2C1 of the TMS570LS3137. I configured the I2C driver with HALCoGen (version 3.00.00) in master mode, address size = 7 bits, count = 8 bits and baud rate = 100 kHz. I had to correct the generated initialization sequence: I2CMDR.BC = 0 instead of 7 and I2CMDR.FDF = 1 (otherwise it doesn’t work) instead of 0. At this time I can write to the EEPROM, but the reading still fails since the second start condition isn’t set correctly. I should set Start Bit - Device Address - Internal Address – Start Bit – Device Address (with read) – Response 0 - … - Response N – Stop Bit, but on the I2C bus I get Start Bit - Device Address - Internal Address– Device Address (with read) – Start Bit – Response 0. My read function is:
// Set the bus in master and transmitter mode and set the START condition
I2C->MDR |= I2C_MASTER | I2C_TRANSMITTER | I2C_START_COND;
// Send the slave address
i2cSendByte(I2C, slaveDeviceAddress);
// Send the internal address
i2cSendByte(I2C, (Uint8)readAddress);
// Set the bus in master, repeat and receiver mode and set the START condition
I2C->MDR &= ~I2C_TRANSMITTER;
I2C->MDR |= I2C_MASTER | I2C_REPEATMODE | I2C_START_COND;
// Send the slave address (for reading)
i2cSendByte(I2C, slaveDeviceAddress | 0x01);
// Receive n-1 bytes
while (readLength > 1)
{
*readBuffer++ = i2cReceiveByte(I2C);
readLength--;
}
// Receive the last bytes (and set the STOP condition)
I2C->MDR |= I2C_STOP_COND;
*readBuffer = i2cReceiveByte(I2C);
// Wait until the bus isn't busy and the master mode bit is cleared
while (I2C->STR & I2C_BUSBUSY);
while (I2C->MDR & I2C_MASTER);
Hi Daniel,
Your question has been forwarded to our I2C expert. We will get back to you as soon as possible.
Regards, Sunil
Know nothing about the TMS570. My guess is that you are turning off transmit too early. These lines
// Set the bus in master, repeat and receiver mode and set the START conditionI2C->MDR &= ~I2C_TRANSMITTER;I2C->MDR |= I2C_MASTER | I2C_REPEATMODE | I2C_START_COND;// Send the slave address (for reading)i2cSendByte(I2C, slaveDeviceAddress | 0x01);
should be:
// Send another start bit.I2C->MDR |= I2C_MASTER | I2C_START_COND;// Send the slave address (for reading)i2cSendByte(I2C, slaveDeviceAddress | 0x01);// Switch to receive.I2C->MDR &= ~I2C_TRANSMITTER;
Judging from the data sheet, I don't think I2C_REPEATMODE needs to be used. Also, usually the last byte received is usually not ACKed by the master. I suspect there is another way to code this using I2C APIs without free data format and handle the last ACK.
I already tried your sequence for reading, but doing so I don’t get the second start bit at all! Do you have any I2C example for the TMS570 than the delivered with HALCoGen version 3.00.00 (send and receive done over the internal loopback)?
Sorry to hear that did not work. No experience with TMS570 and HalCoGen. I don't see FDF often. Not familiar with it. The I2C controller in the TMS570 looks similiar to controllers in all of TI's ARM processors. I2C code from those processors can be used a reference. I am guessing that code that uses the non-FDF mode would be something like this:
/* Assumes I2CMDR.RM =0 and I2CMDR.FDF = 0 */I2C->SAR = slaveDeviceAddress>>1;// 7-bit addressI2C->MDR |= I2C_MASTER;I2C->CNT = 1;I2C->DXR = (Uint8)readAddress;I2C->MDR |= I2C_TRANSMITTER | I2C_START_COND;while (I2C->STR & I2C_BUSBUSY);I2C->CNT = readLength;I2C->MDR &= ~I2C_TRANSMITTER;I2C->MDR |= I2C_START_COND | I2C_STOP_COND;while(readLength){ while (I2C->STR & I2C_RXRDY); *readBuffer++ = I2C->DRR; readLength--;}while (I2C->STR & I2C_BUSBUSY);while (I2C->MDR & I2C_MASTER);
The I2C state machine automatically handles the start/address and stop parts.
That's all I got. Hopefully Sunil Oak's I2C expert will respond soon.
EDIT: Forgot to convert 8-bit address to 7 bit-address
Daniel,
I'm preparing a sample I2C data transfer code for TMS570 . Hope it will help.
- Pratip
I had a look into your case where you are trying to write and read from Microchip 24AA01 EEPROM.
Looks like you need not have to be in FDF mode to communicate with it , you can try with repeat mode ON and set appropriate Salve Address if you need the following format both for write and read operations:
Start Bit - Device Address - Internal Address – Start Bit – Device Address (with read) – Response 0 - … - Response N – Stop Bit,
i2cREG1->SAR = sadd;
i2cREG1->MDR |= RepeatMode
You can keep the I2CMDR.BC = 0 and I2CMDR.FDF = 0 . By the way Halcogen doesn't support FDF format at the moment.
This should generate a start bit for each byte of data transfer.
if I'm refering the right datasheet from microchip the slave address needs to be
(Edit:) 0xA0 (i.e) 1010XXX
Do give this a shot and let me know how it moves.
I apologise if there has been much of a delay from us to give you any help.
Hi Pratip
The slave address of the EEPROM is 0xA0 [from Microchip 24A001 data sheet]:
I have already tried with FDF = 0, RM = 0 and RM = 1, but I had no success with both combinations L
I can successfully write in the EEPROM (the EEPROM acknowledges my commands and data), but only with FDF = 1, RM = 0 and BC = 0.
The EEPROM is the only device on the I2C bus.
I also noticed that the bus frequency is set incorrectly by HALCoGen. I would like to have a bus speed of 100 kHz:- HALCoGen sets PSC = 9 and CLKH = CLKL = 0x1E => 113.6 kHz- I calculated PSC = 9 and CLKH = CLKL = 0x23 => 100 kHz
- Daniel
Yes the slave add is 0xA0 .Since the slave expects a start condition on each byte I expect the RM to be ON.
Let me have a closer look.
1. In the FDF , direction cannot be changed throught out that transfer. If FDF works for you , could you try with few consecutive writes followed by consecutive reads.
2. While using RM , my concern is the way we switch from write to read , refer the link I2C Tips .
3. I guess you could also try with RM=0 and try sending one word at a time by setting STT each time
and set the I2CCNT : i2cREG1->CNT = cnt;
See if the attached file helps.4747.i2c.c
Once I have my hardware up , I can send you some verified sample codes.
I''ll be looking forward for your observations !!!!
Were you able to read from the EEPROM ?
Try with the attached file and see if it helps. I'll close on this ticket , you may raise a new one if you need further help.
7635.i2c.zip
-Pratip
Pratip
I’m back from vacation ;-)
With the I2C Tips <http://processors.wiki.ti.com/index.php/I2C_Tips> I could solve the problems with the I2C! I have configured the I2C with RM=0, FDF=0 and SAR=0x50 (=0xA0>>1).
I have seen that you are using HALCoGen version 03.01.00, I still have version 03.00.00! Is the newer version already available, or when will it be?
Thanks Daniel
I'm glad that the I2C tips page helped solve your issue! HALCoGen 3.01 is a TI internal release. The next HALCoGen update will happen 1H2012.
I would like to know what is the purpose of the SAR register if, anyway, we have to send it in each message:
«
...
// Send the slave addressi2cSendByte(I2C, slaveDeviceAddress);
»
What it is handled exactly by the i2c controller? The ACK? The slave address?
I have generated the Halcogen code, it`s fine but I still don't know how to use it. It doesn't work.
I communicate as a Master to a single slave (light transmitter ISL76683 from TI). Do you have some example? (Other than from Halcogen).
Simon
Hmmm...no response from the TI guys. Some guesses. The SAR is not used when the controller is in Free Data Format mode. In that mode you must manage the entire transaction. In the non FDF mode, SAR is used by I2C controller to automatically send the start bit, slave address and read/write bit automatically.