|
16 | 16 | * |
17 | 17 | * Description: M480 MAC driver source file |
18 | 18 | */ |
| 19 | +#include <stdbool.h> |
19 | 20 | #include "m480_eth.h" |
20 | 21 | #include "mbed_toolchain.h" |
21 | | -#define NU_TRACE |
| 22 | +//#define NU_TRACE |
22 | 23 | #include "numaker_eth_hal.h" |
23 | 24 |
|
24 | 25 | #define ETH_TRIGGER_RX() do{EMAC->RXST = 0;}while(0) |
@@ -46,6 +47,9 @@ void *nu_userData = NULL; |
46 | 47 |
|
47 | 48 | extern void ack_emac_rx_isr(void); |
48 | 49 |
|
| 50 | +static bool isPhyReset = false; |
| 51 | +static uint16_t phyLPAval = 0; |
| 52 | + |
49 | 53 | // PTP source clock is 84MHz (Real chip using PLL). Each tick is 11.90ns |
50 | 54 | // Assume we want to set each tick to 100ns. |
51 | 55 | // Increase register = (100 * 2^31) / (10^9) = 214.71 =~ 215 = 0xD7 |
@@ -118,6 +122,7 @@ static int reset_phy(void) |
118 | 122 | return(-1); |
119 | 123 | } else { |
120 | 124 | reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA); |
| 125 | + phyLPAval = reg; |
121 | 126 |
|
122 | 127 | if(reg & ADVERTISE_100FULL) { |
123 | 128 | NU_DEBUGF(("100 full\n")); |
@@ -269,14 +274,32 @@ void numaker_eth_init(uint8_t *mac_addr) |
269 | 274 | EMAC_CAMCTL_ABP_Msk; |
270 | 275 | EMAC->CAMEN = 1; // Enable CAM entry 0 |
271 | 276 | /* Limit the max receive frame length to 1514 + 4 */ |
272 | | - EMAC->MRFL = 1518; |
273 | | - |
| 277 | + EMAC->MRFL = NU_ETH_MAX_FLEN; |
| 278 | + |
274 | 279 | /* Set RX FIFO threshold as 8 words */ |
275 | 280 | EMAC->FIFOCTL = 0x00200100; |
276 | | - |
277 | | - /* Limit the max receive frame length to 1514 + 4 */ |
278 | | - EMAC->MRFL = NU_ETH_MAX_FLEN; |
279 | | - reset_phy(); |
| 281 | + |
| 282 | + if (isPhyReset != true) |
| 283 | + { |
| 284 | + if (!reset_phy()) |
| 285 | + { |
| 286 | + isPhyReset = true; |
| 287 | + } |
| 288 | + } else { |
| 289 | + if (phyLPAval & ADVERTISE_100FULL) { |
| 290 | + NU_DEBUGF(("100 full\n")); |
| 291 | + EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk); |
| 292 | + } else if (phyLPAval & ADVERTISE_100HALF) { |
| 293 | + NU_DEBUGF(("100 half\n")); |
| 294 | + EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_FUDUP_Msk) | EMAC_CTL_OPMODE_Msk; |
| 295 | + } else if (phyLPAval & ADVERTISE_10FULL) { |
| 296 | + NU_DEBUGF(("10 full\n")); |
| 297 | + EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_OPMODE_Msk) | EMAC_CTL_FUDUP_Msk; |
| 298 | + } else { |
| 299 | + NU_DEBUGF(("10 half\n")); |
| 300 | + EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk); |
| 301 | + } |
| 302 | + } |
280 | 303 |
|
281 | 304 | EMAC_ENABLE_RX(); |
282 | 305 | EMAC_ENABLE_TX(); |
@@ -329,12 +352,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf) |
329 | 352 | if (status & RXFD_RXGD) { |
330 | 353 | *buf = cur_rx_desc_ptr->buf; |
331 | 354 | *len = status & 0xFFFF; |
332 | | - if( *len > 1514 ) { |
| 355 | + // length of payload should be <= 1514 |
| 356 | + if ( *len > (NU_ETH_MAX_FLEN - 4) ) { |
333 | 357 | NU_DEBUGF(("%s... unexpected long packet length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf)); |
334 | | - |
335 | 358 | *len = 0; // Skip this unexpected long packet |
336 | 359 | } |
337 | | - if( *len == 1514 ) NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf)); |
| 360 | + if (*len == (NU_ETH_MAX_FLEN - 4)) NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf)); |
338 | 361 | } |
339 | 362 | return 0; |
340 | 363 | } |
|
0 commit comments