Skip to content

Commit 16dacb7

Browse files
committed
Refine M487 EMAC bus reset
1 parent c937fda commit 16dacb7

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

features/netsocket/emac-drivers/TARGET_NUVOTON_EMAC/TARGET_M480/m480_eth.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
*
1717
* Description: M480 MAC driver source file
1818
*/
19+
#include <stdbool.h>
1920
#include "m480_eth.h"
2021
#include "mbed_toolchain.h"
21-
#define NU_TRACE
22+
//#define NU_TRACE
2223
#include "numaker_eth_hal.h"
2324

2425
#define ETH_TRIGGER_RX() do{EMAC->RXST = 0;}while(0)
@@ -46,6 +47,9 @@ void *nu_userData = NULL;
4647

4748
extern void ack_emac_rx_isr(void);
4849

50+
static bool isPhyReset = false;
51+
static uint16_t phyLPAval = 0;
52+
4953
// PTP source clock is 84MHz (Real chip using PLL). Each tick is 11.90ns
5054
// Assume we want to set each tick to 100ns.
5155
// Increase register = (100 * 2^31) / (10^9) = 214.71 =~ 215 = 0xD7
@@ -118,6 +122,7 @@ static int reset_phy(void)
118122
return(-1);
119123
} else {
120124
reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA);
125+
phyLPAval = reg;
121126

122127
if(reg & ADVERTISE_100FULL) {
123128
NU_DEBUGF(("100 full\n"));
@@ -269,14 +274,32 @@ void numaker_eth_init(uint8_t *mac_addr)
269274
EMAC_CAMCTL_ABP_Msk;
270275
EMAC->CAMEN = 1; // Enable CAM entry 0
271276
/* Limit the max receive frame length to 1514 + 4 */
272-
EMAC->MRFL = 1518;
273-
277+
EMAC->MRFL = NU_ETH_MAX_FLEN;
278+
274279
/* Set RX FIFO threshold as 8 words */
275280
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+
}
280303

281304
EMAC_ENABLE_RX();
282305
EMAC_ENABLE_TX();
@@ -329,12 +352,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
329352
if (status & RXFD_RXGD) {
330353
*buf = cur_rx_desc_ptr->buf;
331354
*len = status & 0xFFFF;
332-
if( *len > 1514 ) {
355+
// length of payload should be <= 1514
356+
if ( *len > (NU_ETH_MAX_FLEN - 4) ) {
333357
NU_DEBUGF(("%s... unexpected long packet length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
334-
335358
*len = 0; // Skip this unexpected long packet
336359
}
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));
338361
}
339362
return 0;
340363
}

features/netsocket/emac-drivers/TARGET_NUVOTON_EMAC/numaker_emac.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ void NUMAKER_EMAC::thread_function(void* pvParameters)
186186

187187
if (flags & FLAG_RX) {
188188
nu_enet->packet_rx();
189-
} else if (flags & FLAG_BUS_RESET) {
190-
NU_DEBUGF(("RX BUS error and reset bus\r\n"));
189+
}
190+
if (flags & FLAG_BUS_RESET) {
191+
NU_DEBUGF(("BUS error and reset bus\r\n"));
191192
nu_enet->bus_reset();
192193
}
193194
}

0 commit comments

Comments
 (0)