实现串口驱动,移植方便
This commit is contained in:
104
Modules/uart/src/uart.c
Normal file
104
Modules/uart/src/uart.c
Normal file
@ -0,0 +1,104 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : uart.c
|
||||
* @brief : UART driver module source file
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "uart.h"
|
||||
#include <string.h>
|
||||
|
||||
/* UART instance */
|
||||
static uart_t uart;
|
||||
|
||||
/**
|
||||
* @brief Initialize UART module
|
||||
*/
|
||||
void uart_init(void) {
|
||||
/* Initialize hardware abstraction layer */
|
||||
hal_uart_init();
|
||||
|
||||
/* Set default configuration */
|
||||
uart.config.baudrate = 115200;
|
||||
uart.config.parity = HAL_UART_PARITY_NONE;
|
||||
uart.config.stopbits = HAL_UART_STOPBITS_1;
|
||||
uart.config.databits = HAL_UART_DATABITS_8;
|
||||
|
||||
/* Configure UART with default settings */
|
||||
hal_uart_config((hal_uart_config_t *)&uart.config);
|
||||
|
||||
uart.initialized = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure UART parameters
|
||||
* @param config: UART configuration structure
|
||||
*/
|
||||
void uart_config(const uart_config_t *config) {
|
||||
if (config == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Update configuration */
|
||||
uart.config = *config;
|
||||
|
||||
/* Configure hardware */
|
||||
hal_uart_config((hal_uart_config_t *)&uart.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send data over UART
|
||||
* @param data: Pointer to data buffer
|
||||
* @param length: Data length in bytes
|
||||
*/
|
||||
void uart_send(const uint8_t *data, size_t length) {
|
||||
if (data == NULL || length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
hal_uart_send(data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send string over UART
|
||||
* @param str: Pointer to string
|
||||
*/
|
||||
void uart_send_string(const char *str) {
|
||||
if (str == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
hal_uart_send((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 uart_receive(uint8_t *data, size_t length) {
|
||||
if (data == NULL || length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hal_uart_receive(data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if UART is ready to send
|
||||
* @retval 1 if ready, 0 otherwise
|
||||
*/
|
||||
uint8_t uart_is_tx_ready(void) {
|
||||
return hal_uart_is_tx_ready();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if UART has data to receive
|
||||
* @retval 1 if data available, 0 otherwise
|
||||
*/
|
||||
uint8_t uart_is_rx_ready(void) {
|
||||
return hal_uart_is_rx_ready();
|
||||
}
|
||||
Reference in New Issue
Block a user