You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

//
// Created by lidong on 25-8-4.
//
#include "usart.h"
#ifdef HAL_UART_MODULE_ENABLED
UART_HandleTypeDef DebugUartHandle;
/**
* @brief DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
* @param None
* @retval None
*/
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
#if defined(__GNUC__)
setvbuf(stdout, NULL, _IONBF, 0 );
#endif
__HAL_RCC_USART2_CLK_ENABLE();
DebugUartHandle.Instance = DEBUG_USART;
DebugUartHandle.Init.BaudRate = USART_BAUDRATE;
DebugUartHandle.Init.WordLength = UART_WORDLENGTH_8B;
DebugUartHandle.Init.StopBits = UART_STOPBITS_1;
DebugUartHandle.Init.Parity = UART_PARITY_NONE;
DebugUartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
DebugUartHandle.Init.Mode = UART_MODE_TX_RX;
DebugUartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
DebugUartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_Init(&DebugUartHandle);
USART_RX_GPIO_CLK_ENABLE();
USART_TX_GPIO_CLK_ENABLE();
/**USART GPIO Configuration
PA2 ------> USART1_TX
PA3 ------> USART1_RX
*/
GPIO_InitStruct.Pin = USART_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USART_TX_AF;
HAL_GPIO_Init(USART_TX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USART_RX_PIN;
GPIO_InitStruct.Alternate = USART_RX_AF;
HAL_GPIO_Init(USART_RX_GPIO_PORT, &GPIO_InitStruct);
/* ENABLE NVIC */
HAL_NVIC_SetPriority(USART_IRQ,0,1);
HAL_NVIC_EnableIRQ(USART_IRQ );
}
#if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/**
* @brief writes a character to the usart
* @param ch
* *f
* @retval the character
*/
int fputc(int ch, FILE *f)
{
/* Send a byte to USART */
HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
return (ch);
}
/**
* @brief get a character from the usart
* @param *f
* @retval a character
*/
int fgetc(FILE *f)
{
int ch;
HAL_UART_Receive(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
return (ch);
}
#elif defined(__ICCARM__)
/**
* @brief writes a character to the usart
* @param ch
* *f
* @retval the character
*/
int putchar(int ch)
{
/* Send a byte to USART */
HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
return (ch);
}
#elif defined(__GNUC__)
/**
* @brief writes a character to the usart
* @param ch
* @retval the character
*/
int __io_putchar(int ch)
{
/* Send a byte to USART */
HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
return ch;
}
int _write(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx=0;DataIdx<len;DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
#endif
#endif