经过验证的正常运行的初始化程序
void I2CMasterInit(void)
{
// I2C_DeInit(I2C_MASTER);
I2CMaster_RCC_Configuration();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);// enable GPIOB CLOCK
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Reset sEE_I2C IP */
RCC_APB1PeriphResetCmd(I2C_MASTER_CLK, ENABLE);
/* Release reset signal of sEE_I2C IP */
RCC_APB1PeriphResetCmd(I2C_MASTER_CLK, DISABLE);
I2CMaster_GPIO_Configuration();
// I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;//;I2C_Mode_I2C, very importan ,or I2C can not be work
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//for fast model
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_MSATER_SPEED;
// I2CMaster_NVIC_Configuration();
/*!< Enable SMBus EVT interrupt */
I2C_Cmd(I2C_MASTER, ENABLE);
I2C_Init(I2C_MASTER, &I2C_InitStructure);
I2CMaster_NVIC_Configuration();
I2C_ITConfig(I2C_MASTER, I2C_IT_EVT | I2C_IT_ERR| I2C_IT_BUF, ENABLE);//| I2C_IT_ERR very importan ,or I2C can not be work
}
void I2CMaster_RCC_Configuration(void)
{
/* Enable peripheral clocks --------------------------------------------------*/
RCC_APB1PeriphClockCmd(I2C_MASTER_CLK, ENABLE);
}
void I2CMaster_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// I2C I0口初始化.
// Configure I2C1 pins: SCL and SDA ---------------------------------------
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
// GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SCL | I2C_MASTER_SDA;
//CONFIG scl
GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2C_MASTER_GPIO, &GPIO_InitStructure);
//CONFIG SDA
GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SDA;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2C_MASTER_GPIO, &GPIO_InitStructure);
}
void I2CMaster_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 1 bit for pre-emption priority, 3 bits for subpriority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Configure and enable I2CMASTER interrupt --------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = I2C_MASTER_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVICPreemptionPriority_I2CMASTER; //same as mido
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVICSubPriorityPriority_I2CMASTER_EV;
NVIC_Init(&NVIC_InitStructure);
/* Configure and enable SI2CMASTER interrupt --------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = I2C_MASTER_ER_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVICPreemptionPriority_I2CMASTER; //same as mido
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVICSubPriorityPriority_I2CMASTER_ER;
NVIC_Init(&NVIC_InitStructure);
} |