实现串口驱动,移植方便
This commit is contained in:
154
HAL/Src/hal_uart.c
Normal file
154
HAL/Src/hal_uart.c
Normal file
@ -0,0 +1,154 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : hal_uart.c
|
||||
* @brief : UART hardware abstraction layer source file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "hal_uart.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_hal_uart.h"
|
||||
|
||||
/* UART handle */
|
||||
static UART_HandleTypeDef huart1;
|
||||
|
||||
/**
|
||||
* @brief Initialize UART hardware
|
||||
*/
|
||||
void hal_uart_init(void) {
|
||||
/* UART initialization is handled by HAL_Init() */
|
||||
|
||||
/* Configure UART GPIO pins */
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* Enable UART clock */
|
||||
__HAL_RCC_USART1_CLK_ENABLE();
|
||||
|
||||
/* Enable GPIO clocks */
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
/* Configure USART1 Tx (PA9) as alternate function push-pull */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* Configure USART1 Rx (PA10) as alternate function push-pull */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* Initialize UART */
|
||||
huart1.Instance = USART1;
|
||||
huart1.Init.BaudRate = 115200;
|
||||
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;
|
||||
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK) {
|
||||
/* Initialization Error */
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure UART parameters
|
||||
* @param config: UART configuration structure
|
||||
*/
|
||||
void hal_uart_config(const hal_uart_config_t *config) {
|
||||
if (config == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Update UART configuration */
|
||||
huart1.Init.BaudRate = config->baudrate;
|
||||
|
||||
/* Convert parity */
|
||||
switch (config->parity) {
|
||||
case HAL_UART_PARITY_NONE:
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
break;
|
||||
case HAL_UART_PARITY_ODD:
|
||||
huart1.Init.Parity = UART_PARITY_ODD;
|
||||
break;
|
||||
case HAL_UART_PARITY_EVEN:
|
||||
huart1.Init.Parity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default:
|
||||
huart1.Init.Parity = UART_PARITY_NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Convert stop bits */
|
||||
huart1.Init.StopBits = (config->stopbits == HAL_UART_STOPBITS_1) ?
|
||||
UART_STOPBITS_1 : UART_STOPBITS_2;
|
||||
|
||||
/* Convert data bits */
|
||||
huart1.Init.WordLength = (config->databits == HAL_UART_DATABITS_8) ?
|
||||
UART_WORDLENGTH_8B : UART_WORDLENGTH_9B;
|
||||
|
||||
/* Re-initialize UART */
|
||||
if (HAL_UART_Init(&huart1) != HAL_OK) {
|
||||
/* Initialization Error */
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send data over UART
|
||||
* @param data: Pointer to data buffer
|
||||
* @param length: Data length in bytes
|
||||
*/
|
||||
void hal_uart_send(const uint8_t *data, size_t length) {
|
||||
if (data == NULL || length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_UART_Transmit(&huart1, (uint8_t *)data, length, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Receive data over UART
|
||||
* @param data: Pointer to data buffer
|
||||
* @param length: Data length to receive in bytes
|
||||
* @retval Number of bytes received
|
||||
*/
|
||||
size_t hal_uart_receive(uint8_t *data, size_t length) {
|
||||
if (data == NULL || length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef status = HAL_UART_Receive(&huart1, data, length, HAL_MAX_DELAY);
|
||||
|
||||
if (status == HAL_OK) {
|
||||
return length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if UART is ready to send
|
||||
* @retval 1 if ready, 0 otherwise
|
||||
*/
|
||||
uint8_t hal_uart_is_tx_ready(void) {
|
||||
return (HAL_UART_GetState(&huart1) == HAL_UART_STATE_READY) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if UART has data to receive
|
||||
* @retval 1 if data available, 0 otherwise
|
||||
*/
|
||||
uint8_t hal_uart_is_rx_ready(void) {
|
||||
return (HAL_UART_GetState(&huart1) == HAL_UART_STATE_READY) ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user