这是一个百兆以太网PHY通用的一个初始化程序

/* Put the PHY in reset mode */

  if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_RESET)) != HAL_OK)

  {

    /* In case of write timeout */

    err = ETH_ERROR;

    

    /* Config MAC and DMA */

    ETH_MACDMAConfig(heth, err);

    

    /* Set the ETH peripheral state to READY */

    heth->State = HAL_ETH_STATE_READY;

    

    /* Return HAL_ERROR */

    return HAL_ERROR;

  }

  

  /* Delay to assure PHY reset */

  HAL_Delay(PHY_RESET_DELAY);

  

  if((heth->Init).AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)

  {

    /* Get tick */

    tickstart = HAL_GetTick();

    

    /* We wait for linked status */

    do

    {

      HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);

      

      /* Check for the Timeout */

      if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_LINKED_STATE)

      {

        /* In case of write timeout */

        err = ETH_ERROR;

      

        /* Config MAC and DMA */

        ETH_MACDMAConfig(heth, err);

        

        heth->State= HAL_ETH_STATE_READY;

  

        /* Process Unlocked */

        __HAL_UNLOCK(heth);

    

        return HAL_TIMEOUT;

      }

} while (((phyreg & PHY_LINKED_STATUS) != PHY_LINKED_STATUS));

    

    /* Enable Auto-Negotiation */

    if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_AUTONEGOTIATION)) != HAL_OK)

    {

      /* In case of write timeout */

      err = ETH_ERROR;

      

      /* Config MAC and DMA */

      ETH_MACDMAConfig(heth, err);

      

      /* Set the ETH peripheral state to READY */

      heth->State = HAL_ETH_STATE_READY;

      

      /* Return HAL_ERROR */

      return HAL_ERROR;   

    }

    

    /* Get tick */

    tickstart = HAL_GetTick();

    

    /* Wait until the auto-negotiation will be completed */

    do

    {

      HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);

      

      /* Check for the Timeout */

      if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_AUTONEGO_COMPLETED)

      {

        /* In case of write timeout */

        err = ETH_ERROR;

      

        /* Config MAC and DMA */

        ETH_MACDMAConfig(heth, err);
 

刚刚一些寄存器和状态位的宏定义

#define PHY_BCR                         ((uint16_t)0x00U)    /*!< Transceiver Basic Control Register   */

#define PHY_BSR                         ((uint16_t)0x01U)    /*!< Transceiver Basic Status Register    */

#define PHY_RESET                       ((uint16_t)0x8000U)  /*!< PHY Reset */

#define PHY_LOOPBACK                    ((uint16_t)0x4000U)  /*!< Select loop-back mode */

#define PHY_FULLDUPLEX_100M             ((uint16_t)0x2100U)  /*!< Set the full-duplex mode at 100 Mb/s */

#define PHY_HALFDUPLEX_100M             ((uint16_t)0x2000U)  /*!< Set the half-duplex mode at 100 Mb/s */

#define PHY_FULLDUPLEX_10M              ((uint16_t)0x0100U)  /*!< Set the full-duplex mode at 10 Mb/s  */

#define PHY_HALFDUPLEX_10M              ((uint16_t)0x0000U)  /*!< Set the half-duplex mode at 10 Mb/s  */

#define PHY_AUTONEGOTIATION             ((uint16_t)0x1000U)  /*!< Enable auto-negotiation function     */

#define PHY_RESTART_AUTONEGOTIATION     ((uint16_t)0x0200U)  /*!< Restart auto-negotiation function    */

#define PHY_POWERDOWN                   ((uint16_t)0x0800U)  /*!< Select the power down mode           */

#define PHY_ISOLATE                     ((uint16_t)0x0400U)  /*!< Isolate PHY from MII                 */

#define PHY_AUTONEGO_COMPLETE           ((uint16_t)0x0020U)  /*!< Auto-Negotiation process completed   */

#define PHY_LINKED_STATUS               ((uint16_t)0x0004U)  /*!< Valid link established               */

#define PHY_JABBER_DETECTION            ((uint16_t)0x0002U)  /*!< Jabber condition detected            */

  

/* Section 4: Extended PHY Registers */

#define PHY_SR                          ((uint16_t)0x1FU)    /*!< PHY special control/ status register Offset     */

#define ANLPAR                          ((uint16_t)0x05U)    /*!< PHY special control/ status register Offset     */

#define PHY_SPEED_STATUS                ((uint16_t)0x0100U)  /*!< PHY Speed mask                                  */

#define PHY_DUPLEX_STATUS               ((uint16_t)0x0010U)  /*!< PHY Duplex mask  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值