usart.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "usart.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. /* USART1 init function */
  24. void MX_USART1_UART_Init(void)
  25. {
  26. LL_USART_InitTypeDef USART_InitStruct = {0};
  27. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  28. /* Peripheral clock enable */
  29. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
  30. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  31. /**USART1 GPIO Configuration
  32. PB6 ------> USART1_TX
  33. PB7 ------> USART1_RX
  34. */
  35. GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  36. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  37. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  38. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  39. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  40. GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
  41. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  42. USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
  43. USART_InitStruct.BaudRate = 115200;
  44. USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  45. USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  46. USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  47. USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
  48. USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  49. USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  50. LL_USART_Init(USART1, &USART_InitStruct);
  51. LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
  52. LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
  53. LL_USART_DisableFIFO(USART1);
  54. LL_USART_EnableAutoBaudRate(USART1);
  55. LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_STARTBIT);
  56. LL_USART_ConfigAsyncMode(USART1);
  57. /* USER CODE BEGIN WKUPType USART1 */
  58. /* USER CODE END WKUPType USART1 */
  59. LL_USART_Enable(USART1);
  60. /* Polling USART1 initialisation */
  61. while((!(LL_USART_IsActiveFlag_TEACK(USART1))) || (!(LL_USART_IsActiveFlag_REACK(USART1))))
  62. {
  63. }
  64. }
  65. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/