| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /**
- ******************************************************************************
- * @file usart.c
- * @brief This file provides code for the configuration
- * of the USART instances.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- //#include "stdio.h"
- #include "string.h"
- #include "usart/usart.h"
- #include <rtthread.h>
- #include "boot/boot.h"
- UART_HandleTypeDef UartHandle;
- void MX_USART1_UART_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- __HAL_RCC_USART1_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- //__HAL_RCC_GPIOA_CLK_ENABLE();
- /**USART1 GPIO Configuration
- PB6 ------> USART1_TX
- PB7 ------> USART1_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF0_USART1;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- UartHandle.Instance = USART1;
- UartHandle.Init.BaudRate = 9600;//
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.Mode = UART_MODE_RX | UART_MODE_TX;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
- UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&UartHandle) != HAL_OK)
- {
- ;
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&UartHandle, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- ;
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&UartHandle, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- ;
- }
- //必须打开!!!!
- if(HAL_UARTEx_EnableFifoMode(&UartHandle) != HAL_OK)
- //if (HAL_UARTEx_DisableFifoMode(&UartHandle) != HAL_OK)
- {
- ;
- }
- }
- //超级终端上打印字符串
- void Serial_PutString(uint8_t *p_string,uint8_t p_len)
- {
- HAL_UART_Transmit(&UartHandle, p_string, p_len, TX_TIMEOUT);
- }
- //向超级终端传输一个字节
- HAL_StatusTypeDef Serial_PutByte( uint8_t param )
- {
- /* May be timeouted... */
- if (UartHandle.gState == HAL_TIMEOUT )
- {
- UartHandle.gState = HAL_OK;
- }
- return HAL_UART_Transmit(&UartHandle, ¶m, 1, TX_TIMEOUT);
- }
|