Skip to main content
Associate
October 18, 2023
Solved

HAL_I2C_Mem_Read get stuck on the line

Hi,

I am using NUCLEO stm32f030r8 to communicate with LIS3DH accelerometer, connection is in such a way that

vcc - 3v3

GND - GND

SCL/D15 - SCL

SDA/D14 - SDA

I want to read from who iam i register 0x0F, we use sparkfun board hence we checked SDO pin is giving 3v3

so the dev_addr is given 0x19

it get stuck in the line HAL_I2C_Mem_Read(&hi2c1, dev_addr, reg_adr, I2C_MEMADD_SIZE_8BIT, &who_am_i, 1, HAL_MAX_DELAY); it means something is not correct kindly help me to solve the issue

code : 

 

 

uint8_t dev_addr = 0x19<<1;
 
static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E; //0x0x00707CBB
  hi2c1.Init.OwnAddress1 = 0;
  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();
  }
 
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
uint8_t status=0;
uint8_t reg_adr=0x0F;
uint8_t who_am_i=0;
status = HAL_I2C_Mem_Read(&hi2c1, dev_addr, reg_adr, I2C_MEMADD_SIZE_8BIT, &who_am_i, 1, HAL_MAX_DELAY);
if(status!=HAL_OK)
{
sprintf(disp_buff,"MEM_READ_FAILED");
print_scrn(disp_buff);
}
sprintf(disp_buff,"who_IM_I: %x",who_am_i);
print_scrn(disp_buff);
    This topic has been closed for replies.
    MidhunBest answer
    Associate
    October 18, 2023

    Hii, 

    Thank you for your support have solved the issue slight confusion between the pins had made the issue the software automatically selects PB7 and PB6 but the sda and scl pin in the board is PB8 and PB9

    4 replies

    Foued_KH
    ST Employee
    October 18, 2023

    Hello @Midhun , 

    Please make sure that you are using the correct address.
    You can connect an oscilloscope to check SDA and SCL behavior .

    Foued

    To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    MidhunAuthor
    Associate
    October 18, 2023

    Can you please guide me to choose the correct i2c address, Im using LIS3DH sparkfun board SDO PIN is not  connected (kept open) only VCC SDA SCL is connected. So i'm currently using (0x18<<1) as an i2c address for HAL_I2C_Mem_Read function.

    Foued_KH
    ST Employee
    October 18, 2023

    I think you should check this table : ( DS : https://www.st.com/resource/en/datasheet/lis3dh.pdf )

    Foued_KH_0-1697624933983.png
    Foued

     

    To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    MidhunAuthorBest answer
    Associate
    October 18, 2023

    Hii, 

    Thank you for your support have solved the issue slight confusion between the pins had made the issue the software automatically selects PB7 and PB6 but the sda and scl pin in the board is PB8 and PB9