NOTICE: This website will be shut down in the near future. Product content has moved to nxp.com. Forum content and FAQs have been moved to community.nxp.com. We encourage you to create a user account on nxp.com to use the new community forums and access NXP microcontroller content. We greatly appreciate your contributions and look forward to seeing you at our new web location.

 

I2C EEPROM Problem

3 replies [Last post]
aralath
Offline
Joined: 2016-03-04
Posts: 2

Hi there,

I am trying to write a single byte and read it back using I2C bus. The EEPROM I use is AT24C256. I used I2C bus with other MCUs like 17xx and they worked perfectly, however I couldn't do it with LPC1549. Here is the code:

/* */
#define I2C_CLK_DIVIDER (40)
#define I2C_BITRATE (100000)
/* Standard I2C mode */
#define I2C_MODE (0)
Ui32 dataWr, dataRd;

/* Setup I2C handle and parameters */

void setupI2CMaster()
{
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | I2C_MODE);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | I2C_MODE);
Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);

/* Enable I2C clock and reset I2C peripheral - the boot ROM does not
do this */
Chip_I2C_Init(LPC_I2C0);

/* Setup clock rate for I2C */
Chip_I2C_SetClockDiv(LPC_I2C0, I2C_CLK_DIVIDER);

/* Setup I2CM transfer rate */
Chip_I2CM_SetBusSpeed(LPC_I2C0, I2C_BITRATE);

/* Enable Master Mode */
Chip_I2CM_Enable(LPC_I2C0);
}

void EEPROM_Wrr(void){

dataWr = 100;

LPC_I2C->MSTDAT = (0x50<<1) | 0; // address and 0 for RWn bit

LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTART; // send start

while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));

LPC_I2C->MSTDAT = dataWr; // send data

LPC_I2C->MSTCTL = I2C_MSTCTL_MSTCONTINUE; // continue transaction

while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));

LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTOP; // send stop
}

void EEPROM_Rdd(void){

LPC_I2C->MSTDAT = (0x50<<1) | 1; // address and 1 for RWn bit

LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTART; // send start

while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));

dataRd = LPC_I2C->MSTDAT; // read data

LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTOP; // send stop

}
int main(){

setupI2CMaster();

EEPROM_Wrr();
for(Wait = 0; Wait<30000; Wait++);
EEPROM_Rdd();
}

The init part is from LPC_Xpresso library. The rest is from the user manual code example. Write protection is disabled for the EEPROM. Also I tried to use E2 by software I\O method, again it worked perfectly. Sadly, I2C bus did not work. When I debug the code, it stucks in ""while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING))"" .

Any help is greatly appreciated!

5
Your rating: None Average: 5 (1 vote)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
DennisFrie
Offline
Joined: 2014-05-11
Posts: 44

Hi
I have a similar problem. I've been debugging a bit and it seems like the continue transmission, or set "master continue" bit, doesn't have any effect. Starting a new transaction, sending the nack etc. works perfectly fine, but when I set the "master continue" bit, nothing happens.
Have you found a solution to your problem?

Edit; Sorry, in my case it was a wrong I2C address causing the problem.

aralath
Offline
Joined: 2016-03-04
Posts: 2

Glad that you solved your problem Smile Mine is still not working though.

DennisFrie
Offline
Joined: 2014-05-11
Posts: 44

I don't use the LPC-predefined functions myself, but it seems very similar to what I have working. One thing I noticed, as the last thing in EEPROM_Wrr() you send the stop-command, but don't use the while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING)); to ensure the I2C is ready again. You do have a dummy wait-loop, but I can't see if wait is defined as volatile to ensure proper delay-functionality? If not, the compiler might just skip the for-loop, go directly to EEPROM_Rdd but can't use the I2C, as it's still busy sending the stop-command.
Only an idea, don't see anything else obvious. Do you have a logic analyzer to verify bit-output and acknowledgement from slave-device?

feedback