Hi.
I have some problems witch i2c on mkl15z128.
I try to communicate with RTC DS1338. For beginning i try get on the bug START condition, Slave address write and STOP condition.
But i have problems.
I use PTE0/TPE1 GPIO pins.
My init function
void init_I2C(void)
{
SIM_SCGC4 |= SIM_SCGC4_I2C1_MASK; //Turn on clock to I2C1 module
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;
/* configure GPIO for I2C1 function */
PORTE_PCR0 = PORT_PCR_ISF_MASK | PORT_PCR_MUX(6);
PORTE_PCR1 = PORT_PCR_ISF_MASK | PORT_PCR_MUX(6);
I2C1_F = I2C_F_ICR(14) | I2C_F_MULT(0);
I2C1_C1 = I2C_C1_IICEN_MASK;
}
My functions for write i take from examples
void i2c_Wait(void)
{
while((I2C1_S & I2C_S_IICIF_MASK)==0)
{
wdtFeed();
}
I2C1_S |= I2C_S_IICIF_MASK;
}
void IIC_StartTransmission (unsigned char SlaveID, unsigned char Mode)
{
if(Mode == MWSR)
{
/* set transmission mode */
MasterTransmission = MWSR;
}
else
{
/* set transmission mode */
MasterTransmission = MRSW;
}
/* shift ID in right possition */
SlaveID = (unsigned char) MMA7660_I2C_ADDRESS << 1;
/* Set R/W bit at end of Slave Address */
SlaveID |= (unsigned char)MasterTransmission;
/* send start signal */
i2c_Start();
/* send ID with W/R bit */
i2c_write_byte(SlaveID);
}
void MMA7660WriteRegister(unsigned char u8RegisterAddress, unsigned char u8Data)
{
/* send data to slave */
IIC_StartTransmission(SlaveID,MWSR);
i2c_Wait();
/* Send I2C address */
I2C1_D = u8RegisterAddress;
i2c_Wait();
/* Send data */
I2C1_D = u8Data;
i2c_Wait();
i2c_Stop();
//Pause();
}
When i call MMA7660WriteRegister(0x1, 0x2) i have reboot device by WDT.
When i insert wdtFeed in while loop i have no reboot.
while((I2C1_S & I2C_S_IICIF_MASK)==0)
{
wdtFeed();
}
I have START condition but in I2C1_S i have no I2C_S_IICIF_MASK after START.
I have no idea what i do wrong. Pleas help me.
Hello serj none:
I assume you have pullup resistors in SCL and SDA lines, correct?
I would recommend checking the bus with an oscilloscope or logic analyzer. The IICIF flag should set as soon as 9 clock cycles are shifted out (8-bit data + ack/nack). The flag should set disregarding if the slave responds or not.
And some observations about your code:
- You are using I2C1 (not I2C0). Just double check that i2c_start(), i2c_write_byte() and i2c_Stop() functions refer to this I2C module instance.
- For address cycle the bit I2C1_C1[TX] should be set to '1', indicating Transmit mode. I do not see this in your code.
- Your function IIC_StartTransmission receives SlaveID as parameter, but then completely ignores such value and overwrites it with MMA7660_I2C_ADDRESS. Verify that you use the correct address for your RTC slave device.
I hope these comments help.
Regards!,
Jorge Gonzalez
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank you.
There are was shoted SCL and SCK lines.
I use i2c_1 and all fanctions referred to i2c_1.
I think examples from IAR not fully corrected.
After simple code i2s_start() in my project dos not work - a wrong slave address transmitted.
But if i write
i2c_start();
while((I2C1_S & (1<<5)) == 0); //waite while BUSY bit set
in any cases transmit correct slave address.
Also I change i2c_stop() :
i2c_stop();
while((I2C1_S & (1<<5)) != 0); //waite while BUSY bit clear
Now my code work stable and relable.
Thank you fo help.