Question
stm32f407vgt - I2c2 BUSY flag hangs from after RCC enabled and wont be cleared
Posted on August 11, 2014 at 19:24
Hi
First of all, I know there are other threads herein which deals with the I2Cx BUSY flag problem, and I have tried the solutions suggested in them to no relieve. Therefor I now, as a last resort opens a new thread about the same topic. The BUSY flag is set in the same moment as the I2C pheripheral clock is enabled in line 6 (RCC). The BUSY then remains set except for when i tried to do a pheripheral reset. As long as the reset was asserted BUSY was not set, but as soon it was released again BUSY was set again. The SCL and SDA pins are both high and remains high. I am not able to see any glitches using an oscilloscope. I am able to set or clr both pins by either doing it in sw or by shorting pins to ground so there are nothing keeping the bus high by force. I have also tried to generate both stop and start conditions on the bus, but the BUSY flag remains high. I have also tried to enable the I2C block before initlaising it to no use. I am stuck. Then I have also tried to reorder the order things are done, i.e. when to enable clocks and to initialize I2C befor GPIOs and such and many other not so smart variants youdt to have tried it. Its always the same result. As soon as the pheripheral clock for the I2C is turned on the BUSY flag goes on and remains on. I use many other features on the chip like DCIM and SPI and these works like expected, it's only the I2C2 that does not work right now. If you have the time and skill I would be very happy for an helping hand. Thanks in advance (see code below) Vidar (Z) The code I use for initialization is given below, but first the definition I use in the code and which gives you the GPIOs used and such (EDIT: Tagged my error in red, should read GPIO_Mode_AF)
// I2C interface
#define I2C_CHANNEL I2C2
#define I2C_AF GPIO_AF_I2C2
#define I2C_RCC RCC_APB1Periph_I2C2
#define I2C_GPIO_RCC RCC_AHB1Periph_GPIOB
#define I2C_PORT GPIOB
#define I2C_READ 0x01
#define I2C_WRITE 0x00
#define I2C_SCL_PIN GPIO_Pin_10 /* PB.10 - AF */
#define I2C_SCL_PORT GPIOB /* GPIOB */
#define I2C_SCL_RCC RCC_AHB1Periph_GPIOB
#define I2C_SCL_PIN_NR GPIO_PinSource10
#define I2C_SDA_PIN GPIO_Pin_11 /* PB.11 - AF */
#define I2C_SDA_PORT GPIOB /* GPIOB */
#define I2C_SDA_RCC RCC_AHB1Periph_GPIOB
#define I2C_SDA_PIN_NR GPIO_PinSource11
void
I2C_init(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(I2C_RCC, ENABLE);
RCC_AHB1PeriphClockCmd(I2C_GPIO_RCC, ENABLE);
/* Configure port pins used by I2C */
GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
I2C_PORT->BSRRL = I2C_SCL_PIN | I2C_SDA_PIN;
// Set high
/* Connect I2C dedicated port pins to use the alternative function */
GPIO_PinAFConfig(I2C_SCL_PORT, I2C_SCL_PIN_NR, I2C_AF);
GPIO_PinAFConfig(I2C_SDA_PORT, I2C_SDA_PIN_NR, I2C_AF);
/* Configure I2C */
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
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_Init(I2C_CHANNEL, &I2C_InitStructure);
// enable I2C_CHANNEL
I2C_Cmd(I2C_CHANNEL, ENABLE);
}
#i2c-busy-bit-set #stm32f4 #i2c #discovery