You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.7 KiB

2 years ago
2 years ago
2 years ago
11 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. UART_HandleTypeDef huart1;
  25. DMA_HandleTypeDef hdma_usart1_rx;
  26. /* USART1 init function */
  27. void MX_USART1_UART_Init(void)
  28. {
  29. /* USER CODE BEGIN USART1_Init 0 */
  30. /* USER CODE END USART1_Init 0 */
  31. /* USER CODE BEGIN USART1_Init 1 */
  32. /* USER CODE END USART1_Init 1 */
  33. huart1.Instance = USART1;
  34. huart1.Init.BaudRate = 460800;
  35. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  36. huart1.Init.StopBits = UART_STOPBITS_1;
  37. huart1.Init.Parity = UART_PARITY_NONE;
  38. huart1.Init.Mode = UART_MODE_TX_RX;
  39. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  40. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  41. if (HAL_UART_Init(&huart1) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. /* USER CODE BEGIN USART1_Init 2 */
  46. /* USER CODE END USART1_Init 2 */
  47. }
  48. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  49. {
  50. GPIO_InitTypeDef GPIO_InitStruct = {0};
  51. if(uartHandle->Instance==USART1)
  52. {
  53. /* USER CODE BEGIN USART1_MspInit 0 */
  54. /* USER CODE END USART1_MspInit 0 */
  55. /* USART1 clock enable */
  56. __HAL_RCC_USART1_CLK_ENABLE();
  57. __HAL_RCC_GPIOA_CLK_ENABLE();
  58. /**USART1 GPIO Configuration
  59. PA9 ------> USART1_TX
  60. PA10 ------> USART1_RX
  61. */
  62. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  63. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  64. GPIO_InitStruct.Pull = GPIO_NOPULL;
  65. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  66. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  67. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  68. /* USART1 DMA Init */
  69. /* USART1_RX Init */
  70. hdma_usart1_rx.Instance = DMA2_Stream2;
  71. hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
  72. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  73. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  74. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  75. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  76. hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  77. hdma_usart1_rx.Init.Mode = DMA_NORMAL;
  78. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
  79. hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  80. if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
  81. {
  82. Error_Handler();
  83. }
  84. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
  85. /* USER CODE BEGIN USART1_MspInit 1 */
  86. /* USER CODE END USART1_MspInit 1 */
  87. }
  88. }
  89. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  90. {
  91. if(uartHandle->Instance==USART1)
  92. {
  93. /* USER CODE BEGIN USART1_MspDeInit 0 */
  94. /* USER CODE END USART1_MspDeInit 0 */
  95. /* Peripheral clock disable */
  96. __HAL_RCC_USART1_CLK_DISABLE();
  97. /**USART1 GPIO Configuration
  98. PA9 ------> USART1_TX
  99. PA10 ------> USART1_RX
  100. */
  101. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  102. /* USART1 DMA DeInit */
  103. HAL_DMA_DeInit(uartHandle->hdmarx);
  104. /* USER CODE BEGIN USART1_MspDeInit 1 */
  105. /* USER CODE END USART1_MspDeInit 1 */
  106. }
  107. }
  108. /* USER CODE BEGIN 1 */
  109. /* USER CODE END 1 */