[STM32U5] 【NUCLEO-U575ZI-Q测评】串口2

[复制链接]
588|1
 楼主| 比神乐 发表于 2023-2-28 11:45 | 显示全部楼层 |阅读模式
本帖最后由 比神乐 于 2023-2-28 11:48 编辑

今天搞了一下串口2,发送PD5,接收PD6。
原理图:
1.jpg
代码:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */
  4. /* USER CODE END Includes */

  5. /* Private typedef -----------------------------------------------------------*/
  6. /* USER CODE BEGIN PTD */

  7. /* USER CODE END PTD */

  8. /* Private define ------------------------------------------------------------*/
  9. /* USER CODE BEGIN PD */
  10. #define TRANSMITTER_BOARD
  11. /* USER CODE END PD */

  12. /* Private macro -------------------------------------------------------------*/
  13. /* USER CODE BEGIN PM */

  14. /* USER CODE END PM */

  15. /* Private variables ---------------------------------------------------------*/

  16. UART_HandleTypeDef huart2;

  17. /* USER CODE BEGIN PV */
  18. __IO uint32_t UserButtonStatus = 0;  /* set to 1 after User Button interrupt  */

  19. /* Buffer used for transmission */
  20. uint8_t aTxBuffer[] = " **** UART_TwoBoards_ComPolling ****  **** UART_TwoBoards_ComPolling ****  **** UART_TwoBoards_ComPolling **** ";

  21. /* Buffer used for reception */
  22. uint8_t aRxBuffer[RXBUFFERSIZE];

  23. /* USER CODE END PV */

  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void SystemPower_Config(void);
  27. static void MX_GPIO_Init(void);
  28. static void MX_ICACHE_Init(void);
  29. static void MX_USART2_UART_Init(void);
  30. /* USER CODE BEGIN PFP */
  31. static uint16_t Buffercmp(uint8_t *pBuffer1, uint8_t *pBuffer2, uint16_t BufferLength);
  32. /* USER CODE END PFP */

  33. /* Private user code ---------------------------------------------------------*/
  34. /* USER CODE BEGIN 0 */

  35. /* USER CODE END 0 */

  36. /**
  37.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  38.   * @retval int
  39.   */
  40. int main(void)
  41. {
  42.   /* USER CODE BEGIN 1 */
  43.   /* STM32U5xx HAL library initialization:
  44.        - Configure the Flash prefetch
  45.        - Configure the Systick to generate an interrupt each 1 msec
  46.        - Set NVIC Group Priority to 3
  47.        - Low Level Initialization
  48.      */
  49.   /* USER CODE END 1 */

  50.   /* MCU Configuration--------------------------------------------------------*/

  51.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  52.   HAL_Init();

  53.   /* USER CODE BEGIN Init */

  54.   /* USER CODE END Init */

  55.   /* Configure the system clock */
  56.   SystemClock_Config();

  57.   /* Configure the System Power */
  58.   SystemPower_Config();

  59.   /* USER CODE BEGIN SysInit */

  60.   /* USER CODE END SysInit */

  61.   /* Initialize all configured peripherals */
  62.   MX_GPIO_Init();
  63.   MX_ICACHE_Init();
  64.   MX_USART2_UART_Init();
  65.   /* USER CODE BEGIN 2 */
  66.   /* Configure LED2 and LED3 */
  67.   BSP_LED_Init(LED2);
  68.   BSP_LED_Init(LED3);

  69. #ifdef TRANSMITTER_BOARD

  70.   /* Configure User push-button in Interrupt mode */
  71.   BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

  72.   /* Wait for User push-button press before starting the Communication.
  73.      In the meantime, LED2 is blinking */
  74.   while (UserButtonStatus == 0)
  75.   {
  76.     /* Toggle LED2*/
  77.     BSP_LED_Toggle(LED2);
  78.     HAL_Delay(100);
  79.   }

  80.   BSP_LED_Off(LED2);

  81.   /* The board sends the message and expects to receive it back */

  82.   /*##-1- Start the transmission process #####################################*/
  83.   /* While the UART in reception process, user can transmit data through
  84.      "aTxBuffer" buffer */
  85.   if (HAL_UART_Transmit(&huart2, (uint8_t *)aTxBuffer, TXBUFFERSIZE, 5000) != HAL_OK)
  86.   {
  87.     Error_Handler();
  88.   }


  89.   /*##-2- Put UART peripheral in reception process ###########################*/
  90.   if (HAL_UART_Receive(&huart2, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
  91.   {
  92.     Error_Handler();
  93.   }

  94. #else

  95.   /* The board receives the message and sends it back */

  96.   /*##-1- Put UART peripheral in reception process ###########################*/
  97.   if (HAL_UART_Receive(&huart2, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 0x1FFFFFF) != HAL_OK)
  98.   {
  99.     Error_Handler();
  100.   }

  101.   /*##-2- Start the transmission process #####################################*/
  102.   /* While the UART in reception process, user can transmit data through
  103.      "aTxBuffer" buffer */
  104.   if (HAL_UART_Transmit(&huart2, (uint8_t *)aTxBuffer, TXBUFFERSIZE, 5000) != HAL_OK)
  105.   {
  106.     Error_Handler();
  107.   }

  108. #endif /* TRANSMITTER_BOARD */

  109.   /*##-3- Compare the sent and received buffers ##############################*/
  110.   if (Buffercmp((uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, RXBUFFERSIZE))
  111.   {
  112.     Error_Handler();
  113.   }

  114.   /* Turn on LED2 if test passes then enter infinite loop */
  115.   BSP_LED_On(LED2);
  116.   /* USER CODE END 2 */

  117.   /* Infinite loop */
  118.   /* USER CODE BEGIN WHILE */
  119.   while (1)
  120.   {
  121.     /* USER CODE END WHILE */

  122.     /* USER CODE BEGIN 3 */

  123.   }
  124.   /* USER CODE END 3 */
  125. }

  126. /**
  127.   * [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
  128.   * @retval None
  129.   */
  130. void SystemClock_Config(void)
  131. {
  132.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  133.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  134.   /** Configure the main internal regulator output voltage
  135.   */
  136.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  137.   {
  138.     Error_Handler();
  139.   }

  140.   /** Initializes the CPU, AHB and APB busses clocks
  141.   */
  142.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  143.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  144.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  145.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  146.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  147.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  148.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  149.   RCC_OscInitStruct.PLL.PLLM = 1;
  150.   RCC_OscInitStruct.PLL.PLLN = 80;
  151.   RCC_OscInitStruct.PLL.PLLP = 2;
  152.   RCC_OscInitStruct.PLL.PLLQ = 2;
  153.   RCC_OscInitStruct.PLL.PLLR = 2;
  154.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  155.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  156.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  157.   {
  158.     Error_Handler();
  159.   }

  160.   /** Initializes the CPU, AHB and APB busses clocks
  161.   */
  162.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  163.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  164.                               |RCC_CLOCKTYPE_PCLK3;
  165.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  166.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  167.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  168.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  169.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  170.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  171.   {
  172.     Error_Handler();
  173.   }
  174. }

  175. /**
  176.   * @brief Power Configuration
  177.   * @retval None
  178.   */
  179. static void SystemPower_Config(void)
  180. {

  181.   /*
  182.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
  183.    */
  184.   HAL_PWREx_DisableUCPDDeadBattery();

  185.   /*
  186.    * Switch to SMPS regulator instead of LDO
  187.    */
  188.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  189.   {
  190.     Error_Handler();
  191.   }
  192. }

  193. /**
  194.   * @brief ICACHE Initialization Function
  195.   * @param None
  196.   * @retval None
  197.   */
  198. static void MX_ICACHE_Init(void)
  199. {

  200.   /* USER CODE BEGIN ICACHE_Init 0 */

  201.   /* USER CODE END ICACHE_Init 0 */

  202.   /* USER CODE BEGIN ICACHE_Init 1 */

  203.   /* USER CODE END ICACHE_Init 1 */

  204.   /** Enable instruction cache in 1-way (direct mapped cache)
  205.   */
  206.   if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  207.   {
  208.     Error_Handler();
  209.   }
  210.   if (HAL_ICACHE_Enable() != HAL_OK)
  211.   {
  212.     Error_Handler();
  213.   }
  214.   /* USER CODE BEGIN ICACHE_Init 2 */

  215.   /* USER CODE END ICACHE_Init 2 */

  216. }

  217. /**
  218.   * @brief USART2 Initialization Function
  219.   * @param None
  220.   * @retval None
  221.   */
  222. static void MX_USART2_UART_Init(void)
  223. {

  224.   /* USER CODE BEGIN USART2_Init 0 */

  225.   /* USER CODE END USART2_Init 0 */

  226.   /* USER CODE BEGIN USART2_Init 1 */

  227.   /* USER CODE END USART2_Init 1 */
  228.   huart2.Instance = USART2;
  229.   huart2.Init.BaudRate = 9600;
  230.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  231.   huart2.Init.StopBits = UART_STOPBITS_1;
  232.   huart2.Init.Parity = UART_PARITY_NONE;
  233.   huart2.Init.Mode = UART_MODE_TX_RX;
  234.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  235.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  236.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  237.   huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  238.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  239.   if (HAL_UART_Init(&huart2) != HAL_OK)
  240.   {
  241.     Error_Handler();
  242.   }
  243.   if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  244.   {
  245.     Error_Handler();
  246.   }
  247.   if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  248.   {
  249.     Error_Handler();
  250.   }
  251.   if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
  252.   {
  253.     Error_Handler();
  254.   }
  255.   /* USER CODE BEGIN USART2_Init 2 */

  256.   /* USER CODE END USART2_Init 2 */

  257. }

  258. /**
  259.   * @brief GPIO Initialization Function
  260.   * @param None
  261.   * @retval None
  262.   */
  263. static void MX_GPIO_Init(void)
  264. {

  265.   /* GPIO Ports Clock Enable */
  266.   __HAL_RCC_GPIOD_CLK_ENABLE();

  267. }

  268. /* USER CODE BEGIN 4 */
  269. /**
  270.   * @brief  UART error callbacks
  271.   * @param  UartHandle: UART handle
  272.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   This example shows a simple way to report transfer error, and you can
  273.   *         add your own implementation.
  274.   * @retval None
  275.   */
  276. void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
  277. {
  278.   Error_Handler();
  279. }


  280. /**
  281.   * @brief EXTI line detection callbacks
  282.   * @param GPIO_Pin: Specifies the pins connected EXTI line
  283.   * @retval None
  284.   */
  285. void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
  286. {
  287.   if (GPIO_Pin == BUTTON_USER_PIN)
  288.   {
  289.     UserButtonStatus = 1;
  290.   }
  291. }

  292. /**
  293.   * @brief  Compares two buffers.
  294.   * @param  pBuffer1, pBuffer2: buffers to be compared.
  295.   * @param  BufferLength: buffer's length
  296.   * @retval 0  : pBuffer1 identical to pBuffer2
  297.   *         >0 : pBuffer1 differs from pBuffer2
  298.   */
  299. static uint16_t Buffercmp(uint8_t *pBuffer1, uint8_t *pBuffer2, uint16_t BufferLength)
  300. {
  301.   while (BufferLength--)
  302.   {
  303.     if ((*pBuffer1) != *pBuffer2)
  304.     {
  305.       return BufferLength;
  306.     }
  307.     pBuffer1++;
  308.     pBuffer2++;
  309.   }

  310.   return 0;
  311. }

  312. /* USER CODE END 4 */

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

  321.   /* Turn LED3 on */
  322.   BSP_LED_On(LED3);
  323.   while (1);
  324.   /* USER CODE END Error_Handler_Debug */
  325. }

  326. #ifdef  USE_FULL_ASSERT
  327. /**
  328.   * @brief  Reports the name of the source file and the source line number
  329.   *         where the assert_param error has occurred.
  330.   * @param  file: pointer to the source file name
  331.   * @param  line: assert_param error line source number
  332.   * @retval None
  333.   */
  334. void assert_failed(uint8_t *file, uint32_t line)
  335. {
  336.   /* USER CODE BEGIN 6 */
  337.   /* User can add his own implementation to report the file name and line number,
  338.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  339.   /* Infinite loop */
  340.   while (1)
  341.   {
  342.   }
  343.   /* USER CODE END 6 */
  344. }
  345. #endif /* USE_FULL_ASSERT */
效果图:
0.jpg
3.jpg
Undshing 发表于 2024-1-12 16:29 | 显示全部楼层
那个usb转ttl的下载器挺好看的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部