/*stm32 iic³õʼ»¯*/ void pin_init1() { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* Enable GPIOB,E,F,G clock */ //DEBUG("power pin init 2\r\n"); RCC_ADCCLKConfig(RCC_PCLK2_Div4); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* Enable I2C2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Pin = BATS_I2C_SCL_PIN; GPIO_Init(BATS_I2C_SCL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_I2C_SDA_PIN; GPIO_Init(BATS_I2C_SDA_PORT, &GPIO_InitStructure); /* Config pin */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = BATS_I2C_SEL_PIN; GPIO_Init(BATS_I2C_SEL_PORT, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = BATS_SEL_C_PIN; // GPIO_Init(BATS_SEL_C_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_B_PIN; GPIO_Init(BATS_SEL_B_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_A_PIN; GPIO_Init(BATS_SEL_A_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAC_PIN; //GPIO_Init(BATS_SEL_STAC_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAB_PIN; GPIO_Init(BATS_SEL_STAB_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAA_PIN; GPIO_Init(BATS_SEL_STAA_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_CHARGE_CTL_PIN; //GPIO_Init(BATS_C_CHARGE_CTL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_ABC_CHARGE_FAULT_PIN; GPIO_Init(BATS_ABC_CHARGE_FAULT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_ABC_CHARGE_CHRG_PIN; GPIO_Init(BATS_ABC_CHARGE_CHRG_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PG_3V3_PIN; GPIO_Init(PG_3V3_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_CHARGE_STAT_PIN; GPIO_Init(BATS_A_CHARGE_STAT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_CHARGE_CTL_PIN; GPIO_Init(BATS_A_CHARGE_CTL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_CHARGE_STAT_PIN; GPIO_Init(BATS_B_CHARGE_STAT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_CHARGE_CTL_PIN; GPIO_Init(BATS_B_CHARGE_CTL_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_CHARGE_STAT_PIN; //GPIO_Init(BATS_C_CHARGE_STAT_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_SEL_C_PIN; //GPIO_Init(BATS_SEL_C_PORT, &GPIO_InitStructure); //ADC channel config GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Pin = BATS_AB_MON_I_PIN; GPIO_Init(BATS_AB_MON_I_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = V3P3_MON_V_PIN; GPIO_Init(V3P3_MON_V_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_V_MON_PIN; GPIO_Init(BATS_A_V_MON_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_V_MON_PIN; GPIO_Init(BATS_B_V_MON_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_V_MON_PIN; //GPIO_Init(BATS_C_V_MON_PORT, &GPIO_InitStructure); //I2C Config I2C_SoftwareResetCmd(I2C2,ENABLE); I2C_SoftwareResetCmd(I2C2,DISABLE); I2C_Cmd(I2C2, ENABLE); I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x79; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 50000; I2C_Init(I2C2, &I2C_InitStructure); //DEBUG("power pin init 1\r\n"); I2C_CalculatePEC(I2C2, ENABLE); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC3, &ADC_InitStructure); ADC_Cmd(ADC3, ENABLE); ADC_ResetCalibration(ADC3); while(ADC_GetResetCalibrationStatus(ADC3)); ADC_StartCalibration(ADC3); while(ADC_GetCalibrationStatus(ADC3)); //DEBUG("power pin init over\r\n"); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* Enable I2C1 and I2C2 ----------------------------------------------------*/ I2C_Cmd(I2C1, ENABLE); I2C_Cmd(I2C2, ENABLE); /* I2C1 configuration ------------------------------------------------------*/ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = ClockSpeed; I2C_Init(I2C1, &I2C_InitStructure); /* I2C2 configuration ------------------------------------------------------*/ I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7; I2C_Init(I2C2, &I2C_InitStructure); I2C_CalculatePEC(I2C1, ENABLE); I2C_CalculatePEC(I2C2, ENABLE); /* Enable I2C1 event and buffer interrupts */ I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF, ENABLE); /* Enable I2C1 event and buffer interrupts */ I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_BUF, ENABLE); /*----- Transmission Phase -------------------------------------------------*/ /* Set data direction to transmitter */ Direction = Transmitter; /* Send I2C1 START condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Wait until all data and the PEC value are received */ /* I2C2_Buffer_Rx buffer will contain the data plus the PEC value */ while(Rx2_Idx < Tx1BufferSize) { } /* Check the corectness of the I2C1 transmitted data */ TransferStatus1 = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, Tx1BufferSize); /* TransferStatus1 = PASSED, if the transmitted and received data are equal */ /* TransferStatus1 = FAILED, if the transmitted and received data are different */ /*----- Reception Phase --------------------------------------------------*/ /* Re-configure and enable I2C1 event interrupt to have the higher priority */ 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); /* Wait until end of Slave transmission */ while(Rx1_Idx < Tx2BufferSize) { } /* Check the corectness of the I2C1 received data */ TransferStatus2 = Buffercmp(I2C2_Buffer_Tx, I2C1_Buffer_Rx, Tx2BufferSize); /* TransferStatus2 = PASSED, if the transmitted and received data are equal */ /* TransferStatus2 = FAILED, if the transmitted and received data are different */ while(1) { } }
/************************************************************* * 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 }