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 ;
@@ -162,7 +167,7 @@ static void init_rx_desc(void)
162
167
rx_desc [i ].status1 = OWNERSHIP_EMAC ;
163
168
rx_desc [i ].buf = & rx_buf [i ][0 ];
164
169
rx_desc [i ].status2 = 0 ;
165
- rx_desc [i ].next = & rx_desc [(i + 1 ) % TX_DESCRIPTOR_NUM ];
170
+ rx_desc [i ].next = & rx_desc [(i + 1 ) % RX_DESCRIPTOR_NUM ];
166
171
}
167
172
EMAC -> RXDSA = (unsigned int )& rx_desc [0 ];
168
173
return ;
@@ -261,6 +266,11 @@ void numaker_eth_init(uint8_t *mac_addr)
261
266
EMAC_CAMCTL_AMP_Msk |
262
267
EMAC_CAMCTL_ABP_Msk ;
263
268
EMAC -> CAMEN = 1 ; // Enable CAM entry 0
269
+ /* Limit the max receive frame length to 1514 + 4 */
270
+ EMAC -> MRFL = 1518 ;
271
+
272
+ /* Set RX FIFO threshold as 8 words */
273
+ EMAC -> FIFOCTL = 0x00200100 ;
264
274
265
275
reset_phy ();
266
276
@@ -281,20 +291,22 @@ unsigned int m_status;
281
291
282
292
void EMAC_RX_IRQHandler (void )
283
293
{
284
- // NU_DEBUGF(("%s ... nu_eth_txrx_cb=0x%x\r\n", __FUNCTION__, nu_eth_txrx_cb));
285
294
m_status = EMAC -> INTSTS & 0xFFFF ;
286
295
EMAC -> INTSTS = m_status ;
287
296
if (m_status & EMAC_INTSTS_RXBEIF_Msk ) {
288
297
// Shouldn't goes here, unless descriptor corrupted
289
- NU_DEBUGF (("RX descriptor corrupted \r\n" ));
290
- //return;
298
+ mbed_error_printf ("### RX Bus error [0x%x]\r\n" , m_status );
299
+ if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('B' , nu_userData );
300
+ return ;
291
301
}
302
+ EMAC_DISABLE_INT (EMAC , (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk ));
292
303
if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('R' , nu_userData );
293
304
}
294
305
295
306
296
307
void numaker_eth_trigger_rx (void )
297
308
{
309
+ EMAC_ENABLE_INT (EMAC , (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk ));
298
310
ETH_TRIGGER_RX ();
299
311
}
300
312
@@ -313,6 +325,12 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
313
325
if (status & RXFD_RXGD ) {
314
326
* buf = cur_rx_desc_ptr -> buf ;
315
327
* len = status & 0xFFFF ;
328
+ if ( * len > 1514 ) {
329
+ NU_DEBUGF (("%s... unexpected long packet length=%d, buf=0x%x\r\n" , __FUNCTION__ , * len , * buf ));
330
+
331
+ * len = 0 ; // Skip this unexpected long packet
332
+ }
333
+ if ( * len == 1514 ) NU_DEBUGF (("%s... length=%d, buf=0x%x\r\n" , __FUNCTION__ , * len , * buf ));
316
334
}
317
335
return 0 ;
318
336
}
@@ -331,6 +349,8 @@ void EMAC_TX_IRQHandler(void)
331
349
EMAC -> INTSTS = status ;
332
350
if (status & EMAC_INTSTS_TXBEIF_Msk ) {
333
351
// Shouldn't goes here, unless descriptor corrupted
352
+ mbed_error_printf ("### TX Bus error [0x%x]\r\n" , status );
353
+ if (nu_eth_txrx_cb != NULL ) nu_eth_txrx_cb ('B' , nu_userData );
334
354
return ;
335
355
}
336
356
0 commit comments