cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C, IsDeviceReady Error

SKvan.1
Associate

Trying to communicate over I2C but keep getting error when using HAL_I2C_IsDeviceReady(). I am using a device from acconeer (XM132/XE132) which has MCU (STM32G071CBU6). Im gonna communicate the radar on the device (A111). Datasheets for the XM132 device: https://developer.acconeer.com/download/xm132-module-software-user-guide-pdf/

https://developer.acconeer.com/download/xm132-datasheet-pdf/

I cant figure out why it keeps giving me error, when i have tried the possible addresses. Can anyone spot something wrong, could it be the configuration? I think the right address should be 0x51, left shifted, but have also used counter to check. Most of the code is generated from CubeMX.

int main(void)

{

 /* USER CODE BEGIN 1 */

// HAL_StatusTypeDef ret;

// uint8_t buf[12];

// int16_t val;

 /* 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_DMA_Init();

 MX_I2C1_Init();

 MX_RTC_Init();

 MX_SPI1_Init();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 MX_DAC1_Init();

 /* USER CODE BEGIN 2 */

 HAL_I2C_MspInit(&hi2c1);

 HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

 uint16_t counter;

 HAL_StatusTypeDef IDR_Result;

 uint8_t I2C_Address;

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

for(counter = 0 ; counter < 255; counter++){

IDR_Result = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(counter<<1), 3, 100);

if(IDR_Result == HAL_OK){

printf("DONE");

I2C_Address = counter;

}

}

  /* USER CODE BEGIN 3 */

  EnableBootLoader();

  // Delay in order to handle rampup of voltage from buck converter

  HAL_Delay(10);

  pin_state_t id_gpio1 = GetPinState(GPIOB, GPIO_PIN_13);

  printf("PB13: %s\n", id_gpio1 == PIN_STATE_LOW ? "LOW" : id_gpio1 == PIN_STATE_HIGH ? "HIGH" : "FLOAT");

  //acconeer_main(0, NULL);

  //while (1)

   //;

 }

 /* USER CODE END 3 */

}

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 = 0x10707DBC;

 hi2c1.Init.OwnAddress1 = 164;

 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();

 }

 /* USER CODE BEGIN I2C1_Init 2 */

 /* USER CODE END I2C1_Init 2 */

}

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hi2c->Instance==I2C1)

 {

 /* USER CODE BEGIN I2C1_MspInit 0 */

 /* USER CODE END I2C1_MspInit 0 */

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**I2C1 GPIO Configuration

  PB6  ------> I2C1_SCL

  PB7  ------> I2C1_SDA

  */

  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; //GPIO_SPEED_FREQ_LOW;

  GPIO_InitStruct.Alternate = GPIO_AF6_I2C1;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* Peripheral clock enable */

  __HAL_RCC_I2C1_CLK_ENABLE();

  /* I2C1 DMA Init */

  /* I2C1_RX Init */

  hdma_i2c1_rx.Instance = DMA1_Channel5;

  hdma_i2c1_rx.Init.Request = DMA_REQUEST_I2C1_RX;

  hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;

  hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

  hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

  hdma_i2c1_rx.Init.Mode = DMA_NORMAL;

  hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW;

  if (HAL_DMA_Init(&hdma_i2c1_rx) != HAL_OK)

  {

   Error_Handler();

  }

  __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx);

  /* I2C1_TX Init */

  hdma_i2c1_tx.Instance = DMA1_Channel6;

  hdma_i2c1_tx.Init.Request = DMA_REQUEST_I2C1_TX;

  hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;

  hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

  hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

  hdma_i2c1_tx.Init.Mode = DMA_NORMAL;

  hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW;

  if (HAL_DMA_Init(&hdma_i2c1_tx) != HAL_OK)

  {

   Error_Handler();

  }

  __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c1_tx);

  /* I2C1 interrupt Init */

  HAL_NVIC_SetPriority(I2C1_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(I2C1_IRQn);

 /* USER CODE BEGIN I2C1_MspInit 1 */

 /* USER CODE END I2C1_MspInit 1 */

 }

}

0 REPLIES 0