3232#include "extmod/modnetwork.h"
3333#include "mpu.h"
3434#include "eth.h"
35+ #include "eth_phy.h"
3536
3637#if defined(MICROPY_HW_ETH_MDC )
3738
4041#include "lwip/dhcp.h"
4142#include "netif/ethernet.h"
4243
43- // ETH PHY register definitions (for LAN8742 and LAN8720/LAN8710)
44- #undef PHY_BCR
45- #define PHY_BCR (0x0000)
46- #define PHY_BCR_SOFT_RESET (0x8000)
47- #define PHY_BCR_AUTONEG_EN (0x1000)
48- #define PHY_BCR_POWER_DOWN (0x0800U)
49-
50- #undef PHY_BSR
51- #define PHY_BSR (0x0001)
52- #define PHY_BSR_LINK_STATUS (0x0004)
53- #define PHY_BSR_AUTONEG_DONE (0x0020)
54-
55- #define PHY_SCSR (0x001f)
56- #define PHY_SCSR_SPEED_Pos (2)
57- #define PHY_SCSR_SPEED_Msk (7 << PHY_SCSR_SPEED_Pos)
58- #define PHY_SCSR_SPEED_10HALF (1 << PHY_SCSR_SPEED_Pos)
59- #define PHY_SCSR_SPEED_10FULL (5 << PHY_SCSR_SPEED_Pos)
60- #define PHY_SCSR_SPEED_100HALF (2 << PHY_SCSR_SPEED_Pos)
61- #define PHY_SCSR_SPEED_100FULL (6 << PHY_SCSR_SPEED_Pos)
62-
6344// ETH DMA RX and TX descriptor definitions
6445#if defined(STM32H5 )
6546#define RX_DESCR_3_OWN_Pos (31)
@@ -137,6 +118,7 @@ typedef struct _eth_t {
137118 struct netif netif ;
138119 struct dhcp dhcp_struct ;
139120 uint32_t phy_addr ;
121+ int16_t (* phy_get_link_status )(uint32_t phy_addr );
140122} eth_t ;
141123
142124static eth_dma_t eth_dma __attribute__((aligned (16384 )));
@@ -146,7 +128,7 @@ eth_t eth_instance;
146128static void eth_mac_deinit (eth_t * self );
147129static void eth_process_frame (eth_t * self , size_t len , const uint8_t * buf );
148130
149- static void eth_phy_write (uint32_t phy_addr , uint32_t reg , uint32_t val ) {
131+ void eth_phy_write (uint32_t phy_addr , uint32_t reg , uint32_t val ) {
150132 #if defined(STM32H5 ) || defined(STM32H7 )
151133 while (ETH -> MACMDIOAR & ETH_MACMDIOAR_MB ) {
152134 }
@@ -174,7 +156,7 @@ static void eth_phy_write(uint32_t phy_addr, uint32_t reg, uint32_t val) {
174156 #endif
175157}
176158
177- static uint32_t eth_phy_read (uint32_t phy_addr , uint32_t reg ) {
159+ uint32_t eth_phy_read (uint32_t phy_addr , uint32_t reg ) {
178160 #if defined(STM32H5 ) || defined(STM32H7 )
179161 while (ETH -> MACMDIOAR & ETH_MACMDIOAR_MB ) {
180162 }
@@ -202,10 +184,17 @@ static uint32_t eth_phy_read(uint32_t phy_addr, uint32_t reg) {
202184 #endif
203185}
204186
205- void eth_init (eth_t * self , int mac_idx , uint32_t phy_addr ) {
187+ int eth_init (eth_t * self , int mac_idx , uint32_t phy_addr , int phy_type ) {
206188 mp_hal_get_mac (mac_idx , & self -> netif .hwaddr [0 ]);
207189 self -> netif .hwaddr_len = 6 ;
208190 self -> phy_addr = phy_addr ;
191+ if (phy_type == ETH_PHY_DP83825 || phy_type == ETH_PHY_DP83848 ) {
192+ self -> phy_get_link_status = eth_phy_dp838xx_get_link_status ;
193+ } else if (phy_type == ETH_PHY_LAN8720 || phy_type == ETH_PHY_LAN8742 ) {
194+ self -> phy_get_link_status = eth_phy_lan87xx_get_link_status ;
195+ } else {
196+ return -1 ;
197+ }
209198
210199 // Configure GPIO
211200 mp_hal_pin_config_alt_static (MICROPY_HW_ETH_MDC , MP_HAL_PIN_MODE_ALT , MP_HAL_PIN_PULL_NONE , STATIC_AF_ETH_MDC );
@@ -230,6 +219,7 @@ void eth_init(eth_t *self, int mac_idx, uint32_t phy_addr) {
230219 #else
231220 __HAL_RCC_ETH_CLK_ENABLE ();
232221 #endif
222+ return 0 ;
233223}
234224
235225void eth_set_trace (eth_t * self , uint32_t value ) {
@@ -381,6 +371,14 @@ static int eth_mac_init(eth_t *self) {
381371 break ;
382372 case 1 :
383373 if (bsr & PHY_BSR_LINK_STATUS ) {
374+ // Announce all modes
375+ eth_phy_write (self -> phy_addr , PHY_ANAR ,
376+ PHY_ANAR_SPEED_10HALF |
377+ PHY_ANAR_SPEED_10FULL |
378+ PHY_ANAR_SPEED_100HALF |
379+ PHY_ANAR_SPEED_100FULL |
380+ PHY_ANAR_IEEE802_3 );
381+ // Start autonegotiate.
384382 eth_phy_write (self -> phy_addr , PHY_BCR , PHY_BCR_AUTONEG_EN );
385383 phy_state = 2 ;
386384 }
@@ -396,7 +394,7 @@ static int eth_mac_init(eth_t *self) {
396394 }
397395
398396 // Get register with link status
399- uint16_t phy_scsr = eth_phy_read (self -> phy_addr , PHY_SCSR );
397+ uint16_t phy_scsr = self -> phy_get_link_status (self -> phy_addr );
400398
401399 // Burst mode configuration
402400 #if defined(STM32H5 ) || defined(STM32H7 )
@@ -505,9 +503,9 @@ static int eth_mac_init(eth_t *self) {
505503
506504 // Set main MAC control register
507505 ETH -> MACCR =
508- ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_10FULL ? ETH_MACCR_DM
509- : ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_100HALF ? ETH_MACCR_FES
510- : ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_100FULL ? (ETH_MACCR_FES | ETH_MACCR_DM )
506+ phy_scsr == PHY_SPEED_10FULL ? ETH_MACCR_DM
507+ : phy_scsr == PHY_SPEED_100HALF ? ETH_MACCR_FES
508+ : phy_scsr == PHY_SPEED_100FULL ? (ETH_MACCR_FES | ETH_MACCR_DM )
511509 : 0
512510 ;
513511 mp_hal_delay_ms (2 );
0 commit comments