usart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. ******************************************************************************
  3. * File Name : USART.c
  4. * Description : This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "usart.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. UART_HandleTypeDef huart4;
  24. UART_HandleTypeDef huart1;
  25. UART_HandleTypeDef huart2;
  26. /* UART4 init function */
  27. void MX_UART4_Init(void)
  28. {
  29. huart4.Instance = UART4;
  30. huart4.Init.BaudRate = 115200;
  31. huart4.Init.WordLength = UART_WORDLENGTH_8B;
  32. huart4.Init.StopBits = UART_STOPBITS_1;
  33. huart4.Init.Parity = UART_PARITY_NONE;
  34. huart4.Init.Mode = UART_MODE_TX_RX;
  35. huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  36. huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  37. if (HAL_UART_Init(&huart4) != HAL_OK)
  38. {
  39. Error_Handler();
  40. }
  41. }
  42. /* USART1 init function */
  43. void MX_USART1_UART_Init(void)
  44. {
  45. huart1.Instance = USART1;
  46. huart1.Init.BaudRate = 115200;
  47. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  48. huart1.Init.StopBits = UART_STOPBITS_1;
  49. huart1.Init.Parity = UART_PARITY_NONE;
  50. huart1.Init.Mode = UART_MODE_TX_RX;
  51. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  52. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  53. if (HAL_UART_Init(&huart1) != HAL_OK)
  54. {
  55. Error_Handler();
  56. }
  57. }
  58. /* USART2 init function */
  59. void MX_USART2_UART_Init(void)
  60. {
  61. huart2.Instance = USART2;
  62. huart2.Init.BaudRate = 38400;
  63. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  64. huart2.Init.StopBits = UART_STOPBITS_1;
  65. huart2.Init.Parity = UART_PARITY_NONE;
  66. huart2.Init.Mode = UART_MODE_TX_RX;
  67. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  68. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  69. if (HAL_UART_Init(&huart2) != HAL_OK)
  70. {
  71. Error_Handler();
  72. }
  73. }
  74. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  75. {
  76. GPIO_InitTypeDef GPIO_InitStruct = {0};
  77. if(uartHandle->Instance==UART4)
  78. {
  79. /* USER CODE BEGIN UART4_MspInit 0 */
  80. /* USER CODE END UART4_MspInit 0 */
  81. /* UART4 clock enable */
  82. __HAL_RCC_UART4_CLK_ENABLE();
  83. __HAL_RCC_GPIOC_CLK_ENABLE();
  84. /**UART4 GPIO Configuration
  85. PC10 ------> UART4_TX
  86. PC11 ------> UART4_RX
  87. */
  88. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  89. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  90. GPIO_InitStruct.Pull = GPIO_PULLUP;
  91. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  92. GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  93. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  94. /* UART4 interrupt Init */
  95. HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
  96. HAL_NVIC_EnableIRQ(UART4_IRQn);
  97. /* USER CODE BEGIN UART4_MspInit 1 */
  98. /* USER CODE END UART4_MspInit 1 */
  99. }
  100. else if(uartHandle->Instance==USART1)
  101. {
  102. /* USER CODE BEGIN USART1_MspInit 0 */
  103. /* USER CODE END USART1_MspInit 0 */
  104. /* USART1 clock enable */
  105. __HAL_RCC_USART1_CLK_ENABLE();
  106. __HAL_RCC_GPIOB_CLK_ENABLE();
  107. /**USART1 GPIO Configuration
  108. PB6 ------> USART1_TX
  109. PB7 ------> USART1_RX
  110. */
  111. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  112. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  113. GPIO_InitStruct.Pull = GPIO_PULLUP;
  114. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  115. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  116. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  117. /* USART1 interrupt Init */
  118. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  119. HAL_NVIC_EnableIRQ(USART1_IRQn);
  120. /* USER CODE BEGIN USART1_MspInit 1 */
  121. /* USER CODE END USART1_MspInit 1 */
  122. }
  123. else if(uartHandle->Instance==USART2)
  124. {
  125. /* USER CODE BEGIN USART2_MspInit 0 */
  126. /* USER CODE END USART2_MspInit 0 */
  127. /* USART2 clock enable */
  128. __HAL_RCC_USART2_CLK_ENABLE();
  129. __HAL_RCC_GPIOD_CLK_ENABLE();
  130. /**USART2 GPIO Configuration
  131. PD5 ------> USART2_TX
  132. PD6 ------> USART2_RX
  133. */
  134. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
  135. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  136. GPIO_InitStruct.Pull = GPIO_PULLUP;
  137. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  138. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  139. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  140. /* USART2 interrupt Init */
  141. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  142. HAL_NVIC_EnableIRQ(USART2_IRQn);
  143. /* USER CODE BEGIN USART2_MspInit 1 */
  144. /* USER CODE END USART2_MspInit 1 */
  145. }
  146. }
  147. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  148. {
  149. if(uartHandle->Instance==UART4)
  150. {
  151. /* USER CODE BEGIN UART4_MspDeInit 0 */
  152. /* USER CODE END UART4_MspDeInit 0 */
  153. /* Peripheral clock disable */
  154. __HAL_RCC_UART4_CLK_DISABLE();
  155. /**UART4 GPIO Configuration
  156. PC10 ------> UART4_TX
  157. PC11 ------> UART4_RX
  158. */
  159. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
  160. /* UART4 interrupt Deinit */
  161. HAL_NVIC_DisableIRQ(UART4_IRQn);
  162. /* USER CODE BEGIN UART4_MspDeInit 1 */
  163. /* USER CODE END UART4_MspDeInit 1 */
  164. }
  165. else if(uartHandle->Instance==USART1)
  166. {
  167. /* USER CODE BEGIN USART1_MspDeInit 0 */
  168. /* USER CODE END USART1_MspDeInit 0 */
  169. /* Peripheral clock disable */
  170. __HAL_RCC_USART1_CLK_DISABLE();
  171. /**USART1 GPIO Configuration
  172. PB6 ------> USART1_TX
  173. PB7 ------> USART1_RX
  174. */
  175. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
  176. /* USART1 interrupt Deinit */
  177. HAL_NVIC_DisableIRQ(USART1_IRQn);
  178. /* USER CODE BEGIN USART1_MspDeInit 1 */
  179. /* USER CODE END USART1_MspDeInit 1 */
  180. }
  181. else if(uartHandle->Instance==USART2)
  182. {
  183. /* USER CODE BEGIN USART2_MspDeInit 0 */
  184. /* USER CODE END USART2_MspDeInit 0 */
  185. /* Peripheral clock disable */
  186. __HAL_RCC_USART2_CLK_DISABLE();
  187. /**USART2 GPIO Configuration
  188. PD5 ------> USART2_TX
  189. PD6 ------> USART2_RX
  190. */
  191. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
  192. /* USART2 interrupt Deinit */
  193. HAL_NVIC_DisableIRQ(USART2_IRQn);
  194. /* USER CODE BEGIN USART2_MspDeInit 1 */
  195. /* USER CODE END USART2_MspDeInit 1 */
  196. }
  197. }
  198. /* USER CODE BEGIN 1 */
  199. #ifdef __GNUC__
  200. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  201. set to 'Yes') calls __io_putchar() */
  202. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  203. #else
  204. #define PUTCHAR_PROTOTYPE int fputc(int ch)
  205. #endif /* __GNUC__ */
  206. /**
  207. * @brief Retargets the C library printf function to the USART.
  208. * @param None
  209. * @retval None
  210. */
  211. PUTCHAR_PROTOTYPE
  212. {
  213. /* Place your implementation of fputc here */
  214. /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  215. HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 0xFFFF);
  216. return ch;
  217. }
  218. /* USER CODE END 1 */
  219. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/