cancel
Showing results for 
Search instead for 
Did you mean: 

BlueNRG-LP I2C1 and I2C2 coexistence

Danny0007
Senior

hello community,

I´m using PB10/PB11 as I2C1 and PB6/PB7 as I2C2.

Danny0007_0-1706436538253.png

I2C1 works correctly.

But I2C2 not, after reset on PB6 and PB7 I just get some messy signals as below, It seems the I2C2 is not initialized with succcess.

Danny0007_1-1706436961888.png

Actually the both I2C are initialized in the same way:

Here is the pin definition for both I2C

//sensor1
#define BSP_I2C                                   I2C1
#define BSP_I2C_CLK_ENABLE()                      LL_APB1_EnableClock(LL_APB1_PERIPH_I2C1)
#define BSP_I2C_CLK_DISABLE()                     LL_APB1_DisableClock(LL_APB1_PERIPH_I2C1)

#define BSP_I2C_DATA_PIN                          LL_GPIO_PIN_10
#define BSP_I2C_DATA_GPIO_PORT                    GPIOB
#define BSP_I2C_DATA_GPIO_PULL                    LL_GPIO_PULL_NO
#define BSP_I2C_DATA_GPIO_AF()                    LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_10, LL_GPIO_AF_2)
#define BSP_I2C_DATA_GPIO_CLK_ENABLE()            LL_AHB_EnableClock(LL_AHB_PERIPH_GPIOB)
#define BSP_I2C_DATA_GPIO_CLK_DISABLE()           LL_AHB_DisableClock(LL_AHB_PERIPH_GPIOB)

#define BSP_I2C_CLK_PIN                           LL_GPIO_PIN_11
#define BSP_I2C_CLK_GPIO_PORT                     GPIOB
#define BSP_I2C_CLK_GPIO_PULL                     LL_GPIO_PULL_NO
#define BSP_I2C_CLK_GPIO_AF()                     LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_11, LL_GPIO_AF_2)
#define BSP_I2C_CLK_GPIO_CLK_ENABLE()             LL_AHB_EnableClock(LL_AHB_PERIPH_GPIOB)
#define BSP_I2C_CLK_GPIO_CLK_DISABLE()            LL_AHB_DisableClock(LL_AHB_PERIPH_GPIOB)

//sensor2
#define BSP_I2C2                                   I2C2
#define BSP_I2C2_CLK_ENABLE()                      LL_APB1_EnableClock(LL_APB1_PERIPH_I2C2)
#define BSP_I2C2_CLK_DISABLE()                     LL_APB1_DisableClock(LL_APB1_PERIPH_I2C2)

#define BSP_I2C2_DATA_PIN                          LL_GPIO_PIN_7
#define BSP_I2C2_DATA_GPIO_PORT                    GPIOB
#define BSP_I2C2_DATA_GPIO_PULL                    LL_GPIO_PULL_NO
#define BSP_I2C2_DATA_GPIO_AF()                    LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_7, LL_GPIO_AF_0)
#define BSP_I2C2_DATA_GPIO_CLK_ENABLE()            LL_AHB_EnableClock(LL_AHB_PERIPH_GPIOB)
#define BSP_I2C2_DATA_GPIO_CLK_DISABLE()           LL_AHB_DisableClock(LL_AHB_PERIPH_GPIOB)

#define BSP_I2C2_CLK_PIN                           LL_GPIO_PIN_6
#define BSP_I2C2_CLK_GPIO_PORT                     GPIOB
#define BSP_I2C2_CLK_GPIO_PULL                     LL_GPIO_PULL_NO
#define BSP_I2C2_CLK_GPIO_AF()                     LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_6, LL_GPIO_AF_0)
#define BSP_I2C2_CLK_GPIO_CLK_ENABLE()             LL_AHB_EnableClock(LL_AHB_PERIPH_GPIOB)
#define BSP_I2C2_CLK_GPIO_CLK_DISABLE()            LL_AHB_DisableClock(LL_AHB_PERIPH_GPIOB)

Here is the init function for both I2C

//sensor1

void BSP_I2C_Init(void)
{
  /* Initialize the GPIOs associated to the I2C port */
  BSP_I2C_DATA_GPIO_CLK_ENABLE();
  LL_GPIO_SetPinMode(BSP_I2C_DATA_GPIO_PORT, BSP_I2C_DATA_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetPinSpeed(BSP_I2C_DATA_GPIO_PORT, BSP_I2C_DATA_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(BSP_I2C_DATA_GPIO_PORT, BSP_I2C_DATA_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  LL_GPIO_SetPinPull(BSP_I2C_DATA_GPIO_PORT, BSP_I2C_DATA_PIN, BSP_I2C_DATA_GPIO_PULL);
  BSP_I2C_DATA_GPIO_AF();

  BSP_I2C_CLK_GPIO_CLK_ENABLE();
  LL_GPIO_SetPinMode(BSP_I2C_CLK_GPIO_PORT, BSP_I2C_CLK_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetPinSpeed(BSP_I2C_CLK_GPIO_PORT, BSP_I2C_CLK_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(BSP_I2C_CLK_GPIO_PORT, BSP_I2C_CLK_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  LL_GPIO_SetPinPull(BSP_I2C_CLK_GPIO_PORT, BSP_I2C_CLK_PIN, BSP_I2C_CLK_GPIO_PULL);
  BSP_I2C_CLK_GPIO_AF();

  /* Initialize the I2C clock */
  BSP_I2C_CLK_ENABLE();

  LL_I2C_Disable(BSP_I2C);

  /* Configure the SDA setup, hold time and the SCL high, low period
   * For Fast-mode 400 kHz, PRESC | 0h | SCLDEL | SDADEL | SCLH | SCLL
   *                          1h  | 0h |    3h  |   2h   |  03h |  09h
   */
  LL_I2C_SetTiming(BSP_I2C, 0x10320309);

  /* Enable Clock stretching */
  LL_I2C_EnableClockStretching(BSP_I2C);

  /* Enable Peripheral in I2C mode */
  LL_I2C_SetMode(BSP_I2C, LL_I2C_MODE_I2C);

  /* Enable the I2C peripheral */
  LL_I2C_Enable(BSP_I2C);

  /* Enable I2C transfer complete/error interrupts:
   *  - Enable Receive Interrupt
   *  - Enable Not acknowledge received interrupt
   *  - Enable Error interrupts
   *  - Enable Stop interrupt
   */
//  LL_I2C_EnableIT_TX(BSP_I2C);
//  LL_I2C_EnableIT_RX(BSP_I2C);
//  LL_I2C_EnableIT_TC(BSP_I2C);
//  LL_I2C_EnableIT_NACK(BSP_I2C);
//  LL_I2C_EnableIT_ERR(BSP_I2C);
//  LL_I2C_EnableIT_STOP(BSP_I2C);
}
 
//sensor2
void BSP_I2C2_Init(void)
{
  /* Initialize the GPIOs associated to the I2C port */
  BSP_I2C2_DATA_GPIO_CLK_ENABLE();
  LL_GPIO_SetPinMode(BSP_I2C2_DATA_GPIO_PORT, BSP_I2C2_DATA_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetPinSpeed(BSP_I2C2_DATA_GPIO_PORT, BSP_I2C2_DATA_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(BSP_I2C2_DATA_GPIO_PORT, BSP_I2C2_DATA_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  LL_GPIO_SetPinPull(BSP_I2C2_DATA_GPIO_PORT, BSP_I2C2_DATA_PIN, BSP_I2C2_DATA_GPIO_PULL);
  BSP_I2C2_DATA_GPIO_AF();
  BSP_I2C2_CLK_GPIO_CLK_ENABLE();
  LL_GPIO_SetPinMode(BSP_I2C2_CLK_GPIO_PORT, BSP_I2C2_CLK_PIN, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetPinSpeed(BSP_I2C2_CLK_GPIO_PORT, BSP_I2C2_CLK_PIN, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinOutputType(BSP_I2C2_CLK_GPIO_PORT, BSP_I2C2_CLK_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
  LL_GPIO_SetPinPull(BSP_I2C2_CLK_GPIO_PORT, BSP_I2C2_CLK_PIN, BSP_I2C2_CLK_GPIO_PULL);
  BSP_I2C2_CLK_GPIO_AF();
  /* Initialize the I2C clock */
  BSP_I2C2_CLK_ENABLE();

  LL_I2C_Disable(BSP_I2C2);
  /* Configure the SDA setup, hold time and the SCL high, low period
   * For Fast-mode 400 kHz, PRESC | 0h | SCLDEL | SDADEL | SCLH | SCLL
   *                          1h  | 0h |    3h  |   2h   |  03h |  09h
   */
  LL_I2C_SetTiming(BSP_I2C2, 0x10320309);
  /* Enable Clock stretching */
  LL_I2C_EnableClockStretching(BSP_I2C2);
  /* Enable Peripheral in I2C mode */
  LL_I2C_SetMode(BSP_I2C2, LL_I2C_MODE_I2C);
  /* Enable the I2C peripheral */
  LL_I2C_Enable(BSP_I2C2);
  /* Enable I2C transfer complete/error interrupts:
   *  - Enable Receive Interrupt
   *  - Enable Not acknowledge received interrupt
   *  - Enable Error interrupts
   *  - Enable Stop interrupt
   */
//  LL_I2C_EnableIT_TX(BSP_I2C);
//  LL_I2C_EnableIT_RX(BSP_I2C);
//  LL_I2C_EnableIT_TC(BSP_I2C);
//  LL_I2C_EnableIT_NACK(BSP_I2C);
//  LL_I2C_EnableIT_ERR(BSP_I2C);
//  LL_I2C_EnableIT_STOP(BSP_I2C);
}
 
***********************************************************************
any idea?
thanks in advance.

Danny

10 REPLIES 10
Rene Lenerve
ST Employee

Hi Danny0007,

Great that it's working now.

For BlueNRG-LP and CubeMX, sorry for the confusion, this MCU is not available in cubeMX I was merely thinking about the timing computation for the I²C, but as the peripheral clock is fixed for I²C, there is no need to recompute timings, those provided in the Reference Manual are sufficient.

Hope this can help you

Kind Regards.