cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Clock held low by STM32? STM32 is slave.

mattreed9
Associate II
Posted on July 11, 2016 at 20:28

I've programmed several I2C applications, but never as the slave.

Using an STM32F031 Nucleo-32 board, connected to a Linux system with other I2C devices.

I would post code examples, but at this point the only code that exists is the I2C initialization as produced by CUBE32 (NONE of my code, except for code to flash the LED).

At first, when I hooked up the STM32 the Linux I2C bus would basically halt (timeout on every read).

I hooked up the scope and found that the STM32 is pulling the clock line low(?)

A closer look revealed that the clock line would be high, until there was traffic on the bus, then it would pull it low(?).

I have no idea why this would be, there are no break-points and clock-stretching is off.

Here is the init code, as produced by CUBE32:

----------------------------------------------

  hi2c1.Instance = I2C1;

  hi2c1.Init.Timing = 0x2000090E;

  hi2c1.Init.OwnAddress1 = 16;

  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

  hi2c1.Init.OwnAddress2 = 0;

  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;

  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

  if (HAL_I2C_Init(&hi2c1) != HAL_OK)

  {

    Error_Handler();

  }

    /**Configure Analogue filter 

    */

  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)

  {

    Error_Handler();

  }

----------------------------------------------

And here is my LED blink code:

----------------------------------------------

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 if (0 == (HAL_GetTick() & 0x1FF))

 {

   if (0 == been_set)

   {

     been_set = 1;

     HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);

   }

 }

 else

 {

   been_set = 0;

 }

  }

  /* USER CODE END 3 */

----------------------------------------------

Any ideas? Anyone?

Thanks!

-Matt

#nucleo #stm32f031 #nucleo32
1 REPLY 1
mattreed9
Associate II
Posted on July 11, 2016 at 22:59

Found the problem:

----------------------------------------------

  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

----------------------------------------------

This means ''NoStretch'' is disabled, i.e. stretch is enabled.

Since I had programmed no messages to be sent out, the clock was being held low to stretch the response time.

By setting 

----------------------------------------------

  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;

----------------------------------------------

I have turned off clock stretching and it does not happen anymore.

-Matt