STM32 I2C hardware interrupt implementation method

Publisher:乐呵的挑Latest update time:2018-06-03 Source: eefocusKeywords:STM32  I2C Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The flow chart is as follows:

 

The I2C interrupt processing function is as follows:

 

/**
  * @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
}


Keywords:STM32  I2C Reference address:STM32 I2C hardware interrupt implementation method

Previous article:STM32 I2C Hardware
Next article:About I2C hardware DMA implementation of STM32

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号