usart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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) 2024 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 huart3;
  25. /* USART1 init function */
  26. void MX_USART1_UART_Init(void)
  27. {
  28. /* USER CODE BEGIN USART1_Init 0 */
  29. /* USER CODE END USART1_Init 0 */
  30. LL_USART_InitTypeDef USART_InitStruct = {0};
  31. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  32. /* Peripheral clock enable */
  33. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
  34. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  35. /**USART1 GPIO Configuration
  36. PA9 ------> USART1_TX
  37. PA10 ------> USART1_RX
  38. */
  39. GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
  40. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  41. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  42. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  43. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  44. GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
  45. GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
  46. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  47. /* USART1 interrupt Init */
  48. NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
  49. NVIC_EnableIRQ(USART1_IRQn);
  50. /* USER CODE BEGIN USART1_Init 1 */
  51. /* USER CODE END USART1_Init 1 */
  52. USART_InitStruct.BaudRate = 115200;
  53. USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  54. USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  55. USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  56. USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
  57. USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  58. USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  59. LL_USART_Init(USART1, &USART_InitStruct);
  60. LL_USART_ConfigAsyncMode(USART1);
  61. LL_USART_Enable(USART1);
  62. /* USER CODE BEGIN USART1_Init 2 */
  63. /* USER CODE END USART1_Init 2 */
  64. }
  65. /* USART2 init function */
  66. void MX_USART2_UART_Init(void)
  67. {
  68. /* USER CODE BEGIN USART2_Init 0 */
  69. /* USER CODE END USART2_Init 0 */
  70. LL_USART_InitTypeDef USART_InitStruct = {0};
  71. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  72. /* Peripheral clock enable */
  73. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
  74. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  75. /**USART2 GPIO Configuration
  76. PA1 ------> USART2_RTS
  77. PA2 ------> USART2_TX
  78. PA3 ------> USART2_RX
  79. */
  80. GPIO_InitStruct.Pin = LL_GPIO_PIN_1 | LL_GPIO_PIN_2;
  81. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  82. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  83. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  84. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  85. GPIO_InitStruct.Pin = LL_GPIO_PIN_3;
  86. GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
  87. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  88. /* USART2 interrupt Init */
  89. NVIC_SetPriority(USART2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
  90. NVIC_EnableIRQ(USART2_IRQn);
  91. /* USER CODE BEGIN USART2_Init 1 */
  92. /* USER CODE END USART2_Init 1 */
  93. USART_InitStruct.BaudRate = 115200;
  94. USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  95. USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  96. USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  97. USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
  98. USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_RTS;
  99. USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  100. LL_USART_Init(USART2, &USART_InitStruct);
  101. LL_USART_ConfigAsyncMode(USART2);
  102. LL_USART_Enable(USART2);
  103. /* USER CODE BEGIN USART2_Init 2 */
  104. /* USER CODE END USART2_Init 2 */
  105. }
  106. /* USART3 init function */
  107. void MX_USART3_UART_Init(void)
  108. {
  109. /* USER CODE BEGIN USART3_Init 0 */
  110. /* USER CODE END USART3_Init 0 */
  111. /* USER CODE BEGIN USART3_Init 1 */
  112. /* USER CODE END USART3_Init 1 */
  113. huart3.Instance = USART3;
  114. huart3.Init.BaudRate = 115200;
  115. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  116. huart3.Init.StopBits = UART_STOPBITS_1;
  117. huart3.Init.Parity = UART_PARITY_NONE;
  118. huart3.Init.Mode = UART_MODE_TX_RX;
  119. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  120. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  121. if (HAL_UART_Init(&huart3) != HAL_OK)
  122. {
  123. Error_Handler();
  124. }
  125. /* USER CODE BEGIN USART3_Init 2 */
  126. /* USER CODE END USART3_Init 2 */
  127. }
  128. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  129. {
  130. GPIO_InitTypeDef GPIO_InitStruct = {0};
  131. if (uartHandle->Instance == USART3)
  132. {
  133. /* USER CODE BEGIN USART3_MspInit 0 */
  134. /* USER CODE END USART3_MspInit 0 */
  135. /* USART3 clock enable */
  136. __HAL_RCC_USART3_CLK_ENABLE();
  137. __HAL_RCC_GPIOB_CLK_ENABLE();
  138. /**USART3 GPIO Configuration
  139. PB10 ------> USART3_TX
  140. PB11 ------> USART3_RX
  141. */
  142. GPIO_InitStruct.Pin = GPIO_PIN_10;
  143. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  144. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  145. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  146. GPIO_InitStruct.Pin = GPIO_PIN_11;
  147. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  148. GPIO_InitStruct.Pull = GPIO_NOPULL;
  149. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  150. /* USART3 interrupt Init */
  151. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  152. HAL_NVIC_EnableIRQ(USART3_IRQn);
  153. /* USER CODE BEGIN USART3_MspInit 1 */
  154. /* USER CODE END USART3_MspInit 1 */
  155. }
  156. }
  157. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  158. {
  159. if (uartHandle->Instance == USART3)
  160. {
  161. /* USER CODE BEGIN USART3_MspDeInit 0 */
  162. /* USER CODE END USART3_MspDeInit 0 */
  163. /* Peripheral clock disable */
  164. __HAL_RCC_USART3_CLK_DISABLE();
  165. /**USART3 GPIO Configuration
  166. PB10 ------> USART3_TX
  167. PB11 ------> USART3_RX
  168. */
  169. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
  170. /* USART3 interrupt Deinit */
  171. HAL_NVIC_DisableIRQ(USART3_IRQn);
  172. /* USER CODE BEGIN USART3_MspDeInit 1 */
  173. /* USER CODE END USART3_MspDeInit 1 */
  174. }
  175. }
  176. /* USER CODE BEGIN 1 */
  177. /* USER CODE END 1 */