Skip to content

Commit 54b3417

Browse files
committed
Refine M487 EMAC bus reset
1 parent a993582 commit 54b3417

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

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

Lines changed: 33 additions & 8 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
@@ -116,6 +120,7 @@ static int reset_phy(void)
116120
return(-1);
117121
} else {
118122
reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA);
123+
phyLPAval = reg;
119124

120125
if(reg & ADVERTISE_100FULL) {
121126
NU_DEBUGF(("100 full\n"));
@@ -267,12 +272,32 @@ void numaker_eth_init(uint8_t *mac_addr)
267272
EMAC_CAMCTL_ABP_Msk;
268273
EMAC->CAMEN = 1; // Enable CAM entry 0
269274
/* Limit the max receive frame length to 1514 + 4 */
270-
EMAC->MRFL = 1518;
271-
275+
EMAC->MRFL = NU_ETH_MAX_FLEN;
276+
272277
/* Set RX FIFO threshold as 8 words */
273278
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+
}
276301

277302
EMAC_ENABLE_RX();
278303
EMAC_ENABLE_TX();
@@ -325,12 +350,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
325350
if (status & RXFD_RXGD) {
326351
*buf = cur_rx_desc_ptr->buf;
327352
*len = status & 0xFFFF;
328-
if( *len > 1514 ) {
353+
// length of payload should be <= 1514
354+
if ( *len > (NU_ETH_MAX_FLEN - 4) ) {
329355
NU_DEBUGF(("%s... unexpected long packet length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
330-
331356
*len = 0; // Skip this unexpected long packet
332357
}
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));
334359
}
335360
return 0;
336361
}

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)