Hello community,
I am trying to connect I2C light sensor (polling) to i.MX RT1010 EVK using MCUXpresso IDE
The sensor TSL2561 I2C slave is connect to J57. I just want to detect the sensor by reading register TSL2561_REGISTER_ID = 0xa.
J57
GND = pin 14
3.3V = pin 16
i2C1_SDA = pin 18
i2C1_SCL = pin 20
status_t result;
BOARD_ConfigMPU();
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();/*Clock setting for LPI2C*/
CLOCK_SetMux(kCLOCK_Lpi2cMux, LPI2C_CLOCK_SOURCE_SELECT);
CLOCK_SetDiv(kCLOCK_Lpi2cDiv, LPI2C_CLOCK_SOURCE_DIVIDER);LPI2C_MasterGetDefaultConfig(&masterConfig);
/* Change the default baudrate configuration */
masterConfig.baudRate_Hz = LPI2C_BAUDRATE;/* Initialize the LPI2C master peripheral */
LPI2C_MasterInit(EXAMPLE_I2C_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY);/* Initialize sensor devices */
g_master_txBuff[0] = TSL2561_REGISTER_ID;
result = wrI2Cdata();
if (result != kStatus_Success)
printf("\r\nI2C device initialize failed! %d\r\n",result);result = rdI2Cdata();
if (result != kStatus_Success)
printf("\r\nI2C device initialize failed! %d\r\n",result);
status_t wrI2Cdata (void) {
status_t reval;
size_t txCount = 0x1;reval = LPI2C_MasterStart(EXAMPLE_I2C_MASTER, g_sensor_address[0], kLPI2C_Write);
if (kStatus_Success == reval) {LPI2C_MasterGetFifoCounts(EXAMPLE_I2C_MASTER, NULL, &txCount);
while (txCount){
LPI2C_MasterGetFifoCounts(EXAMPLE_I2C_MASTER, NULL, &txCount);
}
/* Check communicate with slave successful or not */
while (LPI2C_MasterGetStatusFlags(EXAMPLE_I2C_MASTER) & kLPI2C_MasterNackDetectFlag){
}reval = LPI2C_MasterSend(EXAMPLE_I2C_MASTER,g_master_txBuff , 1);
if (reval != kStatus_Success)
return reval;reval = LPI2C_MasterStop(EXAMPLE_I2C_MASTER);
}
return reval;
}
status_t rdI2Cdata (void)
{
status_t reval;
size_t txCount = 0x1;reval = LPI2C_MasterStart(EXAMPLE_I2C_MASTER, g_sensor_address[0], kLPI2C_Read);
if (kStatus_Success == reval) {LPI2C_MasterGetFifoCounts(EXAMPLE_I2C_MASTER, NULL, &txCount);
while (txCount)
{
LPI2C_MasterGetFifoCounts(EXAMPLE_I2C_MASTER, NULL, &txCount);
}
/* Check communicate with slave successful or not */
while (LPI2C_MasterGetStatusFlags(EXAMPLE_I2C_MASTER) & kLPI2C_MasterNackDetectFlag)
{
}reval = LPI2C_MasterReceive(EXAMPLE_I2C_MASTER, g_master_rxBuff, 1);
if (reval != kStatus_Success)
{
return reval;
}reval = LPI2C_MasterStop(EXAMPLE_I2C_MASTER);
}
return reval;
}
I2C device initialize failed! 909
I2C device initialize failed! 909
Hello Marcio,
I'm not familiar with the sensor you are using, however, I recommend you using the example polling_b2b_master from the SDK, this example works properly. I would also recommend you to check that the configurations you are using for the Master are supported by your sensor, for example, you can verify that the baudrate that you are setting on the Master is not too fast for your sensor.
Best regards,
Victor