cancel
Showing results for 
Search instead for 
Did you mean: 

Why I2C communication fails between Ilps22qs and my nucleo?

Zenk91
Associate II

I am working with STM32nucleoL476RG on a code able to establish an I2C communication between the pressure sensor Ilps22qs and my board. I am using the Qvar functionality, so I followed this pseudocode as reported on Qvar documentation:

  • Write(0x10, 0x08); // ODR set to 1 Hz
  • Write(0x12, 0x80); // QVAR_enable

And then reading on register 2Ah, 29h, 28h to read the stored data:

  • MultiRead(0x28, (uint8_t*)&qvar_out, 3)

As you can see on my code reported below, I used the HAL library functions HAL_I2C_Mem_Write() and HAL_I2C_Mem_Read() to perform write and read operations and then HAL uart functions to send the data on a terminal. The code builds without errors but when I run it nothing happens. The trouble is related only on the I2C part, I am sure because I tested the uart by sending some characters and it works correctly. I tried to execute a debug, I noticed that when I arrive on the first I2C function the program gets in stuck.

What is wrong in my code? Thank you very much.

#include "main.h"
#define devAddress0 (0x5C<<1)
 
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
 
int main(void)
{
  char val[3];
 
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
 
  while (1)
  {
 
	  // ODR set at 1 Hz
	  HAL_I2C_Mem_Write(&hi2c1, devAddress0, 0x10, 1, 0x08, 1, HAL_MAX_DELAY);
 
	  // Qvar enabled
	  HAL_I2C_Mem_Write(&hi2c1, devAddress0, 0x12, 1, 0x80, 1, HAL_MAX_DELAY);
 
	  // Data reading
	  HAL_I2C_Mem_Read(&hi2c1, devAddress0, 0x2A, 1, val[0], 1, HAL_MAX_DELAY);
	  HAL_I2C_Mem_Read(&hi2c1, devAddress0, 0x29, 1, val[1], 1, HAL_MAX_DELAY);
	  HAL_I2C_Mem_Read(&hi2c1, devAddress0, 0x28, 1, val[2], 1, HAL_MAX_DELAY);
 
	  HAL_UART_Transmit(&huart2, " ", 1, HAL_MAX_DELAY);
 
	  HAL_UART_Transmit(&huart2, (uint8_t*)val[1], 1, HAL_MAX_DELAY);
	  HAL_UART_Transmit(&huart2, " ", 1, HAL_MAX_DELAY);
 
	  HAL_UART_Transmit(&huart2, (uint8_t*)val[2], 1, HAL_MAX_DELAY);
 
	  HAL_UART_Transmit(&huart2, "\n", 1, HAL_MAX_DELAY);
	  HAL_UART_Transmit(&huart2, "\r", 1, HAL_MAX_DELAY);
 
  }
 
}
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}
 
static void MX_I2C1_Init(void)
{
 
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x10909CEC;
  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();
  }
 
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
 
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
static void MX_USART2_UART_Init(void)
{
 
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
 
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
 
  GPIO_InitStruct.Pin = LD2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
 
}
 
void Error_Handler(void)
{
 
  __disable_irq();
  while (1)
  {
 
}
 
#ifdef  USE_FULL_ASSERT
 
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
}
#endif

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

> What is wrong in my code?

Not checking the result returned by HAL_I2C_Mem_Read, HAL_I2C_Mem_Write etc.

These return a value that should be checked, even though it's boring.

The UART functions also return interesting values.

View solution in original post

4 REPLIES 4
Pavel A.
Evangelist III

> What is wrong in my code?

Not checking the result returned by HAL_I2C_Mem_Read, HAL_I2C_Mem_Write etc.

These return a value that should be checked, even though it's boring.

The UART functions also return interesting values.

Excuse me if my question sounds too much trivial, but I am new with this platform. Where can I check the result returned by these functions?

HAL_StatusTypeDef st;
 
st = HAL_I2C_Mem_Write(...);
if (st != HAL_OK) {
   // oops. why? what?
}

Something like that.

Foued_KH
ST Employee

Hello @Zenk91​,

Please check the results via an oscilloscope :

  • Write :

0693W00000Y8uRKQAZ.png 

  • Read :

0693W00000Y8uNxQAJ.pngIf nothings appears, please check the error flags set in the status register.

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.