/** * @brief This function handles I2Cx event interrupt request. * @param None * @retval None */ void I2Cx_EV_IRQHandler(void) { #if defined (I2C_SLAVE) /* Get Last I2C Event */ Event = I2C_GetLastEvent(I2Cx); switch (Event) { /*****************************************************************************/ /* Slave Transmitter Events */ /* */ /* ***************************************************************************/ /* Check on EV1 */ case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]); /* Enable I2C event interrupt */ I2C_ITConfig(I2Cx, I2C_IT_BUF, ENABLE); break; /* Check on EV3 */ case I2C_EVENT_SLAVE_BYTE_TRANSMITTING: case I2C_EVENT_SLAVE_BYTE_TRANSMITTED: if (Tx_Idx < NbrOfDataToSend) I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]); else /* Disable I2C event interrupt */ I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_BUF, DISABLE); break; default: break; } #endif /* I2C_SLAVE*/ #if defined (I2C_MASTER) /*****************************************************************************/ /* Master Receiver */ /* */ /* ***************************************************************************/ #ifdef I2C_10BITS_ADDRESS /* Check on SB Flag and clear it */ if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET) { if (Send_HeaderStatus == 0x00) { /* Send Header to Slave for write */ I2C_SendData(I2Cx, HEADER_ADDRESS_Write); Send_HeaderStatus = 0x01; GenerateStartStatus = 0x01; } else { /* Send Header to Slave for Read */ I2C_SendData(I2Cx, HEADER_ADDRESS_Read); Send_HeaderStatus = 0x00; if (NumberOfByteToReceive == 0x03) { /* Disable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE); } else { /* Enable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE); } } } /* Check on ADD10 Flag */ else if(I2C_GetITStatus(I2Cx, I2C_IT_ADD10)== SET) { /* Send slave Address */ I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter); } #else /* I2C_7BITS_ADDRESS */ /* Check on EV5 */ if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET) { /* Send slave Address for read */ I2C_Send7bitAddress(I2Cx, SLAVE_ADDRESS, I2C_Direction_Receiver); if (NumberOfByteToReceive == 0x03) { /* Disable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE); } else { /* Enable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE); } } #endif /* I2C_10BITS_ADDRESS */ else if(I2C_GetITStatus(I2Cx, I2C_IT_ADDR)== SET) { if (NumberOfByteToReceive == 1) { I2C_AcknowledgeConfig(I2Cx, DISABLE); } /* Clear ADDR Register */ (void)(I2Cx->SR1); (void)(I2Cx->SR2); if (GenerateStartStatus == 0x00) { if (NumberOfByteToReceive == 1) { I2C_GenerateSTOP(I2Cx, ENABLE); } if (NumberOfByteToReceive == 2) { I2C_AcknowledgeConfig(I2Cx, DISABLE); I2C_PECPositionConfig(I2Cx, I2C_PECPosition_Next); /* Disable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE); } } #ifdef I2C_10BITS_ADDRESS if (GenerateStartStatus == 0x01) { /* Repeated Start */ I2C_GenerateSTART(I2Cx, ENABLE); GenerateStartStatus = 0x00; } #endif /* I2C_10BITS_ADDRESS */ } else if((I2C_GetITStatus(I2Cx, I2C_IT_RXNE)== SET)&&(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== RESET)) { /* Store I2C received data */ RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx); NumberOfByteToReceive--; if (NumberOfByteToReceive == 0x03) { /* Disable buffer Interrupts */ I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE); } if (NumberOfByteToReceive == 0x00) { /* Disable Error and Buffer Interrupts */ I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE); } } /* BUSY, MSL and RXNE flags */ else if(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== SET) { /* if Three bytes remaining for reception */ if (NumberOfByteToReceive == 3) { I2C_AcknowledgeConfig(I2Cx, DISABLE); /* Store I2C received data */ RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx); NumberOfByteToReceive--; } else if (NumberOfByteToReceive == 2) { I2C_GenerateSTOP(I2Cx, ENABLE); /* Store I2C received data */ RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx); NumberOfByteToReceive--; /* Store I2C received data */ RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx); NumberOfByteToReceive--; /* Disable Error and Buffer Interrupts */ I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE); } else { /* Store I2C received data */ RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx); NumberOfByteToReceive--; } } #endif /* I2C_MASTER*/ }
/************************************************************* * I2C initialization code template **************************************************************/ void I2C1_Init() { #if (STRCMP($interruptEn$, 0) == 0) NVIC_InitTypeDef NVIC_InitStructure; #endif I2C_InitTypeDef I2C_InitStructure; //PUT_A_NEW_LINE_HERE // // Enable I2C clock // RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); #if (STRCMP($SCL1$, DISABLE) == 0 || STRCMP($SDA1$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enable AFIO and I2C clock // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //PUT_A_NEW_LINE_HERE // // I2C SCL pin remap to PB8, I2C SDA pin remap to PB8 // GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE); #endif //PUT_A_NEW_LINE_HERE // // I2C config // I2C_InitStructure.I2C_Mode = $mode$; I2C_InitStructure.I2C_DutyCycle = $dutyCycle$; I2C_InitStructure.I2C_OwnAddress1 = $ownAddress$; I2C_InitStructure.I2C_Ack = $ack$; I2C_InitStructure.I2C_AcknowledgedAddress = $ackAddress$; I2C_InitStructure.I2C_ClockSpeed = $speed$; I2C_Init($I2C$, &I2C_InitStructure); //PUT_A_NEW_LINE_HERE // // Enable I2C // I2C_Cmd($I2C$, ENABLE); I2C_Init($I2C$, &I2C_InitStructure); #if (STRCMP($interruptEn$, 0) == 0) //PUT_A_NEW_LINE_HERE // // Enable I2C DualAddress // I2C_OwnAddress2Config($I2C$, $ownAddress2$); I2C_DualAddressCmd($I2C$, ENABLE); #endif #if($modeSelect$) //PUT_A_NEW_LINE_HERE // // Transmits the address byte to select the slave device // I2C_Send7bitAddress($I2C$, $slaveAddress$, $transferDirection$); #endif #if(STRCMP($GenCallEn$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables I2C1 general call feature // I2C_GeneralCallCmd($I2C$, ENABLE); #endif #if(STRCMP($StrchClkEn$, DISABLE) == 0) //PUT_A_NEW_LINE_HERE // // Enables I2C1 Clock stretching // I2C_StretchClockCmd($I2C$, ENABLE); #endif #if (STRCMP($interruptEn$, 0) == 0) //PUT_A_NEW_LINE_HERE // // Interrupt config // I2C_ITConfig($I2C$, $interruptEn$, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif #if($pecTransmitEn$) //PUT_A_NEW_LINE_HERE // // PEC Transmit Enable // #if($pecTransmitEn$) I2C_TransmitPEC($I2C$, ENABLE); #endif #if($pecCalculateEn$) I2C_CalculatePEC($I2C$, ENABLE); #endif #if(STRCMP($pecPosition$, I2C_PECPosition_Next)) I2C_PECPositionConfig($I2C$, $pecPosition$); #endif #if(STRCMP($pecPosition$, I2C_PECPosition_Current)) I2C_PECPositionConfig($I2C$, $pecPosition$); #endif #endif #if(!STRCMP($mode$, I2C_Mode_I2C)) //PUT_A_NEW_LINE_HERE // // PEC Transmit Enable // I2C_SMBusAlertConfig($I2C$, $SMBusAlertCfg$); I2C_ARPCmd($I2C$, $ARPEn$); #endif }