usart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. ******************************************************************************
  3. * @file usart.c
  4. * @brief This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 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. typedef struct __FILE FILE;
  23. /* USER CODE END 0 */
  24. UART_HandleTypeDef huart1;
  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. /* USER CODE BEGIN USART1_Init 1 */
  31. /* USER CODE END USART1_Init 1 */
  32. huart1.Instance = USART1;
  33. huart1.Init.BaudRate = 115200;
  34. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  35. huart1.Init.StopBits = UART_STOPBITS_1;
  36. huart1.Init.Parity = UART_PARITY_NONE;
  37. huart1.Init.Mode = UART_MODE_TX_RX;
  38. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  39. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  40. if (HAL_UART_Init(&huart1) != HAL_OK)
  41. {
  42. Error_Handler();
  43. }
  44. /* USER CODE BEGIN USART1_Init 2 */
  45. /* USER CODE END USART1_Init 2 */
  46. }
  47. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  48. {
  49. GPIO_InitTypeDef GPIO_InitStruct = {0};
  50. if(uartHandle->Instance==USART1)
  51. {
  52. /* USER CODE BEGIN USART1_MspInit 0 */
  53. /* USER CODE END USART1_MspInit 0 */
  54. /* USART1 clock enable */
  55. __HAL_RCC_USART1_CLK_ENABLE();
  56. __HAL_RCC_GPIOA_CLK_ENABLE();
  57. /**USART1 GPIO Configuration
  58. PA9 ------> USART1_TX
  59. PA10 ------> USART1_RX
  60. */
  61. GPIO_InitStruct.Pin = GPIO_PIN_9;
  62. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  63. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  64. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  65. GPIO_InitStruct.Pin = GPIO_PIN_10;
  66. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  67. GPIO_InitStruct.Pull = GPIO_NOPULL;
  68. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  69. /* USER CODE BEGIN USART1_MspInit 1 */
  70. /* USER CODE END USART1_MspInit 1 */
  71. }
  72. }
  73. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  74. {
  75. if(uartHandle->Instance==USART1)
  76. {
  77. /* USER CODE BEGIN USART1_MspDeInit 0 */
  78. /* USER CODE END USART1_MspDeInit 0 */
  79. /* Peripheral clock disable */
  80. __HAL_RCC_USART1_CLK_DISABLE();
  81. /**USART1 GPIO Configuration
  82. PA9 ------> USART1_TX
  83. PA10 ------> USART1_RX
  84. */
  85. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  86. /* USER CODE BEGIN USART1_MspDeInit 1 */
  87. /* USER CODE END USART1_MspDeInit 1 */
  88. }
  89. }
  90. /* USER CODE BEGIN 1 */
  91. #ifdef __GNUC__
  92. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  93. set to 'Yes') calls __io_putchar() */
  94. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  95. #else
  96. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  97. #endif /* __GNUC__ */
  98. /**
  99. * @brief Retargets the C library printf function to the USART.
  100. * @param None
  101. * @retval None
  102. */
  103. PUTCHAR_PROTOTYPE
  104. {
  105. /* Place your implementation of fputc here */
  106. /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  107. HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  108. return ch;
  109. }
  110. /* USER CODE END 1 */
  111. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/