//______________________________________________________________________________________ _i2c *Initialize_I2C(int addr, int speed) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; _i2c *p=calloc(1,sizeof(_i2c)); if(addr) p->addr=addr; if(speed) p->speed=speed; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); I2C_DeInit(I2C1); I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = speed; I2C_Init(I2C1, &I2C_InitStructure); I2C_StretchClockCmd(I2C1, ENABLE); I2C_ARPCmd(I2C1, ENABLE); I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_Cmd(I2C1, ENABLE); I2C_ITConfig(I2C1, I2C_IT_ERR , ENABLE); return p; }
/************************************************************* * 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 }