作者:armrunc
转自:http://www.stmcu.org/bbs/article_244_195599.html
流程图如下:
I2C 中断处理函数如下:
/**
* @brief This function handles I2C1 Event interrupt request, tx, rx
* buffer and number of bytes will be changed.
* @param None
* @retval None
*/
void I2C1_EV_IRQHandler(void)
{
#ifdef ARC_I2C_IRQ
uint32_t i2cEvent;
I2C_param_struct __IO *pI2C_param;
pI2C_param = ARC_get_I2C_param();
i2cEvent = I2C_GetLastEvent(I2C1);
switch (i2cEvent)
{
case I2C_EVENT_MASTER_MODE_SELECT:/* EV5 */
if(pI2C_param->I2C_DIRECTION == ARC_I2C_DIRECTION_TX)
{
I2C_Send7bitAddress(I2C1, pI2C_param->DeviceAddr, I2C_Direction_Transmitter);
}
else
{
I2C_Send7bitAddress(I2C1, pI2C_param->DeviceAddr, I2C_Direction_Receiver);
}
break;
/* Master Transmitter -------------------------------------------------------*/
case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
I2C_SendData(I2C1, pI2C_param->SubAddr);
if(pI2C_param->TxNumOfBytes == 0)
I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);
break;
case I2C_EVENT_MASTER_BYTE_TRANSMITTING: /* Without BTF, EV8 */
if(pI2C_param->TX_I2C_Index < pI2C_param->TxNumOfBytes)
{
I2C_SendData(I2C1, pI2C_param->TxData[pI2C_param->TX_I2C_Index++]);
}
else
{
I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);
}
break;
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: /* With BTF EV8-2 */
if(pI2C_param->TX_Generate_stop == 1)
{
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_EVT, DISABLE);
}
else
{
pI2C_param->I2C_DIRECTION = ARC_I2C_DIRECTION_RX;
I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
}
break;
/* Master Receiver -------------------------------------------------------*/
case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED:
if(pI2C_param->RX_I2C_Index == (pI2C_param->RxNumOfBytes - 1))
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
}
break;
case I2C_EVENT_MASTER_BYTE_RECEIVED:
pI2C_param->RxData[pI2C_param->RX_I2C_Index++] = I2C_ReceiveData (I2C1);
if(pI2C_param->RX_I2C_Index == (pI2C_param->RxNumOfBytes - 1))
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
}
break;
default:
break;
}
#endif
}