I2C EEPROM Problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C EEPROM Problem

1,137 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by aralath on Fri May 13 01:18:50 MST 2016
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!
Labels (1)
0 Kudos
4 Replies

841 Views
lpcware
NXP Employee
NXP Employee
bump
0 Kudos

841 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by DennisFrie on Mon Jun 13 12:28:33 MST 2016
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?
0 Kudos

841 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by aralath on Mon Jun 13 04:24:33 MST 2016
Glad that you solved your problem  :)  Mine is still not working though.
0 Kudos

841 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by DennisFrie on Sat Jun 11 10:50:53 MST 2016
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.
0 Kudos