/*---------------------------------------------------------------------------------------------------------*/
/* I2C Rx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterRx(uint32_t u32Status)
{
if (u32Status == 0x08) /* START has been transmitted and prepare SLA+W */
{
I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1)); /* Write SLA+W to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
}
else if (u32Status == 0x28) /* DATA has been transmitted and ACK has been received */
{
if (g_u8DataLen != 2)
{
I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else
{
I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_SI);
}
}
else if (u32Status == 0x10) /* Repeat START has been transmitted and prepare SLA+R */
{
I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1) | 0x01); /* Write SLA+R to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x40) /* SLA+R has been transmitted and ACK has been received */
{
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x58) /* DATA has been received and NACK has been returned */
{
g_u8RxData = I2C_GET_DATA(I2C0);
I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
g_u8EndFlag = 1;
}
else
{
/* TO DO */
printf("Status 0x%x is NOT processed\n", u32Status);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* I2C Tx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
if (u32Status == 0x08) /* START has been transmitted */
{
I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1); /* Write SLA+W to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else if (u32Status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
}
else if (u32Status == 0x28) /* DATA has been transmitted and ACK has been received */
{
if (g_u8DataLen != 3)
{
I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
I2C_SET_CONTROL_REG(I2C0, I2C_SI);
}
else
{
I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
g_u8EndFlag = 1;
}
}
else
{
/* TO DO */
printf("Status 0x%x is NOT processed\n", u32Status);
}
}
例子中用到的这些状态,我在其他厂家单片机里看到的也是这几个,对应的含义状态也一样。
|