优化实现串口驱动,SPI驱动 W25QXX还需要初始化验证修复

This commit is contained in:
冯佳
2026-01-23 09:59:43 +08:00
parent 51e8d79f78
commit b166bee1a9
69 changed files with 6253 additions and 1178 deletions

View File

@ -0,0 +1,26 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_stm32f4.h
* @brief : STM32F4 architecture specific HAL header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_STM32F4_H
#define HAL_STM32F4_H
/* Include STM32F4 HAL headers */
#include "stm32f4xx_hal.h"
/* Include common HAL headers */
#include "../../hal_gpio.h"
#include "../../hal_uart.h"
#include "../../hal_delay.h"
/**
* @brief STM32F4 specific initialization
*/
void hal_stm32f4_init(void);
#endif /* HAL_STM32F4_H */

View File

@ -0,0 +1,59 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_stm32f4_spi.h
* @brief : STM32F4 specific SPI HAL header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef __HAL_STM32F4_SPI_H__
#define __HAL_STM32F4_SPI_H__
#include "hal.h"
#include "hal_spi.h"
/**
* @brief STM32F4 specific SPI initialization
* @param instance SPI instance identifier
* @param config SPI configuration structure
* @return true if initialization is successful, false otherwise
*/
bool hal_stm32f4_spi_init(hal_spi_instance_t instance, const hal_spi_config_t *config);
/**
* @brief STM32F4 specific SPI deinitialization
* @param instance SPI instance identifier
* @return true if deinitialization is successful, false otherwise
*/
bool hal_stm32f4_spi_deinit(hal_spi_instance_t instance);
/**
* @brief STM32F4 specific SPI transmit implementation
* @param instance SPI instance identifier
* @param p_data Pointer to data buffer to transmit
* @param size Size of data to transmit
* @return true if transmission is successful, false otherwise
*/
bool hal_stm32f4_spi_transmit(hal_spi_instance_t instance, const uint8_t *p_data, uint16_t size);
/**
* @brief STM32F4 specific SPI receive implementation
* @param instance SPI instance identifier
* @param p_data Pointer to data buffer to receive
* @param size Size of data to receive
* @return true if reception is successful, false otherwise
*/
bool hal_stm32f4_spi_receive(hal_spi_instance_t instance, uint8_t *p_data, uint16_t size);
/**
* @brief STM32F4 specific SPI transmit and receive implementation
* @param instance SPI instance identifier
* @param p_tx_data Pointer to data buffer to transmit
* @param p_rx_data Pointer to data buffer to receive
* @param size Size of data to transmit/receive
* @return true if transmission and reception are successful, false otherwise
*/
bool hal_stm32f4_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *p_tx_data, uint8_t *p_rx_data, uint16_t size);
#endif /* __HAL_STM32F4_SPI_H__ */

47
HAL/Inc/hal.h Normal file
View File

@ -0,0 +1,47 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal.h
* @brief : Hardware Abstraction Layer common header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_H
#define HAL_H
/* Define supported architectures */
#define HAL_ARCH_STM32F1 0
#define HAL_ARCH_STM32F4 1
#define HAL_ARCH_STM32F7 2
#define HAL_ARCH_STM32L4 3
/* Select target architecture */
#ifndef HAL_TARGET_ARCH
#define HAL_TARGET_ARCH HAL_ARCH_STM32F4
#endif
/* Include architecture specific headers */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
#include "arch/stm32f1/hal_stm32f1.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
#include "arch/stm32f4/hal_stm32f4.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
#include "arch/stm32f7/hal_stm32f7.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
#include "arch/stm32l4/hal_stm32l4.h"
#else
#error "Unsupported HAL architecture: " #HAL_TARGET_ARCH
#endif
/* Architecture compatibility check */
#if HAL_TARGET_ARCH < HAL_ARCH_STM32F1 || HAL_TARGET_ARCH > HAL_ARCH_STM32L4
#error "Invalid HAL architecture selection"
#endif
/**
* @brief HAL module initialization
*/
void hal_init(void);
#endif /* HAL_H */

View File

@ -58,12 +58,49 @@ typedef enum {
HAL_GPIO_PORT_K = 10U
} hal_gpio_port_t;
/**
* @brief GPIO mode definitions
*/
typedef enum {
HAL_GPIO_MODE_INPUT = 0U,
HAL_GPIO_MODE_OUTPUT_PP = 1U,
HAL_GPIO_MODE_OUTPUT_OD = 2U,
HAL_GPIO_MODE_AF_PP = 3U,
HAL_GPIO_MODE_AF_OD = 4U,
HAL_GPIO_MODE_ANALOG = 5U,
HAL_GPIO_MODE_IT_RISING = 6U,
HAL_GPIO_MODE_IT_FALLING = 7U,
HAL_GPIO_MODE_IT_RISING_FALLING = 8U
} hal_gpio_mode_t;
/**
* @brief GPIO speed definitions
*/
typedef enum {
HAL_GPIO_SPEED_LOW = 0U,
HAL_GPIO_SPEED_MEDIUM = 1U,
HAL_GPIO_SPEED_HIGH = 2U,
HAL_GPIO_SPEED_VERY_HIGH = 3U
} hal_gpio_speed_t;
/**
* @brief GPIO pull-up/pull-down definitions
*/
typedef enum {
HAL_GPIO_PULL_NO = 0U,
HAL_GPIO_PULL_UP = 1U,
HAL_GPIO_PULL_DOWN = 2U
} hal_gpio_pull_t;
/**
* @brief GPIO configuration structure
*/
typedef struct {
hal_gpio_port_t port;
hal_gpio_pin_t pin;
hal_gpio_mode_t mode;
hal_gpio_speed_t speed;
hal_gpio_pull_t pull;
} hal_gpio_config_t;
/**
@ -71,6 +108,12 @@ typedef struct {
*/
void hal_gpio_init(void);
/**
* @brief Configure GPIO pin
* @param config: GPIO configuration structure
*/
void hal_gpio_configure_pin(const hal_gpio_config_t *config);
/**
* @brief Write GPIO pin state
* @param port: GPIO port

117
HAL/Inc/hal_spi.h Normal file
View File

@ -0,0 +1,117 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_spi.h
* @brief : SPI hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_SPI_H
#define HAL_SPI_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
/**
* @brief SPI instance identifier definitions
*/
typedef enum {
HAL_SPI_INSTANCE_1 = 0,
HAL_SPI_INSTANCE_2,
HAL_SPI_INSTANCE_3,
HAL_SPI_INSTANCE_4,
HAL_SPI_INSTANCE_5,
HAL_SPI_INSTANCE_6,
HAL_SPI_INSTANCE_MAX
} hal_spi_instance_t;
/**
* @brief SPI mode definitions
*/
typedef enum {
HAL_SPI_MODE_MASTER = 0,
HAL_SPI_MODE_SLAVE
} hal_spi_mode_t;
/**
* @brief SPI clock polarity definitions
*/
typedef enum {
HAL_SPI_POLARITY_LOW = 0,
HAL_SPI_POLARITY_HIGH
} hal_spi_polarity_t;
/**
* @brief SPI clock phase definitions
*/
typedef enum {
HAL_SPI_PHASE_1EDGE = 0,
HAL_SPI_PHASE_2EDGE
} hal_spi_phase_t;
/**
* @brief SPI data bits definitions
*/
typedef enum {
HAL_SPI_DATABITS_8 = 0,
HAL_SPI_DATABITS_16
} hal_spi_databits_t;
/**
* @brief SPI configuration structure
*/
typedef struct {
hal_spi_instance_t instance;
hal_spi_mode_t mode;
uint32_t baudrate;
hal_spi_polarity_t polarity;
hal_spi_phase_t phase;
hal_spi_databits_t databits;
} hal_spi_config_t;
/**
* @brief Initialize SPI hardware
* @param instance SPI instance identifier
* @param config SPI configuration structure
* @return true if initialization is successful, false otherwise
*/
bool hal_spi_init(hal_spi_instance_t instance, const hal_spi_config_t *config);
/**
* @brief Deinitialize SPI hardware
* @param instance SPI instance identifier
* @return true if deinitialization is successful, false otherwise
*/
bool hal_spi_deinit(hal_spi_instance_t instance);
/**
* @brief Transmit data over specific SPI instance
* @param instance: SPI instance identifier
* @param data: Pointer to data buffer
* @param size: Data length in bytes
* @return true if transmission is successful, false otherwise
*/
bool hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *data, uint16_t size);
/**
* @brief Receive data from specific SPI instance
* @param instance: SPI instance identifier
* @param data: Pointer to data buffer
* @param size: Data length to receive in bytes
* @return true if reception is successful, false otherwise
*/
bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *data, uint16_t size);
/**
* @brief Transmit and receive data simultaneously over specific SPI instance
* @param instance: SPI instance identifier
* @param tx_data: Pointer to transmit data buffer
* @param rx_data: Pointer to receive data buffer
* @param size: Data length in bytes
* @return true if transmission and reception are successful, false otherwise
*/
bool hal_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *tx_data, uint8_t *rx_data, uint16_t size);
#endif /* HAL_SPI_H */

View File

@ -38,10 +38,24 @@ typedef enum {
HAL_UART_DATABITS_9 = 1U
} hal_uart_databits_t;
/**
* @brief UART instance identifier definitions
*/
typedef enum {
HAL_UART_INSTANCE_1 = 0U,
HAL_UART_INSTANCE_2,
HAL_UART_INSTANCE_3,
HAL_UART_INSTANCE_4,
HAL_UART_INSTANCE_5,
HAL_UART_INSTANCE_6,
HAL_UART_INSTANCE_MAX
} hal_uart_instance_t;
/**
* @brief UART configuration structure
*/
typedef struct {
hal_uart_instance_t instance;
uint32_t baudrate;
hal_uart_parity_t parity;
hal_uart_stopbits_t stopbits;
@ -54,36 +68,40 @@ typedef struct {
void hal_uart_init(void);
/**
* @brief Configure UART parameters
* @brief Configure UART parameters for specific instance
* @param config: UART configuration structure
*/
void hal_uart_config(const hal_uart_config_t *config);
/**
* @brief Send data over UART
* @brief Send data over specific UART instance
* @param instance: UART instance identifier
* @param data: Pointer to data buffer
* @param length: Data length in bytes
*/
void hal_uart_send(const uint8_t *data, size_t length);
void hal_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t length);
/**
* @brief Receive data over UART
* @brief Receive data from specific UART instance
* @param instance: UART instance identifier
* @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);
size_t hal_uart_receive(hal_uart_instance_t instance, uint8_t *data, size_t length);
/**
* @brief Check if UART is ready to send
* @brief Check if specific UART instance is ready to send
* @param instance: UART instance identifier
* @retval 1 if ready, 0 otherwise
*/
uint8_t hal_uart_is_tx_ready(void);
uint8_t hal_uart_is_tx_ready(hal_uart_instance_t instance);
/**
* @brief Check if UART has data to receive
* @brief Check if specific UART instance has data to receive
* @param instance: UART instance identifier
* @retval 1 if data available, 0 otherwise
*/
uint8_t hal_uart_is_rx_ready(void);
uint8_t hal_uart_is_rx_ready(hal_uart_instance_t instance);
#endif /* HAL_UART_H */