#include "UART\bsp_uart.h"
#include "stdio.h"
UART_HandleTypeDef huart1;
/* 加入以下代码, 支持printf函数, 而不需要选择use MicroLIB */
//1个预处理+2个定义+3个函数+1个重映射
#pragma import(__use_no_semihosting)//确保不从C库中使用半主机函数
struct __FILE{ //避免HAL库某些情况下报错
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
FILE __stdout; /* FILE 在 stdio.h里面定义. 避免HAL库某些情况下报错*/
/* 不使用半主机模式,至少需要重定义_ttywrch\_sys_exit\_sys_command_string函数,以同时兼容AC6和AC5模式 */
int _ttywrch(int ch){
ch = ch;
return ch;
}
void _sys_exit(int x){
x = x;
}
char *_sys_command_string(char *cmd, int len){
return NULL;
}
/* MDK下需要重定义fputc函数, printf函数最终会通过调用fputc输出字符串到串口 */
int fputc(int ch, FILE *f){
while ((USART1->ISR & 0X40) == 0); /* 等待上一个字符发送完成 */
USART1->TDR = (uint8_t)ch; /* 将要发送的字符 ch 写入到DR寄存器 */
return ch;
}
///* 需要选择use MicroLIB */
//int fputc(int ch, FILE *f){
// uint8_t temp[1] = {ch};
// HAL_UART_Transmit(&huart1, temp, 1, 50);//huart1需要根据你的配置修改
// return ch;
//}
DMA_HandleTypeDef hdma_usart1_tx;
void UART1_Init(void){
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle){
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(uartHandle->Instance==USART1)
{
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 DMA Init */
/* USART1_TX Init */
hdma_usart1_tx.Instance = DMA2_Channel4; /*usart1 tx 对应 dma2,通道 4,数据流 7*/
hdma_usart1_tx.Init.Request = DMA_REQUEST_USART1_TX; /* 串口1发送DMA请求 */
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; /* 方向:从内存到外设 */
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE; /* 外设地址不增 */
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE; /* 内存地址自增 */
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; /* 外设数据单位 **/
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; /* 内存数据单位 8bit*/
hdma_usart1_tx.Init.Mode = DMA_NORMAL; /*DMA 模式:不循环 */
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH; /* 优先级:高 */
if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK) /* Configure the DMA stream */
{
Error_Handler();
}
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx); /* Associate the DMA handle */
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle){
if(uartHandle->Instance==USART1)
{
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
}
}
LQBMK|UART\bsp_uart.h
于 2024-10-30 06:53:53 首次发布