Hi, Phil,
Regarding your question, I am not sure whether the K66 has succeeded to write the value to the register of the analog device, as you know that IIC module needs ACK handshaking signal, so i can not test your application code on my board.
From your code, I have not found any problem. when you call the api function: I2C_WriteRegister(i2cCom0_IDX, 0xB0, 0xFF, 0xFF);, the K66 writes the 0xB0(slave address), 0xFF(I suppose register address) and 0xFF to the IIC slave device in sequence via IIC, in the process, an IIC interrupt happens, it is normal because you enable the IIC interrupt, in the ISR, the Bit0 RXAK of I2Cx_S is tested, if the RXAK bit is set, the kStatus_I2C_ReceivedNak is assigned to status variable with the code:
master->status = kStatus_I2C_ReceivedNak;
so the I2C_DRV_MasterSendDataBlocking() return the status value.
Note that I2C_DRV_MasterIRQHandler() is the only function that kStatus_I2C_ReceivedNak is assigned to status variable.
do you have the other alternative on the IIC slave device to confirm whether the data has written or not?
BR
Xiangjun Rong
.
This is the IIC ISR in SDK.
void I2C_DRV_MasterIRQHandler(uint32_t instance)
{
assert(instance < I2C_INSTANCE_COUNT);
I2C_Type * base = g_i2cBase[instance];
/* Clear the interrupt flag*/
I2C_HAL_ClearInt(base);
/* Get current runtime structure */
i2c_master_state_t * master = (i2c_master_state_t *)g_i2cStatePtr[instance];
/* Get current master transfer direction */
i2c_direction_t direction = I2C_HAL_GetDirMode(base);
bool wasArbLost = I2C_HAL_GetStatusFlag(base, kI2CArbitrationLost);
if (wasArbLost)
{
I2C_HAL_ClearArbitrationLost(base);
master->status = kStatus_I2C_AribtrationLost;
/* Disable I2C interrupt in the peripheral.*/
I2C_HAL_SetIntCmd(base, false);
if (master->isBlocking)
{
OSA_SemaPost(&master->irqSync);
}
return;
}
/* Exit immediately if there is no transfer in progress OR not in master mode */
if ((!I2C_HAL_GetStatusFlag(base, kI2CBusBusy)) ||
(!I2C_HAL_IsMaster(base)))
{
return;
}
/* Handle send */
if (direction == kI2CSend)
{
/* Check whether we got an ACK or NAK from the former byte we sent */
if (I2C_HAL_GetStatusFlag(base, kI2CReceivedNak))
{
/* Record that we got a NAK */
master->status = kStatus_I2C_ReceivedNak;
/* Got a NAK, so we're done with this transfer */
I2C_DRV_CompleteTransfer(instance);
}
else
{
/* Continue send if still have data. TxSize/txBuff index need
* increment first because one byte is already sent in order
* to trigger interrupt */
if (--master->txSize > 0)
{
/* Transmit next byte and update buffer index */
I2C_HAL_WriteByte(base, *(++master->txBuff));
}
else
{
/* Finish send data, send STOP, disable interrupt */
I2C_DRV_CompleteTransfer(instance);
}
}
}
else /* Handle receive */
{
switch (--master->rxSize)
{
case 0x0U:
/* Finish receive data, send STOP, disable interrupt */
I2C_DRV_CompleteTransfer(instance);
break;
case 0x1U:
/* For the byte before last, we need to set NAK */
I2C_HAL_SendNak(base);
break;
default :
I2C_HAL_SendAck(base);
break;
}
/* Read recently received byte into buffer and update buffer index */
*(master->rxBuff++) = I2C_HAL_ReadByte(base);
}
}