0

我正在尝试在 STM32L051 上实现一个持续准备接收(发送时除外)的中断驱动的 I2C 从机。问题是我在 HAL_I2C_ErrorCallback 中不断收到 HAL_I2C_ERROR_OVR 错误。下面的代码只是一个示例,配置为接收 3 字节消息并以单字节状态响应。一旦主设备发送,运行此代码会触发 OVR 错误,然后似乎会持续触发 I2C1_IRQHandler,从而占用其他操作。从站不能使用拉伸,所以我禁用了它,如下面的代码所示。我对 HAL 相当陌生,并且大部分时间都在使用 STM32Fxx 系列和 std 外设库。我已经查看了已发布的示例,甚至将一些示例复制到了我更复杂的代码库中,结果却得到了相同的 OVR 错误。你们中的任何人有任何建议/建议/等吗?这可以推动我找到一个可行的解决方案吗?

系统时钟速度:16MHz

I2C 总线速度:10KHz

谢谢。

//**** Slave Rx/Tx ****//
int main(void)
{
  /* USER CODE BEGIN 1 */
    int responseSize = 1;
    int bytesToReceive = 3; 
    bool error = false; 
  /* USER CODE END 1 */
  /* MCU Configuration----------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

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


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  i2c_response[0] = 0x01;
  responseSize = 1;

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
        //**** Slave Rx/Tx Interrupt based ****//
        if(slaveRxCallbackTriggered)
        {   
            slaveRxCallbackTriggered = false;

            if(responseSize > 0)
            {
                if(!IOModule_TransmitToMaster(i2c_response,responseSize))
                {
                    error = true;
                }
            }           

            IOModule_ReceiveFromMaster(bytesToReceive); 
        }
        else
            IOModule_ReceiveFromMaster(bytesToReceive);

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

/*------------------------------------------------------------------------------
    I2C Receive data from master
 *------------------------------------------------------------------------------*/
bool IOModule_ReceiveFromMaster(int numberOfBytes)
{
    bool result = false;

    if(HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_READY)
    {       
        HAL_StatusTypeDef status = HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t *)i2c_rx_array, numberOfBytes);

        if(status == HAL_OK)
        {
            result = true;
        }
    }


    return result;
}

/*------------------------------------------------------------------------------
    I2C Transmit data to master
 *------------------------------------------------------------------------------*/
bool IOModule_TransmitToMaster(uint8_t txBuffer[], int bufferSize)
{
    bool result = false;    

    while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
  {
  } 

    HAL_StatusTypeDef status = HAL_I2C_Slave_Transmit(&hi2c1, (uint8_t*)txBuffer, bufferSize, 10000);

    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_RESET);

    if(status == HAL_OK)
    {   
        result = true;
    }

    return result;
}

/*------------------------------------------------------------------------------
    I2C Slave Rx Complete Callback
 *------------------------------------------------------------------------------*/
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2CxHandle)
{   
    slaveRxCallbackTriggered = true;
}

/*------------------------------------------------------------------------------
    I2C Error Callback
 *------------------------------------------------------------------------------*/
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
    lastErrorCode = HAL_I2C_GetError(&hi2c1);
}

//I2C1 init
void MX_I2C1_Init(void)
{
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x000006C5;
  hi2c1.Init.OwnAddress1 = 0x24;
  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_ENABLE;
  HAL_I2C_Init(&hi2c1);

    /**Configure Analogue filter 
    */
  HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE);
}


void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {  
    /**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_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();
  }
}

主 Tx/Rx

int main(void)
{
    //**** Master Tx/Rx ****//
    ioModule_response[0] = 0xA0;
    ioModule_response[1] = 0x01;
    ioModule_response[2] = 0xE3;

    while(1)
    {
        if(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)0x24, (uint8_t*)ioModule_response, 3, 10000)== HAL_OK)
        {
            if(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)0x24, (uint8_t *)i2c_rx_array, 1, 10000) != HAL_OK)
            {
                            //Blink LED and Pause
                LED_Show_StatusCode(2,false);
                HAL_Delay(500);
            }
            else if(i2c_rx_array[0] != 1)
            {
                            //Blink LED and Pause
                LED_Show_StatusCode(3,false);
                HAL_Delay(500);
            }
        }
    }
}

这是 STM32Cube MX 生成的 IRQ 处理程序。

void I2C1_IRQHandler(void)
{
  /* USER CODE BEGIN I2C1_IRQn 0 */

  /* USER CODE END I2C1_IRQn 0 */
  if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
    HAL_I2C_ER_IRQHandler(&hi2c1);
  } else {
    HAL_I2C_EV_IRQHandler(&hi2c1);
  }
  /* USER CODE BEGIN I2C1_IRQn 1 */

  /* USER CODE END I2C1_IRQn 1 */
}

更新:我目前正在探索 LL API,因为这看起来更像是我过去使用的标准外设库。

4

1 回答 1

3

我的解决方案是完全放弃 HAL 并按照 imbearr 的建议使用 LL API。如前所述,我过去使用过 STM32 标准外设库,LL 在很多方面都非常相似。

感谢 imbearr 的帮助。

于 2016-07-25T16:17:57.957 回答