AnsweredAssumed Answered

I2C startup

Question asked by E P on Jun 8, 2017
Latest reply on Apr 2, 2018 by Marco Nehmelman



OK so just for the record, I guess, I replaced HAL_I2C_Master_Transmit_DMA() with HAL_I2C_Mem_Write(), which seems to be the appropriate command for what I'm doing, and that is working fine.


One question I still have: first I tried HAL_I2C_Mem_Write_DMA(), and what I found is that it would send the address but not the data, one time, and then never work again until the board was power cycled.  I'm not terribly distraught about it, because I'm not sending a lot of data, but has anyone experienced that behavior?  I'd like to understand it.




Original post


Hi all,


I have a silly I2C question.  I'm using base code generated by STM32CubeMX (v4.21) for the STM32F427 (firmware 1.14).  Among other things, it sets up one I2C interface.  The generated MX_I2C1_Init looks like this:



/* I2C1 init function */
void MX_I2C1_Init(void)

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)



... it seems to run, and when the function gets called the SDA and SCL lines float high, so that seems fine.


Then later I try to actually use it.  Condensed code:

uint8_t ucPacket[2];

ucPacket[0] = 0x0;      // Register
ucPacket[1] = 0x40;     // Command

while (HAL_I2C_Master_Transmit_DMA(&hi2c1, LP5521_ADDR_WRITE, (uint8_t *)ucPacket, 2) != HAL_OK)
      if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)


the while() bit is plagiarized from some example code in the Cube package (I2C_TwoBoards_ComDMA).


When HAL_I2C_Master_Transmit_DMA, the SDA and SCL lines get pulled low, then just stay there forever.  There are half a dozen HAL_I2C_Master_Transmit_DMA lines after this one, and all of them return HAL_OK, then the program continues without a hiccup, and the SDA/SCL lines stay low until I reboot the board.


Is there something that needs to get run to kick off the transfer?  Searching the web, I see in older (pre-HAL?) code snippets a I2C_GenerateSTART() function, but that doesn't seem to exist in the generated code, nor in the example code provided with Cube.


Any hints on what might be going on here?