准备驱动移植适配Rust

This commit is contained in:
冯佳
2026-01-29 15:08:30 +08:00
parent e879b18602
commit 1bdeca55ea
68 changed files with 4371 additions and 4392 deletions

View File

@ -10,6 +10,7 @@ target_sources(bsp PRIVATE
Src/bsp_board_manager.c
Src/bsp_w25qxx.c
Src/bsp_key.c
Src/bsp_eth.c
)
# Add BSP include directories

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_board.h
* @brief : Board support package board abstraction header file
* @brief : 板级支持包板级抽象头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -18,9 +18,11 @@
#include "hal_i2c.h"
#include "hal_can.h"
#include "hal_adc.h"
#include "hal_eth.h"
#include "bsp_eth.h"
/**
* @brief BSP configuration version
* @brief BSP 配置版本
*/
#define BSP_CONFIG_VERSION_MAJOR 1
#define BSP_CONFIG_VERSION_MINOR 0
@ -28,49 +30,49 @@
#define BSP_CONFIG_VERSION (BSP_CONFIG_VERSION_MAJOR << 16 | BSP_CONFIG_VERSION_MINOR << 8 | BSP_CONFIG_VERSION_PATCH)
/**
* @brief Board LED configuration structure
* @brief 板级 LED 配置结构体
*/
typedef struct {
uint8_t enable; /*!< LED enable flag */
hal_gpio_port_t port; /*!< LED GPIO port */
hal_gpio_pin_t pin; /*!< LED GPIO pin */
hal_gpio_mode_t mode; /*!< LED GPIO mode */
hal_gpio_speed_t speed; /*!< LED GPIO speed */
hal_gpio_pull_t pull; /*!< LED GPIO pull configuration */
uint8_t enable; /*!< LED 使能标志 */
hal_gpio_port_t port; /*!< LED GPIO 端口 */
hal_gpio_pin_t pin; /*!< LED GPIO 引脚 */
hal_gpio_mode_t mode; /*!< LED GPIO 模式 */
hal_gpio_speed_t speed; /*!< LED GPIO 速度 */
hal_gpio_pull_t pull; /*!< LED GPIO 上拉配置 */
} bsp_led_config_t;
/**
* @brief Board LEDs configuration structure
* @brief 板级 LEDs 配置结构体
*/
typedef struct {
uint8_t enable; /*!< LEDs enable flag */
uint8_t count; /*!< Number of LEDs */
const bsp_led_config_t* leds; /*!< Pointer to LEDs configuration array */
uint8_t enable; /*!< LEDs 使能标志 */
uint8_t count; /*!< LED 数量 */
const bsp_led_config_t* leds; /*!< 指向 LEDs 配置数组的指针 */
} bsp_leds_config_t;
/**
* @brief Board button configuration structure
* @brief 板级按键配置结构体
*/
typedef struct {
hal_gpio_port_t port; /*!< Button GPIO port */
hal_gpio_pin_t pin; /*!< Button GPIO pin */
hal_gpio_mode_t mode; /*!< Button GPIO mode */
hal_gpio_speed_t speed; /*!< Button GPIO speed */
hal_gpio_pull_t pull; /*!< Button GPIO pull configuration */
uint8_t active_high; /*!< Button active high flag */
hal_gpio_port_t port; /*!< 按键 GPIO 端口 */
hal_gpio_pin_t pin; /*!< 按键 GPIO 引脚 */
hal_gpio_mode_t mode; /*!< 按键 GPIO 模式 */
hal_gpio_speed_t speed; /*!< 按键 GPIO 速度 */
hal_gpio_pull_t pull; /*!< 按键 GPIO 上拉配置 */
uint8_t active_high; /*!< 按键高电平有效标志 */
} bsp_button_config_t;
/**
* @brief Board buttons configuration structure
* @brief 板级按键组配置结构体
*/
typedef struct {
uint8_t enable; /*!< Buttons enable flag */
uint8_t count; /*!< Number of buttons */
const bsp_button_config_t* buttons; /*!< Pointer to buttons configuration array */
uint8_t enable; /*!< 按键使能标志 */
uint8_t count; /*!< 按键数量 */
const bsp_button_config_t* buttons; /*!< 指向按键配置数组的指针 */
} bsp_buttons_config_t;
/**
* @brief UART instance identifier definitions
* @brief UART 实例标识符定义
*/
typedef enum {
BSP_UART_INSTANCE_1 = 0,
@ -83,251 +85,258 @@ typedef enum {
} bsp_uart_instance_t;
/**
* @brief Board UART configuration structure
* @brief 板级 UART 配置结构体
*/
typedef struct {
uint8_t enable; /*!< UART enable flag */
bsp_uart_instance_t instance; /*!< UART instance */
uint32_t baudrate; /*!< UART baudrate */
hal_uart_parity_t parity; /*!< UART parity */
hal_uart_stopbits_t stopbits; /*!< UART stop bits */
hal_uart_databits_t databits; /*!< UART data bits */
hal_gpio_port_t tx_port; /*!< UART TX port */
hal_gpio_pin_t tx_pin; /*!< UART TX pin */
hal_gpio_port_t rx_port; /*!< UART RX port */
hal_gpio_pin_t rx_pin; /*!< UART RX pin */
uint8_t enable; /*!< UART 使能标志 */
bsp_uart_instance_t instance; /*!< UART 实例 */
uint32_t baudrate; /*!< UART 波特率 */
hal_uart_parity_t parity; /*!< UART 校验位 */
hal_uart_stopbits_t stopbits; /*!< UART 停止位 */
hal_uart_databits_t databits; /*!< UART 数据位 */
hal_gpio_port_t tx_port; /*!< UART TX 端口 */
hal_gpio_pin_t tx_pin; /*!< UART TX 引脚 */
hal_gpio_port_t rx_port; /*!< UART RX 端口 */
hal_gpio_pin_t rx_pin; /*!< UART RX 引脚 */
} bsp_uart_config_t;
/**
* @brief Board SPI configuration structure
* @brief 板级 SPI 配置结构体
*/
typedef struct {
uint8_t enable; /*!< SPI enable flag */
hal_spi_instance_t instance; /*!< SPI instance */
hal_spi_mode_t mode; /*!< SPI mode */
uint32_t baudrate; /*!< SPI baudrate */
hal_spi_polarity_t polarity; /*!< SPI clock polarity */
hal_spi_phase_t phase; /*!< SPI clock phase */
hal_spi_databits_t databits; /*!< SPI data bits */
hal_gpio_port_t sck_port; /*!< SPI SCK port */
hal_gpio_pin_t sck_pin; /*!< SPI SCK pin */
hal_gpio_port_t miso_port; /*!< SPI MISO port */
hal_gpio_pin_t miso_pin; /*!< SPI MISO pin */
hal_gpio_port_t mosi_port; /*!< SPI MOSI port */
hal_gpio_pin_t mosi_pin; /*!< SPI MOSI pin */
uint8_t enable; /*!< SPI 使能标志 */
hal_spi_instance_t instance; /*!< SPI 实例 */
hal_spi_mode_t mode; /*!< SPI 模式 */
uint32_t baudrate; /*!< SPI 波特率 */
hal_spi_polarity_t polarity; /*!< SPI 时钟极性 */
hal_spi_phase_t phase; /*!< SPI 时钟相位 */
hal_spi_databits_t databits; /*!< SPI 数据位 */
hal_gpio_port_t sck_port; /*!< SPI SCK 端口 */
hal_gpio_pin_t sck_pin; /*!< SPI SCK 引脚 */
hal_gpio_port_t miso_port; /*!< SPI MISO 端口 */
hal_gpio_pin_t miso_pin; /*!< SPI MISO 引脚 */
hal_gpio_port_t mosi_port; /*!< SPI MOSI 端口 */
hal_gpio_pin_t mosi_pin; /*!< SPI MOSI 引脚 */
} bsp_spi_config_t;
/**
* @brief Board I2C configuration structure
* @brief 板级 I2C 配置结构体
*/
typedef struct {
uint8_t enable; /*!< I2C enable flag */
hal_i2c_instance_t instance; /*!< I2C instance */
hal_i2c_speed_t speed; /*!< I2C speed */
hal_i2c_address_mode_t address_mode; /*!< I2C address mode */
hal_i2c_dutycycle_t dutycycle; /*!< I2C duty cycle */
uint16_t own_address1; /*!< I2C own address 1 */
hal_gpio_port_t scl_port; /*!< I2C SCL port */
hal_gpio_pin_t scl_pin; /*!< I2C SCL pin */
hal_gpio_port_t sda_port; /*!< I2C SDA port */
hal_gpio_pin_t sda_pin; /*!< I2C SDA pin */
uint8_t enable; /*!< I2C 使能标志 */
hal_i2c_instance_t instance; /*!< I2C 实例 */
hal_i2c_speed_t speed; /*!< I2C 速度 */
hal_i2c_address_mode_t address_mode; /*!< I2C 地址模式 */
hal_i2c_dutycycle_t dutycycle; /*!< I2C 占空比 */
uint16_t own_address1; /*!< I2C 自身地址 1 */
hal_gpio_port_t scl_port; /*!< I2C SCL 端口 */
hal_gpio_pin_t scl_pin; /*!< I2C SCL 引脚 */
hal_gpio_port_t sda_port; /*!< I2C SDA 端口 */
hal_gpio_pin_t sda_pin; /*!< I2C SDA 引脚 */
} bsp_i2c_config_t;
/**
* @brief Board CAN configuration structure
* @brief 板级 CAN 配置结构体
*/
typedef struct {
uint8_t enable; /*!< CAN enable flag */
hal_can_instance_t instance; /*!< CAN instance */
hal_can_mode_t mode; /*!< CAN mode */
uint32_t prescaler; /*!< CAN prescaler */
uint8_t sync_jump_width; /*!< CAN sync jump width */
uint8_t time_segment1; /*!< CAN time segment 1 */
uint8_t time_segment2; /*!< CAN time segment 2 */
hal_gpio_port_t rx_port; /*!< CAN RX port */
hal_gpio_pin_t rx_pin; /*!< CAN RX pin */
hal_gpio_port_t tx_port; /*!< CAN TX port */
hal_gpio_pin_t tx_pin; /*!< CAN TX pin */
uint8_t enable; /*!< CAN 使能标志 */
hal_can_instance_t instance; /*!< CAN 实例 */
hal_can_mode_t mode; /*!< CAN 模式 */
uint32_t prescaler; /*!< CAN 预分频器 */
uint8_t sync_jump_width; /*!< CAN 同步跳转宽度 */
uint8_t time_segment1; /*!< CAN 时间段 1 */
uint8_t time_segment2; /*!< CAN 时间段 2 */
hal_gpio_port_t rx_port; /*!< CAN RX 端口 */
hal_gpio_pin_t rx_pin; /*!< CAN RX 引脚 */
hal_gpio_port_t tx_port; /*!< CAN TX 端口 */
hal_gpio_pin_t tx_pin; /*!< CAN TX 引脚 */
} bsp_can_config_t;
/**
* @brief Board ADC channel configuration structure
* @brief 板级 ADC 通道配置结构体
*/
typedef struct {
uint8_t enable; /*!< ADC channel enable flag */
hal_adc_channel_t channel; /*!< ADC channel */
hal_adc_sampletime_t sampletime; /*!< ADC sample time */
uint8_t rank; /*!< ADC channel rank */
uint8_t enable; /*!< ADC 通道使能标志 */
hal_adc_channel_t channel; /*!< ADC 通道 */
hal_adc_sampletime_t sampletime; /*!< ADC 采样时间 */
uint8_t rank; /*!< ADC 通道优先级 */
} bsp_adc_channel_config_t;
/**
* @brief Board ADC configuration structure
* @brief 板级 ADC 配置结构体
*/
typedef struct {
uint8_t enable; /*!< ADC enable flag */
hal_adc_instance_t instance; /*!< ADC instance */
hal_adc_resolution_t resolution; /*!< ADC resolution */
uint8_t scan_conversion_mode; /*!< ADC scan conversion mode */
uint8_t continuous_conversion_mode; /*!< ADC continuous conversion mode */
uint8_t channel_count; /*!< Number of ADC channels */
const bsp_adc_channel_config_t* channels; /*!< Pointer to ADC channels configuration array */
uint8_t enable; /*!< ADC 使能标志 */
hal_adc_instance_t instance; /*!< ADC 实例 */
hal_adc_resolution_t resolution; /*!< ADC 分辨率 */
uint8_t scan_conversion_mode; /*!< ADC 扫描转换模式 */
uint8_t continuous_conversion_mode; /*!< ADC 连续转换模式 */
uint8_t channel_count; /*!< ADC 通道数量 */
const bsp_adc_channel_config_t* channels; /*!< 指向 ADC 通道配置数组的指针 */
} bsp_adc_config_t;
/**
* @brief Board W25QXX configuration structure
* @brief 板级 W25QXX 配置结构体
*/
typedef struct {
uint8_t enable; /*!< W25QXX enable flag */
hal_gpio_port_t cs_port; /*!< W25QXX CS port */
hal_gpio_pin_t cs_pin; /*!< W25QXX CS pin */
uint8_t spi_instance; /*!< SPI instance used by W25QXX */
uint8_t enable; /*!< W25QXX 使能标志 */
hal_gpio_port_t cs_port; /*!< W25QXX CS 端口 */
hal_gpio_pin_t cs_pin; /*!< W25QXX CS 引脚 */
uint8_t spi_instance; /*!< W25QXX 使用的 SPI 实例 */
} bsp_w25qxx_config_t;
/**
* @brief Board peripheral initialization function type
* @brief 板级外设初始化函数类型
*/
typedef hal_ret_t (*bsp_periph_init_func_t)(const void* config);
/**
* @brief Board ID structure
* @brief 板级 ID 结构体
*/
typedef struct {
uint16_t vendor_id; /*!< Board vendor ID */
uint16_t product_id; /*!< Board product ID */
uint32_t serial_number; /*!< Board serial number */
uint16_t vendor_id; /*!< 板级厂商 ID */
uint16_t product_id; /*!< 板级产品 ID */
uint32_t serial_number; /*!< 板级序列号 */
} bsp_board_id_t;
/**
* @brief Board feature flags
* @brief 板级特性标志
*/
typedef enum {
BSP_BOARD_FEATURE_LED = (1 << 0), /*!< Board has LED support */
BSP_BOARD_FEATURE_BUTTON = (1 << 1), /*!< Board has button support */
BSP_BOARD_FEATURE_UART = (1 << 2), /*!< Board has UART support */
BSP_BOARD_FEATURE_SPI = (1 << 3), /*!< Board has SPI support */
BSP_BOARD_FEATURE_I2C = (1 << 4), /*!< Board has I2C support */
BSP_BOARD_FEATURE_CAN = (1 << 5), /*!< Board has CAN support */
BSP_BOARD_FEATURE_ADC = (1 << 6), /*!< Board has ADC support */
BSP_BOARD_FEATURE_DAC = (1 << 7), /*!< Board has DAC support */
BSP_BOARD_FEATURE_TIMER = (1 << 8), /*!< Board has TIMER support */
BSP_BOARD_FEATURE_RTC = (1 << 9), /*!< Board has RTC support */
BSP_BOARD_FEATURE_W25QXX = (1 << 10), /*!< Board has W25QXX support */
BSP_BOARD_FEATURE_DMA = (1 << 11), /*!< Board has DMA support */
BSP_BOARD_FEATURE_ETH = (1 << 12), /*!< Board has ETH support */
BSP_BOARD_FEATURE_USB = (1 << 13), /*!< Board has USB support */
BSP_BOARD_FEATURE_SDIO = (1 << 14), /*!< Board has SDIO support */
BSP_BOARD_FEATURE_FSMC = (1 << 15), /*!< Board has FSMC support */
BSP_BOARD_FEATURE_LED = (1 << 0), /*!< 板级支持 LED */
BSP_BOARD_FEATURE_BUTTON = (1 << 1), /*!< 板级支持按键 */
BSP_BOARD_FEATURE_UART = (1 << 2), /*!< 板级支持 UART */
BSP_BOARD_FEATURE_SPI = (1 << 3), /*!< 板级支持 SPI */
BSP_BOARD_FEATURE_I2C = (1 << 4), /*!< 板级支持 I2C */
BSP_BOARD_FEATURE_CAN = (1 << 5), /*!< 板级支持 CAN */
BSP_BOARD_FEATURE_ADC = (1 << 6), /*!< 板级支持 ADC */
BSP_BOARD_FEATURE_DAC = (1 << 7), /*!< 板级支持 DAC */
BSP_BOARD_FEATURE_TIMER = (1 << 8), /*!< 板级支持 TIMER */
BSP_BOARD_FEATURE_RTC = (1 << 9), /*!< 板级支持 RTC */
BSP_BOARD_FEATURE_W25QXX = (1 << 10), /*!< 板级支持 W25QXX */
BSP_BOARD_FEATURE_DMA = (1 << 11), /*!< 板级支持 DMA */
BSP_BOARD_FEATURE_ETH = (1 << 12), /*!< 板级支持 ETH */
BSP_BOARD_FEATURE_USB = (1 << 13), /*!< 板级支持 USB */
BSP_BOARD_FEATURE_SDIO = (1 << 14), /*!< 板级支持 SDIO */
BSP_BOARD_FEATURE_FSMC = (1 << 15), /*!< 板级支持 FSMC */
} bsp_board_feature_t;
/**
* @brief Board hardware information structure
* @brief 板级硬件信息结构体
*/
typedef struct {
uint32_t clock_speed; /*!< Board clock speed */
uint32_t flash_size; /*!< Flash size in bytes */
uint32_t ram_size; /*!< RAM size in bytes */
uint32_t eeprom_size; /*!< EEPROM size in bytes */
uint32_t sram_size; /*!< SRAM size in bytes */
uint8_t cpu_core; /*!< CPU core type */
uint8_t cpu_bits; /*!< CPU bits (32 or 64) */
uint32_t clock_speed; /*!< 板级时钟速度 */
uint32_t flash_size; /*!< Flash 大小(字节) */
uint32_t ram_size; /*!< RAM 大小(字节) */
uint32_t eeprom_size; /*!< EEPROM 大小(字节) */
uint32_t sram_size; /*!< SRAM 大小(字节) */
uint8_t cpu_core; /*!< CPU 核心类型 */
uint8_t cpu_bits; /*!< CPU 位数(32 64 */
} bsp_board_hw_info_t;
/**
* @brief Board peripheral configuration structure
* @brief 板级外设配置结构体
*/
typedef struct {
/* UART configurations */
uint8_t uart_count; /*!< Number of UARTs */
const bsp_uart_config_t* uarts; /*!< Pointer to UARTs configuration array */
/* UART 配置 */
uint8_t uart_count; /*!< UART 数量 */
const bsp_uart_config_t* uarts; /*!< 指向 UART 配置数组的指针 */
/* SPI configurations */
uint8_t spi_count; /*!< Number of SPIs */
const bsp_spi_config_t* spis; /*!< Pointer to SPIs configuration array */
/* SPI 配置 */
uint8_t spi_count; /*!< SPI 数量 */
const bsp_spi_config_t* spis; /*!< 指向 SPI 配置数组的指针 */
/* I2C configurations */
uint8_t i2c_count; /*!< Number of I2Cs */
const bsp_i2c_config_t* i2cs; /*!< Pointer to I2Cs configuration array */
/* I2C 配置 */
uint8_t i2c_count; /*!< I2C 数量 */
const bsp_i2c_config_t* i2cs; /*!< 指向 I2C 配置数组的指针 */
/* CAN configurations */
uint8_t can_count; /*!< Number of CANs */
const bsp_can_config_t* cans; /*!< Pointer to CANs configuration array */
/* CAN 配置 */
uint8_t can_count; /*!< CAN 数量 */
const bsp_can_config_t* cans; /*!< 指向 CAN 配置数组的指针 */
/* ADC configurations */
uint8_t adc_count; /*!< Number of ADCs */
const bsp_adc_config_t* adcs; /*!< Pointer to ADCs configuration array */
/* ADC 配置 */
uint8_t adc_count; /*!< ADC 数量 */
const bsp_adc_config_t* adcs; /*!< 指向 ADC 配置数组的指针 */
} bsp_board_periph_config_t;
/**
* @brief Board initialization function pointers structure
* @brief 板级以太网配置结构体
*/
// bsp_eth_config_t 在 bsp_eth.h 中定义
/**
* @brief 板级初始化函数指针结构体
*/
typedef struct {
bsp_periph_init_func_t led_init; /*!< LED initialization function */
bsp_periph_init_func_t button_init; /*!< Button initialization function */
bsp_periph_init_func_t uart_init; /*!< UART initialization function */
bsp_periph_init_func_t spi_init; /*!< SPI initialization function */
bsp_periph_init_func_t i2c_init; /*!< I2C initialization function */
bsp_periph_init_func_t can_init; /*!< CAN initialization function */
bsp_periph_init_func_t adc_init; /*!< ADC initialization function */
bsp_periph_init_func_t w25qxx_init; /*!< W25QXX initialization function */
bsp_periph_init_func_t led_init; /*!< LED 初始化函数 */
bsp_periph_init_func_t button_init; /*!< 按键初始化函数 */
bsp_periph_init_func_t uart_init; /*!< UART 初始化函数 */
bsp_periph_init_func_t spi_init; /*!< SPI 初始化函数 */
bsp_periph_init_func_t i2c_init; /*!< I2C 初始化函数 */
bsp_periph_init_func_t can_init; /*!< CAN 初始化函数 */
bsp_periph_init_func_t adc_init; /*!< ADC 初始化函数 */
bsp_periph_init_func_t w25qxx_init; /*!< W25QXX 初始化函数 */
bsp_periph_init_func_t eth_init; /*!< 以太网初始化函数 */
} bsp_board_init_funcs_t;
/**
* @brief Board configuration structure
* @brief 板级配置结构体
*/
typedef struct {
uint32_t version; /*!< Configuration version */
const char* name; /*!< Board name */
const char* description; /*!< Board description */
bsp_board_id_t id; /*!< Board ID information */
uint32_t version; /*!< 配置版本 */
const char* name; /*!< 板级名称 */
const char* description; /*!< 板级描述 */
bsp_board_id_t id; /*!< 板级 ID 信息 */
/* Board features */
uint32_t features; /*!< Board feature flags */
/* 板级特性 */
uint32_t features; /*!< 板级特性标志 */
/* Hardware information */
bsp_board_hw_info_t hw_info; /*!< Hardware information */
/* 硬件信息 */
bsp_board_hw_info_t hw_info; /*!< 硬件信息 */
/* Peripheral configurations */
bsp_leds_config_t leds; /*!< LEDs configuration */
bsp_buttons_config_t buttons; /*!< Buttons configuration */
bsp_w25qxx_config_t w25qxx; /*!< W25QXX configuration */
/* 外设配置 */
bsp_leds_config_t leds; /*!< LEDs 配置 */
bsp_buttons_config_t buttons; /*!< 按键配置 */
bsp_w25qxx_config_t w25qxx; /*!< W25QXX 配置 */
bsp_eth_config_t eth; /*!< 以太网配置 */
/* Peripheral arrays */
bsp_board_periph_config_t periphs; /*!< Peripheral configurations */
/* 外设数组 */
bsp_board_periph_config_t periphs; /*!< 外设配置 */
/* Initialization functions */
bsp_board_init_funcs_t init_funcs; /*!< Initialization function pointers */
/* 初始化函数 */
bsp_board_init_funcs_t init_funcs; /*!< 初始化函数指针 */
/* Additional board-specific configuration */
void* custom_config; /*!< Custom board configuration pointer */
size_t custom_config_size; /*!< Custom board configuration size */
/* 板级特定配置 */
void* custom_config; /*!< 自定义板级配置指针 */
size_t custom_config_size; /*!< 自定义板级配置大小 */
/* Board revision information */
uint8_t major_rev; /*!< Major revision */
uint8_t minor_rev; /*!< Minor revision */
uint8_t patch_rev; /*!< Patch revision */
const char* revision_desc; /*!< Revision description */
/* 板级版本信息 */
uint8_t major_rev; /*!< 主版本 */
uint8_t minor_rev; /*!< 次版本 */
uint8_t patch_rev; /*!< 补丁版本 */
const char* revision_desc; /*!< 版本描述 */
} bsp_board_config_t;
/**
* @brief STM32F407VET6 board configuration extern declaration
* @brief STM32F407VET6 板级配置外部声明
*/
extern const bsp_board_config_t stm32f407vet6_board_config;
/**
* @brief Board hardware initialization
* @retval HAL status code
* @brief 板级硬件初始化
* @retval HAL 状态码
*/
hal_ret_t bsp_board_init(void);
/**
* @brief Get board name
* @retval Board name string
* @brief 获取板级名称
* @retval 板级名称字符串
*/
const char* bsp_board_get_name(void);
/**
* @brief Get board configuration
* @retval Pointer to board configuration structure
* @brief 获取板级配置
* @retval 指向板级配置结构体的指针
*/
const bsp_board_config_t* bsp_board_get_config(void);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_board_manager.h
* @brief : Board support package manager header file
* @brief : 板级支持包管理器头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -15,48 +15,48 @@
#include "bsp_board.h"
/**
* @brief Get number of supported boards
* @retval Number of supported boards
* @brief 获取支持的板卡数量
* @retval 支持的板卡数量
*/
uint8_t bsp_board_get_count(void);
/**
* @brief Get board configuration by index
* @param index: Board configuration index
* @retval Pointer to board configuration structure, NULL if invalid index
* @brief 通过索引获取板卡配置
* @param index: 板卡配置索引
* @retval 指向板卡配置结构体的指针,无效索引返回 NULL
*/
const bsp_board_config_t* bsp_board_get_by_index(uint8_t index);
/**
* @brief Get board configuration by name
* @param name: Board name string
* @retval Pointer to board configuration structure, NULL if not found
* @brief 通过名称获取板卡配置
* @param name: 板卡名称字符串
* @retval 指向板卡配置结构体的指针,未找到返回 NULL
*/
const bsp_board_config_t* bsp_board_get_by_name(const char* name);
/**
* @brief Set current board configuration by index
* @param index: Board configuration index
* @retval HAL status code
* @brief 通过索引设置当前板卡配置
* @param index: 板卡配置索引
* @retval HAL 状态码
*/
hal_ret_t bsp_board_set_by_index(uint8_t index);
/**
* @brief Set current board configuration by name
* @param name: Board name string
* @retval HAL status code
* @brief 通过名称设置当前板卡配置
* @param name: 板卡名称字符串
* @retval HAL 状态码
*/
hal_ret_t bsp_board_set_by_name(const char* name);
/**
* @brief Get current board configuration
* @retval Pointer to current board configuration structure
* @brief 获取当前板卡配置
* @retval 指向当前板卡配置结构体的指针
*/
const bsp_board_config_t* bsp_board_get_config(void);
/**
* @brief Get current board index
* @retval Current board index
* @brief 获取当前板卡索引
* @retval 当前板卡索引
*/
uint8_t bsp_board_get_current_index(void);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_config.h
* @brief : Board support package configuration file header
* @brief : 板级支持包配置文件头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -16,7 +16,7 @@
#include "hal.h"
/**
* @brief BSP configuration file format definitions
* @brief BSP 配置文件格式定义
*/
#define BSP_CONFIG_FILE_VERSION "1.0.0"
#define BSP_CONFIG_MAX_LINE_LENGTH 256
@ -25,7 +25,7 @@
#define BSP_CONFIG_MAX_VALUE_LENGTH 128
/**
* @brief BSP configuration section type
* @brief BSP 配置节类型
*/
typedef enum {
BSP_CONFIG_SECTION_NONE = 0,
@ -36,7 +36,7 @@ typedef enum {
} bsp_config_section_t;
/**
* @brief BSP configuration entry
* @brief BSP 配置条目
*/
typedef struct {
const char* section;
@ -45,7 +45,7 @@ typedef struct {
} bsp_config_entry_t;
/**
* @brief BSP configuration parser context
* @brief BSP 配置解析器上下文
*/
typedef struct {
FILE* file;
@ -56,7 +56,7 @@ typedef struct {
} bsp_config_parser_t;
/**
* @brief BSP configuration file operations
* @brief BSP 配置文件操作
*/
typedef struct {
hal_ret_t (*open)(const char* filename, bsp_config_parser_t* parser);
@ -69,115 +69,115 @@ typedef struct {
} bsp_config_ops_t;
/**
* @brief Initialize BSP configuration system
* @retval HAL status code
* @brief 初始化 BSP 配置系统
* @retval HAL 状态码
*/
hal_ret_t bsp_config_init(void);
/**
* @brief Deinitialize BSP configuration system
* @retval HAL status code
* @brief 反初始化 BSP 配置系统
* @retval HAL 状态码
*/
hal_ret_t bsp_config_deinit(void);
/**
* @brief Load BSP configuration from file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 从文件加载 BSP 配置
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_config_load(const char* filename);
/**
* @brief Save BSP configuration to file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 保存 BSP 配置到文件
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_config_save(const char* filename);
/**
* @brief Get configuration value
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store value
* @param max_length: Maximum length of value buffer
* @retval HAL status code
* @brief 获取配置值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储值的指针
* @param max_length: 值缓冲区的最大长度
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_value(const char* section, const char* key, char* value, size_t max_length);
/**
* @brief Set configuration value
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Configuration value
* @retval HAL status code
* @brief 设置配置值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 配置值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_value(const char* section, const char* key, const char* value);
/**
* @brief Get configuration value as integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store integer value
* @retval HAL status code
* @brief 获取配置值作为整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储整数值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_int(const char* section, const char* key, int* value);
/**
* @brief Set configuration value as integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Integer value
* @retval HAL status code
* @brief 设置配置值作为整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 整数值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_int(const char* section, const char* key, int value);
/**
* @brief Get configuration value as unsigned integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store unsigned integer value
* @retval HAL status code
* @brief 获取配置值作为无符号整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储无符号整数值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_uint(const char* section, const char* key, uint32_t* value);
/**
* @brief Set configuration value as unsigned integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Unsigned integer value
* @retval HAL status code
* @brief 设置配置值作为无符号整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 无符号整数值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_uint(const char* section, const char* key, uint32_t value);
/**
* @brief Get configuration value as boolean
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store boolean value
* @retval HAL status code
* @brief 获取配置值作为布尔值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储布尔值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_bool(const char* section, const char* key, uint8_t* value);
/**
* @brief Set configuration value as boolean
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Boolean value
* @retval HAL status code
* @brief 设置配置值作为布尔值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 布尔值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_bool(const char* section, const char* key, uint8_t value);
/**
* @brief Parse BSP modules from configuration
* @param config: Pointer to BSP board configuration structure to fill
* @retval HAL status code
* @brief 从配置解析 BSP 模块
* @param config: 指向要填充的 BSP 板卡配置结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_parse_modules(bsp_board_config_t* config);
/**
* @brief Initialize BSP from configuration file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 从配置文件初始化 BSP
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_init_from_config(const char* filename);

169
BSP/Inc/bsp_eth.h Normal file
View File

@ -0,0 +1,169 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : bsp_eth.h
* @brief : 板级支持包以太网LAN8720驱动头文件
******************************************************************************
*/
/* USER CODE END Header */
#ifndef BSP_ETH_H
#define BSP_ETH_H
#include "hal.h"
#include "hal_eth.h"
/**
* @brief LAN8720 PHY 地址定义
*/
#define LAN8720_PHY_ADDR 0x00
/**
* @brief LAN8720 PHY 寄存器定义
*/
#define LAN8720_REG_BSR 0x01 /*!< 基本状态寄存器 */
#define LAN8720_REG_BCR 0x00 /*!< 基本控制寄存器 */
#define LAN8720_REG_PID1 0x02 /*!< PHY 标识符 1 */
#define LAN8720_REG_PID2 0x03 /*!< PHY 标识符 2 */
#define LAN8720_REG_ANAR 0x04 /*!< 自动协商通告寄存器 */
#define LAN8720_REG_ANLPAR 0x05 /*!< 自动协商链路伙伴能力寄存器 */
#define LAN8720_REG_ANER 0x06 /*!< 自动协商扩展寄存器 */
#define LAN8720_REG_DR 0x0E /*!< 设备修订寄存器 */
#define LAN8720_REG_CR 0x10 /*!< 控制寄存器 */
#define LAN8720_REG_SR 0x11 /*!< 状态寄存器 */
/**
* @brief LAN8720 BSR 寄存器位定义
*/
#define LAN8720_BSR_LINK_STATUS (1 << 2) /*!< 链路状态位 */
#define LAN8720_BSR_AUTO_NEG_COMPLETE (1 << 5) /*!< 自动协商完成 */
#define LAN8720_BSR_100BASE_T4 (1 << 11) /*!< 支持 100BASE-T4 */
#define LAN8720_BSR_100BASE_TX_FD (1 << 12) /*!< 支持 100BASE-TX 全双工 */
#define LAN8720_BSR_100BASE_TX_HD (1 << 13) /*!< 支持 100BASE-TX 半双工 */
#define LAN8720_BSR_10BASE_T_FD (1 << 14) /*!< 支持 10BASE-T 全双工 */
#define LAN8720_BSR_10BASE_T_HD (1 << 15) /*!< 支持 10BASE-T 半双工 */
/**
* @brief LAN8720 BCR 寄存器位定义
*/
#define LAN8720_BCR_RESET (1 << 15) /*!< 软件复位 */
#define LAN8720_BCR_AUTO_NEG_EN (1 << 12) /*!< 自动协商使能 */
#define LAN8720_BCR_SPEED_SELECT (1 << 13) /*!< 速度选择 (1=100Mbps, 0=10Mbps) */
#define LAN8720_BCR_DUPLEX_MODE (1 << 8) /*!< 双工模式 (1=全双工, 0=半双工) */
#define LAN8720_BCR_RESTART_AN (1 << 9) /*!< 重启自动协商 */
/**
* @brief LAN8720 PHY 标识符
*/
#define LAN8720_PID1_VALUE 0x0007
#define LAN8720_PID2_VALUE 0xC0F0
/**
* @brief 以太网配置结构体
*/
typedef struct {
uint8_t enable; /*!< 以太网使能标志 */
hal_eth_instance_t instance; /*!< 以太网实例 */
hal_eth_mode_t mode; /*!< 以太网模式 */
hal_eth_speed_t speed; /*!< 以太网速度 */
hal_eth_phy_addr_t phy_addr; /*!< PHY 地址 */
hal_eth_mac_addr_t mac_addr; /*!< MAC 地址 */
uint8_t auto_negotiation; /*!< 自动协商使能标志 */
uint8_t interrupt_enable; /*!< 中断使能标志 */
} bsp_eth_config_t;
/**
* @brief 以太网状态结构体
*/
typedef struct {
uint8_t link_up; /*!< 链路状态 */
uint8_t duplex; /*!< 双工模式 (0: 半双工, 1: 全双工) */
uint32_t speed; /*!< 速度 (Mbps) */
uint32_t rx_packets; /*!< 接收数据包计数 */
uint32_t tx_packets; /*!< 发送数据包计数 */
uint32_t rx_errors; /*!< 接收错误计数 */
uint32_t tx_errors; /*!< 发送错误计数 */
uint16_t phy_id1; /*!< PHY 标识符 1 */
uint16_t phy_id2; /*!< PHY 标识符 2 */
} bsp_eth_status_t;
/**
* @brief 初始化以太网LAN8720模块
* @param config: 指向以太网配置结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_init(const bsp_eth_config_t* config);
/**
* @brief 反初始化以太网LAN8720模块
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_deinit(void);
/**
* @brief 获取以太网LAN8720状态
* @param status: 指向以太网状态结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_get_status(bsp_eth_status_t* status);
/**
* @brief 设置以太网 MAC 地址
* @param mac_addr: 指向 MAC 地址结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_set_mac_addr(const hal_eth_mac_addr_t* mac_addr);
/**
* @brief 获取以太网 MAC 地址
* @param mac_addr: 指向 MAC 地址结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_get_mac_addr(hal_eth_mac_addr_t* mac_addr);
/**
* @brief 检查以太网链路状态
* @param link_up: 存储链路状态的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_check_link(uint8_t* link_up);
/**
* @brief 复位以太网 PHYLAN8720
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_reset_phy(void);
/**
* @brief 读取 LAN8720 PHY 寄存器
* @param reg_addr: 寄存器地址
* @param value: 存储寄存器值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_read_phy_reg(uint16_t reg_addr, uint16_t* value);
/**
* @brief 写入 LAN8720 PHY 寄存器
* @param reg_addr: 寄存器地址
* @param value: 寄存器值
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_write_phy_reg(uint16_t reg_addr, uint16_t value);
/**
* @brief 检测 LAN8720 PHY 存在
* @param detected: 存储检测结果的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_detect_phy(uint8_t* detected);
/**
* @brief 配置 LAN8720 PHY
* @param mode: 以太网模式
* @param speed: 以太网速度
* @param auto_neg: 自动协商使能
* @retval HAL 状态码
*/
hal_ret_t bsp_eth_configure_phy(hal_eth_mode_t mode, hal_eth_speed_t speed, uint8_t auto_neg);
#endif /* BSP_ETH_H */

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_init.h
* @brief : Board support package initialization header file
* @brief : 板级支持包初始化头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -13,26 +13,26 @@
#include "bsp_board.h"
/**
* @brief Initialize board support package
* @retval HAL status code
* @brief 初始化板级支持包
* @retval HAL 状态码
*/
hal_ret_t bsp_init(void);
/**
* @brief Initialize board GPIO
* @retval HAL status code
* @brief 初始化板级 GPIO
* @retval HAL 状态码
*/
hal_ret_t bsp_gpio_init(void);
/**
* @brief Get board name
* @retval Board name string
* @brief 获取板级名称
* @retval 板级名称字符串
*/
const char* bsp_get_board_name(void);
/**
* @brief Get current board configuration
* @retval Pointer to board configuration structure
* @brief 获取当前板级配置
* @retval 指向板级配置结构体的指针
*/
const bsp_board_config_t* bsp_get_board_config(void);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_key.h
* @brief : Board support package key driver header file
* @brief : 板级支持包按键驱动头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -13,7 +13,7 @@
#include <stdint.h>
/**
* @brief Key ID definitions
* @brief 按键 ID 定义
*/
typedef enum {
BSP_KEY_ID_KEY0 = 0,
@ -23,7 +23,7 @@ typedef enum {
} bsp_key_id_t;
/**
* @brief Key state definitions
* @brief 按键状态定义
*/
typedef enum {
BSP_KEY_STATE_RELEASED = 0,
@ -31,80 +31,80 @@ typedef enum {
} bsp_key_state_t;
/**
* @brief Key event definitions
* @brief 按键事件定义
*/
typedef enum {
BSP_KEY_EVENT_NONE = 0,
BSP_KEY_EVENT_PRESSED, /* Key pressed event */
BSP_KEY_EVENT_RELEASED, /* Key released event */
BSP_KEY_EVENT_LONG_PRESSED, /* Key long pressed event */
BSP_KEY_EVENT_REPEAT, /* Key repeat event */
BSP_KEY_EVENT_SHORT_PRESSED /* Key short pressed event */
BSP_KEY_EVENT_PRESSED, /* 按键按下事件 */
BSP_KEY_EVENT_RELEASED, /* 按键释放事件 */
BSP_KEY_EVENT_LONG_PRESSED, /* 按键长按事件 */
BSP_KEY_EVENT_REPEAT, /* 按键重复事件 */
BSP_KEY_EVENT_SHORT_PRESSED /* 按键短按事件 */
} bsp_key_event_t;
/**
* @brief Initialize all keys
* @brief 初始化所有按键
*/
void bsp_key_init(void);
/**
* @brief Update key state (call this function periodically, e.g. every 10ms)
* @brief 更新按键状态(定期调用此函数,例如每 10ms
*/
void bsp_key_update(void);
/**
* @brief Read debounced key state
* @param key_id: Key ID
* @retval Key state
* @brief 读取去抖后的按键状态
* @param key_id: 按键 ID
* @retval 按键状态
*/
bsp_key_state_t bsp_key_read(bsp_key_id_t key_id);
/**
* @brief Check if key is pressed
* @param key_id: Key ID
* @retval 1 if pressed, 0 otherwise
* @brief 检查按键是否按下
* @param key_id: 按键 ID
* @retval 按下返回 1否则返回 0
*/
uint8_t bsp_key_is_pressed(bsp_key_id_t key_id);
/**
* @brief Check if key is released
* @param key_id: Key ID
* @retval 1 if released, 0 otherwise
* @brief 检查按键是否释放
* @param key_id: 按键 ID
* @retval 释放返回 1否则返回 0
*/
uint8_t bsp_key_is_released(bsp_key_id_t key_id);
/**
* @brief Check for key press event (edge detection)
* @param key_id: Key ID
* @retval 1 if key was pressed, 0 otherwise
* @brief 检查按键按下事件(边沿检测)
* @param key_id: 按键 ID
* @retval 按键按下返回 1否则返回 0
*/
uint8_t bsp_key_get_press_event(bsp_key_id_t key_id);
/**
* @brief Check for key release event (edge detection)
* @param key_id: Key ID
* @retval 1 if key was released, 0 otherwise
* @brief 检查按键释放事件(边沿检测)
* @param key_id: 按键 ID
* @retval 按键释放返回 1否则返回 0
*/
uint8_t bsp_key_get_release_event(bsp_key_id_t key_id);
/**
* @brief Check for key long pressed event
* @param key_id: Key ID
* @retval 1 if key was long pressed, 0 otherwise
* @brief 检查按键长按事件
* @param key_id: 按键 ID
* @retval 按键长按返回 1否则返回 0
*/
uint8_t bsp_key_get_long_press_event(bsp_key_id_t key_id);
/**
* @brief Check for key repeat event
* @param key_id: Key ID
* @retval 1 if key repeat event occurred, 0 otherwise
* @brief 检查按键重复事件
* @param key_id: 按键 ID
* @retval 按键重复事件发生返回 1否则返回 0
*/
uint8_t bsp_key_get_repeat_event(bsp_key_id_t key_id);
/**
* @brief Check for key short pressed event
* @param key_id: Key ID
* @retval 1 if key was short pressed, 0 otherwise
* @brief 检查按键短按事件
* @param key_id: 按键 ID
* @retval 按键短按返回 1否则返回 0
*/
uint8_t bsp_key_get_short_press_event(bsp_key_id_t key_id);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_module.h
* @brief : Board support package module management header file
* @brief : 板级支持包模块管理头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -13,7 +13,7 @@
#include "hal.h"
/**
* @brief BSP module type definitions
* @brief BSP 模块类型定义
*/
typedef enum {
BSP_MODULE_TYPE_LED = 0,
@ -31,7 +31,7 @@ typedef enum {
} bsp_module_type_t;
/**
* @brief BSP module state definitions
* @brief BSP 模块状态定义
*/
typedef enum {
BSP_MODULE_STATE_UNINIT = 0,
@ -42,7 +42,7 @@ typedef enum {
} bsp_module_state_t;
/**
* @brief BSP module priority definitions
* @brief BSP 模块优先级定义
*/
typedef enum {
BSP_MODULE_PRIORITY_HIGHEST = 0,
@ -53,36 +53,36 @@ typedef enum {
} bsp_module_priority_t;
/**
* @brief BSP module configuration structure
* @brief BSP 模块配置结构体
*/
typedef struct bsp_module_config {
bsp_module_type_t type; /*!< Module type */
const char* name; /*!< Module name */
bsp_module_priority_t priority; /*!< Module priority */
uint32_t version; /*!< Module version */
uint8_t instance; /*!< Module instance */
uint8_t enable; /*!< Module enable flag */
void* config_data; /*!< Module configuration data */
size_t config_size; /*!< Module configuration size */
uint32_t dependencies; /*!< Module dependencies bitmask */
bsp_module_type_t type; /*!< 模块类型 */
const char* name; /*!< 模块名称 */
bsp_module_priority_t priority; /*!< 模块优先级 */
uint32_t version; /*!< 模块版本 */
uint8_t instance; /*!< 模块实例 */
uint8_t enable; /*!< 模块使能标志 */
void* config_data; /*!< 模块配置数据 */
size_t config_size; /*!< 模块配置大小 */
uint32_t dependencies; /*!< 模块依赖位掩码 */
} bsp_module_config_t;
/**
* @brief BSP module operations structure
* @brief BSP 模块操作结构体
*/
typedef struct {
hal_ret_t (*init)(void* config); /*!< Module initialization function */
hal_ret_t (*deinit)(void); /*!< Module deinitialization function */
hal_ret_t (*configure)(void* config); /*!< Module configuration function */
hal_ret_t (*start)(void); /*!< Module start function */
hal_ret_t (*stop)(void); /*!< Module stop function */
hal_ret_t (*reset)(void); /*!< Module reset function */
bsp_module_state_t (*get_state)(void); /*!< Module get state function */
hal_ret_t (*control)(uint32_t cmd, void* param); /*!< Module control function */
hal_ret_t (*init)(void* config); /*!< 模块初始化函数 */
hal_ret_t (*deinit)(void); /*!< 模块反初始化函数 */
hal_ret_t (*configure)(void* config); /*!< 模块配置函数 */
hal_ret_t (*start)(void); /*!< 模块启动函数 */
hal_ret_t (*stop)(void); /*!< 模块停止函数 */
hal_ret_t (*reset)(void); /*!< 模块复位函数 */
bsp_module_state_t (*get_state)(void); /*!< 模块获取状态函数 */
hal_ret_t (*control)(uint32_t cmd, void* param); /*!< 模块控制函数 */
} bsp_module_ops_t;
/**
* @brief BSP module version structure
* @brief BSP 模块版本结构体
*/
typedef struct {
uint8_t major;
@ -91,42 +91,42 @@ typedef struct {
} bsp_module_version_t;
/**
* @brief Forward declaration of bsp_module_t
* @brief bsp_module_t 的前向声明
*/
typedef struct bsp_module bsp_module_t;
/**
* @brief BSP module event callback type
* @brief BSP 模块事件回调类型
*/
typedef hal_ret_t (*bsp_module_event_callback_t)(const bsp_module_t* sender, uint32_t event, void* data);
/**
* @brief BSP module structure
* @brief BSP 模块结构体
*/
typedef struct bsp_module {
bsp_module_config_t config; /*!< Module configuration */
bsp_module_ops_t ops; /*!< Module operations */
bsp_module_state_t state; /*!< Module state */
bsp_module_version_t version; /*!< Module version */
struct bsp_module* next; /*!< Pointer to next module in the list */
struct bsp_module* prev; /*!< Pointer to previous module in the list */
void* private_data; /*!< Module private data */
bsp_module_event_callback_t event_callback; /*!< Event callback */
bsp_module_config_t config; /*!< 模块配置 */
bsp_module_ops_t ops; /*!< 模块操作 */
bsp_module_state_t state; /*!< 模块状态 */
bsp_module_version_t version; /*!< 模块版本 */
struct bsp_module* next; /*!< 链表中下一个模块的指针 */
struct bsp_module* prev; /*!< 链表中上一个模块的指针 */
void* private_data; /*!< 模块私有数据 */
bsp_module_event_callback_t event_callback; /*!< 事件回调 */
} bsp_module_t;
/**
* @brief BSP module event definitions
* @brief BSP 模块事件定义
*/
#define BSP_MODULE_EVENT_INIT (1 << 0) /*!< Module initialized */
#define BSP_MODULE_EVENT_DEINIT (1 << 1) /*!< Module deinitialized */
#define BSP_MODULE_EVENT_START (1 << 2) /*!< Module started */
#define BSP_MODULE_EVENT_STOP (1 << 3) /*!< Module stopped */
#define BSP_MODULE_EVENT_ERROR (1 << 4) /*!< Module error */
#define BSP_MODULE_EVENT_CONFIG (1 << 5) /*!< Module configured */
#define BSP_MODULE_EVENT_CUSTOM (1 << 8) /*!< Custom module event base */
#define BSP_MODULE_EVENT_INIT (1 << 0) /*!< 模块已初始化 */
#define BSP_MODULE_EVENT_DEINIT (1 << 1) /*!< 模块已反初始化 */
#define BSP_MODULE_EVENT_START (1 << 2) /*!< 模块已启动 */
#define BSP_MODULE_EVENT_STOP (1 << 3) /*!< 模块已停止 */
#define BSP_MODULE_EVENT_ERROR (1 << 4) /*!< 模块错误 */
#define BSP_MODULE_EVENT_CONFIG (1 << 5) /*!< 模块已配置 */
#define BSP_MODULE_EVENT_CUSTOM (1 << 8) /*!< 自定义模块事件基础 */
/**
* @brief BSP module auto-registration macros
* @brief BSP 模块自动注册宏
*/
#define BSP_MODULE_REGISTER(module) \
static const bsp_module_config_t __bsp_module_##module##_config = { \
@ -173,228 +173,228 @@ typedef struct bsp_module {
}
/**
* @brief BSP module manager structure
* @brief BSP 模块管理器结构体
*/
typedef struct {
bsp_module_t* modules[BSP_MODULE_TYPE_MAX]; /*!< Modules by type */
bsp_module_t* module_list; /*!< Linked list of all modules */
uint32_t module_count; /*!< Total number of modules */
bsp_module_t* modules[BSP_MODULE_TYPE_MAX]; /*!< 按类型存储的模块 */
bsp_module_t* module_list; /*!< 所有模块的链表 */
uint32_t module_count; /*!< 模块总数 */
} bsp_module_manager_t;
/**
* @brief Initialize BSP module manager
* @retval HAL status code
* @brief 初始化 BSP 模块管理器
* @retval HAL 状态码
*/
hal_ret_t bsp_module_manager_init(void);
/**
* @brief Register a BSP module
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 注册一个 BSP 模块
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_register(bsp_module_t* module);
/**
* @brief Unregister a BSP module
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 注销一个 BSP 模块
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_unregister(bsp_module_t* module);
/**
* @brief Initialize all registered BSP modules
* @retval HAL status code
* @brief 初始化所有注册的 BSP 模块
* @retval HAL 状态码
*/
hal_ret_t bsp_module_init_all(void);
/**
* @brief Deinitialize all registered BSP modules
* @retval HAL status code
* @brief 反初始化所有注册的 BSP 模块
* @retval HAL 状态码
*/
hal_ret_t bsp_module_deinit_all(void);
/**
* @brief Start all registered BSP modules
* @retval HAL status code
* @brief 启动所有注册的 BSP 模块
* @retval HAL 状态码
*/
hal_ret_t bsp_module_start_all(void);
/**
* @brief Stop all registered BSP modules
* @retval HAL status code
* @brief 停止所有注册的 BSP 模块
* @retval HAL 状态码
*/
hal_ret_t bsp_module_stop_all(void);
/**
* @brief Initialize a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval HAL status code
* @brief 初始化指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval HAL 状态码
*/
hal_ret_t bsp_module_init(bsp_module_type_t type, uint8_t instance);
/**
* @brief Deinitialize a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval HAL status code
* @brief 反初始化指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval HAL 状态码
*/
hal_ret_t bsp_module_deinit(bsp_module_type_t type, uint8_t instance);
/**
* @brief Configure a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @param config: Module configuration data
* @param size: Module configuration size
* @retval HAL status code
* @brief 配置指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @param config: 模块配置数据
* @param size: 模块配置大小
* @retval HAL 状态码
*/
hal_ret_t bsp_module_configure(bsp_module_type_t type, uint8_t instance, void* config, size_t size);
/**
* @brief Start a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval HAL status code
* @brief 启动指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval HAL 状态码
*/
hal_ret_t bsp_module_start(bsp_module_type_t type, uint8_t instance);
/**
* @brief Stop a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval HAL status code
* @brief 停止指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval HAL 状态码
*/
hal_ret_t bsp_module_stop(bsp_module_type_t type, uint8_t instance);
/**
* @brief Reset a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval HAL status code
* @brief 复位指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval HAL 状态码
*/
hal_ret_t bsp_module_reset(bsp_module_type_t type, uint8_t instance);
/**
* @brief Get state of a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @param state: Pointer to store module state
* @retval HAL status code
* @brief 获取指定 BSP 模块的状态
* @param type: 模块类型
* @param instance: 模块实例
* @param state: 存储模块状态的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_get_state(bsp_module_type_t type, uint8_t instance, bsp_module_state_t* state);
/**
* @brief Control a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @param cmd: Control command
* @param param: Control parameter
* @retval HAL status code
* @brief 控制指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @param cmd: 控制命令
* @param param: 控制参数
* @retval HAL 状态码
*/
hal_ret_t bsp_module_control(bsp_module_type_t type, uint8_t instance, uint32_t cmd, void* param);
/**
* @brief Get a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval Pointer to module structure, or NULL if not found
* @brief 获取指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval 指向模块结构体的指针,未找到返回 NULL
*/
bsp_module_t* bsp_module_get(bsp_module_type_t type, uint8_t instance);
/**
* @brief Get all modules of a specific type
* @param type: Module type
* @retval Pointer to module list, or NULL if no modules found
* @brief 获取指定类型的所有模块
* @param type: 模块类型
* @retval 指向模块列表的指针,未找到模块返回 NULL
*/
bsp_module_t* bsp_module_get_by_type(bsp_module_type_t type);
/**
* @brief Get module by name
* @param name: Module name
* @retval Pointer to module structure, or NULL if not found
* @brief 通过名称获取模块
* @param name: 模块名称
* @retval 指向模块结构体的指针,未找到返回 NULL
*/
bsp_module_t* bsp_module_get_by_name(const char* name);
/**
* @brief Get total number of registered modules
* @retval Number of registered modules
* @brief 获取注册模块的总数
* @retval 注册模块的数量
*/
uint32_t bsp_module_get_count(void);
/**
* @brief Check if all dependencies of a module are satisfied
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 检查模块的所有依赖是否满足
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_check_dependencies(const bsp_module_t* module);
/**
* @brief Get module manager instance
* @retval Pointer to module manager structure
* @brief 获取模块管理器实例
* @retval 指向模块管理器结构体的指针
*/
bsp_module_manager_t* bsp_module_get_manager(void);
/**
* @brief Register a module event callback
* @param type: Module type
* @param instance: Module instance
* @param callback: Event callback function
* @retval HAL status code
* @brief 注册模块事件回调
* @param type: 模块类型
* @param instance: 模块实例
* @param callback: 事件回调函数
* @retval HAL 状态码
*/
hal_ret_t bsp_module_register_event_callback(bsp_module_type_t type, uint8_t instance, bsp_module_event_callback_t callback);
/**
* @brief Trigger a module event
* @param module: Pointer to module structure
* @param event: Event to trigger
* @param data: Event data
* @retval HAL status code
* @brief 触发模块事件
* @param module: 指向模块结构体的指针
* @param event: 要触发的事件
* @param data: 事件数据
* @retval HAL 状态码
*/
hal_ret_t bsp_module_trigger_event(const bsp_module_t* module, uint32_t event, void* data);
/**
* @brief Find modules by priority range
* @param min_priority: Minimum priority
* @param max_priority: Maximum priority
* @param count: Pointer to store number of modules found
* @retval Pointer to array of module pointers, or NULL if no modules found
* @brief 按优先级范围查找模块
* @param min_priority: 最低优先级
* @param max_priority: 最高优先级
* @param count: 存储找到的模块数量的指针
* @retval 指向模块指针数组的指针,未找到模块返回 NULL
*/
bsp_module_t** bsp_module_find_by_priority_range(bsp_module_priority_t min_priority, bsp_module_priority_t max_priority, uint32_t* count);
/**
* @brief Set module enable/disable state
* @param type: Module type
* @param instance: Module instance
* @param enable: Enable flag
* @retval HAL status code
* @brief 设置模块使能/禁用状态
* @param type: 模块类型
* @param instance: 模块实例
* @param enable: 使能标志
* @retval HAL 状态码
*/
hal_ret_t bsp_module_set_enable(bsp_module_type_t type, uint8_t instance, uint8_t enable);
/**
* @brief Check if module is enabled
* @param type: Module type
* @param instance: Module instance
* @param enable: Pointer to store enable state
* @retval HAL status code
* @brief 检查模块是否启用
* @param type: 模块类型
* @param instance: 模块实例
* @param enable: 存储使能状态的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_is_enabled(bsp_module_type_t type, uint8_t instance, uint8_t* enable);
/**
* @brief Set module priority
* @param type: Module type
* @param instance: Module instance
* @param priority: New priority
* @retval HAL status code
* @brief 设置模块优先级
* @param type: 模块类型
* @param instance: 模块实例
* @param priority: 新优先级
* @retval HAL 状态码
*/
hal_ret_t bsp_module_set_priority(bsp_module_type_t type, uint8_t instance, bsp_module_priority_t priority);
/**
* @brief Get module priority
* @param type: Module type
* @param instance: Module instance
* @param priority: Pointer to store priority
* @retval HAL return code
* @brief 获取模块优先级
* @param type: 模块类型
* @param instance: 模块实例
* @param priority: 存储优先级的指针
* @retval HAL 返回码
*/
hal_ret_t bsp_module_get_priority(bsp_module_type_t type, uint8_t instance, bsp_module_priority_t* priority);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_w25qxx.h
* @brief : BSP layer for W25QXX flash memory
* @brief : W25QXX 闪存芯片的 BSP 层头文件
******************************************************************************
*/
/* USER CODE END Header */
@ -15,71 +15,71 @@
#include "hal_gpio.h"
/**
* @brief W25QXX chip select pin configuration
* @brief W25QXX 芯片选择引脚配置
*/
#define W25QXX_CS_PORT HAL_GPIO_PORT_B
#define W25QXX_CS_PIN HAL_GPIO_PIN_0
/**
* @brief W25QXX SPI instance
* @brief W25QXX SPI 实例
*/
#define W25QXX_SPI_INSTANCE HAL_SPI_INSTANCE_1
/**
* @brief Initialize W25QXX flash memory
* @retval true if initialization is successful, false otherwise
* @brief 初始化 W25QXX 闪存芯片
* @retval 初始化成功返回 true失败返回 false
*/
bool bsp_w25qxx_init(void);
/**
* @brief Get W25QXX device information
* @param info Pointer to device information structure to fill
* @retval true if successful, false otherwise
* @brief 获取 W25QXX 设备信息
* @param info 指向设备信息结构体的指针
* @retval 成功返回 true失败返回 false
*/
bool bsp_w25qxx_get_device_info(w25qxx_device_info_t *info);
/**
* @brief Read data from W25QXX flash memory
* @param address Starting address to read from
* @param data Pointer to data buffer to store read data
* @param size Size of data to read
* @retval true if read is successful, false otherwise
* @brief 从 W25QXX 闪存芯片读取数据
* @param address 起始读取地址
* @param data 存储读取数据的缓冲区指针
* @param size 要读取的数据大小
* @retval 读取成功返回 true失败返回 false
*/
bool bsp_w25qxx_read(uint32_t address, uint8_t *data, uint32_t size);
/**
* @brief Write data to W25QXX flash memory
* @param address Starting address to write to
* @param data Pointer to data buffer to write
* @param size Size of data to write
* @retval true if write is successful, false otherwise
* @brief 向 W25QXX 闪存芯片写入数据
* @param address 起始写入地址
* @param data 要写入的数据缓冲区指针
* @param size 要写入的数据大小
* @retval 写入成功返回 true失败返回 false
*/
bool bsp_w25qxx_write(uint32_t address, const uint8_t *data, uint32_t size);
/**
* @brief Erase 4KB block
* @param address Address within the block to erase
* @retval true if erase is successful, false otherwise
* @brief 擦除 4KB 块
* @param address 块内的地址
* @retval 擦除成功返回 true失败返回 false
*/
bool bsp_w25qxx_erase_block_4kb(uint32_t address);
/**
* @brief Erase 32KB block
* @param address Address within the block to erase
* @retval true if erase is successful, false otherwise
* @brief 擦除 32KB
* @param address 块内的地址
* @retval 擦除成功返回 true失败返回 false
*/
bool bsp_w25qxx_erase_block_32kb(uint32_t address);
/**
* @brief Erase 64KB block
* @param address Address within the block to erase
* @retval true if erase is successful, false otherwise
* @brief 擦除 64KB
* @param address 块内的地址
* @retval 擦除成功返回 true失败返回 false
*/
bool bsp_w25qxx_erase_block_64kb(uint32_t address);
/**
* @brief Erase entire chip
* @retval true if erase is successful, false otherwise
* @brief 擦除整个芯片
* @retval 擦除成功返回 true失败返回 false
*/
bool bsp_w25qxx_erase_chip(void);

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_board_manager.c
* @brief : Board support package manager source file
* @brief : 板级支持包管理器源文件
******************************************************************************
*/
/* USER CODE END Header */
@ -14,7 +14,7 @@
extern const bsp_board_config_t stm32f407vet6_board_config;
/**
* @brief List of supported board configurations
* @brief 支持的板卡配置列表
*/
static const bsp_board_config_t* supported_boards[] = {
&stm32f407vet6_board_config,
@ -22,22 +22,22 @@ static const bsp_board_config_t* supported_boards[] = {
};
/**
* @brief Current board configuration index
* @brief 当前板卡配置索引
*/
static uint8_t current_board_index = 0;
/**
* @brief Get number of supported boards
* @retval Number of supported boards
* @brief 获取支持的板卡数量
* @retval 支持的板卡数量
*/
uint8_t bsp_board_get_count(void) {
return sizeof(supported_boards) / sizeof(supported_boards[0]);
}
/**
* @brief Get board configuration by index
* @param index: Board configuration index
* @retval Pointer to board configuration structure, NULL if invalid index
* @brief 通过索引获取板卡配置
* @param index: 板卡配置索引
* @retval 指向板卡配置结构体的指针,无效索引返回 NULL
*/
const bsp_board_config_t* bsp_board_get_by_index(uint8_t index) {
if (index < bsp_board_get_count()) {
@ -47,9 +47,9 @@ const bsp_board_config_t* bsp_board_get_by_index(uint8_t index) {
}
/**
* @brief Get board configuration by name
* @param name: Board name string
* @retval Pointer to board configuration structure, NULL if not found
* @brief 通过名称获取板卡配置
* @param name: 板卡名称字符串
* @retval 指向板卡配置结构体的指针,未找到返回 NULL
*/
const bsp_board_config_t* bsp_board_get_by_name(const char* name) {
for (uint8_t i = 0; i < bsp_board_get_count(); i++) {
@ -62,9 +62,9 @@ const bsp_board_config_t* bsp_board_get_by_name(const char* name) {
}
/**
* @brief Set current board configuration by index
* @param index: Board configuration index
* @retval HAL status code
* @brief 通过索引设置当前板卡配置
* @param index: 板卡配置索引
* @retval HAL 状态码
*/
hal_ret_t bsp_board_set_by_index(uint8_t index) {
if (index < bsp_board_get_count()) {
@ -75,9 +75,9 @@ hal_ret_t bsp_board_set_by_index(uint8_t index) {
}
/**
* @brief Set current board configuration by name
* @param name: Board name string
* @retval HAL status code
* @brief 通过名称设置当前板卡配置
* @param name: 板卡名称字符串
* @retval HAL 状态码
*/
hal_ret_t bsp_board_set_by_name(const char* name) {
if (name == NULL) {
@ -95,16 +95,16 @@ hal_ret_t bsp_board_set_by_name(const char* name) {
}
/**
* @brief Get current board configuration
* @retval Pointer to current board configuration structure
* @brief 获取当前板卡配置
* @retval 指向当前板卡配置结构体的指针
*/
const bsp_board_config_t* bsp_board_get_config(void) {
return supported_boards[current_board_index];
}
/**
* @brief Get current board index
* @retval Current board index
* @brief 获取当前板卡索引
* @retval 当前板卡索引
*/
uint8_t bsp_board_get_current_index(void) {
return current_board_index;

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_config.c
* @brief : Board support package configuration file source
* @brief : 板级支持包配置文件源文件
******************************************************************************
*/
/* USER CODE END Header */
@ -14,7 +14,7 @@
#include <ctype.h>
/**
* @brief Configuration storage entry
* @brief 配置存储条目
*/
typedef struct bsp_config_storage_entry {
char section[BSP_CONFIG_MAX_SECTION_NAME_LENGTH];
@ -24,7 +24,7 @@ typedef struct bsp_config_storage_entry {
} bsp_config_storage_entry_t;
/**
* @brief Configuration storage
* @brief 配置存储
*/
typedef struct {
bsp_config_storage_entry_t* entries;
@ -33,7 +33,7 @@ typedef struct {
} bsp_config_storage_t;
/**
* @brief Configuration storage instance
* @brief 配置存储实例
*/
static bsp_config_storage_t bsp_config_storage = {
.entries = NULL,
@ -42,9 +42,9 @@ static bsp_config_storage_t bsp_config_storage = {
};
/**
* @brief Trim whitespace from a string
* @param str: String to trim
* @return Trimmed string
* @brief 去除字符串中的空白字符
* @param str: 要处理的字符串
* @return 处理后的字符串
*/
static char* bsp_config_trim(char* str) {
char* end;
@ -71,10 +71,10 @@ static char* bsp_config_trim(char* str) {
}
/**
* @brief Open configuration file
* @param filename: Configuration file name
* @param parser: Parser context
* @retval HAL status code
* @brief 打开配置文件
* @param filename: 配置文件名称
* @param parser: 解析器上下文
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_file_open(const char* filename, bsp_config_parser_t* parser) {
if (filename == NULL || parser == NULL) {
@ -95,9 +95,9 @@ static hal_ret_t bsp_config_file_open(const char* filename, bsp_config_parser_t*
}
/**
* @brief Close configuration file
* @param parser: Parser context
* @retval HAL status code
* @brief 关闭配置文件
* @param parser: 解析器上下文
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_file_close(bsp_config_parser_t* parser) {
if (parser == NULL || !parser->initialized) {
@ -115,10 +115,10 @@ static hal_ret_t bsp_config_file_close(bsp_config_parser_t* parser) {
}
/**
* @brief Read a configuration entry from file
* @param parser: Parser context
* @param entry: Configuration entry
* @retval HAL status code
* @brief 从文件读取配置条目
* @param parser: 解析器上下文
* @param entry: 配置条目
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_file_read_entry(bsp_config_parser_t* parser, bsp_config_entry_t* entry) {
if (parser == NULL || entry == NULL || !parser->initialized) {
@ -169,10 +169,10 @@ static hal_ret_t bsp_config_file_read_entry(bsp_config_parser_t* parser, bsp_con
}
/**
* @brief Parse integer value
* @param value: String value to parse
* @param result: Pointer to store result
* @retval HAL status code
* @brief 解析整数值
* @param value: 要解析的字符串值
* @param result: 存储结果的指针
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_parse_int(const char* value, int* result) {
if (value == NULL || result == NULL) {
@ -192,10 +192,10 @@ static hal_ret_t bsp_config_parse_int(const char* value, int* result) {
}
/**
* @brief Parse unsigned integer value
* @param value: String value to parse
* @param result: Pointer to store result
* @retval HAL status code
* @brief 解析无符号整数值
* @param value: 要解析的字符串值
* @param result: 存储结果的指针
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_parse_uint(const char* value, uint32_t* result) {
if (value == NULL || result == NULL) {
@ -215,10 +215,10 @@ static hal_ret_t bsp_config_parse_uint(const char* value, uint32_t* result) {
}
/**
* @brief Parse boolean value
* @param value: String value to parse
* @param result: Pointer to store result
* @retval HAL status code
* @brief 解析布尔值
* @param value: 要解析的字符串值
* @param result: 存储结果的指针
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_parse_bool(const char* value, uint8_t* result) {
if (value == NULL || result == NULL) {
@ -241,11 +241,11 @@ static hal_ret_t bsp_config_parse_bool(const char* value, uint8_t* result) {
}
/**
* @brief Parse string value
* @param value: String value to parse
* @param result: Pointer to store result
* @param max_length: Maximum length of result buffer
* @retval HAL status code
* @brief 解析字符串值
* @param value: 要解析的字符串值
* @param result: 存储结果的指针
* @param max_length: 结果缓冲区的最大长度
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_parse_string(const char* value, char* result, size_t max_length) {
if (value == NULL || result == NULL) {
@ -259,11 +259,11 @@ static hal_ret_t bsp_config_parse_string(const char* value, char* result, size_t
}
/**
* @brief Add a configuration entry to storage
* @param section: Section name
* @param key: Key name
* @param value: Value
* @retval HAL status code
* @brief 添加配置条目到存储
* @param section: 节名称
* @param key: 键名称
* @param value:
* @retval HAL 状态码
*/
static hal_ret_t bsp_config_storage_add(const char* section, const char* key, const char* value) {
// Check if entry already exists
@ -309,7 +309,7 @@ static hal_ret_t bsp_config_storage_add(const char* section, const char* key, co
}
/**
* @brief Clear configuration storage
* @brief 清空配置存储
*/
static void bsp_config_storage_clear(void) {
bsp_config_storage_entry_t* entry = bsp_config_storage.entries;
@ -324,8 +324,8 @@ static void bsp_config_storage_clear(void) {
}
/**
* @brief Initialize BSP configuration system
* @retval HAL status code
* @brief 初始化 BSP 配置系统
* @retval HAL 状态码
*/
hal_ret_t bsp_config_init(void) {
if (bsp_config_storage.initialized) {
@ -339,8 +339,8 @@ hal_ret_t bsp_config_init(void) {
}
/**
* @brief Deinitialize BSP configuration system
* @retval HAL status code
* @brief 反初始化 BSP 配置系统
* @retval HAL 状态码
*/
hal_ret_t bsp_config_deinit(void) {
if (!bsp_config_storage.initialized) {
@ -354,9 +354,9 @@ hal_ret_t bsp_config_deinit(void) {
}
/**
* @brief Load BSP configuration from file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 从文件加载 BSP 配置
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_config_load(const char* filename) {
if (!bsp_config_storage.initialized) {
@ -395,9 +395,9 @@ hal_ret_t bsp_config_load(const char* filename) {
}
/**
* @brief Save BSP configuration to file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 保存 BSP 配置到文件
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_config_save(const char* filename) {
if (!bsp_config_storage.initialized || bsp_config_storage.entry_count == 0) {
@ -432,12 +432,12 @@ hal_ret_t bsp_config_save(const char* filename) {
}
/**
* @brief Get configuration value
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store value
* @param max_length: Maximum length of value buffer
* @retval HAL status code
* @brief 获取配置值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储值的指针
* @param max_length: 值缓冲区的最大长度
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_value(const char* section, const char* key, char* value, size_t max_length) {
if (!bsp_config_storage.initialized || section == NULL || key == NULL || value == NULL) {
@ -458,11 +458,11 @@ hal_ret_t bsp_config_get_value(const char* section, const char* key, char* value
}
/**
* @brief Set configuration value
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Configuration value
* @retval HAL status code
* @brief 设置配置值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 配置值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_value(const char* section, const char* key, const char* value) {
if (!bsp_config_storage.initialized || section == NULL || key == NULL || value == NULL) {
@ -473,11 +473,11 @@ hal_ret_t bsp_config_set_value(const char* section, const char* key, const char*
}
/**
* @brief Get configuration value as integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store integer value
* @retval HAL status code
* @brief 获取配置值作为整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储整数值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_int(const char* section, const char* key, int* value) {
char str_value[BSP_CONFIG_MAX_VALUE_LENGTH];
@ -490,11 +490,11 @@ hal_ret_t bsp_config_get_int(const char* section, const char* key, int* value) {
}
/**
* @brief Set configuration value as integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Integer value
* @retval HAL status code
* @brief 设置配置值作为整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 整数值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_int(const char* section, const char* key, int value) {
char str_value[BSP_CONFIG_MAX_VALUE_LENGTH];
@ -503,11 +503,11 @@ hal_ret_t bsp_config_set_int(const char* section, const char* key, int value) {
}
/**
* @brief Get configuration value as unsigned integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store unsigned integer value
* @retval HAL status code
* @brief 获取配置值作为无符号整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储无符号整数值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_uint(const char* section, const char* key, uint32_t* value) {
char str_value[BSP_CONFIG_MAX_VALUE_LENGTH];
@ -520,11 +520,11 @@ hal_ret_t bsp_config_get_uint(const char* section, const char* key, uint32_t* va
}
/**
* @brief Set configuration value as unsigned integer
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Unsigned integer value
* @retval HAL status code
* @brief 设置配置值作为无符号整数
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 无符号整数值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_uint(const char* section, const char* key, uint32_t value) {
char str_value[BSP_CONFIG_MAX_VALUE_LENGTH];
@ -533,11 +533,11 @@ hal_ret_t bsp_config_set_uint(const char* section, const char* key, uint32_t val
}
/**
* @brief Get configuration value as boolean
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Pointer to store boolean value
* @retval HAL status code
* @brief 获取配置值作为布尔值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 存储布尔值的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_get_bool(const char* section, const char* key, uint8_t* value) {
char str_value[BSP_CONFIG_MAX_VALUE_LENGTH];
@ -550,20 +550,20 @@ hal_ret_t bsp_config_get_bool(const char* section, const char* key, uint8_t* val
}
/**
* @brief Set configuration value as boolean
* @param section: Configuration section name
* @param key: Configuration key name
* @param value: Boolean value
* @retval HAL status code
* @brief 设置配置值作为布尔值
* @param section: 配置节名称
* @param key: 配置键名称
* @param value: 布尔值
* @retval HAL 状态码
*/
hal_ret_t bsp_config_set_bool(const char* section, const char* key, uint8_t value) {
return bsp_config_set_value(section, key, value ? "true" : "false");
}
/**
* @brief Parse BSP modules from configuration
* @param config: Pointer to BSP board configuration structure to fill
* @retval HAL status code
* @brief 从配置解析 BSP 模块
* @param config: 指向要填充的 BSP 板卡配置结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_config_parse_modules(bsp_board_config_t* config) {
if (config == NULL) {
@ -578,9 +578,9 @@ hal_ret_t bsp_config_parse_modules(bsp_board_config_t* config) {
}
/**
* @brief Initialize BSP from configuration file
* @param filename: Configuration file name
* @retval HAL status code
* @brief 从配置文件初始化 BSP
* @param filename: 配置文件名称
* @retval HAL 状态码
*/
hal_ret_t bsp_init_from_config(const char* filename) {
// Load configuration file

353
BSP/Src/bsp_eth.c Normal file
View File

@ -0,0 +1,353 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : bsp_eth.c
* @brief : Board support package Ethernet (LAN8720) driver implementation
******************************************************************************
*/
/* USER CODE END Header */
#include "bsp_eth.h"
/**
* @brief Ethernet configuration
*/
static bsp_eth_config_t eth_config;
static uint8_t eth_initialized = 0;
/**
* @brief Initialize Ethernet (LAN8720) module
* @param config: Pointer to Ethernet configuration structure
* @retval HAL status code
*/
hal_ret_t bsp_eth_init(const bsp_eth_config_t* config)
{
if (!config) {
return HAL_RET_INVALID_PARAM;
}
if (eth_initialized) {
return HAL_RET_OK;
}
/* Store configuration */
eth_config = *config;
/* Convert to HAL configuration */
hal_eth_config_t hal_config;
hal_config.enable = config->enable;
hal_config.instance = config->instance;
hal_config.mode = config->mode;
hal_config.speed = config->speed;
hal_config.phy_addr = config->phy_addr;
hal_config.mac_addr = config->mac_addr;
hal_config.auto_negotiation = config->auto_negotiation;
hal_config.interrupt_enable = config->interrupt_enable;
/* Initialize HAL Ethernet */
if (hal_eth_init(&hal_config) != HAL_RET_OK) {
return HAL_RET_INIT_ERROR;
}
/* Detect LAN8720 PHY */
uint8_t detected;
if (bsp_eth_detect_phy(&detected) != HAL_RET_OK) {
return HAL_RET_INIT_ERROR;
}
if (!detected) {
return HAL_RET_INIT_ERROR;
}
/* Configure PHY */
if (bsp_eth_configure_phy(config->mode, config->speed, config->auto_negotiation) != HAL_RET_OK) {
return HAL_RET_INIT_ERROR;
}
eth_initialized = 1;
return HAL_RET_OK;
}
/**
* @brief Deinitialize Ethernet (LAN8720) module
* @retval HAL status code
*/
hal_ret_t bsp_eth_deinit(void)
{
if (!eth_initialized) {
return HAL_RET_OK;
}
if (hal_eth_deinit(eth_config.instance) != HAL_RET_OK) {
return HAL_RET_DEINIT_ERROR;
}
eth_initialized = 0;
return HAL_RET_OK;
}
/**
* @brief Get Ethernet (LAN8720) status
* @param status: Pointer to Ethernet status structure
* @retval HAL status code
*/
hal_ret_t bsp_eth_get_status(bsp_eth_status_t* status)
{
if (!status) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
/* Get HAL Ethernet status */
hal_eth_status_t hal_status;
if (hal_eth_get_status(eth_config.instance, &hal_status) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
/* Copy status */
status->link_up = hal_status.link_up;
status->duplex = hal_status.duplex;
status->speed = hal_status.speed;
status->rx_packets = hal_status.rx_packets;
status->tx_packets = hal_status.tx_packets;
status->rx_errors = hal_status.rx_errors;
status->tx_errors = hal_status.tx_errors;
/* Get PHY identifiers */
if (bsp_eth_read_phy_reg(LAN8720_REG_PID1, &status->phy_id1) != HAL_RET_OK) {
status->phy_id1 = 0;
}
if (bsp_eth_read_phy_reg(LAN8720_REG_PID2, &status->phy_id2) != HAL_RET_OK) {
status->phy_id2 = 0;
}
return HAL_RET_OK;
}
/**
* @brief Set Ethernet MAC address
* @param mac_addr: Pointer to MAC address structure
* @retval HAL status code
*/
hal_ret_t bsp_eth_set_mac_addr(const hal_eth_mac_addr_t* mac_addr)
{
if (!mac_addr) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_set_mac_addr(eth_config.instance, mac_addr) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
eth_config.mac_addr = *mac_addr;
return HAL_RET_OK;
}
/**
* @brief Get Ethernet MAC address
* @param mac_addr: Pointer to MAC address structure
* @retval HAL status code
*/
hal_ret_t bsp_eth_get_mac_addr(hal_eth_mac_addr_t* mac_addr)
{
if (!mac_addr) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_get_mac_addr(eth_config.instance, mac_addr) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
return HAL_RET_OK;
}
/**
* @brief Check Ethernet link status
* @param link_up: Pointer to store link up status
* @retval HAL status code
*/
hal_ret_t bsp_eth_check_link(uint8_t* link_up)
{
if (!link_up) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_check_link(eth_config.instance, link_up) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
return HAL_RET_OK;
}
/**
* @brief Reset Ethernet PHY (LAN8720)
* @retval HAL status code
*/
hal_ret_t bsp_eth_reset_phy(void)
{
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_reset_phy(eth_config.instance, eth_config.phy_addr) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
return HAL_RET_OK;
}
/**
* @brief Read LAN8720 PHY register
* @param reg_addr: Register address
* @param value: Pointer to store register value
* @retval HAL status code
*/
hal_ret_t bsp_eth_read_phy_reg(uint16_t reg_addr, uint16_t* value)
{
if (!value) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_read_phy_reg(eth_config.instance, eth_config.phy_addr, reg_addr, value) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
return HAL_RET_OK;
}
/**
* @brief Write LAN8720 PHY register
* @param reg_addr: Register address
* @param value: Register value
* @retval HAL status code
*/
hal_ret_t bsp_eth_write_phy_reg(uint16_t reg_addr, uint16_t value)
{
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
if (hal_eth_write_phy_reg(eth_config.instance, eth_config.phy_addr, reg_addr, value) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
return HAL_RET_OK;
}
/**
* @brief Detect LAN8720 PHY presence
* @param detected: Pointer to store detection result
* @retval HAL status code
*/
hal_ret_t bsp_eth_detect_phy(uint8_t* detected)
{
if (!detected) {
return HAL_RET_INVALID_PARAM;
}
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
/* Read PHY identifiers */
uint16_t id1, id2;
if (bsp_eth_read_phy_reg(LAN8720_REG_PID1, &id1) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
if (bsp_eth_read_phy_reg(LAN8720_REG_PID2, &id2) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
/* Check if this is a LAN8720 PHY */
if (id1 == LAN8720_PID1_VALUE && (id2 & 0xFFF0) == LAN8720_PID2_VALUE) {
*detected = 1;
} else {
*detected = 0;
}
return HAL_RET_OK;
}
/**
* @brief Configure LAN8720 PHY
* @param mode: Ethernet mode
* @param speed: Ethernet speed
* @param auto_neg: Auto-negotiation enable
* @retval HAL status code
*/
hal_ret_t bsp_eth_configure_phy(hal_eth_mode_t mode, hal_eth_speed_t speed, uint8_t auto_neg)
{
if (!eth_initialized) {
return HAL_RET_INIT_ERROR;
}
/* Read current BCR register */
uint16_t bcr;
if (bsp_eth_read_phy_reg(LAN8720_REG_BCR, &bcr) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
/* Clear relevant bits */
bcr &= ~(LAN8720_BCR_AUTO_NEG_EN | LAN8720_BCR_SPEED_SELECT | LAN8720_BCR_DUPLEX_MODE);
if (auto_neg) {
/* Enable auto-negotiation */
bcr |= LAN8720_BCR_AUTO_NEG_EN;
bcr |= LAN8720_BCR_RESTART_AN;
} else {
/* Manual configuration */
if (speed == HAL_ETH_SPEED_100MBPS) {
bcr |= LAN8720_BCR_SPEED_SELECT;
}
if (mode == HAL_ETH_MODE_FULLDUPLEX) {
bcr |= LAN8720_BCR_DUPLEX_MODE;
}
}
/* Write BCR register */
if (bsp_eth_write_phy_reg(LAN8720_REG_BCR, bcr) != HAL_RET_OK) {
return HAL_RET_ERROR;
}
/* Wait for configuration to complete */
if (auto_neg) {
uint32_t timeout = 1000;
uint16_t bsr;
while (timeout--) {
if (bsp_eth_read_phy_reg(LAN8720_REG_BSR, &bsr) == HAL_RET_OK) {
if (bsr & LAN8720_BSR_AUTO_NEG_COMPLETE) {
break;
}
}
HAL_Delay(1);
}
if (timeout == 0) {
return HAL_RET_TIMEOUT;
}
}
return HAL_RET_OK;
}

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_init.c
* @brief : Board support package initialization source file
* @brief : 板级支持包初始化源文件
******************************************************************************
*/
/* USER CODE END Header */
@ -15,8 +15,8 @@
#include "hal_uart.h"
/**
* @brief Initialize board support package
* @retval HAL status code
* @brief 初始化板级支持包
* @retval HAL 状态码
*/
hal_ret_t bsp_init(void) {
/* Get board configuration */
@ -137,8 +137,8 @@ hal_ret_t bsp_init(void) {
}
/**
* @brief Initialize board GPIO using configuration
* @retval HAL status code
* @brief 使用配置初始化板卡 GPIO
* @retval HAL 状态码
*/
hal_ret_t bsp_gpio_init(void) {
/* Get board configuration */
@ -254,16 +254,16 @@ hal_ret_t bsp_gpio_init(void) {
}
/**
* @brief Get board name
* @retval Board name string
* @brief 获取板卡名称
* @retval 板卡名称字符串
*/
const char* bsp_get_board_name(void) {
return bsp_board_get_name();
}
/**
* @brief Get current board configuration
* @retval Pointer to board configuration structure
* @brief 获取当前板卡配置
* @retval 指向板卡配置结构体的指针
*/
const bsp_board_config_t* bsp_get_board_config(void) {
return bsp_board_get_config();

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_key.c
* @brief : Board support package key driver implementation
* @brief : 板级支持包按键驱动实现
******************************************************************************
*/
/* USER CODE END Header */
@ -15,7 +15,7 @@
#include "hal_delay.h"
/**
* @brief Key debounce configuration
* @brief 按键防抖配置
*/
#define KEY_DEBOUNCE_COUNT 5 /* Debounce count (each update is ~10ms, so 50ms debounce) */
#define KEY_LONG_PRESS_TIME 1000 /* Long press time in ms */
@ -24,7 +24,7 @@
#define KEY_UPDATE_INTERVAL 10 /* Update interval in ms */
/**
* @brief Key configuration structure
* @brief 按键配置结构体
*/
typedef struct {
hal_gpio_port_t port;
@ -33,7 +33,7 @@ typedef struct {
} bsp_key_config_t;
/**
* @brief Key state structure for debouncing and event detection
* @brief 按键状态结构体,用于防抖和事件检测
*/
typedef struct {
/* Debounce state */
@ -63,12 +63,12 @@ typedef struct {
} bsp_key_internal_state_t;
/**
* @brief Key state table for debouncing and event detection
* @brief 按键状态表,用于防抖和事件检测
*/
static bsp_key_internal_state_t key_state_table[BSP_KEY_ID_MAX] = {0};
/**
* @brief Get current board button configuration
* @brief 获取当前板卡按键配置
*/
static const bsp_button_config_t* bsp_key_get_button_config(bsp_key_id_t key_id) {
const bsp_board_config_t* board_config = bsp_board_get_config();
@ -79,9 +79,9 @@ static const bsp_button_config_t* bsp_key_get_button_config(bsp_key_id_t key_id)
}
/**
* @brief Read raw key state (without debounce)
* @param key_id: Key ID
* @retval Raw key state
* @brief 读取原始按键状态(无防抖)
* @param key_id: 按键 ID
* @retval 原始按键状态
*/
static bsp_key_state_t bsp_key_read_raw(bsp_key_id_t key_id) {
bsp_key_state_t state;
@ -107,8 +107,8 @@ static bsp_key_state_t bsp_key_read_raw(bsp_key_id_t key_id) {
}
/**
* @brief Get current system time in milliseconds
* @retval Current time in ms
* @brief 获取当前系统时间(毫秒)
* @retval 当前时间(毫秒)
*/
static uint32_t bsp_key_get_time_ms(void) {
/* Use HAL tick function */
@ -116,7 +116,7 @@ static uint32_t bsp_key_get_time_ms(void) {
}
/**
* @brief Initialize all keys
* @brief 初始化所有按键
*/
void bsp_key_init(void) {
uint8_t i;
@ -162,7 +162,7 @@ void bsp_key_init(void) {
}
/**
* @brief Update key state (call this function periodically, e.g. every 10ms)
* @brief 更新按键状态(定期调用此函数,例如每 10ms
*/
void bsp_key_update(void) {
uint8_t i;
@ -241,9 +241,9 @@ void bsp_key_update(void) {
}
/**
* @brief Read debounced key state
* @param key_id: Key ID
* @retval Key state
* @brief 读取防抖后的按键状态
* @param key_id: 按键 ID
* @retval 按键状态
*/
bsp_key_state_t bsp_key_read(bsp_key_id_t key_id) {
if (key_id < BSP_KEY_ID_MAX) {
@ -253,27 +253,27 @@ bsp_key_state_t bsp_key_read(bsp_key_id_t key_id) {
}
/**
* @brief Check if key is pressed
* @param key_id: Key ID
* @retval 1 if pressed, 0 otherwise
* @brief 检查按键是否按下
* @param key_id: 按键 ID
* @retval 按下返回 1否则返回 0
*/
uint8_t bsp_key_is_pressed(bsp_key_id_t key_id) {
return (bsp_key_read(key_id) == BSP_KEY_STATE_PRESSED) ? 1 : 0;
}
/**
* @brief Check if key is released
* @param key_id: Key ID
* @retval 1 if released, 0 otherwise
* @brief 检查按键是否释放
* @param key_id: 按键 ID
* @retval 释放返回 1否则返回 0
*/
uint8_t bsp_key_is_released(bsp_key_id_t key_id) {
return (bsp_key_read(key_id) == BSP_KEY_STATE_RELEASED) ? 1 : 0;
}
/**
* @brief Check for key press event (edge detection)
* @param key_id: Key ID
* @retval 1 if key was pressed, 0 otherwise
* @brief 检查按键按下事件(边沿检测)
* @param key_id: 按键 ID
* @retval 按下事件返回 1否则返回 0
*/
uint8_t bsp_key_get_press_event(bsp_key_id_t key_id) {
uint8_t event = 0;
@ -287,9 +287,9 @@ uint8_t bsp_key_get_press_event(bsp_key_id_t key_id) {
}
/**
* @brief Check for key release event (edge detection)
* @param key_id: Key ID
* @retval 1 if key was released, 0 otherwise
* @brief 检查按键释放事件(边沿检测)
* @param key_id: 按键 ID
* @retval 释放事件返回 1否则返回 0
*/
uint8_t bsp_key_get_release_event(bsp_key_id_t key_id) {
uint8_t event = 0;
@ -303,9 +303,9 @@ uint8_t bsp_key_get_release_event(bsp_key_id_t key_id) {
}
/**
* @brief Check for key long pressed event
* @param key_id: Key ID
* @retval 1 if key was long pressed, 0 otherwise
* @brief 检查按键长按事件
* @param key_id: 按键 ID
* @retval 长按事件返回 1否则返回 0
*/
uint8_t bsp_key_get_long_press_event(bsp_key_id_t key_id) {
uint8_t event = 0;
@ -319,9 +319,9 @@ uint8_t bsp_key_get_long_press_event(bsp_key_id_t key_id) {
}
/**
* @brief Check for key repeat event
* @param key_id: Key ID
* @retval 1 if key repeat event occurred, 0 otherwise
* @brief 检查按键重复事件
* @param key_id: 按键 ID
* @retval 重复事件返回 1否则返回 0
*/
uint8_t bsp_key_get_repeat_event(bsp_key_id_t key_id) {
uint8_t event = 0;
@ -335,9 +335,9 @@ uint8_t bsp_key_get_repeat_event(bsp_key_id_t key_id) {
}
/**
* @brief Check for key short pressed event
* @param key_id: Key ID
* @retval 1 if key was short pressed, 0 otherwise
* @brief 检查按键短按事件
* @param key_id: 按键 ID
* @retval 短按事件返回 1否则返回 0
*/
uint8_t bsp_key_get_short_press_event(bsp_key_id_t key_id) {
uint8_t event = 0;

View File

@ -2,7 +2,7 @@
/**
******************************************************************************
* @file : bsp_module.c
* @brief : Board support package module management source file
* @brief : 板级支持包模块管理源文件
******************************************************************************
*/
/* USER CODE END Header */
@ -11,13 +11,13 @@
#include <string.h>
/**
* @brief Module manager instance
* @brief 模块管理器实例
*/
static bsp_module_manager_t bsp_module_manager;
/**
* @brief Initialize BSP module manager
* @retval HAL status code
* @brief 初始化 BSP 模块管理器
* @retval HAL 状态码
*/
hal_ret_t bsp_module_manager_init(void) {
/* Clear module manager */
@ -35,9 +35,9 @@ hal_ret_t bsp_module_manager_init(void) {
}
/**
* @brief Register a BSP module
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 注册一个 BSP 模块
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_register(bsp_module_t* module) {
if (module == NULL) {
@ -102,9 +102,9 @@ hal_ret_t bsp_module_register(bsp_module_t* module) {
}
/**
* @brief Unregister a BSP module
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 注销一个 BSP 模块
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_unregister(bsp_module_t* module) {
if (module == NULL) {
@ -146,10 +146,10 @@ hal_ret_t bsp_module_unregister(bsp_module_t* module) {
}
/**
* @brief Get a specific BSP module
* @param type: Module type
* @param instance: Module instance
* @retval Pointer to module structure, or NULL if not found
* @brief 获取指定的 BSP 模块
* @param type: 模块类型
* @param instance: 模块实例
* @retval 指向模块结构体的指针,未找到返回 NULL
*/
bsp_module_t* bsp_module_get(bsp_module_type_t type, uint8_t instance) {
if (type >= BSP_MODULE_TYPE_MAX) {
@ -168,9 +168,9 @@ bsp_module_t* bsp_module_get(bsp_module_type_t type, uint8_t instance) {
}
/**
* @brief Get all modules of a specific type
* @param type: Module type
* @retval Pointer to module list, or NULL if no modules found
* @brief 获取指定类型的所有模块
* @param type: 模块类型
* @retval 指向模块列表的指针,未找到模块返回 NULL
*/
bsp_module_t* bsp_module_get_by_type(bsp_module_type_t type) {
if (type >= BSP_MODULE_TYPE_MAX) {
@ -181,9 +181,9 @@ bsp_module_t* bsp_module_get_by_type(bsp_module_type_t type) {
}
/**
* @brief Get module by name
* @param name: Module name
* @retval Pointer to module structure, or NULL if not found
* @brief 通过名称获取模块
* @param name: 模块名称
* @retval 指向模块结构体的指针,未找到返回 NULL
*/
bsp_module_t* bsp_module_get_by_name(const char* name) {
if (name == NULL) {
@ -202,9 +202,9 @@ bsp_module_t* bsp_module_get_by_name(const char* name) {
}
/**
* @brief Check if all dependencies of a module are satisfied
* @param module: Pointer to module structure
* @retval HAL status code
* @brief 检查模块的所有依赖是否满足
* @param module: 指向模块结构体的指针
* @retval HAL 状态码
*/
hal_ret_t bsp_module_check_dependencies(const bsp_module_t* module) {
if (module == NULL) {

View File

@ -2,21 +2,23 @@
/**
******************************************************************************
* @file : stm32f407vet6_board.c
* @brief : STM32F407VET6 board configuration implementation
* @brief : STM32F407VET6 板卡配置实现
******************************************************************************
*/
/* USER CODE END Header */
#include "bsp_board.h"
#include "bsp_config.h"
#include "bsp_eth.h"
#include "hal_gpio.h"
#include "hal_uart.h"
#include "hal_spi.h"
#include "hal_eth.h"
/**
* @brief Default LED initialization function
* @param config: LED configuration structure
* @retval HAL status code
* @brief 默认 LED 初始化函数
* @param config: LED 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_led_init(const void* config) {
const bsp_led_config_t* led_config = (const bsp_led_config_t*)config;
@ -31,9 +33,9 @@ static hal_ret_t default_led_init(const void* config) {
}
/**
* @brief Default button initialization function
* @param config: Button configuration structure
* @retval HAL status code
* @brief 默认按键初始化函数
* @param config: 按键配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_button_init(const void* config) {
const bsp_buttons_config_t* buttons_config = (const bsp_buttons_config_t*)config;
@ -59,9 +61,9 @@ static hal_ret_t default_button_init(const void* config) {
}
/**
* @brief Default UART initialization function
* @param config: UART configuration structure
* @retval HAL status code
* @brief 默认 UART 初始化函数
* @param config: UART 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_uart_init(const void* config) {
const bsp_uart_config_t* uart_config = (const bsp_uart_config_t*)config;
@ -76,9 +78,9 @@ static hal_ret_t default_uart_init(const void* config) {
}
/**
* @brief Default SPI initialization function
* @param config: SPI configuration structure
* @retval HAL status code
* @brief 默认 SPI 初始化函数
* @param config: SPI 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_spi_init(const void* config) {
const bsp_spi_config_t* spi_config = (const bsp_spi_config_t*)config;
@ -94,45 +96,48 @@ static hal_ret_t default_spi_init(const void* config) {
}
/**
* @brief Default I2C initialization function
* @param config: I2C configuration structure
* @retval HAL status code
* @brief 默认 I2C 初始化函数
* @param config: I2C 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_i2c_init(const void* config) {
/* TODO: Implement default I2C initialization */
(void)config;
/* TODO: 实现默认 I2C 初始化 */
return HAL_RET_OK;
}
/**
* @brief Default CAN initialization function
* @param config: CAN configuration structure
* @retval HAL status code
* @brief 默认 CAN 初始化函数
* @param config: CAN 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_can_init(const void* config) {
/* TODO: Implement default CAN initialization */
(void)config;
/* TODO: 实现默认 CAN 初始化 */
return HAL_RET_OK;
}
/**
* @brief Default ADC initialization function
* @param config: ADC configuration structure
* @retval HAL status code
* @brief 默认 ADC 初始化函数
* @param config: ADC 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_adc_init(const void* config) {
/* TODO: Implement default ADC initialization */
(void)config;
/* TODO: 实现默认 ADC 初始化 */
return HAL_RET_OK;
}
/**
* @brief Default W25QXX initialization function
* @param config: W25QXX configuration structure
* @retval HAL status code
* @brief 默认 W25QXX 初始化函数
* @param config: W25QXX 配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_w25qxx_init(const void* config) {
const bsp_w25qxx_config_t* w25qxx_config = (const bsp_w25qxx_config_t*)config;
hal_ret_t status = HAL_RET_OK;
/* Initialize CS pin */
/* 初始化 CS 引脚 */
hal_gpio_config_t gpio_config = {
.port = w25qxx_config->cs_port,
.pin = w25qxx_config->cs_pin,
@ -145,21 +150,31 @@ static hal_ret_t default_w25qxx_init(const void* config) {
return status;
}
/* Deselect chip initially */
/* 初始时取消选择芯片 */
status = hal_gpio_write_pin(w25qxx_config->cs_port, w25qxx_config->cs_pin, HAL_GPIO_PIN_SET);
if (status != HAL_RET_OK) {
return status;
}
/* SPI instance is now just an index, actual SPI initialization is handled separately */
/* SPI 实例现在只是一个索引,实际的 SPI 初始化在别处处理 */
return status;
}
/**
* @brief STM32F407VET6 board button configurations
* @brief 默认以太网初始化函数
* @param config: 以太网配置结构体
* @retval HAL 状态码
*/
static hal_ret_t default_eth_init(const void* config) {
const bsp_eth_config_t* eth_config = (const bsp_eth_config_t*)config;
return bsp_eth_init(eth_config);
}
/**
* @brief STM32F407VET6 板卡按键配置
*/
static const bsp_button_config_t stm32f407vet6_buttons[] = {
/* KEY0 - PE4, active low */
/* KEY0 - PE4, 低电平有效 */
{
.port = HAL_GPIO_PORT_E,
.pin = HAL_GPIO_PIN_4,
@ -168,7 +183,7 @@ static const bsp_button_config_t stm32f407vet6_buttons[] = {
.pull = HAL_GPIO_PULL_UP,
.active_high = 0
},
/* KEY1 - PE3, active low */
/* KEY1 - PE3, 低电平有效 */
{
.port = HAL_GPIO_PORT_E,
.pin = HAL_GPIO_PIN_3,
@ -177,7 +192,7 @@ static const bsp_button_config_t stm32f407vet6_buttons[] = {
.pull = HAL_GPIO_PULL_UP,
.active_high = 0
},
/* WKUP - PA0, active high */
/* WKUP - PA0, 高电平有效 */
{
.port = HAL_GPIO_PORT_A,
.pin = HAL_GPIO_PIN_0,
@ -189,7 +204,7 @@ static const bsp_button_config_t stm32f407vet6_buttons[] = {
};
/**
* @brief STM32F407VET6 buttons configuration
* @brief STM32F407VET6 按键配置
*/
static const bsp_buttons_config_t stm32f407vet6_buttons_config = {
.enable = 1,
@ -198,7 +213,7 @@ static const bsp_buttons_config_t stm32f407vet6_buttons_config = {
};
/**
* @brief STM32F407VET6 board UART configurations
* @brief STM32F407VET6 板卡 UART 配置
*/
static const bsp_uart_config_t stm32f407vet6_uarts[] = {
/* USART1 - PA9(TX), PA10(RX) */
@ -217,7 +232,7 @@ static const bsp_uart_config_t stm32f407vet6_uarts[] = {
};
/**
* @brief STM32F407VET6 board SPI configurations
* @brief STM32F407VET6 板卡 SPI 配置
*/
static const bsp_spi_config_t stm32f407vet6_spis[] = {
/* SPI1 - PA5(SCK), PA6(MISO), PA7(MOSI) */
@ -239,12 +254,12 @@ static const bsp_spi_config_t stm32f407vet6_spis[] = {
};
/**
* @brief STM32F407VET6 board I2C configurations
* @brief STM32F407VET6 板卡 I2C 配置
*/
static const bsp_i2c_config_t stm32f407vet6_i2cs[] = {
/* I2C1 - PB6(SCL), PB7(SDA) */
{
.enable = 0, /* Disabled by default */
.enable = 0, /* 默认禁用 */
.instance = HAL_I2C_INSTANCE_1,
.speed = HAL_I2C_SPEED_STANDARD,
.address_mode = HAL_I2C_ADDRESS_7BIT,
@ -258,12 +273,12 @@ static const bsp_i2c_config_t stm32f407vet6_i2cs[] = {
};
/**
* @brief STM32F407VET6 board CAN configurations
* @brief STM32F407VET6 板卡 CAN 配置
*/
static const bsp_can_config_t stm32f407vet6_cans[] = {
/* CAN1 - PA11(RX), PA12(TX) */
{
.enable = 0, /* Disabled by default */
.enable = 0, /* 默认禁用 */
.instance = HAL_CAN_INSTANCE_1,
.mode = HAL_CAN_MODE_NORMAL,
.prescaler = 6, /* 168MHz / 6 = 28MHz */
@ -278,12 +293,12 @@ static const bsp_can_config_t stm32f407vet6_cans[] = {
};
/**
* @brief STM32F407VET6 board ADC channel configurations
* @brief STM32F407VET6 板卡 ADC 通道配置
*/
static const bsp_adc_channel_config_t stm32f407vet6_adc_channels[] = {
/* ADC1 Channel 0 - PA0 */
{
.enable = 0, /* Disabled by default */
.enable = 0, /* 默认禁用 */
.channel = HAL_ADC_CHANNEL_0,
.sampletime = HAL_ADC_SAMPLETIME_56CYCLES,
.rank = 1
@ -291,12 +306,12 @@ static const bsp_adc_channel_config_t stm32f407vet6_adc_channels[] = {
};
/**
* @brief STM32F407VET6 board ADC configurations
* @brief STM32F407VET6 板卡 ADC 配置
*/
static const bsp_adc_config_t stm32f407vet6_adcs[] = {
/* ADC1 */
{
.enable = 0, /* Disabled by default */
.enable = 0, /* 默认禁用 */
.instance = HAL_ADC_INSTANCE_1,
.resolution = HAL_ADC_RESOLUTION_12B,
.scan_conversion_mode = 0,
@ -307,7 +322,7 @@ static const bsp_adc_config_t stm32f407vet6_adcs[] = {
};
/**
* @brief STM32F407VET6 board LED configurations
* @brief STM32F407VET6 板卡 LED 配置
*/
static const bsp_led_config_t stm32f407vet6_leds[] = {
/* LED0 - PA6 */
@ -331,19 +346,19 @@ static const bsp_led_config_t stm32f407vet6_leds[] = {
};
/**
* @brief STM32F407VET6 board configuration
* @brief STM32F407VET6 板卡配置
*/
const bsp_board_config_t stm32f407vet6_board_config = {
.version = BSP_CONFIG_VERSION,
.name = "STM32F407VET6",
.description = "STM32F407VET6 Development Board",
.description = "STM32F407VET6 开发板",
.id = {
.vendor_id = 0x0483, /* STMicroelectronics */
.product_id = 0x374B, /* STM32F4 Series */
.serial_number = 0x00000001 /* Default serial number */
.product_id = 0x374B, /* STM32F4 系列 */
.serial_number = 0x00000001 /* 默认序列号 */
},
/* Board features */
/* 板卡特性 */
.features = (
BSP_BOARD_FEATURE_LED |
BSP_BOARD_FEATURE_BUTTON |
@ -352,21 +367,22 @@ const bsp_board_config_t stm32f407vet6_board_config = {
BSP_BOARD_FEATURE_I2C |
BSP_BOARD_FEATURE_CAN |
BSP_BOARD_FEATURE_ADC |
BSP_BOARD_FEATURE_W25QXX
BSP_BOARD_FEATURE_W25QXX |
BSP_BOARD_FEATURE_ETH
),
/* Hardware information */
/* 硬件信息 */
.hw_info = {
.clock_speed = 168000000, /* 168 MHz */
.flash_size = 512 * 1024, /* 512 KB */
.ram_size = 192 * 1024, /* 192 KB */
.eeprom_size = 0, /* No internal EEPROM */
.eeprom_size = 0, /* 无内部 EEPROM */
.sram_size = 64 * 1024, /* 64 KB SRAM */
.cpu_core = 0x0F, /* Cortex-M4 */
.cpu_bits = 32 /* 32-bit CPU */
.cpu_bits = 32 /* 32 CPU */
},
/* Peripheral configurations */
/* 外设配置 */
.leds = {
.enable = 1,
.count = sizeof(stm32f407vet6_leds) / sizeof(bsp_led_config_t),
@ -377,33 +393,45 @@ const bsp_board_config_t stm32f407vet6_board_config = {
.enable = 1,
.cs_port = HAL_GPIO_PORT_B,
.cs_pin = HAL_GPIO_PIN_0,
.spi_instance = 0 /* Use SPI instance 0 */
.spi_instance = 0 /* 使用 SPI 实例 0 */
},
.eth = {
.enable = 1,
.instance = HAL_ETH_INSTANCE_1,
.mode = HAL_ETH_MODE_FULLDUPLEX,
.speed = HAL_ETH_SPEED_100MBPS,
.phy_addr = LAN8720_PHY_ADDR,
.mac_addr = {
.byte = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
},
.auto_negotiation = 1,
.interrupt_enable = 1
},
/* Peripheral arrays */
/* 外设数组 */
.periphs = {
/* UART configurations */
/* UART 配置 */
.uart_count = 1,
.uarts = stm32f407vet6_uarts,
/* SPI configurations */
/* SPI 配置 */
.spi_count = 1,
.spis = stm32f407vet6_spis,
/* I2C configurations */
/* I2C 配置 */
.i2c_count = 1,
.i2cs = stm32f407vet6_i2cs,
/* CAN configurations */
/* CAN 配置 */
.can_count = 1,
.cans = stm32f407vet6_cans,
/* ADC configurations */
/* ADC 配置 */
.adc_count = 1,
.adcs = stm32f407vet6_adcs
},
/* Initialization function pointers */
/* 初始化函数指针 */
.init_funcs = {
.led_init = default_led_init,
.button_init = default_button_init,
@ -412,31 +440,32 @@ const bsp_board_config_t stm32f407vet6_board_config = {
.i2c_init = default_i2c_init,
.can_init = default_can_init,
.adc_init = default_adc_init,
.w25qxx_init = default_w25qxx_init
.w25qxx_init = default_w25qxx_init,
.eth_init = default_eth_init
},
/* Additional board-specific configuration */
/* 额外的板卡特定配置 */
.custom_config = NULL,
.custom_config_size = 0,
/* Board revision information */
/* 板卡版本信息 */
.major_rev = 1,
.minor_rev = 0,
.patch_rev = 0,
.revision_desc = "Original release"
.revision_desc = "初始版本"
};
/**
* @brief Get board name
* @retval Board name string
* @brief 获取板卡名称
* @retval 板卡名称字符串
*/
const char* bsp_board_get_name(void) {
return stm32f407vet6_board_config.name;
}
/**
* @brief Get board configuration
* @retval Pointer to board configuration structure
* @brief 获取板卡配置
* @retval 指向板卡配置结构体的指针
*/
const bsp_board_config_t* bsp_board_get_config(void) {
return &stm32f407vet6_board_config;