Hello everyone,I'm working on RA6M5 MCU for I2C master. I try to read data from Sensirion's temperature sensor SHTC3.
I checked the I2C code and make sure:The PIN definitions(P400/P401) are correct.The sensor's I2C address(0x70) is correct.The initialization is also correct.
Here are the commands for SHTC3:static uint16_t mI2cCommand = 0x7866;static uint16_t mSleepCommand = 0xb098;static uint16_t mWakeUpCommand = 0x3517;
When I send command to wake up, it always fails at validate_i2c_event(): // write full buffer err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, fullBuff, bytesWritten, false); // handle error if (FSP_SUCCESS != err) { tsPrintf("** I2CWriteAddrEx: R_IIC_MASTER_Write API failed **\r\n"); gI2c.err = I2C_ERR_NO_ACK; return 0; }
err = validate_i2c_event(); // <<<<==== Failed at this line
Here is the validate_i2c_event():static fsp_err_t validate_i2c_event(void){ uint16_t local_time_out = UINT16_MAX;
/* resetting call back event capture variable */ i2c_event = (i2c_master_event_t)RESET_VALUE;
do { /* This is to avoid infinite loop */ --local_time_out;
if(RESET_VALUE == local_time_out) { return FSP_ERR_TRANSFER_ABORTED; }
}while(i2c_event == RESET_VALUE);
if(i2c_event != I2C_MASTER_EVENT_ABORTED) { i2c_event = (i2c_master_event_t)RESET_VALUE; // Make sure this is always Reset before return return FSP_SUCCESS; }
i2c_event = (i2c_master_event_t)RESET_VALUE; // Make sure this is always Reset before return return FSP_ERR_TRANSFER_ABORTED;}
What could cause this validate event failure? I run out of ideas.
Thanks!
Hello Pilot,
Can you please let us know what is the error that the validate_i2c_event() function returns?
Kind regards,
Sergey
If this response, or one provided by another user, answers your question, please verify the answer. Thank you!RenesasRulz Forum Moderatorhttps://renesasrulz.com/https://academy.renesas.com/https://en-support.renesas.com/knowledgeBase/
Hi Sergey,
Thanks for your reply. When I write to I2C, I got FSP_ERR_TRANSFER_ABORTED. After that, if I try to write it again, I got FSP_ERR_IN_USE error.
Thanks.
Do you have some logic analyzer to see what's going on on the I2C pins?
I have an idea that the problem can be here:
uint16_t local_time_out = UINT16_MAX;
According to the data sheet of the SHTC3 sensor it takes some time between sending the wake up command and sensor being ready. Probably this local_time_out is not enough, try to increase it.
Thanks for the info. I increased the local_time_out to be UINT32_MAX. But it doesn't help.
Another weird thing is, if I put the testing code inside the disable/enable interrupt, the problem described in the previous emails showing up. The code is like this:
itrctl_disable_all_interrupts(); test_sensor(); itrctl_enable_all_interrupts();
If I put the testing code outside of the disable/enable interrupt, the R_IIC_MASTER_Write() doesn't even return. It stuck on that function:
void main() { itrctl_disable_all_interrupts(); // Some code itrctl_enable_all_interrupts(); test_sensor(); ... } void test_sensor() { ... // write full buffer err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, fullBuff, bytesWritten, false); ... }
Is it possible that the clock or interrupt is wrong? How can I check to ensure the clock and interrupt are setup correctly?
If that's possible I'd recommend you to check what's going on on the bus by oscilloscope or logic analyzer. Maybe the sensor doesn't respond at all or holds the SCL line low for some reason.