简体   繁体   English

I2C 通信中的问题 - STM32(HAL 库)

[英]Issue in the I2C communication - STM32 (HAL Library)

I am facing an issue with the I2C communication.我正面临 I2C 通信问题。 During the first run the communication between the MCU and the sensor is okay then I can read and write in the sensor's registers without a problem.在第一次运行期间,MCU 和传感器之间的通信正常,然后我可以毫无问题地读写传感器的寄存器。

However, when I try to debug a second time I receive HAL_BUSY when I am trying to communicate with the sensor, the communication is just reestablished when the power supply of the board is cut and then fed again.但是,当我尝试第二次调试时,当我尝试与传感器通信时收到 HAL_BUSY,当电路板的电源被切断然后再次供电时,通信才重新建立。

The Code snippet to write to the register is the following:写入寄存器的代码片段如下:

HAL_StatusTypeDef MAX30105_WriteRegister(uint8_t reg_addr, uint8_t reg_value){
    HAL_StatusTypeDef returnValue = HAL_ERROR;
 
    //Check if the MAX30105 is ready in a trial of 5 times
    HAL_StatusTypeDef result = HAL_I2C_IsDeviceReady(&hi2c1, MAX30105_ADDR, 5, 10);
    if (result == HAL_OK) // HAL_ERROR or HAL_BUSY or HAL_TIMEOUT
    {
        returnValue = HAL_I2C_Mem_Write(&hi2c1, MAX30105_ADDR, reg_addr , 1, &reg_value, sizeof(reg_value), 10);
        if(returnValue != HAL_OK)
            return returnValue;
    }
    return returnValue;
}

The I2C setup is: I2C 设置是:

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.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}

I tried in the boards STM32F407G and STM32F103 Blue Pill, both the same issue happened.我在 STM32F407G 和 STM32F103 Blue Pill 板上试过,都发生了同样的问题。

The sensor is MAX30105 from Maxim.传感器是Maxim的MAX30105。

Is there something that I am missing for the I2C communication that results in such problem? I2C 通信是否缺少导致此类问题的某些内容?

If you re-debug, your MCU is reset, but your sensor is not.如果您重新调试,您的 MCU 会重置,但您的传感器不会。 Therefore, the sensor is in a state which your MCU does not expect, and the communication doesnt work.因此,传感器处于您的 MCU 不期望的状态,并且通信不起作用。 Probably, the sensor is pulling down SDA and waiting for more clock cycles to finish its transmission.可能传感器正在拉低 SDA 并等待更多时钟周期来完成其传输。

In case you don't know how I2C communication works: Both SCL and SDA lines are pulled up to Vcc.如果您不知道 I2C 通信是如何工作的:SCL 和 SDA 线都被上拉到 Vcc。 SCL should only be pulled down by the master, but all devices on the bus can pull down SDA. SCL 应该只被master 拉低,但是总线上的所有设备都可以拉低SDA。 If multiple devices "disagree" on the status of the bus, one device might pull down SDA, while the master stops toggling SCL.如果多个设备对总线状态“不一致”,则一个设备可能会拉低 SDA,而主设备则停止切换 SCL。 This kills the bus.这会杀死总线。

You should either power cycle your system or clear the I2C bus by sending 9 clock cycles.您应该重启系统或通过发送 9 个时钟周期来清除 I2C 总线。 In 9 clock cycles, any pending transmission of your sensor should be finished and you can resume your communication.在 9 个时钟周期内,传感器的任何未决传输都应该完成,您可以恢复通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM