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!