Ни в какую не хотят приниматься данные, виснет после генерации START в цикле опроса флага:
Код
/* Generate the Start Condition */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on I2C2 EV5 and clear it */
timeout = 10000; /* Initialize timeout value */
while (!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB))
{
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) return 0xFF;
}
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on I2C2 EV5 and clear it */
timeout = 10000; /* Initialize timeout value */
while (!I2C_GetFlagStatus(I2C2, I2C_FLAG_SB))
{
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) return 0xFF;
}
Вот код инициализации периферии:
CODE
#define I2Cx I2C2 //Selected I2C peripheral
#define RCC_APB1Periph_I2Cx RCC_APB1Periph_I2C2 //Bus where the peripheral is connected
#define RCC_AHB1Periph_GPIO_SCL RCC_AHB1Periph_GPIOH //Bus for GPIO Port of SCL
#define RCC_AHB1Periph_GPIO_SDA RCC_AHB1Periph_GPIOH //Bus for GPIO Port of SDA
#define GPIO_AF_I2Cx GPIO_AF_I2C2 //Alternate function for GPIO pins
#define GPIO_SCL GPIOH
#define GPIO_SDA GPIOH
#define GPIO_Pin_SCL GPIO_Pin_4
#define GPIO_Pin_SDA GPIO_Pin_5
#define GPIO_PinSource_SCL GPIO_PinSource4
#define GPIO_PinSource_SDA GPIO_PinSource5
void I2C_LowLevel_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
//Enable the i2c
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE);
//Reset the Peripheral
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, DISABLE);
//Enable the GPIOs for the SCL/SDA Pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SCL | RCC_AHB1Periph_GPIO_SDA, ENABLE);
//Configure and initialize the GPIOs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIO_SCL, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SDA;
GPIO_Init(GPIO_SDA, &GPIO_InitStructure);
//Connect GPIO pins to peripheral
GPIO_PinAFConfig(GPIO_SCL, GPIO_PinSource_SCL, GPIO_AF_I2Cx);
GPIO_PinAFConfig(GPIO_SDA, GPIO_PinSource_SDA, GPIO_AF_I2Cx);
//Configure and Initialize the I2C
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; //We are the master. We don't need this
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000; //400kHz (Fast Mode) (
//Initialize the Peripheral
I2C_Init(I2Cx, &I2C_InitStructure);
// I2C Peripheral Enable
I2C_Cmd(I2Cx, ENABLE);
return;
}
#define RCC_APB1Periph_I2Cx RCC_APB1Periph_I2C2 //Bus where the peripheral is connected
#define RCC_AHB1Periph_GPIO_SCL RCC_AHB1Periph_GPIOH //Bus for GPIO Port of SCL
#define RCC_AHB1Periph_GPIO_SDA RCC_AHB1Periph_GPIOH //Bus for GPIO Port of SDA
#define GPIO_AF_I2Cx GPIO_AF_I2C2 //Alternate function for GPIO pins
#define GPIO_SCL GPIOH
#define GPIO_SDA GPIOH
#define GPIO_Pin_SCL GPIO_Pin_4
#define GPIO_Pin_SDA GPIO_Pin_5
#define GPIO_PinSource_SCL GPIO_PinSource4
#define GPIO_PinSource_SDA GPIO_PinSource5
void I2C_LowLevel_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
//Enable the i2c
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE);
//Reset the Peripheral
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, DISABLE);
//Enable the GPIOs for the SCL/SDA Pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SCL | RCC_AHB1Periph_GPIO_SDA, ENABLE);
//Configure and initialize the GPIOs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIO_SCL, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SDA;
GPIO_Init(GPIO_SDA, &GPIO_InitStructure);
//Connect GPIO pins to peripheral
GPIO_PinAFConfig(GPIO_SCL, GPIO_PinSource_SCL, GPIO_AF_I2Cx);
GPIO_PinAFConfig(GPIO_SDA, GPIO_PinSource_SDA, GPIO_AF_I2Cx);
//Configure and Initialize the I2C
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; //We are the master. We don't need this
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000; //400kHz (Fast Mode) (
//Initialize the Peripheral
I2C_Init(I2Cx, &I2C_InitStructure);
// I2C Peripheral Enable
I2C_Cmd(I2Cx, ENABLE);
return;
}