26
26
27
27
#include <string.h>
28
28
#include "py/mphal.h"
29
+ #include "py/runtime.h"
29
30
#include "py/mperrno.h"
30
31
#include "shared/netutils/netutils.h"
31
32
#include "pin_static_af.h"
32
33
#include "extmod/modnetwork.h"
33
34
#include "mpu.h"
34
35
#include "eth.h"
36
+ #include "phy.h"
35
37
36
38
#if defined(MICROPY_HW_ETH_MDC )
37
39
40
42
#include "lwip/dhcp.h"
41
43
#include "netif/ethernet.h"
42
44
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
-
63
45
// ETH DMA RX and TX descriptor definitions
64
46
#if defined(STM32H5 )
65
47
#define RX_DESCR_3_OWN_Pos (31)
@@ -137,6 +119,7 @@ typedef struct _eth_t {
137
119
struct netif netif ;
138
120
struct dhcp dhcp_struct ;
139
121
uint32_t phy_addr ;
122
+ int16_t (* phy_get_link_status )(uint32_t phy_addr );
140
123
} eth_t ;
141
124
142
125
static eth_dma_t eth_dma __attribute__((aligned (16384 )));
@@ -146,7 +129,7 @@ eth_t eth_instance;
146
129
STATIC void eth_mac_deinit (eth_t * self );
147
130
STATIC void eth_process_frame (eth_t * self , size_t len , const uint8_t * buf );
148
131
149
- STATIC void eth_phy_write (uint32_t phy_addr , uint32_t reg , uint32_t val ) {
132
+ void eth_phy_write (uint32_t phy_addr , uint32_t reg , uint32_t val ) {
150
133
#if defined(STM32H5 ) || defined(STM32H7 )
151
134
while (ETH -> MACMDIOAR & ETH_MACMDIOAR_MB ) {
152
135
}
@@ -172,7 +155,7 @@ STATIC void eth_phy_write(uint32_t phy_addr, uint32_t reg, uint32_t val) {
172
155
#endif
173
156
}
174
157
175
- STATIC uint32_t eth_phy_read (uint32_t phy_addr , uint32_t reg ) {
158
+ uint32_t eth_phy_read (uint32_t phy_addr , uint32_t reg ) {
176
159
#if defined(STM32H5 ) || defined(STM32H7 )
177
160
while (ETH -> MACMDIOAR & ETH_MACMDIOAR_MB ) {
178
161
}
@@ -198,10 +181,17 @@ STATIC uint32_t eth_phy_read(uint32_t phy_addr, uint32_t reg) {
198
181
#endif
199
182
}
200
183
201
- void eth_init (eth_t * self , int mac_idx , int phy_addr ) {
184
+ void eth_init (eth_t * self , int mac_idx , int phy_addr , int phy_type ) {
202
185
mp_hal_get_mac (mac_idx , & self -> netif .hwaddr [0 ]);
203
186
self -> netif .hwaddr_len = 6 ;
204
187
self -> phy_addr = phy_addr ;
188
+ if (phy_type == PHY_DP83848 ) {
189
+ self -> phy_get_link_status = phy_dp83848_get_link_status ;
190
+ } else if (phy_type == PHY_LAN8720 || phy_type == PHY_LAN8742 ) {
191
+ self -> phy_get_link_status = phy_lan87xx_get_link_status ;
192
+ } else {
193
+ mp_raise_ValueError (MP_ERROR_TEXT ("Invalid phy_type" ));
194
+ }
205
195
206
196
// Configure GPIO
207
197
mp_hal_pin_config_alt_static (MICROPY_HW_ETH_MDC , MP_HAL_PIN_MODE_ALT , MP_HAL_PIN_PULL_NONE , STATIC_AF_ETH_MDC );
@@ -377,6 +367,14 @@ STATIC int eth_mac_init(eth_t *self) {
377
367
break ;
378
368
case 1 :
379
369
if (bsr & PHY_BSR_LINK_STATUS ) {
370
+ // Announce all modes
371
+ eth_phy_write (self -> phy_addr , PHY_ANAR ,
372
+ PHY_ANAR_SPEED_10HALF |
373
+ PHY_ANAR_SPEED_10FULL |
374
+ PHY_ANAR_SPEED_100HALF |
375
+ PHY_ANAR_SPEED_100FULL |
376
+ PHY_ANAR_IEEE802_3 );
377
+ // Start autonegotiate.
380
378
eth_phy_write (self -> phy_addr , PHY_BCR , PHY_BCR_AUTONEG_EN );
381
379
phy_state = 2 ;
382
380
}
@@ -392,7 +390,7 @@ STATIC int eth_mac_init(eth_t *self) {
392
390
}
393
391
394
392
// Get register with link status
395
- uint16_t phy_scsr = eth_phy_read (self -> phy_addr , PHY_SCSR );
393
+ uint16_t phy_scsr = self -> phy_get_link_status (self -> phy_addr );
396
394
397
395
// Burst mode configuration
398
396
#if defined(STM32H5 ) || defined(STM32H7 )
@@ -501,9 +499,9 @@ STATIC int eth_mac_init(eth_t *self) {
501
499
502
500
// Set main MAC control register
503
501
ETH -> MACCR =
504
- ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_10FULL ? ETH_MACCR_DM
505
- : ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_100HALF ? ETH_MACCR_FES
506
- : ( phy_scsr & PHY_SCSR_SPEED_Msk ) == PHY_SCSR_SPEED_100FULL ? (ETH_MACCR_FES | ETH_MACCR_DM )
502
+ phy_scsr == PHY_SPEED_10FULL ? ETH_MACCR_DM
503
+ : phy_scsr == PHY_SPEED_100HALF ? ETH_MACCR_FES
504
+ : phy_scsr == PHY_SPEED_100FULL ? (ETH_MACCR_FES | ETH_MACCR_DM )
507
505
: 0
508
506
;
509
507
mp_hal_delay_ms (2 );
0 commit comments