Files
stm32f407ve_black/BSP/Inc/bsp_eth.h
2026-01-29 15:08:30 +08:00

170 lines
5.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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 */