实现串口驱动,移植方便

This commit is contained in:
冯佳
2026-01-22 16:36:56 +08:00
parent 2ef4dac5bd
commit 51e8d79f78
151 changed files with 4064 additions and 11050 deletions

32
HAL/Inc/hal_delay.h Normal file
View File

@ -0,0 +1,32 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_delay.h
* @brief : Delay hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_DELAY_H
#define HAL_DELAY_H
#include <stdint.h>
/**
* @brief Initialize delay module
*/
void hal_delay_init(void);
/**
* @brief Delay in milliseconds
* @param ms: Delay time in milliseconds
*/
void hal_delay_ms(uint32_t ms);
/**
* @brief Delay in microseconds
* @param us: Delay time in microseconds
*/
void hal_delay_us(uint32_t us);
#endif /* HAL_DELAY_H */

97
HAL/Inc/hal_gpio.h Normal file
View File

@ -0,0 +1,97 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_gpio.h
* @brief : GPIO hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_GPIO_H
#define HAL_GPIO_H
/**
* @brief GPIO pin state definitions
*/
typedef enum {
HAL_GPIO_PIN_RESET = 0U,
HAL_GPIO_PIN_SET = 1U
} hal_gpio_pin_state_t;
/**
* @brief GPIO pin definitions
*/
typedef enum {
HAL_GPIO_PIN_0 = 0U,
HAL_GPIO_PIN_1 = 1U,
HAL_GPIO_PIN_2 = 2U,
HAL_GPIO_PIN_3 = 3U,
HAL_GPIO_PIN_4 = 4U,
HAL_GPIO_PIN_5 = 5U,
HAL_GPIO_PIN_6 = 6U,
HAL_GPIO_PIN_7 = 7U,
HAL_GPIO_PIN_8 = 8U,
HAL_GPIO_PIN_9 = 9U,
HAL_GPIO_PIN_10 = 10U,
HAL_GPIO_PIN_11 = 11U,
HAL_GPIO_PIN_12 = 12U,
HAL_GPIO_PIN_13 = 13U,
HAL_GPIO_PIN_14 = 14U,
HAL_GPIO_PIN_15 = 15U,
HAL_GPIO_PIN_ALL = 0xFFFFU
} hal_gpio_pin_t;
/**
* @brief GPIO port definitions
*/
typedef enum {
HAL_GPIO_PORT_A = 0U,
HAL_GPIO_PORT_B = 1U,
HAL_GPIO_PORT_C = 2U,
HAL_GPIO_PORT_D = 3U,
HAL_GPIO_PORT_E = 4U,
HAL_GPIO_PORT_F = 5U,
HAL_GPIO_PORT_G = 6U,
HAL_GPIO_PORT_H = 7U,
HAL_GPIO_PORT_I = 8U,
HAL_GPIO_PORT_J = 9U,
HAL_GPIO_PORT_K = 10U
} hal_gpio_port_t;
/**
* @brief GPIO configuration structure
*/
typedef struct {
hal_gpio_port_t port;
hal_gpio_pin_t pin;
} hal_gpio_config_t;
/**
* @brief Initialize GPIO hardware
*/
void hal_gpio_init(void);
/**
* @brief Write GPIO pin state
* @param port: GPIO port
* @param pin: GPIO pin
* @param state: GPIO pin state
*/
void hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_state_t state);
/**
* @brief Toggle GPIO pin state
* @param port: GPIO port
* @param pin: GPIO pin
*/
void hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin);
/**
* @brief Read GPIO pin state
* @param port: GPIO port
* @param pin: GPIO pin
* @retval GPIO pin state
*/
hal_gpio_pin_state_t hal_gpio_read_pin(hal_gpio_port_t port, hal_gpio_pin_t pin);
#endif /* HAL_GPIO_H */

89
HAL/Inc/hal_uart.h Normal file
View File

@ -0,0 +1,89 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_uart.h
* @brief : UART hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_UART_H
#define HAL_UART_H
#include <stdint.h>
#include <stddef.h>
/**
* @brief UART parity definitions
*/
typedef enum {
HAL_UART_PARITY_NONE = 0U,
HAL_UART_PARITY_ODD = 1U,
HAL_UART_PARITY_EVEN = 2U
} hal_uart_parity_t;
/**
* @brief UART stop bits definitions
*/
typedef enum {
HAL_UART_STOPBITS_1 = 0U,
HAL_UART_STOPBITS_2 = 1U
} hal_uart_stopbits_t;
/**
* @brief UART data bits definitions
*/
typedef enum {
HAL_UART_DATABITS_8 = 0U,
HAL_UART_DATABITS_9 = 1U
} hal_uart_databits_t;
/**
* @brief UART configuration structure
*/
typedef struct {
uint32_t baudrate;
hal_uart_parity_t parity;
hal_uart_stopbits_t stopbits;
hal_uart_databits_t databits;
} hal_uart_config_t;
/**
* @brief Initialize UART hardware
*/
void hal_uart_init(void);
/**
* @brief Configure UART parameters
* @param config: UART configuration structure
*/
void hal_uart_config(const hal_uart_config_t *config);
/**
* @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);
/**
* @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);
/**
* @brief Check if UART is ready to send
* @retval 1 if ready, 0 otherwise
*/
uint8_t hal_uart_is_tx_ready(void);
/**
* @brief Check if UART has data to receive
* @retval 1 if data available, 0 otherwise
*/
uint8_t hal_uart_is_rx_ready(void);
#endif /* HAL_UART_H */