硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1846|回复: 1
收起左侧

[RL-TCPnet V7.X] 【原创】符合CMSIS-Driver驱动规范的以太网DM9000驱动实现,含STM32H7,STM32F407和STM32F429支持(2020-11-29)

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2020-11-29 10:05:44 | 显示全部楼层 |阅读模式
这个驱动的实现,耗费了我很多天的精力才做稳定,虽然有点舍不得,但还是将其分享给大家,此贴驱动是专门为V5,V6和V7开发板的双网口教程准备的。


STM32H7:
ETH_DM9000.c
ETH_DM9000.h
DM9000_BSP.c
DM9000_BSP.h

STM32F407:
ETH_DM9000.c
ETH_DM9000.h
DM9000_BSP.c
DM9000_BSP.h

STM32F429:
DM9000_BSP.c
DM9000_BSP.h
ETH_DM9000.c
ETH_DM9000.h


  1. /*
  2. *********************************************************************************************************
  3. *
  4. *        模块名称 : CMSIS-Driver
  5. *        文件名称 : ETH_DM9000.c
  6. *        版    本 : V1.0
  7. *        说    明 : CMSIS-Driver的DM9000底层驱动实现【安富莱原创】
  8. *
  9. *        修改记录 :
  10. *                版本号    日期        作者     说明
  11. *                V1.0    2020-11-21   Eric2013 正式发布
  12. *
  13. *        Copyright (C), 2020-2030, 安富莱电子 www.armfly.com
  14. *
  15. *********************************************************************************************************
  16. */
  17. #include <string.h>
  18. #include "cmsis_compiler.h"

  19. #include "Driver_ETH_MAC.h"
  20. #include "Driver_ETH_PHY.h"
  21. #include "ETH_DM9000.h"
  22. #include "DM9000_BSP.h"


  23. #define ARM_ETH_MAC_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,0) /* driver version */
  24. #define ARM_ETH_PHY_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,0) /* driver version */


  25. /* Ethernet MAC driver number (default) */
  26. #ifndef ETH_MAC_NUM
  27. #define ETH_MAC_NUM             1
  28. #endif

  29. /* Ethernet PHY driver number (default) */
  30. #ifndef ETH_PHY_NUM
  31. #define ETH_PHY_NUM             1
  32. #endif

  33. #ifdef __clang__
  34.   #define __rbit(v)             __builtin_arm_rbit(v)
  35. #endif

  36. /* Driver Version */
  37. static const ARM_DRIVER_VERSION MAC_DriverVersion = {
  38.   ARM_ETH_MAC_API_VERSION,
  39.   ARM_ETH_MAC_DRV_VERSION
  40. };

  41. /* Driver Capabilities */
  42. static const ARM_ETH_MAC_CAPABILITIES MAC_DriverCapabilities = {
  43.   0,                                /* checksum_offload_rx_ip4  */
  44.   0,                                /* checksum_offload_rx_ip6  */
  45.   0,                                /* checksum_offload_rx_udp  */
  46.   0,                                /* checksum_offload_rx_tcp  */
  47.   0,                                /* checksum_offload_rx_icmp */
  48.   0,                                /* checksum_offload_tx_ip4  */
  49.   0,                                /* checksum_offload_tx_ip6  */
  50.   0,                                /* checksum_offload_tx_udp  */
  51.   0,                                /* checksum_offload_tx_tcp  */
  52.   0,                                /* checksum_offload_tx_icmp */
  53.   0,                                /* media_interface          */
  54.   0,                                /* mac_address              */
  55.   1,                                /* event_rx_frame           */
  56.   0,                                /* event_tx_frame           */
  57.   0,                                /* event_wakeup             */
  58.   0,                                /* precision_timer          */
  59.   0                                 /* reserved                 */
  60. };

  61. /* Driver control structure */
  62. static ETH_CTRL ETH = { 0, 0, 0, 0, 0 };

  63. /* Local functions */
  64. static uint32_t reg_rd (uint8_t reg);
  65. static void     reg_wr (uint8_t reg, uint32_t val);


  66. #define   NET_BASE_ADDR                0x68400000UL
  67. #define   NET_REG_ADDR                (*((volatile uint16_t *) NET_BASE_ADDR))
  68. #define   NET_REG_DATA                (*((volatile uint16_t *) (NET_BASE_ADDR + 0x00080000)))

  69. /**
  70.   Read register value.

  71.   \param[in]   reg  Register to read
  72. */
  73. static uint32_t reg_rd (uint8_t reg) {
  74.         
  75.         NET_REG_ADDR = reg;
  76.         return (NET_REG_DATA);
  77. }

  78. /**
  79.   Write register value.

  80.   \param[in]   reg  Register to write
  81.   \param[in]   val  Value to write
  82. */
  83. static void reg_wr (uint8_t reg, uint32_t val) {
  84.         
  85.         NET_REG_ADDR = reg;
  86.         NET_REG_DATA = val;
  87.         
  88. }

  89. /* Ethernet Driver functions */

  90. /**
  91.   \fn          ARM_DRIVER_VERSION GetVersion (void)
  92.   \brief       Get driver version.
  93.   \return      \ref ARM_DRIVER_VERSION
  94. */
  95. static ARM_DRIVER_VERSION MAC_GetVersion (void) {
  96.   return MAC_DriverVersion;
  97. }

  98. /**
  99.   \fn          ARM_ETH_MAC_CAPABILITIES GetCapabilities (void)
  100.   \brief       Get driver capabilities.
  101.   \return      \ref ARM_ETH_MAC_CAPABILITIES
  102. */
  103. static ARM_ETH_MAC_CAPABILITIES MAC_GetCapabilities (void) {
  104.   return MAC_DriverCapabilities;
  105. }

  106. /**
  107.   \fn          int32_t Initialize (ARM_ETH_MAC_SignalEvent_t cb_event)
  108.   \brief       Initialize Ethernet MAC Device.
  109.   \param[in]   cb_event  Pointer to \ref ARM_ETH_MAC_SignalEvent
  110.   \return      \ref execution_status
  111. */
  112. static int32_t MAC_Initialize (ARM_ETH_MAC_SignalEvent_t cb_event) {
  113.   (void)cb_event;

  114.   if (ETH.Flags & ETH_INIT) { return ARM_DRIVER_OK; }

  115.   /* Determine number of CPU cycles within 1us */
  116.   if (SystemCoreClock < 1000000U) {
  117.     ETH.Us_Cyc = 1U;
  118.   } else {
  119.     ETH.Us_Cyc = (SystemCoreClock + (1000000U-1U)) / 1000000U;
  120.   }

  121.   ETH.Flags = ETH_INIT;
  122.   ETH.cb_event = cb_event;

  123.   etherdev_init();
  124.   
  125.   return ARM_DRIVER_OK;
  126. }

  127. /**
  128.   \fn          int32_t Uninitialize (void)
  129.   \brief       De-initialize Ethernet MAC Device.
  130.   \return      \ref execution_status
  131. */
  132. static int32_t MAC_Uninitialize (void) {

  133.   ETH.Flags = 0U;

  134.   return ARM_DRIVER_OK;
  135. }

  136. /**
  137.   \fn          int32_t PowerControl (ARM_POWER_STATE state)
  138.   \brief       Control Ethernet MAC Device Power.
  139.   \param[in]   state  Power state
  140.   \return      \ref execution_status
  141. */
  142. static int32_t MAC_PowerControl (ARM_POWER_STATE state) {
  143.   switch ((int32_t)state) {
  144.     case ARM_POWER_OFF:
  145.       ETH.Flags &= ETH_POWER;
  146.       break;

  147.     case ARM_POWER_LOW:
  148.       return ARM_DRIVER_ERROR_UNSUPPORTED;

  149.     case ARM_POWER_FULL:
  150.       if ((ETH.Flags & ETH_INIT) == 0) {
  151.         return ARM_DRIVER_ERROR;
  152.       }
  153.       if (ETH.Flags & ETH_POWER) {
  154.         return ARM_DRIVER_OK;
  155.       }

  156.       ETH.Flags |= ETH_POWER;
  157.       break;

  158.     default:
  159.       return ARM_DRIVER_ERROR_UNSUPPORTED;
  160.   }

  161.   return ARM_DRIVER_OK;
  162. }

  163. /**
  164.   \fn          int32_t GetMacAddress (ARM_ETH_MAC_ADDR *ptr_addr)
  165.   \brief       Get Ethernet MAC Address.
  166.   \param[in]   ptr_addr  Pointer to address
  167.   \return      \ref execution_status
  168. */
  169. static int32_t MAC_GetMacAddress (ARM_ETH_MAC_ADDR *ptr_addr) {
  170.   
  171.   if (ptr_addr == NULL) {
  172.     return ARM_DRIVER_ERROR_PARAMETER;
  173.   }

  174.   if ((ETH.Flags & ETH_POWER) == 0U) {
  175.     return ARM_DRIVER_ERROR;
  176.   }
  177.   
  178.   ptr_addr->b[0] = reg_rd (DM9000_REG_PAR + 0);
  179.   ptr_addr->b[1] = reg_rd (DM9000_REG_PAR + 1);
  180.   ptr_addr->b[2] = reg_rd (DM9000_REG_PAR + 2);
  181.   ptr_addr->b[3] = reg_rd (DM9000_REG_PAR + 3);
  182.   ptr_addr->b[4] = reg_rd (DM9000_REG_PAR + 4);
  183.   ptr_addr->b[5] = reg_rd (DM9000_REG_PAR + 5);

  184.   return ARM_DRIVER_OK;
  185. }

  186. /**
  187.   \fn          int32_t SetMacAddress (const ARM_ETH_MAC_ADDR *ptr_addr)
  188.   \brief       Set Ethernet MAC Address.
  189.   \param[in]   ptr_addr  Pointer to address
  190.   \return      \ref execution_status
  191. */
  192. static int32_t MAC_SetMacAddress (const ARM_ETH_MAC_ADDR *ptr_addr) {

  193.   if (ptr_addr == NULL) {
  194.     return ARM_DRIVER_ERROR_PARAMETER;
  195.   }

  196.   if ((ETH.Flags & ETH_POWER) == 0U) {
  197.     return ARM_DRIVER_ERROR;
  198.   }
  199.   
  200.   reg_wr (DM9000_REG_PAR + 0, ptr_addr->b[0]);
  201.   reg_wr (DM9000_REG_PAR + 1, ptr_addr->b[1]);
  202.   reg_wr (DM9000_REG_PAR + 2, ptr_addr->b[2]);
  203.   reg_wr (DM9000_REG_PAR + 3, ptr_addr->b[3]);
  204.   reg_wr (DM9000_REG_PAR + 4, ptr_addr->b[4]);
  205.   reg_wr (DM9000_REG_PAR + 5, ptr_addr->b[5]);

  206.   return ARM_DRIVER_OK;
  207. }

  208. /**
  209.   \fn          int32_t SetAddressFilter (const ARM_ETH_MAC_ADDR *ptr_addr,
  210.                                                uint32_t          num_addr)
  211.   \brief       Configure Address Filter.
  212.   \param[in]   ptr_addr  Pointer to addresses
  213.   \param[in]   num_addr  Number of addresses to configure
  214.   \return      \ref execution_status
  215. */
  216. static int32_t MAC_SetAddressFilter (const ARM_ETH_MAC_ADDR *ptr_addr, uint32_t num_addr) {
  217.   if ((ptr_addr == NULL) && (num_addr != 0U)) {
  218.     return ARM_DRIVER_ERROR_PARAMETER;
  219.   }

  220.   if ((ETH.Flags & ETH_POWER) == 0U) {
  221.     return ARM_DRIVER_ERROR;
  222.   }

  223.   return ARM_DRIVER_OK;
  224. }

  225. /**
  226.   \fn          int32_t SendFrame (const uint8_t *frame, uint32_t len, uint32_t flags)
  227.   \brief       Send Ethernet frame.
  228.   \param[in]   frame  Pointer to frame buffer with data to send
  229.   \param[in]   len    Frame buffer length in bytes
  230.   \param[in]   flags  Frame transmit flags (see ARM_ETH_MAC_TX_FRAME_...)
  231.   \return      \ref execution_status
  232. */
  233. static int32_t MAC_SendFrame (const uint8_t *frame, uint32_t len, uint32_t flags) {
  234.   if ((frame == NULL) || (len == 0U)) {
  235.     return ARM_DRIVER_ERROR_PARAMETER;
  236.   }

  237.   if ((ETH.Flags & ETH_POWER) == 0U) {
  238.     return ARM_DRIVER_ERROR;
  239.   }
  240.   
  241.   dm9k_send_packet((uint8_t *)frame, len);

  242.   return ARM_DRIVER_OK;
  243. }

  244. /**
  245.   \fn          int32_t ReadFrame (uint8_t *frame, uint32_t len)
  246.   \brief       Read data of received Ethernet frame.
  247.   \param[in]   frame  Pointer to frame buffer for data to read into
  248.   \param[in]   len    Frame buffer length in bytes
  249.   \return      number of data bytes read or execution status
  250.                  - value >= 0: number of data bytes read
  251.                  - value < 0: error occurred, value is execution status as defined with \ref execution_status
  252. */
  253. static int32_t MAC_ReadFrame (uint8_t *frame, uint32_t len) {
  254.         int32_t rval = ARM_DRIVER_OK;
  255.         
  256.         uint16_t *src2 = (uint16_t *)frame;
  257.         uint16_t temp;
  258.         uint32_t blkCnt;
  259.         
  260.         
  261.         blkCnt = len >> 3U;
  262.                
  263.         while (blkCnt > 0U)
  264.         {
  265.                 *src2++ = NET_REG_DATA;
  266.                 *src2++ = NET_REG_DATA;
  267.                 *src2++ = NET_REG_DATA;
  268.                 *src2++ = NET_REG_DATA;
  269.                 blkCnt--;
  270.         }

  271.         blkCnt = (len/2) % 0x4U;

  272.         while (blkCnt > 0U)
  273.         {
  274.                 *src2++ = NET_REG_DATA;
  275.                 blkCnt--;
  276.         }
  277.         
  278.         if(len%2)
  279.         {
  280.                 frame[len - 1] = (uint8_t)NET_REG_DATA;
  281.         }
  282.         
  283.         temp = NET_REG_DATA;
  284.         temp = NET_REG_DATA;        

  285.     (void)temp;
  286.         return (rval);
  287. }

  288. /**
  289.   \fn          uint32_t GetRxFrameSize (void)
  290.   \brief       Get size of received Ethernet frame.
  291.   \return      number of bytes in received frame
  292. */
  293. static uint32_t MAC_GetRxFrameSize (void) {
  294.     uint32_t len = 0;

  295.     if ((ETH.Flags & ETH_POWER) == 0U) {
  296.         return (0U);
  297.     }

  298.     len = GetSize();

  299.     return (len);
  300. }

  301. /**
  302.   \fn          int32_t GetRxFrameTime (ARM_ETH_MAC_TIME *time)
  303.   \brief       Get time of received Ethernet frame.
  304.   \param[in]   time  Pointer to time structure for data to read into
  305.   \return      \ref execution_status
  306. */
  307. static int32_t MAC_GetRxFrameTime (ARM_ETH_MAC_TIME *time) {
  308.   (void)time;
  309.   return ARM_DRIVER_ERROR_UNSUPPORTED;
  310. }

  311. /**
  312.   \fn          int32_t GetTxFrameTime (ARM_ETH_MAC_TIME *time)
  313.   \brief       Get time of transmitted Ethernet frame.
  314.   \param[in]   time  Pointer to time structure for data to read into
  315.   \return      \ref execution_status
  316. */
  317. static int32_t MAC_GetTxFrameTime (ARM_ETH_MAC_TIME *time) {
  318.   (void)time;
  319.   return ARM_DRIVER_ERROR_UNSUPPORTED;
  320. }

  321. /**
  322.   \fn          int32_t ControlTimer (uint32_t control, ARM_ETH_MAC_TIME *time)
  323.   \brief       Control Precision Timer.
  324.   \param[in]   control  Operation
  325.   \param[in]   time     Pointer to time structure
  326.   \return      \ref execution_status
  327. */
  328. static int32_t MAC_ControlTimer (uint32_t control, ARM_ETH_MAC_TIME *time) {
  329.   (void)control;
  330.   (void)time;
  331.   return ARM_DRIVER_ERROR_UNSUPPORTED;
  332. }

  333. /**
  334.   \fn          int32_t Control (uint32_t control, uint32_t arg)
  335.   \brief       Control Ethernet Interface.
  336.   \param[in]   control  Operation
  337.   \param[in]   arg      Argument of operation (optional)
  338.   \return      \ref execution_status
  339. */
  340. static int32_t MAC_Control (uint32_t control, uint32_t arg) {

  341.   if ((ETH.Flags & ETH_POWER) == 0U) {
  342.     return ARM_DRIVER_ERROR;
  343.   }

  344.   switch (control) {
  345.     case ARM_ETH_MAC_CONFIGURE:
  346.       break;

  347.     case ARM_ETH_MAC_CONTROL_TX:
  348.       break;

  349.     case ARM_ETH_MAC_CONTROL_RX:
  350.       break;

  351.     case ARM_ETH_MAC_FLUSH:
  352.       break;

  353.     default:
  354.       return ARM_DRIVER_ERROR_UNSUPPORTED;
  355.   }
  356.   return ARM_DRIVER_OK;
  357. }


  358. /**
  359.   \fn          int32_t PHY_Read (uint8_t phy_addr, uint8_t reg_addr, uint16_t *data)
  360.   \brief       Read Ethernet PHY Register through Management Interface.
  361.   \param[in]   phy_addr  5-bit device address
  362.   \param[in]   reg_addr  5-bit register address
  363.   \param[out]  data      Pointer where the result is written to
  364.   \return      \ref execution_status
  365. */
  366. static int32_t PHY_Read (uint8_t phy_addr, uint8_t reg_addr, uint16_t *data) {
  367.     int32_t  status = ARM_DRIVER_OK;

  368.     *data = dm9k_phy_read(reg_addr);
  369.     return (status);
  370. }

  371. /**
  372.   \fn          int32_t PHY_Write (uint8_t phy_addr, uint8_t reg_addr, uint16_t data)
  373.   \brief       Write Ethernet PHY Register through Management Interface.
  374.   \param[in]   phy_addr  5-bit device address
  375.   \param[in]   reg_addr  5-bit register address
  376.   \param[in]   data      16-bit data to write
  377.   \return      \ref execution_status
  378. */
  379. static int32_t PHY_Write (uint8_t phy_addr, uint8_t reg_addr, uint16_t data) {
  380.   int32_t  status  = ARM_DRIVER_OK;

  381.         dm9k_phy_write(reg_addr, data);
  382.   return (status);
  383. }

  384. extern __weak void dm9k_re_packet(void);
  385. void dm9k_re_packet(void)
  386. {
  387.         ETH.cb_event(ARM_ETH_MAC_EVENT_RX_FRAME);
  388. }

  389. /* MAC Driver Control Block */
  390. extern
  391. ARM_DRIVER_ETH_MAC ARM_Driver_ETH_MAC_(ETH_MAC_NUM);
  392. ARM_DRIVER_ETH_MAC ARM_Driver_ETH_MAC_(ETH_MAC_NUM) = {
  393.   MAC_GetVersion,
  394.   MAC_GetCapabilities,
  395.   MAC_Initialize,
  396.   MAC_Uninitialize,
  397.   MAC_PowerControl,
  398.   MAC_GetMacAddress,
  399.   MAC_SetMacAddress,
  400.   MAC_SetAddressFilter,
  401.   MAC_SendFrame,
  402.   MAC_ReadFrame,
  403.   MAC_GetRxFrameSize,
  404.   MAC_GetRxFrameTime,
  405.   MAC_GetTxFrameTime,
  406.   MAC_ControlTimer,
  407.   MAC_Control,
  408.   PHY_Read,
  409.   PHY_Write
  410. };


  411. /* Driver Version */
  412. static const ARM_DRIVER_VERSION PHY_DriverVersion = {
  413.   ARM_ETH_PHY_API_VERSION,
  414.   ARM_ETH_PHY_DRV_VERSION
  415. };

  416. /**
  417.   \fn          ARM_DRV_VERSION GetVersion (void)
  418.   \brief       Get driver version.
  419.   \return      \ref ARM_DRV_VERSION
  420. */
  421. static ARM_DRIVER_VERSION PHY_GetVersion (void) {
  422.   return PHY_DriverVersion;
  423. }

  424. /**
  425.   \fn          int32_t Initialize (ARM_ETH_PHY_Read_t  fn_read,
  426.                                    ARM_ETH_PHY_Write_t fn_write)
  427.   \brief       Initialize Ethernet PHY Device.
  428.   \param[in]   fn_read   Pointer to \ref ARM_ETH_MAC_PHY_Read
  429.   \param[in]   fn_write  Pointer to \ref ARM_ETH_MAC_PHY_Write
  430.   \return      \ref execution_status
  431. */
  432. static int32_t PHY_Initialize (ARM_ETH_PHY_Read_t fn_read, ARM_ETH_PHY_Write_t fn_write) {
  433.   /* PHY_Read and PHY_Write will be re-used, no need to register them again */
  434.   (void)fn_read;
  435.   (void)fn_write;
  436.   return ARM_DRIVER_OK;
  437. }

  438. /**
  439.   \fn          int32_t Uninitialize (void)
  440.   \brief       De-initialize Ethernet PHY Device.
  441.   \return      \ref execution_status
  442. */
  443. static int32_t PHY_Uninitialize (void) {
  444.   return ARM_DRIVER_OK;
  445. }

  446. /**
  447.   \fn          int32_t PowerControl (ARM_POWER_STATE state)
  448.   \brief       Control Ethernet PHY Device Power.
  449.   \param[in]   state  Power state
  450.   \return      \ref execution_status
  451. */
  452. static int32_t PHY_PowerControl (ARM_POWER_STATE state) {
  453.   return ARM_DRIVER_OK;
  454. }

  455. /**
  456.   \fn          int32_t SetInterface (uint32_t interface)
  457.   \brief       Set Ethernet Media Interface.
  458.   \param[in]   interface  Media Interface type
  459.   \return      \ref execution_status
  460. */
  461. static int32_t PHY_SetInterface (uint32_t interface) {
  462.   /* Not used in this driver */
  463.   (void)interface;
  464.   return ARM_DRIVER_OK;
  465. }

  466. /**
  467.   \fn          int32_t SetMode (uint32_t mode)
  468.   \brief       Set Ethernet PHY Device Operation mode.
  469.   \param[in]   mode  Operation Mode
  470.   \return      \ref execution_status
  471. */
  472. #include "PHY_DM916x.h"
  473. static int32_t PHY_SetMode (uint32_t mode) {
  474.   uint16_t val = 0;

  475.   if ((ETH.Flags & ETH_POWER) == 0U) { return ARM_DRIVER_ERROR; }

  476.   val &= ~(BMCR_SPEED_SELECT | BMCR_DUPLEX_MODE |
  477.            BMCR_ANEG_EN    | BMCR_LOOPBACK);

  478.   switch (mode & ARM_ETH_PHY_SPEED_Msk) {
  479.     case ARM_ETH_PHY_SPEED_10M:
  480.       break;
  481.     case ARM_ETH_PHY_SPEED_100M:
  482.       val |= BMCR_SPEED_SELECT;
  483.       break;
  484.     default:
  485.       return ARM_DRIVER_ERROR_UNSUPPORTED;
  486.   }

  487.   switch (mode & ARM_ETH_PHY_DUPLEX_Msk) {
  488.     case ARM_ETH_PHY_DUPLEX_HALF:
  489.       break;
  490.     case ARM_ETH_PHY_DUPLEX_FULL:
  491.       val |= BMCR_DUPLEX_MODE;
  492.       break;
  493.   }

  494.   if (mode & ARM_ETH_PHY_AUTO_NEGOTIATE) {
  495.     val |= BMCR_ANEG_EN;
  496.   }

  497.   if (mode & ARM_ETH_PHY_LOOPBACK) {
  498.     val |= BMCR_LOOPBACK;
  499.   }

  500.   if (mode & ARM_ETH_PHY_ISOLATE) {
  501.     return ARM_DRIVER_ERROR_UNSUPPORTED;
  502.   }
  503.   
  504.   /* Apply configured mode */
  505.   return PHY_Write (DM9000_PHY, REG_BMCR, val);
  506. }

  507. /**
  508.   \fn          ARM_ETH_LINK_STATE GetLinkState (void)
  509.   \brief       Get Ethernet PHY Device Link state.
  510.   \return      current link status \ref ARM_ETH_LINK_STATE
  511. */
  512. static ARM_ETH_LINK_STATE PHY_GetLinkState (void) {
  513.   ARM_ETH_LINK_STATE state;
  514.   uint16_t           val = 0U;

  515.   if (ETH.Flags & PHY_POWER) {
  516.         PHY_Read(DM9000_PHY, REG_BMSR, &val);
  517.   }
  518.   state = (val & BMSR_LINK_STAT) ? ARM_ETH_LINK_UP : ARM_ETH_LINK_DOWN;

  519.   return (state);
  520. }

  521. /**
  522.   \fn          ARM_ETH_LINK_INFO GetLinkInfo (void)
  523.   \brief       Get Ethernet PHY Device Link information.
  524.   \return      current link parameters \ref ARM_ETH_LINK_INFO
  525. */
  526. static ARM_ETH_LINK_INFO PHY_GetLinkInfo (void) {

  527.   ARM_ETH_LINK_INFO info;
  528.   uint16_t          val = 0U;

  529.   if (ETH.Flags & PHY_POWER) {
  530.     /* Get operation mode indication from PHY DSCSR register */
  531.     PHY_Read(DM9000_PHY, REG_DSCSR, &val);
  532.   }

  533.   /* Link must be up to get valid state */
  534.   info.speed  = ((val & DSCSR_100M_FD)|(val & DSCSR_100M_HD)) ? ARM_ETH_SPEED_100M  : ARM_ETH_SPEED_10M;
  535.   info.duplex = ((val & DSCSR_100M_FD)|(val & DSCSR_10M_FD)) ? ARM_ETH_DUPLEX_FULL : ARM_ETH_DUPLEX_HALF;


  536.   return (info);
  537. }

  538. /* PHY Driver Control Block */
  539. extern
  540. ARM_DRIVER_ETH_PHY ARM_Driver_ETH_PHY_(ETH_PHY_NUM);
  541. ARM_DRIVER_ETH_PHY ARM_Driver_ETH_PHY_(ETH_PHY_NUM) = {
  542.   PHY_GetVersion,
  543.   PHY_Initialize,
  544.   PHY_Uninitialize,
  545.   PHY_PowerControl,
  546.   PHY_SetInterface,
  547.   PHY_SetMode,
  548.   PHY_GetLinkState,
  549.   PHY_GetLinkInfo
  550. };
复制代码




回复

使用道具 举报

0

主题

13

回帖

13

积分

新手上路

积分
13
发表于 2020-11-29 16:30:17 | 显示全部楼层
感谢硬汉大佬分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-28 01:31 , Processed in 0.178109 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表