cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 I2c won't send data

fontys_student
Associate II

I am trying to get to use I2C on my stm32L4R5ZIT-P.

i am trying to configure a sensor by sending data but the I2C pins wont give a signal?

i dont ude interups or DMA

these are the pins that i use and the setting for I2C:

fontys_student_2-1705416583285.png

 

fontys_student_1-1705416526079.png

this are the includes i use

fontys_student_3-1705416909107.png

this is my code. i don't have any errors or warnings in the code

fontys_student_4-1705416964693.png

how do i fix it and make shure i can send data out?

 

7 REPLIES 7
TDK
Guru

Does HAL_I2C_IsDeviceReady return HAL_OK? Do your other functions return HAL_OK?

If you feel a post has answered your question, please click "Accept as Solution".
Bob S
Principal

What do you mean "won't give a signal"?  Are you looking at the SDA and SCL lines with a scope or logic analyzer?

"I don't have any errors or warnings in the code" -> means no compiler errors or warning.  You need to check the return values from the HAL calls. Do you call MX_I2C2_Init() somewhere before the code you posted?

Is this a custom board or one of the ST Nucleo (or Disco?) boards?  Are there external pull-up resistors on the SCL/SDA lines?  Using only the STM32's internal pull-ups MAY work for testing but should not be relied on in a production device - they are too high a resistance.

Also, when posting code, please don't post images, copy/paste the code into the forum message using the code tags (click on the "..." button then "</>").

i am new to the stm platform. where can i see if the functions give a HAL_OK


@fontys_student wrote:

 where can i see if the functions give a HAL_OK


It will be the return value from the function call; eg, 

HAL_StatusTypeDef result; // for the return value from HAL function calls

result = HAL_I2C_IsDeviceReady( &hi2c2, 0x40<<1, 10, 1000 );

 

BTW: rather than writing `0x40<<1` as a Magic Number, it would be better style to give it a meaningful name; eg,

#define SENSOR_I2C_ADDRESS (0x40<<1)

then you can write:

 

result = HAL_I2C_IsDeviceReady( &hi2c2, SENSOR_I2C_ADDRESS, 10, 1000 );

 

Which is not only clearer & easier to understand, but also easier to maintain; eg, if you find that the address needs changing...

 

when i try do this i get a HAL_busy back

this is my full code. i use the internal pullup of the stm 32

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32L4xx_hal_i2c.h"
#include <string.h>
#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C2_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C2_Init();
  /* USER CODE BEGIN 2 */
if (HAL_I2C_EnableListen_IT(&hi2c2) != HAL_OK)
{
	Error_Handler();
}




  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  //configuration reset
while (1)
  {
	HAL_StatusTypeDef result; // for the return value from HAL function calls

	result = HAL_I2C_IsDeviceReady( &hi2c2, 0x40<<1, 10, 1000 );
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */


uint8_t confReset[2];
confReset[0] = 0x80;
confReset[1] = 0x00;

uint8_t conf;
conf = 0x00;

uint8_t confVal[2];
		  confVal[0] = 0x39;
		  confVal[1] = 0x9F;

uint8_t cal;
cal = 0x05;

uint8_t calVal[2];
calVal[0] = 0x57;
calVal[1] = 0x62;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//configuration reset
HAL_I2C_Master_Transmit(&hi2c2, 0x40<<1, confReset, 2, 10);
//configuration setup
HAL_I2C_Mem_Write (&hi2c2, 0x40<<1, conf, 1, confVal, 2, 10);
HAL_I2C_Mem_Write (&hi2c2, 0x40<<1, cal, 1, calVal, 2, 10);

	  	  // HAL_I2C_Mem Write (hi2c, (0x40<<1), 32768, 16, addata, Size, Timeout);
	  	  // HAL I2C_Mem Read (hi2c, DevAddress, MemAddress, MemAddSize, pData, Size, Timeout);

	      /* USER CODE END WHILE */

	    /* USER CODE END 3 */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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 = 16;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  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_3) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief I2C2 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_I2C2_Init(void)
{

  /* USER CODE BEGIN I2C2_Init 0 */

  /* USER CODE END I2C2_Init 0 */

  /* USER CODE BEGIN I2C2_Init 1 */

  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.Timing = 0x10707DBC;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C2_Init 2 */

  /* USER CODE END I2C2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
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) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

TDK
Guru

Start with blocking functions. Call HAL_I2C_IsDeviceReady and ensure it returns HAL_OK before calling any other I2C function. Don't call HAL_I2C_EnableListen_IT at the start.

If you feel a post has answered your question, please click "Accept as Solution".