|
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
|
@@ -116,6 +120,7 @@ static int reset_phy(void)
|
116 | 120 | return(-1);
|
117 | 121 | } else {
|
118 | 122 | reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA);
|
| 123 | + phyLPAval = reg; |
119 | 124 |
|
120 | 125 | if(reg & ADVERTISE_100FULL) {
|
121 | 126 | NU_DEBUGF(("100 full\n"));
|
@@ -267,12 +272,32 @@ void numaker_eth_init(uint8_t *mac_addr)
|
267 | 272 | EMAC_CAMCTL_ABP_Msk;
|
268 | 273 | EMAC->CAMEN = 1; // Enable CAM entry 0
|
269 | 274 | /* Limit the max receive frame length to 1514 + 4 */
|
270 |
| - EMAC->MRFL = 1518; |
271 |
| - |
| 275 | + EMAC->MRFL = NU_ETH_MAX_FLEN; |
| 276 | + |
272 | 277 | /* Set RX FIFO threshold as 8 words */
|
273 | 278 | EMAC->FIFOCTL = 0x00200100;
|
274 |
| - |
275 |
| - reset_phy(); |
| 279 | + |
| 280 | + if (isPhyReset != true) |
| 281 | + { |
| 282 | + if (!reset_phy()) |
| 283 | + { |
| 284 | + isPhyReset = true; |
| 285 | + } |
| 286 | + } else { |
| 287 | + if (phyLPAval & ADVERTISE_100FULL) { |
| 288 | + NU_DEBUGF(("100 full\n")); |
| 289 | + EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk); |
| 290 | + } else if (phyLPAval & ADVERTISE_100HALF) { |
| 291 | + NU_DEBUGF(("100 half\n")); |
| 292 | + EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_FUDUP_Msk) | EMAC_CTL_OPMODE_Msk; |
| 293 | + } else if (phyLPAval & ADVERTISE_10FULL) { |
| 294 | + NU_DEBUGF(("10 full\n")); |
| 295 | + EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_OPMODE_Msk) | EMAC_CTL_FUDUP_Msk; |
| 296 | + } else { |
| 297 | + NU_DEBUGF(("10 half\n")); |
| 298 | + EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk); |
| 299 | + } |
| 300 | + } |
276 | 301 |
|
277 | 302 | EMAC_ENABLE_RX();
|
278 | 303 | EMAC_ENABLE_TX();
|
@@ -325,12 +350,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
|
325 | 350 | if (status & RXFD_RXGD) {
|
326 | 351 | *buf = cur_rx_desc_ptr->buf;
|
327 | 352 | *len = status & 0xFFFF;
|
328 |
| - if( *len > 1514 ) { |
| 353 | + // length of payload should be <= 1514 |
| 354 | + if ( *len > (NU_ETH_MAX_FLEN - 4) ) { |
329 | 355 | NU_DEBUGF(("%s... unexpected long packet length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
|
330 |
| - |
331 | 356 | *len = 0; // Skip this unexpected long packet
|
332 | 357 | }
|
333 |
| - if( *len == 1514 ) NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf)); |
| 358 | + if (*len == (NU_ETH_MAX_FLEN - 4)) NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf)); |
334 | 359 | }
|
335 | 360 | return 0;
|
336 | 361 | }
|
|
0 commit comments