I2C Fast Mode on STM32L1 with BNO055

I am using stm32l1xx_i2c.c code to communicate over I2C from an STM32L1 to a BNO055. I have that communication working at 100 kHz.

I would now like to make that communication as fast as possible. My PCLK1 frequency is 32 MHz so I realize that I can’t reach the max frequency in fast mode on the BNO055 of 400 kHz but I would like to get as close to that as possible. Here is what I have so far:

GPIO_InitTypeDef GPIO_InitStructure;

I2C_InitTypeDef I2C_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // SDA, SCL

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2); //SCL first

GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

I2C_InitStructure.I2C_ClockSpeed = 400000;

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 = 0;

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_Init(I2C2, &I2C_InitStructure);

I2C_Cmd(I2C2, ENABLE);

I2C_StretchClockCmd(I2C2, ENABLE);

I2C_FastModeDutyCycleConfig(I2C2, I2C_DutyCycle_2);

Sorry for posting! I have found the solution to this problem. I was initializing I2C at the wrong point in my main.c function, so I would basically always fail whenever I tried to use my I2C_Write function. As a result, I had an error-handling function that would de-initialize and then re-initialize I2C at a different speed than I was expecting! I am so sorry for wasting your time and have found that even with a clock at 32 MHz (not a multiple of 10 MHz according to the STM32L1 datasheet), I can still reach 400 kHz just by setting the speed in I2C_InitStructure.I2C_ClockSpeed to 400000.