• 【STM32】HAL库-以太网外设-LAN8720A-LWIP-无操作系统


    开发环境

    • KEIL:MDK_ARM_5.27
    • MCU:STM32F429IGT6
    • PHY_IC:LAN8720A
    • LWIP:LWIP2.1.2
    • STM32CUBEMX:6.6.1
    • HAL:V1.27.1

    LAN8720A使用RMII接口与STM32的ETH外设进行数据通信
    STM32使用SMI接口读/写LAN8720A的寄存器

    硬件原理图

    在这里插入图片描述
    LAN8720A由外部25MHz晶振提供时钟,LED2/NINTSEL引脚配置为下拉,故PHY(LAN8720A)提供50MHz时钟给RMII的NINT/REFCLKO(此时引脚NINT/REFCLKO仅作为时钟输出,中断功能不可用),RMII需要50MHz时钟

    LED1/REGOFF配置为下拉,使用LAN8720A内部稳压器

    上电后MODE引脚的电平会被锁存,用于设置PHY的工作模式,后续也可以写寄存器来修改HPY工作模式,也可以设置PHY为自适应模式,自动适配最佳模式,然后通过读寄存器来获取PHY的工作模式,无非就是速率与双工。

    上电复位后,RXER/PHYAD0,引脚的电平被锁存在一个内部寄存器中;
    可以使用硬件配置将设备的 SMI 地址配置为值 0 或 1,不接的话认为为0。

    CUBEMX配置

    基本的时钟、调试下载、Systick 不过多介绍

    PHY引脚配置

    在这里插入图片描述

    设置MAC地址、接收缓冲区的大小、数据接收模式

    在这里插入图片描述

    设置PHY的类型为LAN8742A

    (最好根据LAN8742A来设置自己使用的PHY,否则CUBEMX不会生成void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)GPIO、DMA、NVIC等初始化函数)
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
    这些生成的宏均在路径Core\Inc\stm32f4xx_hal_conf.h下,大家可以去看看

    生成的void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)在工程文件夹LWIP\Target\ethernetif.c下
    在生成的HAL_ETH_MspInit()函数中添加PHY上电复位代码(其实也可以不用添加,HAL库生成会通过写BCR寄存器RESET位来实现软件复位,然后读取RESET位来确认是否PHY已经复位完毕)

    HAL库生成的HAL_ETH_MspInit()如下:

    /**
      * @brief  Initializes the ETH MSP.
      * @param  ethHandle: ETH handle
      * @retval None
      */
    
    void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
    {
      GPIO_InitTypeDef GPIO_InitStruct = {0};
      if(ethHandle->Instance==ETH)
      {
      /* USER CODE BEGIN ETH_MspInit 0 */
    
      /* USER CODE END ETH_MspInit 0 */
        /* Enable Peripheral clock */
        __HAL_RCC_ETH_CLK_ENABLE();
    
        __HAL_RCC_GPIOC_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();
        __HAL_RCC_GPIOG_CLK_ENABLE();
        /**ETH GPIO Configuration
        PC1     ------> ETH_MDC
        PA1     ------> ETH_REF_CLK
        PA2     ------> ETH_MDIO
        PA7     ------> ETH_CRS_DV
        PC4     ------> ETH_RXD0
        PC5     ------> ETH_RXD1
        PB11     ------> ETH_TX_EN
        PG13     ------> ETH_TXD0
        PG14     ------> ETH_TXD1
        */
        GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
        GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        GPIO_InitStruct.Pin = GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
        GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
        HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
      /* USER CODE BEGIN ETH_MspInit 1 */
    
        /* 手动添加 PHY 需要的复位延时代码 */
        HAL_GPIO_WritePin(LAN8720A_RESET_GPIO_Port, LAN8720A_RESET_Pin, GPIO_PIN_RESET);
        HAL_Delay(60);
        HAL_GPIO_WritePin(LAN8720A_RESET_GPIO_Port, LAN8720A_RESET_Pin, GPIO_PIN_SET);
        HAL_Delay(60);
    
      /* USER CODE END ETH_MspInit 1 */
      }
    }
    
    void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
    {
      if(ethHandle->Instance==ETH)
      {
      /* USER CODE BEGIN ETH_MspDeInit 0 */
    
      /* USER CODE END ETH_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_ETH_CLK_DISABLE();
    
        /**ETH GPIO Configuration
        PC1     ------> ETH_MDC
        PA1     ------> ETH_REF_CLK
        PA2     ------> ETH_MDIO
        PA7     ------> ETH_CRS_DV
        PC4     ------> ETH_RXD0
        PC5     ------> ETH_RXD1
        PB11     ------> ETH_TX_EN
        PG13     ------> ETH_TXD0
        PG14     ------> ETH_TXD1
        */
        HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
    
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7);
    
        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);
    
        HAL_GPIO_DeInit(GPIOG, GPIO_PIN_13|GPIO_PIN_14);
    
      /* USER CODE BEGIN ETH_MspDeInit 1 */
    
      /* USER CODE END ETH_MspDeInit 1 */
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    设置PHY寄存器及相应掩码

    打开 Drivers\BSP\Components\lan8742\lan8742.h 对照LAN8720A的BCR寄存器修改相应位的掩码

    /**
      ******************************************************************************
      * @file    lan8742.h
      * @author  MCD Application Team
      * @brief   This file contains all the functions prototypes for the
      *          lan8742.c PHY driver.
      ******************************************************************************
      * @attention
      *
      * 

    © Copyright (c) 2017 STMicroelectronics. * All rights reserved.

    * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */
    /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef LAN8742_H #define LAN8742_H #ifdef __cplusplus extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include /** @addtogroup BSP * @{ */ /** @addtogroup Component * @{ */ /** @defgroup LAN8742 * @{ */ /* Exported constants --------------------------------------------------------*/ /** @defgroup LAN8742_Exported_Constants LAN8742 Exported Constants * @{ */ /** @defgroup LAN8742_Registers_Mapping LAN8742 Registers Mapping * @{ */ #define LAN8742_BCR ((uint16_t)0x0000U) #define LAN8742_BSR ((uint16_t)0x0001U) #define LAN8742_PHYI1R ((uint16_t)0x0002U) #define LAN8742_PHYI2R ((uint16_t)0x0003U) #define LAN8742_ANAR ((uint16_t)0x0004U) #define LAN8742_ANLPAR ((uint16_t)0x0005U) #define LAN8742_ANER ((uint16_t)0x0006U) #define LAN8742_ANNPTR ((uint16_t)0x0007U) #define LAN8742_ANNPRR ((uint16_t)0x0008U) #define LAN8742_MMDACR ((uint16_t)0x000DU) #define LAN8742_MMDAADR ((uint16_t)0x000EU) #define LAN8742_ENCTR ((uint16_t)0x0010U) #define LAN8742_MCSR ((uint16_t)0x0011U) #define LAN8742_SMR ((uint16_t)0x0012U) #define LAN8742_TPDCR ((uint16_t)0x0018U) #define LAN8742_TCSR ((uint16_t)0x0019U) #define LAN8742_SECR ((uint16_t)0x001AU) #define LAN8742_SCSIR ((uint16_t)0x001BU) #define LAN8742_CLR ((uint16_t)0x001CU) #define LAN8742_ISFR ((uint16_t)0x001DU) #define LAN8742_IMR ((uint16_t)0x001EU) #define LAN8742_PHYSCSR ((uint16_t)0x001FU) /** * @} */ /** @defgroup LAN8742_BCR_Bit_Definition LAN8742 BCR Bit Definition * @{ */ #define LAN8742_BCR_SOFT_RESET ((uint16_t)0x8000U) #define LAN8742_BCR_LOOPBACK ((uint16_t)0x4000U) #define LAN8742_BCR_SPEED_SELECT ((uint16_t)0x2000U) #define LAN8742_BCR_AUTONEGO_EN ((uint16_t)0x1000U) #define LAN8742_BCR_POWER_DOWN ((uint16_t)0x0800U) #define LAN8742_BCR_ISOLATE ((uint16_t)0x0400U) #define LAN8742_BCR_RESTART_AUTONEGO ((uint16_t)0x0200U) #define LAN8742_BCR_DUPLEX_MODE ((uint16_t)0x0100U) /** * @} */ /** @defgroup LAN8742_BSR_Bit_Definition LAN8742 BSR Bit Definition * @{ */ #define LAN8742_BSR_100BASE_T4 ((uint16_t)0x8000U) #define LAN8742_BSR_100BASE_TX_FD ((uint16_t)0x4000U) #define LAN8742_BSR_100BASE_TX_HD ((uint16_t)0x2000U) #define LAN8742_BSR_10BASE_T_FD ((uint16_t)0x1000U) #define LAN8742_BSR_10BASE_T_HD ((uint16_t)0x0800U) #define LAN8742_BSR_100BASE_T2_FD ((uint16_t)0x0400U) #define LAN8742_BSR_100BASE_T2_HD ((uint16_t)0x0200U) #define LAN8742_BSR_EXTENDED_STATUS ((uint16_t)0x0100U) #define LAN8742_BSR_AUTONEGO_CPLT ((uint16_t)0x0020U) #define LAN8742_BSR_REMOTE_FAULT ((uint16_t)0x0010U) #define LAN8742_BSR_AUTONEGO_ABILITY ((uint16_t)0x0008U) #define LAN8742_BSR_LINK_STATUS ((uint16_t)0x0004U) #define LAN8742_BSR_JABBER_DETECT ((uint16_t)0x0002U) #define LAN8742_BSR_EXTENDED_CAP ((uint16_t)0x0001U) /** * @} */ /** @defgroup LAN8742_PHYI1R_Bit_Definition LAN8742 PHYI1R Bit Definition * @{ */ #define LAN8742_PHYI1R_OUI_3_18 ((uint16_t)0xFFFFU) /** * @} */ /** @defgroup LAN8742_PHYI2R_Bit_Definition LAN8742 PHYI2R Bit Definition * @{ */ #define LAN8742_PHYI2R_OUI_19_24 ((uint16_t)0xFC00U) #define LAN8742_PHYI2R_MODEL_NBR ((uint16_t)0x03F0U) #define LAN8742_PHYI2R_REVISION_NBR ((uint16_t)0x000FU) /** * @} */ /** @defgroup LAN8742_ANAR_Bit_Definition LAN8742 ANAR Bit Definition * @{ */ #define LAN8742_ANAR_NEXT_PAGE ((uint16_t)0x8000U) #define LAN8742_ANAR_REMOTE_FAULT ((uint16_t)0x2000U) #define LAN8742_ANAR_PAUSE_OPERATION ((uint16_t)0x0C00U) #define LAN8742_ANAR_PO_NOPAUSE ((uint16_t)0x0000U) #define LAN8742_ANAR_PO_SYMMETRIC_PAUSE ((uint16_t)0x0400U) #define LAN8742_ANAR_PO_ASYMMETRIC_PAUSE ((uint16_t)0x0800U) #define LAN8742_ANAR_PO_ADVERTISE_SUPPORT ((uint16_t)0x0C00U) #define LAN8742_ANAR_100BASE_TX_FD ((uint16_t)0x0100U) #define LAN8742_ANAR_100BASE_TX ((uint16_t)0x0080U) #define LAN8742_ANAR_10BASE_T_FD ((uint16_t)0x0040U) #define LAN8742_ANAR_10BASE_T ((uint16_t)0x0020U) #define LAN8742_ANAR_SELECTOR_FIELD ((uint16_t)0x000FU) /** * @} */ /** @defgroup LAN8742_ANLPAR_Bit_Definition LAN8742 ANLPAR Bit Definition * @{ */ #define LAN8742_ANLPAR_NEXT_PAGE ((uint16_t)0x8000U) #define LAN8742_ANLPAR_REMOTE_FAULT ((uint16_t)0x2000U) #define LAN8742_ANLPAR_PAUSE_OPERATION ((uint16_t)0x0C00U) #define LAN8742_ANLPAR_PO_NOPAUSE ((uint16_t)0x0000U) #define LAN8742_ANLPAR_PO_SYMMETRIC_PAUSE ((uint16_t)0x0400U) #define LAN8742_ANLPAR_PO_ASYMMETRIC_PAUSE ((uint16_t)0x0800U) #define LAN8742_ANLPAR_PO_ADVERTISE_SUPPORT ((uint16_t)0x0C00U) #define LAN8742_ANLPAR_100BASE_TX_FD ((uint16_t)0x0100U) #define LAN8742_ANLPAR_100BASE_TX ((uint16_t)0x0080U) #define LAN8742_ANLPAR_10BASE_T_FD ((uint16_t)0x0040U) #define LAN8742_ANLPAR_10BASE_T ((uint16_t)0x0020U) #define LAN8742_ANLPAR_SELECTOR_FIELD ((uint16_t)0x000FU) /** * @} */ /** @defgroup LAN8742_ANER_Bit_Definition LAN8742 ANER Bit Definition * @{ */ #define LAN8742_ANER_RX_NP_LOCATION_ABLE ((uint16_t)0x0040U) #define LAN8742_ANER_RX_NP_STORAGE_LOCATION ((uint16_t)0x0020U) #define LAN8742_ANER_PARALLEL_DETECT_FAULT ((uint16_t)0x0010U) #define LAN8742_ANER_LP_NP_ABLE ((uint16_t)0x0008U) #define LAN8742_ANER_NP_ABLE ((uint16_t)0x0004U) #define LAN8742_ANER_PAGE_RECEIVED ((uint16_t)0x0002U) #define LAN8742_ANER_LP_AUTONEG_ABLE ((uint16_t)0x0001U) /** * @} */ /** @defgroup LAN8742_ANNPTR_Bit_Definition LAN8742 ANNPTR Bit Definition * @{ */ #define LAN8742_ANNPTR_NEXT_PAGE ((uint16_t)0x8000U) #define LAN8742_ANNPTR_MESSAGE_PAGE ((uint16_t)0x2000U) #define LAN8742_ANNPTR_ACK2 ((uint16_t)0x1000U) #define LAN8742_ANNPTR_TOGGLE ((uint16_t)0x0800U) #define LAN8742_ANNPTR_MESSAGGE_CODE ((uint16_t)0x07FFU) /** * @} */ /** @defgroup LAN8742_ANNPRR_Bit_Definition LAN8742 ANNPRR Bit Definition * @{ */ #define LAN8742_ANNPTR_NEXT_PAGE ((uint16_t)0x8000U) #define LAN8742_ANNPRR_ACK ((uint16_t)0x4000U) #define LAN8742_ANNPRR_MESSAGE_PAGE ((uint16_t)0x2000U) #define LAN8742_ANNPRR_ACK2 ((uint16_t)0x1000U) #define LAN8742_ANNPRR_TOGGLE ((uint16_t)0x0800U) #define LAN8742_ANNPRR_MESSAGGE_CODE ((uint16_t)0x07FFU) /** * @} */ /** @defgroup LAN8742_MMDACR_Bit_Definition LAN8742 MMDACR Bit Definition * @{ */ #define LAN8742_MMDACR_MMD_FUNCTION ((uint16_t)0xC000U) #define LAN8742_MMDACR_MMD_FUNCTION_ADDR ((uint16_t)0x0000U) #define LAN8742_MMDACR_MMD_FUNCTION_DATA ((uint16_t)0x4000U) #define LAN8742_MMDACR_MMD_DEV_ADDR ((uint16_t)0x001FU) /** * @} */ /** @defgroup LAN8742_ENCTR_Bit_Definition LAN8742 ENCTR Bit Definition * @{ */ #define LAN8742_ENCTR_TX_ENABLE ((uint16_t)0x8000U) #define LAN8742_ENCTR_TX_TIMER ((uint16_t)0x6000U) #define LAN8742_ENCTR_TX_TIMER_1S ((uint16_t)0x0000U) #define LAN8742_ENCTR_TX_TIMER_768MS ((uint16_t)0x2000U) #define LAN8742_ENCTR_TX_TIMER_512MS ((uint16_t)0x4000U) #define LAN8742_ENCTR_TX_TIMER_265MS ((uint16_t)0x6000U) #define LAN8742_ENCTR_RX_ENABLE ((uint16_t)0x1000U) #define LAN8742_ENCTR_RX_MAX_INTERVAL ((uint16_t)0x0C00U) #define LAN8742_ENCTR_RX_MAX_INTERVAL_64MS ((uint16_t)0x0000U) #define LAN8742_ENCTR_RX_MAX_INTERVAL_256MS ((uint16_t)0x0400U) #define LAN8742_ENCTR_RX_MAX_INTERVAL_512MS ((uint16_t)0x0800U) #define LAN8742_ENCTR_RX_MAX_INTERVAL_1S ((uint16_t)0x0C00U) #define LAN8742_ENCTR_EX_CROSS_OVER ((uint16_t)0x0002U) #define LAN8742_ENCTR_EX_MANUAL_CROSS_OVER ((uint16_t)0x0001U) /** * @} */ /** @defgroup LAN8742_MCSR_Bit_Definition LAN8742 MCSR Bit Definition * @{ */ #define LAN8742_MCSR_EDPWRDOWN ((uint16_t)0x2000U) #define LAN8742_MCSR_FARLOOPBACK ((uint16_t)0x0200U) #define LAN8742_MCSR_ALTINT ((uint16_t)0x0040U) #define LAN8742_MCSR_ENERGYON ((uint16_t)0x0002U) /** * @} */ /** @defgroup LAN8742_SMR_Bit_Definition LAN8742 SMR Bit Definition * @{ */ #define LAN8742_SMR_MODE ((uint16_t)0x00E0U) #define LAN8742_SMR_PHY_ADDR ((uint16_t)0x001FU) /** * @} */ /** @defgroup LAN8742_TPDCR_Bit_Definition LAN8742 TPDCR Bit Definition * @{ */ #define LAN8742_TPDCR_DELAY_IN ((uint16_t)0x8000U) #define LAN8742_TPDCR_LINE_BREAK_COUNTER ((uint16_t)0x7000U) #define LAN8742_TPDCR_PATTERN_HIGH ((uint16_t)0x0FC0U) #define LAN8742_TPDCR_PATTERN_LOW ((uint16_t)0x003FU) /** * @} */ /** @defgroup LAN8742_TCSR_Bit_Definition LAN8742 TCSR Bit Definition * @{ */ #define LAN8742_TCSR_TDR_ENABLE ((uint16_t)0x8000U) #define LAN8742_TCSR_TDR_AD_FILTER_ENABLE ((uint16_t)0x4000U) #define LAN8742_TCSR_TDR_CH_CABLE_TYPE ((uint16_t)0x0600U) #define LAN8742_TCSR_TDR_CH_CABLE_DEFAULT ((uint16_t)0x0000U) #define LAN8742_TCSR_TDR_CH_CABLE_SHORTED ((uint16_t)0x0200U) #define LAN8742_TCSR_TDR_CH_CABLE_OPEN ((uint16_t)0x0400U) #define LAN8742_TCSR_TDR_CH_CABLE_MATCH ((uint16_t)0x0600U) #define LAN8742_TCSR_TDR_CH_STATUS ((uint16_t)0x0100U) #define LAN8742_TCSR_TDR_CH_LENGTH ((uint16_t)0x00FFU) /** * @} */ /** @defgroup LAN8742_SCSIR_Bit_Definition LAN8742 SCSIR Bit Definition * @{ */ #define LAN8742_SCSIR_AUTO_MDIX_ENABLE ((uint16_t)0x8000U) #define LAN8742_SCSIR_CHANNEL_SELECT ((uint16_t)0x2000U) #define LAN8742_SCSIR_SQE_DISABLE ((uint16_t)0x0800U) #define LAN8742_SCSIR_XPOLALITY ((uint16_t)0x0010U) /** * @} */ /** @defgroup LAN8742_CLR_Bit_Definition LAN8742 CLR Bit Definition * @{ */ #define LAN8742_CLR_CABLE_LENGTH ((uint16_t)0xF000U) /** * @} */ /** @defgroup LAN8742_IMR_ISFR_Bit_Definition LAN8742 IMR ISFR Bit Definition * @{ */ #define LAN8742_INT_8 ((uint16_t)0x0100U) #define LAN8742_INT_7 ((uint16_t)0x0080U) #define LAN8742_INT_6 ((uint16_t)0x0040U) #define LAN8742_INT_5 ((uint16_t)0x0020U) #define LAN8742_INT_4 ((uint16_t)0x0010U) #define LAN8742_INT_3 ((uint16_t)0x0008U) #define LAN8742_INT_2 ((uint16_t)0x0004U) #define LAN8742_INT_1 ((uint16_t)0x0002U) /** * @} */ /** @defgroup LAN8742_PHYSCSR_Bit_Definition LAN8742 PHYSCSR Bit Definition * @{ */ #define LAN8742_PHYSCSR_AUTONEGO_DONE ((uint16_t)0x1000U) #define LAN8742_PHYSCSR_HCDSPEEDMASK ((uint16_t)0x001CU) #define LAN8742_PHYSCSR_10BT_HD ((uint16_t)0x0004U) #define LAN8742_PHYSCSR_10BT_FD ((uint16_t)0x0014U) #define LAN8742_PHYSCSR_100BTX_HD ((uint16_t)0x0008U) #define LAN8742_PHYSCSR_100BTX_FD ((uint16_t)0x0018U) /** * @} */ /** @defgroup LAN8742_Status LAN8742 Status * @{ */ #define LAN8742_STATUS_READ_ERROR ((int32_t)-5) #define LAN8742_STATUS_WRITE_ERROR ((int32_t)-4) #define LAN8742_STATUS_ADDRESS_ERROR ((int32_t)-3) #define LAN8742_STATUS_RESET_TIMEOUT ((int32_t)-2) #define LAN8742_STATUS_ERROR ((int32_t)-1) #define LAN8742_STATUS_OK ((int32_t) 0) #define LAN8742_STATUS_LINK_DOWN ((int32_t) 1) #define LAN8742_STATUS_100MBITS_FULLDUPLEX ((int32_t) 2) #define LAN8742_STATUS_100MBITS_HALFDUPLEX ((int32_t) 3) #define LAN8742_STATUS_10MBITS_FULLDUPLEX ((int32_t) 4) #define LAN8742_STATUS_10MBITS_HALFDUPLEX ((int32_t) 5) #define LAN8742_STATUS_AUTONEGO_NOTDONE ((int32_t) 6) /** * @} */ /** @defgroup LAN8742_IT_Flags LAN8742 IT Flags * @{ */ #define LAN8742_WOL_IT LAN8742_INT_8 #define LAN8742_ENERGYON_IT LAN8742_INT_7 #define LAN8742_AUTONEGO_COMPLETE_IT LAN8742_INT_6 #define LAN8742_REMOTE_FAULT_IT LAN8742_INT_5 #define LAN8742_LINK_DOWN_IT LAN8742_INT_4 #define LAN8742_AUTONEGO_LP_ACK_IT LAN8742_INT_3 #define LAN8742_PARALLEL_DETECTION_FAULT_IT LAN8742_INT_2 #define LAN8742_AUTONEGO_PAGE_RECEIVED_IT LAN8742_INT_1 /** * @} */ /** * @} */ /* Exported types ------------------------------------------------------------*/ /** @defgroup LAN8742_Exported_Types LAN8742 Exported Types * @{ */ typedef int32_t (*lan8742_Init_Func) (void); typedef int32_t (*lan8742_DeInit_Func) (void); typedef int32_t (*lan8742_ReadReg_Func) (uint32_t, uint32_t, uint32_t *); typedef int32_t (*lan8742_WriteReg_Func) (uint32_t, uint32_t, uint32_t); typedef int32_t (*lan8742_GetTick_Func) (void); typedef struct { lan8742_Init_Func Init; lan8742_DeInit_Func DeInit; lan8742_WriteReg_Func WriteReg; lan8742_ReadReg_Func ReadReg; lan8742_GetTick_Func GetTick; } lan8742_IOCtx_t; typedef struct { uint32_t DevAddr; uint32_t Is_Initialized; lan8742_IOCtx_t IO; void *pData; }lan8742_Object_t; /** * @} */ /* Exported macro ------------------------------------------------------------*/ /* Exported functions --------------------------------------------------------*/ /** @defgroup LAN8742_Exported_Functions LAN8742 Exported Functions * @{ */ int32_t LAN8742_RegisterBusIO(lan8742_Object_t *pObj, lan8742_IOCtx_t *ioctx); int32_t LAN8742_Init(lan8742_Object_t *pObj); int32_t LAN8742_DeInit(lan8742_Object_t *pObj); int32_t LAN8742_DisablePowerDownMode(lan8742_Object_t *pObj); int32_t LAN8742_EnablePowerDownMode(lan8742_Object_t *pObj); int32_t LAN8742_StartAutoNego(lan8742_Object_t *pObj); int32_t LAN8742_GetLinkState(lan8742_Object_t *pObj); int32_t LAN8742_SetLinkState(lan8742_Object_t *pObj, uint32_t LinkState); int32_t LAN8742_EnableLoopbackMode(lan8742_Object_t *pObj); int32_t LAN8742_DisableLoopbackMode(lan8742_Object_t *pObj); int32_t LAN8742_EnableIT(lan8742_Object_t *pObj, uint32_t Interrupt); int32_t LAN8742_DisableIT(lan8742_Object_t *pObj, uint32_t Interrupt); int32_t LAN8742_ClearIT(lan8742_Object_t *pObj, uint32_t Interrupt); int32_t LAN8742_GetITStatus(lan8742_Object_t *pObj, uint32_t Interrupt); /** * @} */ #ifdef __cplusplus } #endif #endif /* LAN8742_H */ /** * @} */ /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449

    在这里插入图片描述
    LAN8720A的BSR寄存器
    在这里插入图片描述
    在这里插入图片描述

    注意GPIO复用

    在这里插入图片描述

    LWIP配置

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    添加代码

    • 复位PHY引脚
      本次PI1连接PHY的复位引脚,低电平复位
      在生成的void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)在工程文件夹LWIP\Target\ethernetif.c下添加复位PHY代码,具体延时时间根据PHY来设置

    在这里插入图片描述

    • PHY配置
      置位LAN8720A的自动协商(自适应、自动适配,速率与双工)并等待配置完毕
      在Drivers\BSP\Components\lan8742\lan8742.c的函数LAN8742_Init()
      注意:每次配置最好做备份,因为每使用CUBEMX生成工程,用户代码都会被清除
      在这里插入图片描述
    • 主函数中添加MX_LWIP_Process();
    • Keil工程配置,勾上就对了(不勾上会生成 BKPT 0xAB,此时MCU会死机,具体原因未知)
      在这里插入图片描述

    下载代码,实现现象

    在这里插入图片描述

    工程文件下载链接
    【STM32】HAL库-以太网外设-LAN8720A-LWIP-无操作系统-PING

    说明

    初始化调用过程

    MX_LWIP_Init() -> netif_add() -> ethernetif_init() -> low_level_init() -> HAL_ETH_Init() -> LAN8742_Init() -> ethernet_link_check_state()Get link state

  • 相关阅读:
    【分享】“飞书自建“在集简云平台集成应用的常见问题与解决方案
    (附源码)计算机毕业设计SSM教务排课管理系统
    华为云云耀云服务器L实例评测|docker私有仓库部署手册
    Annaconda环境下ChromeDriver配置及爬虫编写
    基于JAVA医院病历管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    基于FPGA的数字时钟系统设计
    【React】组件实例三大属性state、props、refs
    二十一、SpringBoot + Jwt + Vue 权限管理系统 (2)
    Blender色彩管理的关键概念和使用方法
    warning C4819最简单解决办法
  • 原文地址:https://blog.csdn.net/qq_45607873/article/details/126843107