我们的设备挂了一个i2c的传感器,psoc6当master,通过以下的示例代码来读写传感器的寄存器
{
cy_stc_scb_i2c_master_xfer_config_t transfer;
uint8_t readBuffer [5UL];
uint8_t writeBuffer[2UL] = {0U, 5U};
/* Configure write transac
tion */
transfer.
slaveAddress = 0x08U;
transfer.
buffer = writeBuffer;
transfer.
bufferSize = sizeof(writeBuffer);
transfer.
xferPending = true; /* Do not generate Stop condition at the end of transaction */
/* Initiate write transaction.
* The Start condition is generated to begin this transaction.
*/
(void)
Cy_SCB_I2C_MasterWrite(SCB3, transfer, i2cContext);
/* Wait for transaction completion */
while (0UL != (
CY_SCB_I2C_MASTER_BUSY Cy_SCB_I2C_MasterGetStatus(SCB3, i2cContext)))
{
}
/* Configure read transaction */
transfer.
buffer = readBuffer;
transfer.
bufferSize = sizeof(readBuffer);
transfer.
xferPending = false; /* Generate Stop condition the end of transaction */
/* Initiate read transaction.
* The ReStart condition is generated to begin this transaction because
* previous transaction was completed without Stop.
*/
(void)
Cy_SCB_I2C_MasterRead(SCB3, transfer, i2cContext);
/* Wait for transaction completion */
while (0UL != (
CY_SCB_I2C_MASTER_BUSY Cy_SCB_I2C_MasterGetStatus(SCB3, i2cContext)))
{
}
}
但调试中发现有很大概率在下面的代码中死循环,while (0UL != (
CY_SCB_I2C_MASTER_BUSY Cy_SCB_I2C_MasterGetStatus(SCB3, i2cContext)))
{
}
请问这是啥原因呢?是i2c初始化没做好?还是i2c总线上有冲突?(但总线上我就挂了一个设备)谢谢!