1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-1-20 bluebear233 first version
*/
#include <rtthread.h>
#ifdef RT_USING_LWIP
#include "NuMicro.h"
#include "drv_emac.h"
#include <netif/ethernetif.h>
#include <netif/etharp.h>
#include <lwip/icmp.h>
#include "lwipopts.h"
#define ETH_DEBUG
//#define ETH_RX_DUMP
//#define ETH_TX_DUMP
#ifdef ETH_DEBUG
#define ETH_TRACE rt_kprintf
#else
#define ETH_TRACE(...)
#endif /* ETH_DEBUG */
#if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
static void packet_dump(const char * msg, const struct pbuf* p)
{
rt_uint32_t i;
rt_uint8_t *ptr = p->payload;
ETH_TRACE("%s %d byte\n", msg, p->tot_len);
for(i=0; i<p->tot_len; i++)
{
if( (i%8) == 0 )
{
ETH_TRACE(" ");
}
if( (i%16) == 0 )
{
ETH_TRACE("\r\n");
}
ETH_TRACE("%02x ",*ptr);
ptr++;
}
ETH_TRACE("\n\n");
}
#endif /* dump */
#define ETH_TRIGGER_RX() EMAC->RXST = 0
#define ETH_TRIGGER_TX() EMAC->TXST = 0
#define ETH_ENABLE_TX() EMAC->CTL |= EMAC_CTL_TXON
#define ETH_ENABLE_RX() EMAC->CTL |= EMAC_CTL_RXON
#define ETH_DISABLE_TX() EMAC->CTL &= ~EMAC_CTL_TXON
#define ETH_DISABLE_RX() EMAC->CTL &= ~EMAC_CTL_RXON
#define EMAC_DMARXDESC_CRCEIF_Msk (1ul << 17)
#define ETH_TID_STACK 256
static rt_uint8_t volatile phy_speed = 0;
static rt_uint8_t eth_addr[6];
static struct eth_device eth;
static struct rt_semaphore eth_sem;
static struct rt_thread eth_tid;
static rt_uint32_t eth_stack[ETH_TID_STACK / 4];
static struct eth_descriptor volatile *cur_tx_desc_ptr, *cur_rx_desc_ptr, *fin_tx_desc_ptr;
static struct eth_descriptor rx_desc[RX_DESCRIPTOR_NUM];
static struct eth_descriptor tx_desc[TX_DESCRIPTOR_NUM];
static rt_uint32_t rx_buf[RX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
static rt_uint32_t tx_buf[TX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
static void mdio_write(rt_uint8_t addr, rt_uint8_t reg, rt_uint16_t val)
{
EMAC->MIIMDAT = val;
EMAC->MIIMCTL = (addr << EMAC_MIIMCTL_PHYADDR_Pos) | reg | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_WRITE_Msk | EMAC_MIIMCTL_MDCON_Msk;
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk);
}
static rt_uint16_t mdio_read(rt_uint8_t addr, rt_uint8_t reg)
{
EMAC->MIIMCTL = (addr << EMAC_MIIMCTL_PHYADDR_Pos) | reg | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_MDCON_Msk;
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk);
return EMAC->MIIMDAT;
}
static void init_tx_desc(void)
{
rt_uint32_t i;
cur_tx_desc_ptr = fin_tx_desc_ptr = &tx_desc[0];
for(i = 0; i < TX_DESCRIPTOR_NUM; i++)
{
tx_desc[i].status1 = TXFD_PADEN | TXFD_CRCAPP | TXFD_INTEN;
tx_desc[i].buf = (rt_uint8_t*)tx_buf[i];
tx_desc[i].status2 = 0;
tx_desc[i].next = &tx_desc[(i + 1) % TX_DESCRIPTOR_NUM];
}
EMAC->TXDSA = (unsigned int)&tx_desc[0];
return;
}
static void init_rx_desc(void)
{
rt_uint32_t i;
cur_rx_desc_ptr = &rx_desc[0];
for(i = 0; i < RX_DESCRIPTOR_NUM; i++)
{
rx_desc[i].status1 = OWNERSHIP_EMAC;
rx_desc[i].buf = (rt_uint8_t*)rx_buf[i];
rx_desc[i].status2 = 0;
rx_desc[i].next = &rx_desc[(i + 1) % RX_DESCRIPTOR_NUM];
}
EMAC->RXDSA = (unsigned int)&rx_desc[0];
return;
}
static void add_mac_addr(const rt_uint8_t *addr)
{
rt_uint32_t *EMAC_CAMxM;
rt_uint32_t *EMAC_CAMxL;
rt_uint8_t index = 0;
rt_uint8_t mac[6];
for(; index < 13; index ++)
{
EMAC_CAMxM = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0M + (index * 8));
EMAC_CAMxL = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0L + (index * 8));
mac[0] = (*EMAC_CAMxM >> 24) & 0xff;
mac[1] = (*EMAC_CAMxM >> 16) & 0xff;
mac[2] = (*EMAC_CAMxM >> 8) & 0xff;
mac[3] = (*EMAC_CAMxM) & 0xff;
mac[4] = (*EMAC_CAMxL >> 24) & 0xff;
mac[5] = (*EMAC_CAMxL >> 16) & 0xff;
if(memcmp(mac, addr, sizeof(mac)) == 0)
{
return;
}
if(*EMAC_CAMxM == 0 && *EMAC_CAMxL == 0)
{
break;
}
}
RT_ASSERT(index < 13)
*EMAC_CAMxM = (addr[0] << 24) |
(addr[1] << 16) |
(addr[2] << 8) |
addr[3];
*EMAC_CAMxL = (addr[4] << 24) |
(addr[5] << 16);
EMAC->CAMEN |= (1 << index);
}
void EMAC_init()
{
// Reset MAC
EMAC->CTL = EMAC_CTL_RST_Msk;
while(EMAC->CTL & EMAC_CTL_RST_Msk);
init_tx_desc();
init_rx_desc();
EMAC->CAMCTL = EMAC_CAMCTL_CMPEN_Msk | EMAC_CAMCTL_ABP_Msk;
add_mac_addr(eth_addr);
EMAC->CTL |= EMAC_CTL_STRIPCRC_Msk | EMAC_CTL_RXON_Msk | EMAC_CTL_TXON_Msk | EMAC_CTL_RMIIEN_Msk;
EMAC->INTEN = EMAC_INTEN_RXIEN_Msk |
EMAC_INTEN_RXGDIEN_Msk |
EMAC_INTEN_RDUIEN_Msk |
EMAC_INTEN_RXBEIEN_Msk |
EMAC_INTEN_TXIEN_Msk |
EMAC_INTEN_TXBEIEN_Msk;
/* Limit the max receive frame length to 1514 + 4 */
EMAC->MRFL = PACKET_BUFFER_SIZE;
EMAC->RXST = 0; // trigger Rx
}
void EMAC_Reinit(void)
{
rt_uint32_t EMAC_CAMxM[13];
rt_uint32_t EMAC_CAMxL[13];
rt_uint32_t EMAC_CAMEN;
EMAC_CAMEN = EMAC->CAMEN;
for(rt_uint8_t index = 0 ; index < 13; index ++)
{
rt_uint32_t *CAMxM = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0M + (index * 8));
rt_uint32_t *CAMxL = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0L + (index * 8));
EMAC_CAMxM[index] = *CAMxM;
EMAC_CAMxL[index] = *CAMxL;
}
EMAC_init();
for(rt_uint8_t index = 0 ; index < 13; index ++)
{
rt_uint32_t *CAMxM = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0M + (index * 8));
rt_uint32_t *CAMxL = (rt_uint32_t *)((rt_uint32_t)&EMAC->CAM0L + (index * 8));
*CAMxM = EMAC_CAMxM[index];
*CAMxL = EMAC_CAMxL[index];
}
EMAC->CAMEN = EMAC_CAMEN;
phy_speed = 0;
}
void ETH_halt(void)
{
EMAC->CTL &= ~(EMAC_CTL_RXON_Msk | EMAC_CTL_TXON_Msk);
}
__inline static rt_uint8_t *emac_get_tx_buf(void)
{
if(cur_tx_desc_ptr->status1 & OWNERSHIP_EMAC)
{
return(RT_NULL);
}
else
{
return(cur_tx_desc_ptr->buf);
}
}
__inline static void ETH_trigger_tx(rt_uint16_t length)
{
struct eth_descriptor volatile *desc;
cur_tx_desc_ptr->status2 = (unsigned int)length;
desc = cur_tx_desc_ptr->next; // in case TX is transmitting and overwrite next pointer before we can update cur_tx_desc_ptr
cur_tx_desc_ptr->status1 |= OWNERSHIP_EMAC;
cur_tx_desc_ptr = desc;
}
#if LWIP_IPV4 && LWIP_IGMP
static err_t igmp_mac_filter( struct netif *netif, const ip4_addr_t *ip4_addr, u8_t action )
{
rt_uint8_t mac[6];
const uint8_t *p = (const uint8_t *)ip4_addr;
mac[0] = 0x01;
mac[1] = 0x00;
mac[2] = 0x5E;
mac[3] = *(p+1) & 0x7F;
mac[4] = *(p+2);
mac[5] = *(p+3);
add_mac_addr(mac);
if(1)
{
rt_kprintf("%s %s %s ", __FUNCTION__, (action==NETIF_ADD_MAC_FILTER)?"add":"del", ip4addr_ntoa(ip4_addr));
rt_kprintf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
return 0;
}
#endif /* LWIP_IPV4 && LWIP_IGMP */
/*
* M480 EMAC Driver for RT-Thread
* Change Logs:
* Date Author Notes
* 2017-12-31 Bluebear233 first implementation
*/
void EMAC_RX_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
unsigned int status = EMAC->INTSTS;
if(status & EMAC_INTSTS_RDUIF_Msk)
{
EMAC->INTEN &= ~(EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk);
eth_device_ready(ð);
}
else if(status & EMAC_INTSTS_RXGDIF_Msk)
{
EMAC->INTEN &= ~EMAC_INTEN_RXGDIEN_Msk;
eth_device_ready(ð);
}
if(status & EMAC_INTSTS_RXBEIF_Msk)
{
ETH_TRACE("Reinit Rx EMAC\n");
EMAC->INTSTS = EMAC_INTSTS_RXBEIF_Msk;
EMAC_Reinit();
}
/* leave interrupt */
rt_interrupt_leave();
}
void EMAC_TX_IRQHandler(void)
{
rt_interrupt_enter();
unsigned int status = EMAC->INTSTS;
if(status & EMAC_INTSTS_TXCPIF_Msk)
{
EMAC->INTEN &= ~EMAC_INTEN_TXCPIEN_Msk;
rt_sem_release(ð_sem);
}
if(status & EMAC_INTSTS_TXBEIF_Msk)
{
ETH_TRACE("Reinit Tx EMAC\n");
EMAC->INTSTS = EMAC_INTSTS_TXBEIF_Msk;
EMAC_Reinit();
}
rt_interrupt_leave();
}
#define PHY_LINK_MASK (1<<0)
#define PHY_10FULL_MASK (1<<1)
#define PHY_100FULL_MASK (1<<2)
#define PHY_10HALF_MASK (1<<3)
#define PHY_100HALF_MASK (1<<4)
#define PHY_ANLPA_DR100_TX_FULL (1UL << 8UL)
#define PHY_ANLPA_DR100_TX_HALF (1UL << 7UL)
#define PHY_ANLPA_DR10_TX_FULL (1UL << 6UL)
#define PHY_ANLPA_DR10_TX_HALF (1UL << 5UL)
void eth_entry(void *param)
{
uint8_t phy_addr = 0xFF;
uint8_t phy_speed_new = 0;
/* phy search */
{
rt_uint32_t i;
rt_uint16_t temp;
for(i=0; i<=0x1F; i++)
{
temp = mdio_read(i, 0x02);
if( temp != 0xFFFF )
{
phy_addr = i;
break;
}
}
} /* phy search */
if(phy_addr == 0xFF)
{
ETH_TRACE("phy not probe!\n");
return;
}
else
{
ETH_TRACE("found a phy, address:0x%02X\n", phy_addr);
}
/* RESET PHY */
mdio_write(phy_addr, MII_BMCR, BMCR_RESET);
while (1)
{
rt_thread_delay(RT_TICK_PER_SECOND);
rt_uint16_t reg = mdio_read(phy_addr, MII_BMCR);
if ((reg & BMCR_RESET) == 0)
{
break;
}
}
mdio_write(phy_addr, MII_ADVERTISE, ADVERTISE_CSMA |
ADVERTISE_10HALF |
ADVERTISE_10FULL |
ADVERTISE_100HALF |
ADVERTISE_100FULL);
{
uint16_t reg = mdio_read(phy_addr, MII_BMCR);
mdio_write(phy_addr, MII_BMCR, reg | BMCR_ANRESTART);
}
while(1)
{
uint16_t status = mdio_read(phy_addr, MII_BMSR);
phy_speed_new = 0;
if((status & (BMSR_ANEGCAPABLE | BMSR_LSTATUS)) == (BMSR_ANEGCAPABLE | BMSR_LSTATUS))
{
phy_speed_new = PHY_LINK_MASK;
status = mdio_read(phy_addr, MII_LPA);
if(status & PHY_ANLPA_DR100_TX_FULL)
{
phy_speed_new |= PHY_100FULL_MASK;
}
else if(status & PHY_ANLPA_DR100_TX_HALF)
{
phy_speed_new |= PHY_100HALF_MASK;
}
else if(status & PHY_ANLPA_DR10_TX_FULL)
{
phy_speed_new |= PHY_10FULL_MASK;
}
else if(status & PHY_ANLPA_DR10_TX_HALF)
{
phy_speed_new |= PHY_10HALF_MASK;
}
}
/* linkchange */
if(phy_speed_new != phy_speed)
{
if(phy_speed_new & PHY_LINK_MASK)
{
ETH_TRACE("link up ");
if(phy_speed_new & PHY_100FULL_MASK)
{
ETH_TRACE("100Mbps full-duplex\n");
EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
}
else if(phy_speed_new & PHY_100HALF_MASK)
{
ETH_TRACE("100Mbps half-duplex\n");
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_FUDUP_Msk) | EMAC_CTL_OPMODE_Msk;
}
else if(phy_speed_new & PHY_10FULL_MASK)
{
ETH_TRACE("10Mbps full-duplex\n");
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_OPMODE_Msk) | EMAC_CTL_FUDUP_Msk;
}
else
{
ETH_TRACE("10Mbps half-duplex\n");
EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
}
/* send link up. */
eth_device_linkchange(ð, RT_TRUE);
} /* link up. */
else
{
ETH_TRACE("link down\r\n");
/* send link down. */
eth_device_linkchange(ð, RT_FALSE);
} /* link down. */
phy_speed = phy_speed_new;
} /* linkchange */
rt_thread_delay(RT_TICK_PER_SECOND);
} /* while(1) */
}
static rt_err_t rt_m480_emac_init(rt_device_t dev)
{
/* Unlock protected registers */
SYS_UnlockReg();
CLK_EnableModuleClock(EMAC_MODULE);
// Configure MDC clock rate to HCLK / (127 + 1) = 1.5 MHz if system is running at 192 MHz
CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));
// Configure RMII pins
// SYS->GPA_MFPL |= SYS_GPA_MFPL_PA6MFP_EMAC_RMII_RXERR | SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
SYS->GPA_MFPL |= SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_EMAC_RMII_RXD1 | SYS_GPC_MFPL_PC7MFP_EMAC_RMII_RXD0;
SYS->GPC_MFPH |= SYS_GPC_MFPH_PC8MFP_EMAC_RMII_REFCLK;
SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;
// Enable high slew rate on all RMII TX output pins
PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
(GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN11_Pos) |
(GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN12_Pos);
/* Lock protected registers */
SYS_LockReg();
EMAC_init();
NVIC_SetPriority(EMAC_TX_IRQn, 1);
NVIC_EnableIRQ(EMAC_TX_IRQn);
NVIC_SetPriority(EMAC_RX_IRQn, 1);
NVIC_EnableIRQ(EMAC_RX_IRQn);
rt_sem_init(ð_sem, "eth_sem", 0, RT_IPC_FLAG_FIFO);
rt_thread_init(ð_tid, "eth", eth_entry, RT_NULL, eth_stack, sizeof(eth_stack), RT_THREAD_PRIORITY_MAX - 2, 10);
rt_thread_startup(ð_tid);
#if LWIP_IPV4 && LWIP_IGMP
netif_set_igmp_mac_filter(eth.netif, igmp_mac_filter);
#endif /* LWIP_IPV4 && LWIP_IGMP */
return RT_EOK;
}
static rt_err_t rt_m480_emac_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t rt_m480_emac_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_size_t rt_m480_emac_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
rt_set_errno(-RT_ENOSYS);
return 0;
}
static rt_size_t rt_m480_emac_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
rt_set_errno(-RT_ENOSYS);
return 0;
}
static rt_err_t rt_m480_emac_control(rt_device_t dev, int cmd, void *args)
{
switch(cmd)
{
case NIOCTL_GADDR:
/* get mac address */
if(args) rt_memcpy(args, eth_addr, 6);
else return -RT_ERROR;
break;
default :
break;
}
return RT_EOK;
}
rt_err_t rt_m480_emac_tx(rt_device_t dev, struct pbuf* p)
{
struct pbuf* q;
rt_uint32_t offset;
rt_uint8_t *buf;
buf = emac_get_tx_buf();
/* get free tx buffer */
if(buf == RT_NULL)
{
rt_sem_control(ð_sem, RT_IPC_CMD_RESET, 0);
EMAC->INTSTS = EMAC_INTSTS_TXCPIF_Msk;
EMAC->INTEN |= EMAC_INTEN_TXCPIEN_Msk;
do{
rt_sem_take(ð_sem, 1);
buf = emac_get_tx_buf();
}while(buf == RT_NULL);
}
offset = 0;
for (q = p; q != NULL; q = q->next)
{
rt_uint8_t* ptr;
rt_uint32_t len;
len = q->len;
ptr = q->payload;
// todo 优化复制
memcpy(&buf[offset], ptr, len);
offset += len;
}
#ifdef ETH_TX_DUMP
packet_dump("TX dump", p);
#endif
ETH_trigger_tx(offset);
if(EMAC->INTSTS & EMAC_INTSTS_TDUIF_Msk)
{
EMAC->INTSTS = EMAC_INTSTS_TDUIF_Msk;
ETH_TRIGGER_TX();
}
/* Return SUCCESS */
return RT_EOK;
}
struct pbuf *rt_m480_emac_rx(rt_device_t dev)
{
unsigned int status;
struct pbuf* p;
/* init p pointer */
p = RT_NULL;
start:
status = cur_rx_desc_ptr->status1;
if(status & OWNERSHIP_EMAC)
{
goto end;
}
if ((status & RXFD_RXGD) && !(status & EMAC_DMARXDESC_CRCEIF_Msk))
{
p = pbuf_alloc(PBUF_RAW, status & 0xFFFF, PBUF_RAM);
if (p != RT_NULL)
{
RT_ASSERT(p->next == RT_NULL);
const char * from = (const char *)(cur_rx_desc_ptr->buf);
// todo 优化复制
memcpy(p->payload, from, p->len);
}
}
#ifdef ETH_RX_DUMP
packet_dump("RX dump", p);
#endif /* ETH_RX_DUMP */
cur_rx_desc_ptr->status1 = OWNERSHIP_EMAC;
cur_rx_desc_ptr = cur_rx_desc_ptr->next;
if(p == RT_NULL)
{
goto start;
}
return p;
end:
if(!(EMAC->INTEN & EMAC_INTEN_RDUIEN_Msk))
{
EMAC->INTSTS = (EMAC_INTSTS_RDUIF_Msk | EMAC_INTSTS_RXGDIF_Msk);
EMAC->INTEN |= (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk);
ETH_TRIGGER_RX();
}
else
{
EMAC->INTSTS = EMAC_INTSTS_RXGDIF_Msk;
EMAC->INTEN |= EMAC_INTEN_RXGDIEN_Msk;
}
return RT_NULL;
}
static void rt_hw_m480_emac_register(char *dev_name)
{
rt_uint32_t value = 0;
SYS_UnlockReg();
FMC_Open();
for (rt_uint8_t i = 0; i < 3; i++)
{
value += FMC_ReadUID(i);
}
FMC_Close();
SYS_LockReg();
eth_addr[0] = 0x00;
eth_addr[1] = 0x00;
eth_addr[2] = 0x00;
eth_addr[3] = (value >> 16) & 0xff;
eth_addr[4] = (value >> 8) & 0xff;
eth_addr[5] = (value) & 0xff;
eth.parent.init = rt_m480_emac_init;
eth.parent.open = rt_m480_emac_open;
eth.parent.close = rt_m480_emac_close;
eth.parent.read = rt_m480_emac_read;
eth.parent.write = rt_m480_emac_write;
eth.parent.control = rt_m480_emac_control;
eth.parent.user_data = RT_NULL;
eth.eth_rx = rt_m480_emac_rx;
eth.eth_tx = rt_m480_emac_tx;
/* register eth device */
eth_device_init(ð, dev_name);
}
static int rt_hw_nuc487_emac_init(void)
{
rt_hw_m480_emac_register("eh0");
return RT_EOK;
}
INIT_APP_EXPORT(rt_hw_nuc487_emac_init);
#endif
|