打印
[STM32G0]

stm32g070 谁用到过这个芯片的usart3和usart4?

[复制链接]
1087|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kfawj|  楼主 | 2022-3-22 11:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 kfawj 于 2022-3-23 09:18 编辑

usart3目前可以发出数据,但发出的数据会被自己接收回来。反复产生IDLE故障中断。
usart4目前发出数据与目的数据的长度及内空均不符。
麻烦各位帮忙看看,在此先行谢过!
(下面上代码)

使用特权

评论回复
沙发
kfawj|  楼主 | 2022-3-22 11:26 | 只看该作者
static void MX_USART3_UART_Init(void)
{

  /* USER CODE BEGIN USART3_Init 0 */
  /* USER CODE END USART3_Init 0 */

  /* USER CODE BEGIN USART3_Init 1 */

  /* USER CODE END USART3_Init 1 */
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */
//        __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_FE);
//        __HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_ORE);
        __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
        __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
        __HAL_UART_ENABLE(&huart3);
        HAL_UART_Receive_IT(&huart3, &U3_RxDat_Tmp, 1);
  /* USER CODE END USART3_Init 2 */

}
static void MX_USART4_UART_Init(void)
{

  /* USER CODE BEGIN USART4_Init 0 */

  /* USER CODE END USART4_Init 0 */

  /* USER CODE BEGIN USART4_Init 1 */

  /* USER CODE END USART4_Init 1 */
  huart4.Instance = USART4;
  huart4.Init.BaudRate = 9600;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.Init.ClockPrescaler = UART_PRESCALER_DIV4;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART4_Init 2 */
        __HAL_UART_ENABLE_IT(&huart4, UART_IT_ERR);
        __HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);
        __HAL_UART_ENABLE(&huart4);
        U4_Rx_Enable();       
        HAL_UART_Receive_IT(&huart4, &U4_RxDat_Tmp, 1);
  /* USER CODE END USART4_Init 2 */

}

使用特权

评论回复
板凳
kfawj|  楼主 | 2022-3-22 11:32 | 只看该作者

void USART3_4_IRQHandler(void)
{
  /* USER CODE BEGIN USART3_4_IRQn 0 */

  /* USER CODE END USART3_4_IRQn 0 */
  HAL_UART_IRQHandler(&huart3);
  HAL_UART_IRQHandler(&huart4);
  /* USER CODE BEGIN USART3_4_IRQn 1 */

  /* USER CODE END USART3_4_IRQn 1 */
}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart->Instance == USART3)
        {
                HAL_UART_Receive_IT(&huart3, &U3_RxDat_Tmp, 1);
                if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) == SET)
                {
                        __HAL_UART_CLEAR_IDLEFLAG(&huart3);
                }
                if(U3_Rx_Index >= U3_RXBUFFER_SIZE)
                        U3_Rx_Index = U3_RXBUFFER_SIZE - 1;
                U3_RxBuf[U3_Rx_Index] = U3_RxDat_Tmp;
                U3_Rx_Index++;
                U3_Rx_Delay = 5;
        }
        else if(huart->Instance == USART4)//RS485
        {
                HAL_UART_Receive_IT(&huart4, &U4_RxDat_Tmp, 1);
                if(U4_Rx_Index >= U4_RXBUFFER_SIZE)
                        U4_Rx_Index = U4_RXBUFFER_SIZE - 1;
                U4_RxBuf[U4_Rx_Index] = U4_RxDat_Tmp;
                U4_Rx_Index++;
                U4_Rx_Delay = 5;
        }
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart->Instance == USART3)
        {
                __HAL_UART_CLEAR_IT(&huart3, UART_CLEAR_TCF);
                __HAL_UART_DISABLE_IT(&huart3,  UART_IT_TC);
        }
        else if(huart->Instance == USART4)
        {
                __HAL_UART_CLEAR_IT(&huart4, UART_CLEAR_TCF);
                __HAL_UART_DISABLE_IT(&huart4,  UART_IT_TC);
                U4_Rx_Enable();
                HAL_UART_Receive_IT(&huart4, &U4_RxDat_Tmp, 1);
        }
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{       
         if(huart->Instance == USART3)
        {
                HAL_UART_Receive_IT(&huart3, &U3_RxDat_Tmp, 1);
                __HAL_UART_CLEAR_IDLEFLAG(&huart3);
//                __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
                __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
        }
        else if(huart->Instance == USART4)
        {
                __HAL_UART_CLEAR_IT(&huart4, UART_CLEAR_NEF);
                __HAL_UART_CLEAR_IT(&huart4, UART_CLEAR_FEF);
                __HAL_UART_CLEAR_IT(&huart4, UART_CLEAR_OREF);
                __HAL_UART_CLEAR_IT(&huart4, UART_CLEAR_TCF);
                __HAL_UART_DISABLE_IT(&huart4,  UART_IT_TC);
                U4_Rx_Enable();
                HAL_UART_Receive_IT(&huart4, &U4_RxDat_Tmp, 1);
        }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

44

主题

293

帖子

2

粉丝