AnsweredAssumed Answered

Problems getting I2C pins configured on STM32F427 (Solved- dumb mistake)

Question asked by barrows.geoffrey on Aug 27, 2015
Latest reply on Aug 28, 2015 by Nesrine M
EDIT: Problem solved. When calling GPIO_PinAFConfig in lines 19 and 20 below, I used GPIO_Pin_# rather than the correct GPIO_PinSource#. All my other uses of AF peripherals was correct- I just messed this one up. It took me two long days of debugging to find that... #@#$&@#!!!


I am having problems getting the I2C2 pins configured on an STM32F427, specifically on pins B10 and B11. The initialization sequence is below. Note that GPIO clocks have already been enabled elsewhere before getting to this code.

I am using a board of my own design. Other peripherals used in this project (GPIOs, SPI, and UART) all work.

I have pull up resistors on the two I2C lines. I've tried both 2k and 10k with the same result. I stepped through the code to see what happens. When I reset the code, the I2C lines go high, as expected due to the pull up resistors. Then when I execute the two GPIO_Init(GPIOB,&GPIO_InitStructure) commands, the corresponding I2C line gets pulled down to 0. They never get released. If I run the code without stepping through, the result is the same.

What is bizarre is that I've used essentially the same initialization sequence on other projects boards using an STM32F405, so I don't know why they wouldn't work here. Is there something different about the '427? I am using version 1.5.1 of the peripheral libraries, with the proper #define for using the STM32F427. For development tools I am using IAR EWARM Cortex edition and an ST-Link v2 debugger.

I've also tried, line 14, using GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL with the same result.

Can anyone please provide an insight as to what is happening? Thank You in advance!


02.// I2C2 on pins B10 and B11
03.// Note: GPIO clocks have already been enabled elsewhere
04.// release reset and enable peripheral clock for I2C2
05.RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);   
06.RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
07.RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
08.// Step 1: Connect pins
09.//  set I2C pins
10.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
11.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
12.GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
13.GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
14.GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP; //GPIO_PuPd_NOPULL;
15.GPIO_Init(GPIOB, &GPIO_InitStructure); // B10 drops to 0 when this is executed
16.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
17.GPIO_Init(GPIOB, &GPIO_InitStructure); // B11 drops to 0 when this is executed
18.// Connect I2C pins to alternate function 5 (AF5)
19.GPIO_PinAFConfig(GPIOB, GPIO_Pin_10, GPIO_AF_I2C2);
20.GPIO_PinAFConfig(GPIOB, GPIO_Pin_11, GPIO_AF_I2C2);
22.// Step 2: initialize I2C peripheral   
23.// initialize I2C2 for fast mode
24.I2CInitSt.I2C_Mode =            I2C_Mode_I2C;
25.I2CInitSt.I2C_DutyCycle =       I2C_DutyCycle_2;    // high / low = 2
26.I2CInitSt.I2C_OwnAddress1 =     0x2C;           // set device address CHANGE
27.I2CInitSt.I2C_Ack =         I2C_Ack_Enable;     // enable ACK response
28.I2CInitSt.I2C_ClockSpeed =      400000;         // 400kHz (PCLK1 must be mult. of 10MHz)
29.I2CInitSt.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;   // 7-bit addressing
30.I2C_Init(I2C2, &I2CInitSt);
31.// set I2C2 options
32.//I2C_NACKPositionConfig(I2C2, I2C_NACKPosition_Current);       // CHECK
33.I2C_DMACmd(I2C2, DISABLE);                  // disable DMA
34.I2C_StretchClockCmd(I2C2, ENABLE);              // enable clock stretching
35.I2C_DualAddressCmd(I2C2, DISABLE);              // disable dual addressing
36.// enable I2C2 event and error interrupts (buffer interrupts will be enabled later)
37.// BUF: TxE / RxNE
38.// EVT: ADDR / STOPF / BTF (with no TxE/RxNE) / TxE / RxNE (if BUF, dual?)
40.I2C_ITConfig(I2C2, (I2C_IT_EVT | I2C_IT_ERR), ENABLE);
41.// enable I2C2
42.I2C_Cmd(I2C2, ENABLE);