18
18
*/
19
19
#include "m480_eth.h"
20
20
#include "mbed_toolchain.h"
21
+ #define NU_TRACE
21
22
#include "numaker_eth_hal.h"
22
23
23
24
#define ETH_TRIGGER_RX () do{EMAC->RXST = 0;}while(0)
24
25
#define ETH_TRIGGER_TX () do{EMAC->TXST = 0;}while(0)
25
26
#define ETH_ENABLE_TX () do{EMAC->CTL |= EMAC_CTL_TXON;}while(0)
26
- #define ETH_ENABLE_RX () do{EMAC->CTL |= EMAC_CTL_RXON ;}while(0)
27
+ #define ETH_ENABLE_RX () do{EMAC->CTL |= EMAC_CTL_RXON_Msk ;}while(0)
27
28
#define ETH_DISABLE_TX () do{EMAC->CTL &= ~EMAC_CTL_TXON;}while(0)
28
- #define ETH_DISABLE_RX () do{EMAC->CTL &= ~EMAC_CTL_RXON ;}while(0)
29
+ #define ETH_DISABLE_RX () do{EMAC->CTL &= ~EMAC_CTL_RXON_Msk ;}while(0)
29
30
31
+ #define EMAC_ENABLE_INT (emac , u32eIntSel ) ((emac)->INTEN |= (u32eIntSel))
32
+ #define EMAC_DISABLE_INT (emac , u32eIntSel ) ((emac)->INTEN &= ~ (u32eIntSel))
30
33
31
34
MBED_ALIGN (4 ) struct eth_descriptor rx_desc [RX_DESCRIPTOR_NUM ];
32
35
MBED_ALIGN (4 ) struct eth_descriptor tx_desc [TX_DESCRIPTOR_NUM ];
33
36
34
37
struct eth_descriptor volatile * cur_tx_desc_ptr , * cur_rx_desc_ptr , * fin_tx_desc_ptr ;
35
38
39
+ __attribute__ ((section ("EMAC_RAM" )))
36
40
MBED_ALIGN (4 ) uint8_t rx_buf [RX_DESCRIPTOR_NUM ][PACKET_BUFFER_SIZE ];
41
+ __attribute__ ((section ("EMAC_RAM" )))
37
42
MBED_ALIGN (4 ) uint8_t tx_buf [TX_DESCRIPTOR_NUM ][PACKET_BUFFER_SIZE ];
38
43
39
44
eth_callback_t nu_eth_txrx_cb = NULL ;
@@ -164,7 +169,7 @@ static void init_rx_desc(void)
164
169
rx_desc [i ].status1 = OWNERSHIP_EMAC ;
165
170
rx_desc [i ].buf = & rx_buf [i ][0 ];
166
171
rx_desc [i ].status2 = 0 ;
167
- rx_desc [i ].next = & rx_desc [(i + 1 ) % TX_DESCRIPTOR_NUM ];
172
+ rx_desc [i ].next = & rx_desc [(i + 1 ) % RX_DESCRIPTOR_NUM ];
168
173
}
169
174
EMAC -> RXDSA = (unsigned int )& rx_desc [0 ];
170
175
return ;
@@ -263,6 +268,11 @@ void numaker_eth_init(uint8_t *mac_addr)
263
268
EMAC_CAMCTL_AMP_Msk |
264
269
EMAC_CAMCTL_ABP_Msk ;
265
270
EMAC -> CAMEN = 1 ; // Enable CAM entry 0
271
+ /* Limit the max receive frame length to 1514 + 4 */
272
+ EMAC -> MRFL = 1518 ;
273
+
274
+ /* Set RX FIFO threshold as 8 words */
275
+ EMAC -> FIFOCTL = 0x00200100 ;
266
276
267
277
/* Limit the max receive frame length to 1514 + 4 */
268
278
EMAC -> MRFL = NU_ETH_MAX_FLEN ;
@@ -285,20 +295,22 @@ unsigned int m_status;
285
295
286
296
void EMAC_RX_IRQHandler (void )
287
297
{
288
- // NU_DEBUGF(("%s ... nu_eth_txrx_cb=0x%x\r\n", __FUNCTION__, nu_eth_txrx_cb));
289
298
m_status = EMAC -> INTSTS & 0xFFFF ;
290
299
EMAC -> INTSTS = m_status ;
291
300
if (m_status & EMAC_INTSTS_RXBEIF_Msk ) {
292
301
// Shouldn't goes here, unless descriptor corrupted
293
- NU_DEBUGF (("RX descriptor corrupted \r\n" ));
294
- //return;
302
+ mbed_error_printf ("### RX Bus error [0x%x]\r\n" , m_status );
303
+ if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('B' , nu_userData );
304
+ return ;
295
305
}
306
+ EMAC_DISABLE_INT (EMAC , (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk ));
296
307
if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('R' , nu_userData );
297
308
}
298
309
299
310
300
311
void numaker_eth_trigger_rx (void )
301
312
{
313
+ EMAC_ENABLE_INT (EMAC , (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk ));
302
314
ETH_TRIGGER_RX ();
303
315
}
304
316
@@ -317,6 +329,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
317
329
if (status & RXFD_RXGD ) {
318
330
* buf = cur_rx_desc_ptr -> buf ;
319
331
* len = status & 0xFFFF ;
332
+ if ( * len > 1514 ) {
333
+ NU_DEBUGF (("%s... unexpected long packet length=%d, buf=0x%x\r\n" , __FUNCTION__ , * len , * buf ));
334
+
335
+ * len = 0 ; // Skip this unexpected long packet
336
+ }
337
+ if ( * len == 1514 ) NU_DEBUGF (("%s... length=%d, buf=0x%x\r\n" , __FUNCTION__ , * len , * buf ));
320
338
}
321
339
return 0 ;
322
340
}
@@ -335,6 +353,8 @@ void EMAC_TX_IRQHandler(void)
335
353
EMAC -> INTSTS = status ;
336
354
if (status & EMAC_INTSTS_TXBEIF_Msk ) {
337
355
// Shouldn't goes here, unless descriptor corrupted
356
+ mbed_error_printf ("### TX Bus error [0x%x]\r\n" , status );
357
+ if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('B' , nu_userData );
338
358
return ;
339
359
}
340
360
0 commit comments