STM32H743I-EVAL2_UART_HyperTerminal

姜志行
2023-12-01

前言

再看H743官方板子的剩下的串口HAL库demo.
优先级高的就剩下DMA方式收发数据的 UART_HyperTerminal_DMA 工程。
UART_HyperTerminal_DMA 和 UART_HyperTerminal_IT 区别不大(串口和DMA操作绑定,串口操作是DMA后缀的),都是收发定长数据(一看就倒胃口了),没大用.

因为已经将官方串口demo都过了一遍了,还没找到怎么官方演示怎么收发不定长数据。这咋弄?

去翻stm32h7xx_hal_uart.h,看到带超时收发的函数只有HAL_UART_Transmit()和HAL_UART_Receive(), 但是官方串口demo工程中,并没有演示这2个API怎么用…
用SI将STM32H743I-EVAL\Examples目录加进来,搜HAL_UART_Receive, 因为接收不定长数据是关键。

在\STM32H743I-EVAL\Examples\PWR\PWR_Domain3SystemControl中找到演示串口超时收发的例子。
PWR_Domain3SystemControl 演示的是低功耗模式下的串口通讯。

但是这个demo也有问题,试验发现,如果回包size大于接收缓冲区size, 再发回包时,串口就一直收不到包了。既然是接收缓冲区溢出情况下出的bug, 去查相关资料,玩过的同学的笔记说,需要清接收溢出标志。但是他们都是用库函数做的。

翻H743HAL库实现,翻到了如下清标志的操作。

//  *            @arg @ref UART_CLEAR_OREF     Overrun Error Clear Flag
        __HAL_UART_CLEAR_FLAG(&huart1, UART_CLEAR_OREF); // 清掉接收缓冲区溢出标志

继续试验,发现,虽然可以连续给串口发超量的回包,但是一旦不发了,串口接收超时后,还会接收一个在串口硬件缓冲区中留下的一个溢出没接收的字符。这就显的很怪了。这咋弄?

最后想到的方法是,如果在新的串口接收前,发现有接收缓冲区溢出,就先清掉接收缓冲区溢出标志,然后,带1ms超时去接收一下,将接收到的缓冲区溢出的字符丢弃掉。

        // 接收溢出字节是,超时是1ms! 不是正常的超时值
        rc_status = HAL_UART_Receive(&huart1, (uint8_t*)&aRxBuffer_for_over_run, sizeof(aRxBuffer_for_over_run), 1);
        // aRxBuffer_for_over_run中的内容,是上次接收缓冲区溢出的内容(1字节),抛掉无效的数据,不用了
        if ((HAL_OK != rc_status) && (HAL_TIMEOUT != rc_status)) {
          Error_Handler();
        }

这样改完后,效果就很好了。每发一次,都能接收到回包(如果回包超量,就最多接收缓冲区size那么多的字符); 如果回包没超量,就可以接收全部回包。可以连续的收回包,不会出现一直收不到回包的bug,也不会多收一次缓冲区的溢出字符。

这样改完后,就可以在正式工程用了。
运行效果如下:

    // run result
    /*
    [46] recv len = 0, recv content = []

    * please tell me
    [47] recv len = 1, recv content = [1]

    * please tell me
    [48] recv len = 2, recv content = [12]

    * please tell me
    [49] recv len = 3, recv content = [123]

    * please tell me
    [50] recv len = 4, recv content = [1234]

    * please tell me
    [51] recv len = 5, recv content = [12345]

    * please tell me
    [52] recv len = 6, recv content = [123456]

    * please tell me
    [53] recv len = 7, recv content = [1234567]
    * please tell me
    [54] recv len = 8, recv content = [12345678]


    * please tell me
    // 到这,缓冲区就满了,不管是发多少个字符的回包,只收缓冲区大小那么多回包
    [55] recv len = 9, recv content = [123456789] 
    
    * please tell me
    [56] recv len = 9, recv content = [123456789]

    * please tell me
    [57] recv len = 9, recv content = [a12345678] // 这里不是正确的回包,是因为发的太快了。慢点(配合收发的节奏)就行。

    * please tell me
    [58] recv len = 9, recv content = [123456789]

    * please tell me
    [59] recv len = 9, recv content = [a12345678]

    * please tell me
    [60] recv len = 0, recv content = [] // 这里是不发回包的情况,就收不到回包。
    * please tell me
    [61] recv len = 0, recv content = []

    * please tell me
    */

明天可以看官方其他种类的demo, 串口操作就看到这。

试验

CubeMX配置

和中断方式试验 STM32H743I-EVAL2_UART_HyperTerminal_IT 一样

用CubeMX重建后的可用工程预览

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define TX_BUFFER_SIZE                   80U
#define RX_BUFFER_SIZE                   10U
#define TX_CHUNK_SIZE                 4U
#define LPUART_TIME_OUT               6000 // 6 * 1000 // 6 s

uint8_t aTxBuffer[TX_BUFFER_SIZE] = {'\0'};

uint8_t aRxBuffer[RX_BUFFER_SIZE] = {'\0'};
uint8_t aRxBuffer_for_over_run[8] = {'\0'}; // 为接收接收缓冲区溢出的内容准备的,经过试验,接收溢出只有一个字节,多留点(8个字节)
uint8_t gsz_msg[0x100] = {'\0'};

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t bufferIndex = 0;
  uint32_t i_len_tx_all = 0;
  uint32_t i_len_tx_chunk = 0;
  uint32_t i_len_tx_left = 0;
  uint32_t i_loop_cnt = 0;
  HAL_StatusTypeDef rc_status = HAL_OK;

  /* 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_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  // huart1
  
  /* USER CODE END 2 */
 
 

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  strcpy((char*)aTxBuffer, "* please tell me\r\n");
  i_len_tx_all = strlen((char*)aTxBuffer);
  while (1)
  {
    /* USER CODE END WHILE */
    /* -8- Transmit the aTxBuffer data */
    i_len_tx_left = i_len_tx_all - 1 - bufferIndex;
    if (i_len_tx_left > 0) {
      // 这里演示数据分多次发出
      i_len_tx_chunk = (i_len_tx_left >= TX_CHUNK_SIZE) ? TX_CHUNK_SIZE : i_len_tx_left;
      rc_status = HAL_UART_Transmit(&huart1, (uint8_t*)(aTxBuffer + bufferIndex), i_len_tx_chunk, LPUART_TIME_OUT);
      if (HAL_OK != rc_status)
      {
        Error_Handler();
      }

      bufferIndex += i_len_tx_chunk;
    } else {
      // clear recv buffer
      memset(aRxBuffer, 0, RX_BUFFER_SIZE);
      
      /* -9- Receive the LPUART data */
      // 实际工程中,根据回包设备的响应时间, 将LPUART_TIME_OUT 改成实际的超时值就行. e.g. 100 ~ 300 (100ms ~ 300ms)
      // 先预期一个接收长度 BUFFER_SIZE, 如果收到了 BUFFER_SIZE 长度的回包, 会马上从HAL_UART_Receive返回
      // 如果没收到那么多回包(异常情况<设备没连或协议不对,没回包>, 不定长包), 那么超时返回
      
      // 在处理完接收数据后,处理一下接收溢出(对方发来的数据比我给的接收缓冲区大)的数据
      // 否则再接收,再也收不到东西了,即使对方不停的发
      // 官方demo并没有处理这个问题
      if (SET == __HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE)) {
        // 如果出现接收缓冲区溢出的情况,说明回包发多了(或者要开大点接收缓冲区)
        // 如果出现这个问题,已经有丢包问题了
        
        // 下面的处理,只是为了接收时,不至于接收的不对。丢包处理不了
        
//  *            @arg @ref UART_CLEAR_OREF     Overrun Error Clear Flag
        __HAL_UART_CLEAR_FLAG(&huart1, UART_CLEAR_OREF); // 清掉接收缓冲区溢出标志
        
        // 在接受前,处理过接收溢出之后,回包size如果>接收缓冲区size, 也能马上接收完(因为接收区已经被回包填满了), 不会经过超时等待
        
        // 还剩下一个小问题,如果连续发的数据都超过缓冲区了,最后不发时,还有再接收一个字节(这个字节是多发串中的一个字符)
        // 再接收一次, 将缓冲区内剩余的一个字符接收完,省的下次没回包了,还是显示接收了一个尾巴
        
        // 接收溢出字节是,超时是1ms! 不是正常的超时值
        rc_status = HAL_UART_Receive(&huart1, (uint8_t*)&aRxBuffer_for_over_run, sizeof(aRxBuffer_for_over_run), 1);
        // aRxBuffer_for_over_run中的内容,是上次接收缓冲区溢出的内容(1字节),抛掉无效的数据,不用了
        if ((HAL_OK != rc_status) && (HAL_TIMEOUT != rc_status)) {
          Error_Handler();
        }
      }
      
      // aRxBuffer要少收一个字符, 保持'\0'结尾,因为后面要拷贝到gsz_msg, 否则拷贝时会越界
      rc_status = HAL_UART_Receive(&huart1, (uint8_t*)&aRxBuffer, RX_BUFFER_SIZE - 1, LPUART_TIME_OUT);
      if ((HAL_OK != rc_status) && (HAL_TIMEOUT != rc_status)) {
        Error_Handler();
      }

//      /* -10- Check the received data */
//      if(Buffercmp(aRxBuffer, aTxBuffer, BUFFER_SIZE))
//      {
//        Error_Handler();
//      }
      
      // send recv buf to sender
      memset(gsz_msg, 0, sizeof(gsz_msg)/sizeof(gsz_msg[0]));
      
      // 如果在格式化gsz_msg时,发生越界(可能覆盖了huart1配置啥的),会导致HAL_UART_Transmit()再也收不到东西了
      // 但是程序也没崩溃, 这时看出来,加看门狗和数据安全性检查的重要性
      sprintf((char*)gsz_msg, "[%d] recv len = %d, recv content = [%s]\r\n", 
        ++i_loop_cnt,
        (huart1.RxXferSize - huart1.RxXferCount), // 接收到的数据长度
        (char*)aRxBuffer); // 接收到的内容, 因为这个buf拼出来,gsz_msg肯定能装的下,就先忽略长度检查
      
      rc_status = HAL_UART_Transmit(&huart1, gsz_msg, strlen((char*)gsz_msg), LPUART_TIME_OUT);
      if ((HAL_OK != rc_status) && (HAL_TIMEOUT != rc_status)) {
        Error_Handler();
      }

      bufferIndex = 0;
      HAL_Delay(100);
    }

    /* USER CODE BEGIN 3 */
    
    // run result
    /*
    [46] recv len = 0, recv content = []

    * please tell me
    [47] recv len = 1, recv content = [1]

    * please tell me
    [48] recv len = 2, recv content = [12]

    * please tell me
    [49] recv len = 3, recv content = [123]

    * please tell me
    [50] recv len = 4, recv content = [1234]

    * please tell me
    [51] recv len = 5, recv content = [12345]

    * please tell me
    [52] recv len = 6, recv content = [123456]

    * please tell me
    [53] recv len = 7, recv content = [1234567]
    * please tell me
    [54] recv len = 8, recv content = [12345678]

    * please tell me
    [55] recv len = 9, recv content = [123456789]

    * please tell me
    [56] recv len = 9, recv content = [123456789]

    * please tell me
    [57] recv len = 9, recv content = [a12345678]

    * please tell me
    [58] recv len = 9, recv content = [123456789]

    * please tell me
    [59] recv len = 9, recv content = [a12345678]

    * please tell me
    [60] recv len = 0, recv content = []
    * please tell me
    [61] recv len = 0, recv content = []

    * please tell me
    */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /** Supply configuration update enable 
  */
  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  /** Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 5;
  RCC_OscInitStruct.PLL.PLLN = 192;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Enables the Clock Security System 
  */
  HAL_RCC_EnableCSS();
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  do {
  } while (1);
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
  do {
  } while (1);
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/**
  ******************************************************************************
  * File Name          : gpio.c
  * Description        : This file provides code for the configuration
  *                      of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/** Configure pins
     PA14 (JTCK/SWCLK)   ------> DEBUG_JTCK-SWCLK
     PC15-OSC32_OUT (OSC32_OUT)   ------> RCC_OSC32_OUT
     PC14-OSC32_IN (OSC32_IN)   ------> RCC_OSC32_IN
     PB3 (JTDO/TRACESWO)   ------> DEBUG_JTDO-SWO
     PA13 (JTMS/SWDIO)   ------> DEBUG_JTMS-SWDIO
     PH1-OSC_OUT (PH1)   ------> RCC_OSC_OUT
     PH0-OSC_IN (PH0)   ------> RCC_OSC_IN
*/
void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();

}

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/**
  ******************************************************************************
  * File Name          : DEBUG.c
  * Description        : This file provides code for the configuration
  *                      of the DEBUG instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "debug.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* DEBUG init function */
void MX_DEBUG_Init(void)
{

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_ODD;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**USART1 GPIO Configuration    
    PB15     ------> USART1_RX
    PB14     ------> USART1_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_15|GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration    
    PB15     ------> USART1_RX
    PB14     ------> USART1_TX 
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_15|GPIO_PIN_14);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32h7xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32h7xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex Processor Interruption and Exception Handlers          */ 
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  HAL_RCC_NMI_IRQHandler();
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32H7xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32h7xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : stm32h7xx_hal_msp.c
  * Description        : This file provides code for the MSP Initialization 
  *                      and de-Initialization codes.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
 
/* USER CODE END Define */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */

/* USER CODE END Macro */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */

/* USER CODE END ExternalFunctions */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */
/**
  * Initializes the Global MSP.
  */
void HAL_MspInit(void)
{
  /* USER CODE BEGIN MspInit 0 */

  /* USER CODE END MspInit 0 */

  __HAL_RCC_SYSCFG_CLK_ENABLE();

  /* System interrupt init*/

  /* USER CODE BEGIN MspInit 1 */

  /* USER CODE END MspInit 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

总结

H743官方板子的串口demo有点挫,还好自己找到了解决方法, 来实现不定长数据的收发。

 类似资料:

相关阅读

相关文章

相关问答