usart(835).c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. char g_usart3_rx_buf[USART3_REC_LEN];
  24. char g_usart3_rx_sta = 0; // 接收完成标志 0x33:接收完成
  25. uint8_t g_usart3_rx_len = 0;
  26. /* USER CODE END 0 */
  27. UART_HandleTypeDef huart1;
  28. UART_HandleTypeDef huart3;
  29. DMA_HandleTypeDef hdma_usart3_tx;
  30. DMA_HandleTypeDef hdma_usart3_rx;
  31. /* USART1 init function */
  32. void MX_USART1_UART_Init(void)
  33. {
  34. /* USER CODE BEGIN USART1_Init 0 */
  35. /* USER CODE END USART1_Init 0 */
  36. /* USER CODE BEGIN USART1_Init 1 */
  37. /* USER CODE END USART1_Init 1 */
  38. huart1.Instance = USART1;
  39. huart1.Init.BaudRate = 115200;
  40. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  41. huart1.Init.StopBits = UART_STOPBITS_1;
  42. huart1.Init.Parity = UART_PARITY_NONE;
  43. huart1.Init.Mode = UART_MODE_TX_RX;
  44. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  45. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  46. if (HAL_UART_Init(&huart1) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. /* USER CODE BEGIN USART1_Init 2 */
  51. /* USER CODE END USART1_Init 2 */
  52. }
  53. /* USART3 init function */
  54. void MX_USART3_UART_Init(void)
  55. {
  56. /* USER CODE BEGIN USART3_Init 0 */
  57. /* USER CODE END USART3_Init 0 */
  58. /* USER CODE BEGIN USART3_Init 1 */
  59. /* USER CODE END USART3_Init 1 */
  60. huart3.Instance = USART3;
  61. huart3.Init.BaudRate = 115200;
  62. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  63. huart3.Init.StopBits = UART_STOPBITS_1;
  64. huart3.Init.Parity = UART_PARITY_NONE;
  65. huart3.Init.Mode = UART_MODE_TX_RX;
  66. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  67. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  68. if (HAL_UART_Init(&huart3) != HAL_OK)
  69. {
  70. Error_Handler();
  71. }
  72. /* USER CODE BEGIN USART3_Init 2 */
  73. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  74. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  75. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_TXE);
  76. /* USER CODE END USART3_Init 2 */
  77. }
  78. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  79. {
  80. GPIO_InitTypeDef GPIO_InitStruct = {0};
  81. if(uartHandle->Instance==USART1)
  82. {
  83. /* USER CODE BEGIN USART1_MspInit 0 */
  84. /* USER CODE END USART1_MspInit 0 */
  85. /* USART1 clock enable */
  86. __HAL_RCC_USART1_CLK_ENABLE();
  87. __HAL_RCC_GPIOA_CLK_ENABLE();
  88. /**USART1 GPIO Configuration
  89. PA9 ------> USART1_TX
  90. PA10 ------> USART1_RX
  91. */
  92. GPIO_InitStruct.Pin = GPIO_PIN_9;
  93. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  95. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  96. GPIO_InitStruct.Pin = GPIO_PIN_10;
  97. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  98. GPIO_InitStruct.Pull = GPIO_NOPULL;
  99. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  100. /* USART1 interrupt Init */
  101. HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
  102. HAL_NVIC_EnableIRQ(USART1_IRQn);
  103. /* USER CODE BEGIN USART1_MspInit 1 */
  104. /* USER CODE END USART1_MspInit 1 */
  105. }
  106. else if(uartHandle->Instance==USART3)
  107. {
  108. /* USER CODE BEGIN USART3_MspInit 0 */
  109. /* USER CODE END USART3_MspInit 0 */
  110. /* USART3 clock enable */
  111. __HAL_RCC_USART3_CLK_ENABLE();
  112. __HAL_RCC_GPIOD_CLK_ENABLE();
  113. /**USART3 GPIO Configuration
  114. PD8 ------> USART3_TX
  115. PD9 ------> USART3_RX
  116. */
  117. GPIO_InitStruct.Pin = GPIO_PIN_8;
  118. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  119. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  120. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  121. GPIO_InitStruct.Pin = GPIO_PIN_9;
  122. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  123. GPIO_InitStruct.Pull = GPIO_NOPULL;
  124. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  125. __HAL_AFIO_REMAP_USART3_ENABLE();
  126. /* USART3 DMA Init */
  127. /* USART3_TX Init */
  128. hdma_usart3_tx.Instance = DMA1_Channel2;
  129. hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  130. hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  131. hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
  132. hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  133. hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  134. hdma_usart3_tx.Init.Mode = DMA_NORMAL;
  135. hdma_usart3_tx.Init.Priority = DMA_PRIORITY_HIGH;
  136. if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
  141. /* USART3_RX Init */
  142. hdma_usart3_rx.Instance = DMA1_Channel3;
  143. hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  144. hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  145. hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
  146. hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  147. hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  148. hdma_usart3_rx.Init.Mode = DMA_NORMAL;
  149. hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
  150. if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
  151. {
  152. Error_Handler();
  153. }
  154. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
  155. /* USART3 interrupt Init */
  156. HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
  157. HAL_NVIC_EnableIRQ(USART3_IRQn);
  158. /* USER CODE BEGIN USART3_MspInit 1 */
  159. /* USER CODE END USART3_MspInit 1 */
  160. }
  161. }
  162. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  163. {
  164. if(uartHandle->Instance==USART1)
  165. {
  166. /* USER CODE BEGIN USART1_MspDeInit 0 */
  167. /* USER CODE END USART1_MspDeInit 0 */
  168. /* Peripheral clock disable */
  169. __HAL_RCC_USART1_CLK_DISABLE();
  170. /**USART1 GPIO Configuration
  171. PA9 ------> USART1_TX
  172. PA10 ------> USART1_RX
  173. */
  174. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  175. /* USART1 interrupt Deinit */
  176. HAL_NVIC_DisableIRQ(USART1_IRQn);
  177. /* USER CODE BEGIN USART1_MspDeInit 1 */
  178. /* USER CODE END USART1_MspDeInit 1 */
  179. }
  180. else if(uartHandle->Instance==USART3)
  181. {
  182. /* USER CODE BEGIN USART3_MspDeInit 0 */
  183. /* USER CODE END USART3_MspDeInit 0 */
  184. /* Peripheral clock disable */
  185. __HAL_RCC_USART3_CLK_DISABLE();
  186. /**USART3 GPIO Configuration
  187. PD8 ------> USART3_TX
  188. PD9 ------> USART3_RX
  189. */
  190. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9);
  191. /* USART3 DMA DeInit */
  192. HAL_DMA_DeInit(uartHandle->hdmatx);
  193. HAL_DMA_DeInit(uartHandle->hdmarx);
  194. /* USART3 interrupt Deinit */
  195. HAL_NVIC_DisableIRQ(USART3_IRQn);
  196. /* USER CODE BEGIN USART3_MspDeInit 1 */
  197. /* USER CODE END USART3_MspDeInit 1 */
  198. }
  199. }
  200. /* USER CODE BEGIN 1 */
  201. //void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  202. //{
  203. // HAL_StatusTypeDef flag = 0;
  204. // if(huart->Instance == USART3){
  205. // HAL_UARTEx_ReceiveToIdle_IT(huart,(uint8_t *)g_usart3_rx_buf,USART3_REC_LEN);
  206. // if(flag == HAL_OK){
  207. // g_usart3_rx_sta = 0x33;
  208. // }
  209. //
  210. // }
  211. //
  212. //}
  213. //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  214. //{
  215. // if(huart->Instance == USART3)
  216. // {
  217. //
  218. // }
  219. //}
  220. //void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart)
  221. //{
  222. // if(huart->Instance == USART3)
  223. // {
  224. // RxFlag = 1;//置位接收完成标志
  225. //
  226. // }
  227. //}
  228. /* USER CODE END 1 */