usart.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "stdio.h"
  21. #include "string.h"
  22. #include "usart/usart.h"
  23. #include <rtthread.h>
  24. #include "boot/boot.h"
  25. UART_HandleTypeDef UartHandle;
  26. void MX_USART1_UART_Init(void)
  27. {
  28. GPIO_InitTypeDef GPIO_InitStruct = {0};
  29. __HAL_RCC_USART1_CLK_ENABLE();
  30. __HAL_RCC_GPIOB_CLK_ENABLE();
  31. //__HAL_RCC_GPIOA_CLK_ENABLE();
  32. /**USART1 GPIO Configuration
  33. PB6 ------> USART1_TX
  34. PB7 ------> USART1_RX
  35. */
  36. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
  37. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  38. GPIO_InitStruct.Pull = GPIO_PULLUP;
  39. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  40. GPIO_InitStruct.Alternate = GPIO_AF0_USART1;
  41. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  42. /* USER CODE BEGIN USART2_Init 0 */
  43. /* USER CODE END USART2_Init 0 */
  44. /* USER CODE BEGIN USART2_Init 1 */
  45. /* USER CODE END USART2_Init 1 */
  46. UartHandle.Instance = USART1;
  47. UartHandle.Init.BaudRate = 9600;//
  48. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  49. UartHandle.Init.StopBits = UART_STOPBITS_1;
  50. UartHandle.Init.Parity = UART_PARITY_NONE;
  51. UartHandle.Init.Mode = UART_MODE_RX | UART_MODE_TX;
  52. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  53. UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  54. UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  55. UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  56. UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  57. if (HAL_UART_Init(&UartHandle) != HAL_OK)
  58. {
  59. ;
  60. }
  61. if (HAL_UARTEx_SetTxFifoThreshold(&UartHandle, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  62. {
  63. ;
  64. }
  65. if (HAL_UARTEx_SetRxFifoThreshold(&UartHandle, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  66. {
  67. ;
  68. }
  69. //必须打开!!!!
  70. if(HAL_UARTEx_EnableFifoMode(&UartHandle) != HAL_OK)
  71. //if (HAL_UARTEx_DisableFifoMode(&UartHandle) != HAL_OK)
  72. {
  73. ;
  74. }
  75. }
  76. //超级终端上打印字符串
  77. void Serial_PutString(uint8_t *p_string,uint8_t p_len)
  78. {
  79. HAL_UART_Transmit(&UartHandle, p_string, p_len, TX_TIMEOUT);
  80. }
  81. //向超级终端传输一个字节
  82. HAL_StatusTypeDef Serial_PutByte( uint8_t param )
  83. {
  84. /* May be timeouted... */
  85. if (UartHandle.gState == HAL_TIMEOUT )
  86. {
  87. UartHandle.gState = HAL_OK;
  88. }
  89. return HAL_UART_Transmit(&UartHandle, &param, 1, TX_TIMEOUT);
  90. }