USB OTG与Stm32f4板上的I2c冲突

时间:2019-04-12 19:40:15

标签: c embedded i2c stm32f4discovery usb-otg

我有stm32f429发现板,并且遇到USB OTG HS和I2C3之间的冲突。我使用的是cdc usb库,它自己初始化时可以作为虚拟com端口正常工作,并且可以在Windows中进行通信。当初始化任何其他通信外围设备(例如I2C或USART)时,usb vcp在Windows中仍然可见,但是我无法使用腻子或Realterm连接。这是USB初始化和I2C初始化代码。当I2C被注释掉时,通讯正常进行:

void MX_USB_DEVICE_Init(void)
{
  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

  /* USER CODE END USB_DEVICE_Init_PreTreatment */

  /* Init Device Library, add supported class and start the library. */
  if (USBD_Init(&hUsbDeviceHS, &HS_Desc, DEVICE_HS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_RegisterClass(&hUsbDeviceHS, &USBD_CDC) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_CDC_RegisterInterface(&hUsbDeviceHS, &USBD_Interface_fops_HS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_Start(&hUsbDeviceHS) != USBD_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

  /* USER CODE END USB_DEVICE_Init_PostTreatment */
}

static void MX_I2C3_Init(void)
{

  /* USER CODE BEGIN I2C3_Init 0 */

  /* USER CODE END I2C3_Init 0 */

  /* USER CODE BEGIN I2C3_Init 1 */

  /* USER CODE END I2C3_Init 1 */
  hi2c3.Instance = I2C3;
  hi2c3.Init.ClockSpeed = 100000;
  hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c3.Init.OwnAddress1 = 0;
  hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c3.Init.OwnAddress2 = 0;
  hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter 
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter 
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C3_Init 2 */

  /* USER CODE END I2C3_Init 2 */

}

尽管这是一个ISR优先级问题,所以我为两个外围设备尝试了不同的ISR优先级,但均未成功。另外,我没有明确为I2C外设启用ISR。 HAL初始化例程中发生了一些事情,但我一直无法理解。

还有其他人经历过吗?

1 个答案:

答案 0 :(得分:0)

对于感兴趣的任何人,Windows都会获取并设置用于USB通信的cdc线路编码的已知问题,此问题已在此处解决:

https://github.com/Ant1882/STM32F429-Tracealyzer-Demo/commit/4cf6591b3bdff098292349874f8c0c8df7802986#diff-ecfa63e2067d22f9ae74145176a1b736R283