I am trying to exchange data via I2C with TI's IC in MKL03Z32CAF4, and I am using fsl_i2c.c driver to create the code.
I am in an infinite loop with I2C and cannot solve the problem.
First of all, I am trying to use the
I2C_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate_Bps = 100000U;
sourceClock = CLOCK_GetFreq(I2C0_CLK_SRC);//48000000;
I2C_MasterInit(I2C0, &masterConfig, sourceClock);
After doing the following, I did the following code to do READ only.
masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;
masterXfer.direction = kI2C_Read;
masterXfer.subaddress = (uint32_t)reg_ad;
masterXfer.subaddressSize = 1;
masterXfer.data = rxBuff;
masterXfer.dataSize = datasize - 1;
masterXfer.flags = kI2C_TransferDefaultFlag;
I2C_MasterTransferBlocking(I2C0, &masterXfer);
But I feel that I2C is not working properly because it enters an infinite loop in the while() of I2C_MasterTransferBlocking().
If anyone knows a solution, please let me know.
By the way, the following code has an infinite loop at while().
/* Clear all status before transfer.
I2C_MasterClearStatusFlags(base, kClearFlags);
/* Wait until the data register is ready for transmit.
while (! (base->S & kI2C_TransferCompleteFlag))//(base->S & (uint8_t)kI2C_BusBusyFlag)//
{
}
Translated with www.DeepL.com/Translator (free version)
First, check that you are using the latest SDK. My version (for another Kinetis) has a timeout implemented:
In any case, you can easily add such a timeout to your SDK code, I did that for other cases too.
The SDK code is not 'static': you are free to update it for your needs.
Thank you for your response.
The SDK we are using is version 2.3.1 with the last release on 4/17/2018.
I have tried to time out the I2C, but it goes into "return" and the I2C stops. Also, I have a problem with the
"while (! (base->S & kI2C_TransferCompleteFlag))"
to
"while(base->S & (uint8_t)kI2C_BusBusyFlag)"
but base->S does not change anything from 0x20, so it enters an infinite loop.
It seems to me that you might have a hardware problem with your communication: did you check the I2C communication with a logic analyzer, if it makes sense and if the sensor responds?
I do not have a logic analyzer with me, but I do have an oscilloscope.
I checked with the oscilloscope and it seems that no signal is generated So, there is most likely a hardware problem.
I am aware that "base->S" is a register that changes when there is a signal. It seems likely that we are in an infinite loop because the signal itself is not generated.
I will check the hardware.
There could be many reasons: wrong pins, pins not muxed, I2C not clocked.
the 'busy' flag simply tells that there is no response from the bus.
We checked the hardware and found no problems. It seems that the microcontroller is not properly configured.
I will check again from the beginning.
By the way, the reference manual says that the 'busy' flag is set when the start signal is detected, but if the 'busy' flag is set even when there is no response from the bus, that means that the 'busy' flag is set even when there is no response from the bus. So the 'busy' flag is set even when there is no response from the bus.