void i2c_config() { // RCC /* Enable GPIOB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // GPIO GPIO_InitTypeDef GPIO_InitStructure; /* Configure I2C1 pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Enable I2C1 reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); /* Release I2C1 from reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); I2C_DeInit(I2C1); // I2C /* Enable I2C1 */ I2C_Cmd(I2C1, ENABLE); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = ID; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = ClockSpeed; I2C_Init(I2C1, &I2C_InitStructure); I2C_GeneralCallCmd(I2C1, ENABLE); /* Enable Event IT needed for ADDR and STOPF events ITs */ I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE); /* Enable Error IT */ I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); /* Enable Buffer IT (TXE and RXNE ITs) */ I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE); // NVIC /* 1 bit for pre-emption priority, 3 bits for subpriority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_SetPriority(I2C1_EV_IRQn, 0x00); NVIC_EnableIRQ(I2C1_EV_IRQn); NVIC_SetPriority(I2C1_ER_IRQn, 0x01); NVIC_EnableIRQ(I2C1_ER_IRQn); }
/************************************************************* * 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 }