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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2014-07-25 ArdaFu Port to TM4C129X
*/
/**
* @file - tivaif.c
* lwIP Ethernet interface for Stellaris LM4F Devices
*
*/
/**
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICui32AR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <[email protected]>
*
*/
/**
* Copyright (c) 2008-2012 Texas Instruments Incorporated
*
* This file is derived from the ``ethernetif.c'' skeleton Ethernet network
* interface driver for lwIP.
*
*/
#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include <lwip/stats.h>
#include <lwip/snmp.h>
#include "lwip/tcpip.h"
#include "netif/etharp.h"
#include "netif/ppp_oe.h"
/**
* Sanity Check: This interface driver will NOT work if the following defines
* are incorrect.
*
*/
#if (PBUF_LINK_HLEN != 16)
#error "PBUF_LINK_HLEN must be 16 for this interface driver!"
#endif
#if (ETH_PAD_SIZE != 0)
#error "ETH_PAD_SIZE must be 0 for this interface driver!"
#endif
#if (!SYS_LIGHTWEIGHT_PROT)
#error "SYS_LIGHTWEIGHT_PROT must be enabled for this interface driver!"
#endif
/**
* Set the physical address of the PHY we will be using if this is not
* specified in lwipopts.h. We assume 0 for the internal PHY.
*/
#ifndef PHY_PHYS_ADDR
#define PHY_PHYS_ADDR 0
#endif
#if 1
#ifndef EMAC_PHY_CONFIG
#define EMAC_PHY_CONFIG (EMAC_PHY_TYPE_INTERNAL | EMAC_PHY_INT_MDIX_EN | \
EMAC_PHY_AN_100B_T_FULL_DUPLEX)
#endif
#endif
/**
* If necessary, set the defaui32t number of transmit and receive DMA descriptors
* used by the Ethernet MAC.
*
*/
#ifndef NUM_RX_DESCRIPTORS
#define NUM_RX_DESCRIPTORS 4
#endif
#ifndef NUM_TX_DESCRIPTORS
#define NUM_TX_DESCRIPTORS 8
#endif
/**
* Setup processing for PTP (IEEE-1588).
*
*/
#if LWIP_PTPD
extern uint32_t g_ui32SysClk;
extern uint32_t g_ui32PTPTickRate;
extern void lwIPHostGetTime(u32_t *time_s, u32_t *time_ns);
#endif
/**
* Stellaris DriverLib Header Files required for this interface driver.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_emac.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/emac.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/flash.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib/gpio.h"
#include <netif/ethernetif.h>
#include "lwipopts.h"
#include "drv_eth.h"
/* Define those to better describe your network interface. */
#define IFNAME0 't'
#define IFNAME1 'i'
/**
* A structure used to keep track of driver state and error counts.
*/
typedef struct {
uint32_t ui32TXCount;
uint32_t ui32TXCopyCount;
uint32_t ui32TXCopyFailCount;
uint32_t ui32TXNoDescCount;
uint32_t ui32TXBufQueuedCount;
uint32_t ui32TXBufFreedCount;
uint32_t ui32RXBufReadCount;
uint32_t ui32RXPacketReadCount;
uint32_t ui32RXPacketErrCount;
uint32_t ui32RXPacketCBErrCount;
uint32_t ui32RXNoBufCount;
}
tDriverStats;
tDriverStats g_sDriverStats;
#ifdef DEBUG
/**
* Note: This rather weird construction where we invoke the macro with the
* name of the field minus its Hungarian prefix is a workaround for a problem
* experienced with GCC which does not like concatenating tokens after an
* operator, specifically '.' or '->', in a macro.
*/
#define DRIVER_STATS_INC(x) do{ g_sDriverStats.ui32##x++; } while(0)
#define DRIVER_STATS_DEC(x) do{ g_sDriverStats.ui32##x--; } while(0)
#define DRIVER_STATS_ADD(x, inc) do{ g_sDriverStats.ui32##x += (inc); } while(0)
#define DRIVER_STATS_SUB(x, dec) do{ g_sDriverStats.ui32##x -= (dec); } while(0)
#else
#define DRIVER_STATS_INC(x)
#define DRIVER_STATS_DEC(x)
#define DRIVER_STATS_ADD(x, inc)
#define DRIVER_STATS_SUB(x, dec)
#endif
/**
* Helper struct holding a DMA descriptor and the pbuf it currently refers
* to.
*/
typedef struct {
tEMACDMADescriptor Desc;
struct pbuf *pBuf;
} tDescriptor;
typedef struct {
tDescriptor *pDescriptors;
uint32_t ui32NumDescs;
uint32_t ui32Read;
uint32_t ui32Write;
} tDescriptorList;
/**
* Helper struct to hold private data used to operate your ethernet interface.
* Keeping the ethernet address of the MAC in this struct is not necessary
* as it is already kept in the struct netif.
* But this is only an example, anyway...
*/
typedef struct {
struct eth_addr *ethaddr;
/* Add whatever per-interface state that is needed here. */
tDescriptorList *pTxDescList;
tDescriptorList *pRxDescList;
} tStellarisIF;
/**
* Global variable for this interface's private data. Needed to allow
* the interrupt handlers access to this information outside of the
* context of the lwIP netif.
*
*/
tDescriptor g_pTxDescriptors[NUM_TX_DESCRIPTORS];
tDescriptor g_pRxDescriptors[NUM_RX_DESCRIPTORS];
tDescriptorList g_TxDescList = {
g_pTxDescriptors, NUM_TX_DESCRIPTORS, 0, 0
};
tDescriptorList g_RxDescList = {
g_pRxDescriptors, NUM_RX_DESCRIPTORS, 0, 0
};
static tStellarisIF g_StellarisIFData = {
0, &g_TxDescList, &g_RxDescList
};
/**
* Interrupt counters (for debug purposes).
*/
volatile uint32_t g_ui32NormalInts;
volatile uint32_t g_ui32AbnormalInts;
/**
* Status flag for EEE link established
*/
#if EEE_SUPPORT
volatile bool g_bEEELinkActive;
#endif
/**
* A macro which determines whether a pointer is within the SRAM address
* space and, hence, points to a buffer that the Ethernet MAC can directly
* DMA from.
*/
#define PTR_SAFE_FOR_EMAC_DMA(ptr) (((uint32_t)(ptr) >= 0x2000000) && \
((uint32_t)(ptr) < 0x20070000))
typedef struct
{
/* inherit from ethernet device */
struct eth_device parent;
tStellarisIF* dma_if;
/* for rx_thread async get pbuf */
rt_mailbox_t rx_pbuf_mb;
} net_device;
typedef net_device* net_device_t;
static char rx_pbuf_mb_pool[8*4];
static struct rt_mailbox eth_rx_pbuf_mb;
static net_device eth_dev_entry;
static net_device_t eth_dev = ð_dev_entry;
/**
* Initialize the transmit and receive DMA descriptor lists.
*/
void
InitDMADescriptors(void)
{
uint32_t ui32Loop;
/* Transmit list - mark all descriptors as not owned by the hardware */
for(ui32Loop = 0; ui32Loop < NUM_TX_DESCRIPTORS; ui32Loop++)
{
g_pTxDescriptors[ui32Loop].pBuf = (struct pbuf *)0;
g_pTxDescriptors[ui32Loop].Desc.ui32Count = 0;
g_pTxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
g_pTxDescriptors[ui32Loop].Desc.DES3.pLink =
((ui32Loop == (NUM_TX_DESCRIPTORS - 1)) ?
&g_pTxDescriptors[0].Desc : &g_pTxDescriptors[ui32Loop + 1].Desc);
g_pTxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_TX_CTRL_INTERRUPT |
DES0_TX_CTRL_CHAINED | DES0_TX_CTRL_IP_ALL_CKHSUMS;
}
g_TxDescList.ui32Read = 0;
g_TxDescList.ui32Write = 0;
/* Receive list - tag each descriptor with a pbuf and set all fields to
* allow packets to be received.
*/
for(ui32Loop = 0; ui32Loop < NUM_RX_DESCRIPTORS; ui32Loop++)
{
g_pRxDescriptors[ui32Loop].pBuf = pbuf_alloc(PBUF_RAW, PBUF_POOL_BUFSIZE,
PBUF_POOL);
g_pRxDescriptors[ui32Loop].Desc.ui32Count = DES1_RX_CTRL_CHAINED;
if(g_pRxDescriptors[ui32Loop].pBuf)
{
/* Set the DMA to write directly into the pbuf payload. */
g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 =
g_pRxDescriptors[ui32Loop].pBuf->payload;
g_pRxDescriptors[ui32Loop].Desc.ui32Count |=
(g_pRxDescriptors[ui32Loop].pBuf->len << DES1_RX_CTRL_BUFF1_SIZE_S);
g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_RX_CTRL_OWN;
}
else
{
LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_init: pbuf_alloc error\n"));
/* No pbuf available so leave the buffer pointer empty. */
g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = 0;
}
g_pRxDescriptors[ui32Loop].Desc.DES3.pLink =
((ui32Loop == (NUM_RX_DESCRIPTORS - 1)) ?
&g_pRxDescriptors[0].Desc : &g_pRxDescriptors[ui32Loop + 1].Desc);
}
g_TxDescList.ui32Read = 0;
g_TxDescList.ui32Write = 0;
//
// Set the descriptor pointers in the hardware.
//
EMACRxDMADescriptorListSet(EMAC0_BASE, &g_pRxDescriptors[0].Desc);
EMACTxDMADescriptorListSet(EMAC0_BASE, &g_pTxDescriptors[0].Desc);
}
/**
* In this function, the hardware should be initialized.
* Called from tivaif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void
tivaif_hwinit(struct netif *psNetif)
{
uint16_t ui16Val;
/* clear the EEE Link Active flag */
#if EEE_SUPPORT
g_bEEELinkActive = false;
#endif
/* Set MAC hardware address length */
psNetif->hwaddr_len = ETHARP_HWADDR_LEN;
/* Set MAC hardware address */
EMACAddrGet(EMAC0_BASE, 0, &(psNetif->hwaddr[0]));
/* Maximum transfer unit */
psNetif->mtu = 1500;
/* Device capabilities */
psNetif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
/* Initialize the DMA descriptors. */
InitDMADescriptors();
#if defined(EMAC_PHY_IS_EXT_MII) || defined(EMAC_PHY_IS_EXT_RMII)
/* If PHY is external then reset the PHY before configuring it */
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR,
EPHY_BMCR_MIIRESET);
while((EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR) &
EPHY_BMCR_MIIRESET) == EPHY_BMCR_MIIRESET);
#endif
/* Clear any stray PHY interrupts that may be set. */
ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR2);
/* Configure and enable the link status change interrupt in the PHY. */
ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR);
ui16Val |= (EPHY_SCR_INTEN_EXT | EPHY_SCR_INTOE_EXT);
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR, ui16Val);
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1, (EPHY_MISR1_LINKSTATEN |
EPHY_MISR1_SPEEDEN | EPHY_MISR1_DUPLEXMEN | EPHY_MISR1_ANCEN));
/* Read the PHY interrupt status to clear any stray events. */
ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
/**
* Set MAC filtering options. We receive all broadcast and mui32ticast
* packets along with those addressed specifically for us.
*/
EMACFrameFilterSet(EMAC0_BASE, (EMAC_FRMFILTER_HASH_AND_PERFECT |
EMAC_FRMFILTER_PASS_MULTICAST));
#if LWIP_PTPD
//
// Enable timestamping on all received packets.
//
// We set the fine clock adjustment mode and configure the subsecond
// increment to half the 25MHz PTPD clock. This will give us maximum control
// over the clock rate adjustment and keep the arithmetic easy later. It
// should be possible to synchronize with higher accuracy than this with
// appropriate juggling of the subsecond increment count and the addend
// register value, though.
//
EMACTimestampConfigSet(EMAC0_BASE, (EMAC_TS_ALL_RX_FRAMES |
EMAC_TS_DIGITAL_ROLLOVER |
EMAC_TS_PROCESS_IPV4_UDP | EMAC_TS_ALL |
EMAC_TS_PTP_VERSION_1 | EMAC_TS_UPDATE_FINE),
(1000000000 / (25000000 / 2)));
EMACTimestampAddendSet(EMAC0_BASE, 0x80000000);
EMACTimestampEnable(EMAC0_BASE);
#endif
/* Clear any pending MAC interrupts. */
EMACIntClear(EMAC0_BASE, EMACIntStatus(EMAC0_BASE, false));
/* Enable the Ethernet MAC transmitter and receiver. */
EMACTxEnable(EMAC0_BASE);
EMACRxEnable(EMAC0_BASE);
/* Enable the Ethernet RX and TX interrupt source. */
EMACIntEnable(EMAC0_BASE, (EMAC_INT_RECEIVE | EMAC_INT_TRANSMIT |
EMAC_INT_TX_STOPPED | EMAC_INT_RX_NO_BUFFER |
EMAC_INT_RX_STOPPED | EMAC_INT_PHY));
/* Enable the Ethernet interrupt. */
IntEnable(INT_EMAC0);
/* Enable all processor interrupts. */
IntMasterEnable();
/* Tell the PHY to start an auto-negotiation cycle. */
#if defined(EMAC_PHY_IS_EXT_MII) || defined(EMAC_PHY_IS_EXT_RMII)
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR, (EPHY_BMCR_SPEED |
EPHY_BMCR_DUPLEXM | EPHY_BMCR_ANEN | EPHY_BMCR_RESTARTAN));
#else
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR, (EPHY_BMCR_ANEN |
EPHY_BMCR_RESTARTAN));
#endif
}
#ifdef DEBUG
/**
* Dump the chain of pbuf pointers to the debug output.
*/
void
tivaif_trace_pbuf(const char *pcTitle, struct pbuf *p)
{
LWIP_DEBUGF(NETIF_DEBUG, ("%s %08x (%d, %d)", pcTitle, p, p->tot_len,
p->len));
do
{
p = p->next;
if(p)
{
LWIP_DEBUGF(NETIF_DEBUG, ("->%08x(%d)", p, p->len));
}
else
{
LWIP_DEBUGF(NETIF_DEBUG, ("->%08x", p));
}
} while((p != NULL) && (p->tot_len != p->len));
LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
}
#endif
/**
* This function is used to check whether a passed pbuf contains only buffers
* resident in regions of memory that the Ethernet MAC can access. If any
* buffers in the chain are outside a directly-DMAable section of memory,
* the pbuf is copied to SRAM and a different pointer returned. If all
* buffers are safe, the pbuf reference count is incremented and the original
* pointer returned.
*/
static struct pbuf *
tivaif_check_pbuf(struct pbuf *p)
{
struct pbuf *pBuf;
rt_err_t Err;
pBuf = p;
#ifdef DEBUG
tivaif_trace_pbuf("Original:", p);
#endif
/* Walk the list of buffers in the pbuf checking each. */
do
{
/* Does this pbuf's payload reside in memory that the Ethernet DMA
* can access?
*/
if(!PTR_SAFE_FOR_EMAC_DMA(pBuf->payload))
{
/* This buffer is outside the DMA-able memory space so we need
* to copy the pbuf.
*/
pBuf = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_POOL);
/* If we got a new pbuf... */
if(pBuf)
{
/* ...copy the old pbuf into the new one. */
Err = pbuf_copy(pBuf, p);
/* If we failed to copy the pbuf, free the newly allocated one
* and make sure we return a NULL to show a problem.
*/
if(Err != RT_EOK)
{
DRIVER_STATS_INC(TXCopyFailCount);
pbuf_free(pBuf);
pBuf = NULL;
}
else
{
#ifdef DEBUG
tivaif_trace_pbuf("Copied:", pBuf);
#endif
DRIVER_STATS_INC(TXCopyCount);
}
}
/* Reduce the reference count on the original pbuf since we're not
* going to hold on to it after returning from tivaif_transmit.
* Note that we already bumped the reference count at the top of
* tivaif_transmit.
*/
pbuf_free(p);
/* Send back the new pbuf pointer or NULL if an error occurred. */
return(pBuf);
}
/* Move on to the next buffer in the queue */
pBuf = pBuf->next;
}
while(pBuf);
/**
* If we get here, the passed pbuf can be safely used without needing to
* be copied.
*/
return(p);
}
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf might be
* chained.
*
* @param psNetif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return RT_EOK if the packet coui32d be sent
* an err_t value if the packet coui32dn't be sent
*/
static rt_err_t
tivaif_transmit(net_device_t dev, struct pbuf *p)
{
tStellarisIF *pIF;
tDescriptor *pDesc;
struct pbuf *pBuf;
uint32_t ui32NumChained, ui32NumDescs;
bool bFirst;
SYS_ARCH_DECL_PROTECT(lev);
LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_transmit 0x%08x, len %d\n", p,
p->tot_len));
/**
* This entire function must run within a "critical section" to preserve
* the integrity of the transmit pbuf queue.
*/
SYS_ARCH_PROTECT(lev);
/* Update our transmit attempt counter. */
DRIVER_STATS_INC(TXCount);
/**
* Increase the reference count on the packet provided so that we can
* hold on to it until we are finished transmitting its content.
*/
pbuf_ref(p);
/**
* Determine whether all buffers passed are within SRAM and, if not, copy
* the pbuf into SRAM-resident buffers so that the Ethernet DMA can access
* the data.
*/
p = tivaif_check_pbuf(p);
/* Make sure we still have a valid buffer (it may have been copied) */
if(!p)
{
LINK_STATS_INC(link.memerr);
SYS_ARCH_UNPROTECT(lev);
return(-RT_ENOMEM);
}
/* Get our state data from the netif structure we were passed. */
//pIF = (tStellarisIF *)psNetif->state;
pIF = dev->dma_if;
/* Make sure that the transmit descriptors are not all in use */
pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
if(pDesc->pBuf)
{
/**
* The current write descriptor has a pbuf attached to it so this
* implies that the ring is full. Reject this transmit request with a
* memory error since we can't satisfy it just now.
*/
pbuf_free(p);
LINK_STATS_INC(link.memerr);
DRIVER_STATS_INC(TXNoDescCount);
SYS_ARCH_UNPROTECT(lev);
return (-RT_ENOMEM);
}
/* How many pbufs are in the chain passed? */
ui32NumChained = (uint32_t)pbuf_clen(p);
/* How many free transmit descriptors do we have? */
ui32NumDescs = (pIF->pTxDescList->ui32Read > pIF->pTxDescList->ui32Write) ?
(pIF->pTxDescList->ui32Read - pIF->pTxDescList->ui32Write) :
((NUM_TX_DESCRIPTORS - pIF->pTxDescList->ui32Write) +
pIF->pTxDescList->ui32Read);
/* Do we have enough free descriptors to send the whole packet? */
if(ui32NumDescs < ui32NumChained)
{
/* No - we can't transmit this whole packet so return an error. */
pbuf_free(p);
LINK_STATS_INC(link.memerr);
DRIVER_STATS_INC(TXNoDescCount);
SYS_ARCH_UNPROTECT(lev);
return (-RT_ENOMEM);
}
/* Tag the first descriptor as the start of the packet. */
bFirst = true;
pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
/* Here, we know we can send the packet so write it to the descriptors */
pBuf = p;
while(ui32NumChained)
{
/* Get a pointer to the descriptor we will write next. */
pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
/* Fill in the buffer pointer and length */
pDesc->Desc.ui32Count = (uint32_t)pBuf->len;
pDesc->Desc.pvBuffer1 = pBuf->payload;
/* Tag the first descriptor as the start of the packet. */
if(bFirst)
{
bFirst = false;
pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
}
else
{
pDesc->Desc.ui32CtrlStatus = 0;
}
pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_IP_ALL_CKHSUMS |
DES0_TX_CTRL_CHAINED);
/* Decrement our descriptor counter, move on to the next buffer in the
* pbuf chain. */
ui32NumChained--;
pBuf = pBuf->next;
/* Update the descriptor list write index. */
pIF->pTxDescList->ui32Write++;
if(pIF->pTxDescList->ui32Write == NUM_TX_DESCRIPTORS)
{
pIF->pTxDescList->ui32Write = 0;
}
/* If this is the last descriptor, mark it as the end of the packet. */
if(!ui32NumChained)
{
pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_LAST_SEG |
DES0_TX_CTRL_INTERRUPT);
/* Tag the descriptor with the original pbuf pointer. */
pDesc->pBuf = p;
}
else
{
/* Set the lsb of the pbuf pointer. We use this as a signal that
* we should not free the pbuf when we are walking the descriptor
* list while processing the transmit interrupt. We only free the
* pbuf when processing the last descriptor used to transmit its
* chain.
*/
pDesc->pBuf = (struct pbuf *)((uint32_t)p + 1);
}
DRIVER_STATS_INC(TXBufQueuedCount);
/* Hand the descriptor over to the hardware. */
pDesc->Desc.ui32CtrlStatus |= DES0_TX_CTRL_OWN;
}
/* Tell the transmitter to start (in case it had stopped). */
EMACTxDMAPollDemand(EMAC0_BASE);
/* Update lwIP statistics */
LINK_STATS_INC(link.xmit);
SYS_ARCH_UNPROTECT(lev);
return(RT_EOK);
}
/**
* This function will process all transmit descriptors and free pbufs attached
* to any that have been transmitted since we last checked.
*
* This function is called only from the Ethernet interrupt handler.
*
* @param netif the lwip network interface structure for this ethernetif
* @return None.
*/
static void
tivaif_process_transmit(tStellarisIF *pIF)
{
tDescriptorList *pDescList;
uint32_t ui32NumDescs;
/* Get a pointer to the transmit descriptor list. */
pDescList = pIF->pTxDescList;
/* Walk the list until we have checked all descriptors or we reach the
* write pointer or find a descriptor that the hardware is still working
* on.
*/
for(ui32NumDescs = 0; ui32NumDescs < pDescList->ui32NumDescs; ui32NumDescs++)
{
/* Has the buffer attached to this descriptor been transmitted? */
if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_TX_CTRL_OWN)
{
/* No - we're finished. */
break;
}
/* Does this descriptor have a buffer attached to it? */
if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
{
/* Yes - free it if it's not marked as an intermediate pbuf */
if(!((uint32_t)(pDescList->pDescriptors[pDescList->ui32Read].pBuf) & 1))
{
pbuf_free(pDescList->pDescriptors[pDescList->ui32Read].pBuf);
DRIVER_STATS_INC(TXBufFreedCount);
}
pDescList->pDescriptors[pDescList->ui32Read].pBuf = NULL;
}
else
{
/* If the descriptor has no buffer, we are finished. */
break;
}
/* Move on to the next descriptor. */
pDescList->ui32Read++;
if(pDescList->ui32Read == pDescList->ui32NumDescs)
{
pDescList->ui32Read = 0;
}
}
}
/**
* This function will process all receive descriptors that contain newly read
* data and pass complete frames up the lwIP stack as they are found. The
* timestamp of the packet will be placed into the pbuf structure if PTPD is
* enabled.
*
* This function is called only from the Ethernet interrupt handler.
*
* @param psNetif the lwip network interface structure for this ethernetif
* @return None.
*/
static void
tivaif_receive(net_device_t dev)
{
tDescriptorList *pDescList;
tStellarisIF *pIF;
static struct pbuf *pBuf = NULL;
uint32_t ui32DescEnd;
/* Get a pointer to our state data */
pIF = dev->dma_if;
/* Get a pointer to the receive descriptor list. */
pDescList = pIF->pRxDescList;
/* Start with a NULL pbuf so that we don't try to link chain the first
* time round.
*/
//pBuf = NULL;
/* Determine where we start and end our walk of the descriptor list */
ui32DescEnd = pDescList->ui32Read ? (pDescList->ui32Read - 1) : (pDescList->ui32NumDescs - 1);
/* Step through the descriptors that are marked for CPU attention. */
while(pDescList->ui32Read != ui32DescEnd)
{
/* Does the current descriptor have a buffer attached to it? */
if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
{
/* Yes - determine if the host has filled it yet. */
if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_RX_CTRL_OWN)
{
/* The DMA engine still owns the descriptor so we are finished */
break;
}
DRIVER_STATS_INC(RXBufReadCount);
/* If this descriptor contains the end of the packet, fix up the
* buffer size accordingly.
*/
if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_RX_STAT_LAST_DESC)
{
/* This is the last descriptor for the frame so fix up the
* length. It is safe for us to modify the internal fields
* directly here (rather than calling pbuf_realloc) since we
* know each of these pbufs is never chained.
*/
pDescList->pDescriptors[pDescList->ui32Read].pBuf->len =
(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_RX_STAT_FRAME_LENGTH_M) >>
DES0_RX_STAT_FRAME_LENGTH_S;
pDescList->pDescriptors[pDescList->ui32Read].pBuf->tot_len =
pDescList->pDescriptors[pDescList->ui32Read].pBuf->len;
}
if(pBuf)
{
/* Link this pbuf to the last one we looked at since this buffer
* is a continuation of an existing frame (split across multiple
* pbufs). Note that we use pbuf_cat() here rather than
* pbuf_chain() since we don't want to increase the reference
* count of either pbuf - we only want to link them together.
*/
pbuf_cat(pBuf, pDescList->pDescriptors[pDescList->ui32Read].pBuf);
pDescList->pDescriptors[pDescList->ui32Read].pBuf = pBuf;
}
/* Remember the buffer associated with this descriptor. */
pBuf = pDescList->pDescriptors[pDescList->ui32Read].pBuf;
/* Is this the last descriptor for the current frame? */
if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_RX_STAT_LAST_DESC)
{
/* Yes - does the frame contain errors? */
if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
DES0_RX_STAT_ERR)
{
/* This is a bad frame so discard it and update the relevant
* statistics.
*/
LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: packet error\n"));
pbuf_free(pBuf);
LINK_STATS_INC(link.drop);
DRIVER_STATS_INC(RXPacketErrCount);
pBuf = NULL;
}
else
{
/* This is a good frame so pass it up the stack. */
LINK_STATS_INC(link.recv);
DRIVER_STATS_INC(RXPacketReadCount);
#if LWIP_PTPD
/* Place the timestamp in the PBUF if PTPD is enabled */
pBuf->time_s =
pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeHi;
pBuf->time_ns =
pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeLo;
#endif
#if NO_SYS
if(ethernet_input(pBuf, psNetif) != RT_EOK)
#else
//if(tcpip_input(pBuf, psNetif) != ERR_OK)
if(rt_mb_send(dev->rx_pbuf_mb, (rt_uint32_t)pBuf) != RT_EOK)
#endif
{
/* drop the packet */
LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_input: input error\n"));
pbuf_free(pBuf);
/* Adjust the link statistics */
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
DRIVER_STATS_INC(RXPacketCBErrCount);
}
/* We're finished with this packet so make sure we don't try
* to link the next buffer to it.
*/
pBuf = NULL;
}
}
}
/* Allocate a new buffer for this descriptor */
pDescList->pDescriptors[pDescList->ui32Read].pBuf = pbuf_alloc(PBUF_RAW,
PBUF_POOL_BUFSIZE,
PBUF_POOL);
pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count =
DES1_RX_CTRL_CHAINED;
if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
{
/* We got a buffer so fill in the payload pointer and size. */
pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 =
pDescList->pDescriptors[pDescList->ui32Read].pBuf->payload;
pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count |=
(pDescList->pDescriptors[pDescList->ui32Read].pBuf->len <<
DES1_RX_CTRL_BUFF1_SIZE_S);
/* Give this descriptor back to the hardware */
pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus =
DES0_RX_CTRL_OWN;
}
else
{
LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: pbuf_alloc error\n"));
pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 = 0;
/* Update the stats to show we coui32dn't allocate a pbuf. */
DRIVER_STATS_INC(RXNoBufCount);
LINK_STATS_INC(link.memerr);
/* Stop parsing here since we can't leave a broken descriptor in
* the chain.
*/
break;
}
/* Move on to the next descriptor in the chain, taking care to wrap. */
pDescList->ui32Read++;
if(pDescList->ui32Read == pDescList->ui32NumDescs)
{
pDescList->ui32Read = 0;
}
}
}
/**
* Process interrupts from the PHY.
*
* should be called from the Stellaris Ethernet Interrupt Handler. This
* function will read packets from the Stellaris Ethernet fifo and place them
* into a pbuf queue. If the transmitter is idle and there is at least one packet
* on the transmit queue, it will place it in the transmit fifo and start the
* transmitter.
*
*/
void
tivaif_process_phy_interrupt(net_device_t dev)
{
uint16_t ui16Val, ui16Status;
#if EEE_SUPPORT
uint16_t ui16EEEStatus;
#endif
uint32_t ui32Config, ui32Mode, ui32RxMaxFrameSize;
/* Read the PHY interrupt status. This clears all interrupt sources.
* Note that we are only enabling sources in EPHY_MISR1 so we don't
* read EPHY_MISR2.
*/
ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
/* Read the current PHY status. */
ui16Status = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_STS);
/* If EEE mode support is requested then read the value of the Link
* partners status
*/
#if EEE_SUPPORT
ui16EEEStatus = EMACPHYMMDRead(EMAC0_BASE, PHY_PHYS_ADDR, 0x703D);
#endif
/* Has the link status changed? */
if(ui16Val & EPHY_MISR1_LINKSTAT)
{
/* Is link up or down now? */
if(ui16Status & EPHY_STS_LINK)
{
/* Tell lwIP the link is up. */
#if NO_SYS
netif_set_link_up(psNetif);
#else
//tcpip_callback((tcpip_callback_fn)netif_set_link_up, psNetif);
eth_device_linkchange(&(dev->parent), RT_TRUE);
#endif
/* if the link has been advertised as EEE capable then configure
* the MAC register for LPI timers and manually set the PHY link
* status bit
*/
#if EEE_SUPPORT
if(ui16EEEStatus & 0x2)
{
EMACLPIConfig(EMAC0_BASE, true, 1000, 36);
EMACLPILinkSet(EMAC0_BASE);
g_bEEELinkActive = true;
}
#endif
/* In this case we drop through since we may need to reconfigure
* the MAC depending upon the speed and half/fui32l-duplex settings.
*/
}
else
{
/* Tell lwIP the link is down */
#if NO_SYS
netif_set_link_down(psNetif);
#else
//tcpip_callback((tcpip_callback_fn)netif_set_link_down, psNetif);
eth_device_linkchange(&(dev->parent), RT_FALSE);
#endif
/* if the link has been advertised as EEE capable then clear the
* MAC register LPI timers and manually clear the PHY link status
* bit
*/
#if EEE_SUPPORT
g_bEEELinkActive = false;
EMACLPILinkClear(EMAC0_BASE);
EMACLPIConfig(EMAC0_BASE, false, 1000, 0);
#endif
}
}
/* Has the speed or duplex status changed? */
if(ui16Val & (EPHY_MISR1_SPEED | EPHY_MISR1_SPEED | EPHY_MISR1_ANC))
{
/* Get the current MAC configuration. */
EMACConfigGet(EMAC0_BASE, &ui32Config, &ui32Mode,
&ui32RxMaxFrameSize);
/* What speed is the interface running at now?
*/
if(ui16Status & EPHY_STS_SPEED)
{
/* 10Mbps is selected */
ui32Config &= ~EMAC_CONFIG_100MBPS;
}
else
{
/* 100Mbps is selected */
ui32Config |= EMAC_CONFIG_100MBPS;
}
/* Are we in fui32l- or half-duplex mode? */
if(ui16Status & EPHY_STS_DUPLEX)
{
/* Fui32l duplex. */
ui32Config |= EMAC_CONFIG_FULL_DUPLEX;
}
else
{
/* Half duplex. */
ui32Config &= ~EMAC_CONFIG_FULL_DUPLEX;
}
/* Reconfigure the MAC */
EMACConfigSet(EMAC0_BASE, ui32Config, ui32Mode, ui32RxMaxFrameSize);
}
}
/**
* Process tx and rx packets at the low-level interrupt.
*
* should be called from the Stellaris Ethernet Interrupt Handler. This
* function will read packets from the Stellaris Ethernet fifo and place them
* into a pbuf queue. If the transmitter is idle and there is at least one packet
* on the transmit queue, it will place it in the transmit fifo and start the
* transmitter.
*
*/
void
tivaif_interrupt(net_device_t dev, uint32_t ui32Status)
{
/* Update our debug interrupt counters. */
if(ui32Status & EMAC_INT_NORMAL_INT)
{
g_ui32NormalInts++;
}
if(ui32Status & EMAC_INT_ABNORMAL_INT)
{
g_ui32AbnormalInts++;
}
/* Is this an interrupt from the PHY? */
if(ui32Status & EMAC_INT_PHY)
{
tivaif_process_phy_interrupt(dev);
}
/* Process the transmit DMA list, freeing any buffers that have been
* transmitted since our last interrupt.
*/
if(ui32Status & EMAC_INT_TRANSMIT)
{
#if EEE_SUPPORT
if(g_bEEELinkActive)
{
EMACLPIEnter(EMAC0_BASE);
}
#endif
tivaif_process_transmit(dev->dma_if);
}
/**
* Process the receive DMA list and pass all successfully received packets
* up the stack. We also call this function in cases where the receiver has
* stalled due to missing buffers since the receive function will attempt to
* allocate new pbufs for descriptor entries which have none.
*/
if(ui32Status & (EMAC_INT_RECEIVE | EMAC_INT_RX_NO_BUFFER |
EMAC_INT_RX_STOPPED))
{
tivaif_receive(dev);
}
}
#if NETIF_DEBUG
/* Print an IP header by using LWIP_DEBUGF
* @param p an IP packet, p->payload pointing to the IP header
*/
void
tivaif_debug_print(struct pbuf *p)
{
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
u16_t *plen = (u16_t *)p->payload;
LWIP_DEBUGF(NETIF_DEBUG, ("ETH header:\n"));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Length:%5"U16_F" \n",*plen));
LWIP_DEBUGF(NETIF_DEBUG, ("Destination: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->dest.addr[0],
ethhdr->dest.addr[1],
ethhdr->dest.addr[2],
ethhdr->dest.addr[3],
ethhdr->dest.addr[4],
ethhdr->dest.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Source: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->src.addr[0],
ethhdr->src.addr[1],
ethhdr->src.addr[2],
ethhdr->src.addr[3],
ethhdr->src.addr[4],
ethhdr->src.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Type:0x%04"U16_F" \n", ethhdr->type));
}
#endif /* NETIF_DEBUG */
void lwIPEthernetIntHandler(void)
{
uint32_t ui32Status;
#ifdef DEF_INT_TEMPSTAMP
uint32_t ui32TimerStatus;
#endif
//
// Read and Clear the interrupt.
//
ui32Status = MAP_EMACIntStatus(EMAC0_BASE, true);
//
// If the interrupt really came from the Ethernet and not our
// timer, clear it.
//
if(ui32Status)
{
MAP_EMACIntClear(EMAC0_BASE, ui32Status);
}
#ifdef DEF_INT_TEMPSTAMP
//
// Check to see whether a hardware timer interrupt has been reported.
//
if(ui32Status & EMAC_INT_TIMESTAMP)
{
//
// Yes - read and clear the timestamp interrupt status.
//
ui32TimerStatus = EMACTimestampIntStatus(EMAC0_BASE);
//
// If a timer interrupt handler has been registered, call it.
//
if(g_pfnTimerHandler)
{
g_pfnTimerHandler(EMAC0_BASE, ui32TimerStatus);
}
}
#endif
//
// The handling of the interrupt is different based on the use of a RTOS.
//
//
// No RTOS is being used. If a transmit/receive interrupt was active,
// run the low-level interrupt handler.
//
if(ui32Status)
{
tivaif_interrupt(eth_dev, ui32Status);
}
//
// Service the lwIP timers.
//
//lwIPServiceTimers();
}
// OUI:00-12-37 (hex) Texas Instruments, only for test
static int tiva_eth_mac_addr_init(void)
{
int retVal =0;
uint32_t ulUser[2];
uint8_t mac_addr[6];
MAP_FlashUserGet(&ulUser[0], &ulUser[1]);
if((ulUser[0] == 0xffffffff) || (ulUser[1] == 0xffffffff))
{
rt_kprintf("Fail to get mac address from eeprom.\n");
rt_kprintf("Using default mac address\n");
// OUI:00-12-37 (hex) Texas Instruments, only for test
// Configure the hardware MAC address
ulUser[0] = 0x00371200;
ulUser[1] = 0x00563412;
//FlashUserSet(ulUser0, ulUser1);
retVal =-1;
}
//Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC
//address needed to program the hardware registers, then program the MAC
//address into the Ethernet Controller registers.
mac_addr[0] = ((ulUser[0] >> 0) & 0xff);
mac_addr[1] = ((ulUser[0] >> 8) & 0xff);
mac_addr[2] = ((ulUser[0] >> 16) & 0xff);
mac_addr[3] = ((ulUser[1] >> 0) & 0xff);
mac_addr[4] = ((ulUser[1] >> 8) & 0xff);
mac_addr[5] = ((ulUser[1] >> 16) & 0xff);
//
// Program the hardware with its MAC address (for filtering).
//
MAP_EMACAddrSet(EMAC0_BASE, 0, mac_addr);
return retVal;
}
void tiva_eth_lowlevel_init(void)
{
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//
// PF1/PK4/PK6 are used for Ethernet LEDs.
//
MAP_GPIOPinConfigure(GPIO_PF0_EN0LED0);
MAP_GPIOPinConfigure(GPIO_PF4_EN0LED1);
GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_0);
GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_4);
//
// Enable the ethernet peripheral.
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EMAC0);
MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EMAC0);
//
// Enable the internal PHY if it's present and we're being
// asked to use it.
//
if((EMAC_PHY_CONFIG & EMAC_PHY_TYPE_MASK) == EMAC_PHY_TYPE_INTERNAL)
{
//
// We've been asked to configure for use with the internal
// PHY. Is it present?
//
if(SysCtlPeripheralPresent(SYSCTL_PERIPH_EPHY0))
{
//
// Yes - enable and reset it.
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EPHY0);
MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
}
else
{
//
// Internal PHY is not present on this part so hang here.
//
rt_kprintf("Internal PHY is not present on this part.\n");
while(1)
{
}
}
}
//
// Wait for the MAC to come out of reset.
//
while(!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_EMAC0))
{
}
//
// Configure for use with whichever PHY the user requires.
//
MAP_EMACPHYConfigSet(EMAC0_BASE, EMAC_PHY_CONFIG);
//
// Initialize the MAC and set the DMA mode.
//
MAP_EMACInit(EMAC0_BASE, 120000000, //system clock = 120MHz
EMAC_BCONFIG_MIXED_BURST | EMAC_BCONFIG_PRIORITY_FIXED,
4, 4, 0);
//
// Set MAC configuration options.
//
MAP_EMACConfigSet(EMAC0_BASE, (EMAC_CONFIG_FULL_DUPLEX |
EMAC_CONFIG_CHECKSUM_OFFLOAD |
EMAC_CONFIG_7BYTE_PREAMBLE |
EMAC_CONFIG_IF_GAP_96BITS |
EMAC_CONFIG_USE_MACADDR0 |
EMAC_CONFIG_SA_FROM_DESCRIPTOR |
EMAC_CONFIG_BO_LIMIT_1024),
(EMAC_MODE_RX_STORE_FORWARD |
EMAC_MODE_TX_STORE_FORWARD |
EMAC_MODE_TX_THRESHOLD_64_BYTES |
EMAC_MODE_RX_THRESHOLD_64_BYTES), 0);
EMACIntRegister(EMAC0_BASE, lwIPEthernetIntHandler);
}
static rt_err_t eth_dev_init(rt_device_t device)
{
net_device_t net_dev = (net_device_t)device;
struct netif *psNetif = (net_dev->parent.netif);
LWIP_ASSERT("psNetif != NULL", (psNetif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
psNetif->hostname = "t4mc";
#endif /* LWIP_NETIF_HOSTNAME */
/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
//NETIF_INIT_SNMP(psNetif, snmp_ifType_ethernet_csmacd, 1000000);
net_dev->dma_if = &g_StellarisIFData;
/* Remember our MAC address. */
g_StellarisIFData.ethaddr = (struct eth_addr *)&(psNetif->hwaddr[0]);
/* Initialize the hardware */
tivaif_hwinit(psNetif);
return RT_EOK;
}
/* control the interface */
static rt_err_t eth_dev_control(rt_device_t dev, int cmd, void *args)
{
switch(cmd)
{
case NIOCTL_GADDR:
/* get mac address */
if(args)
MAP_EMACAddrGet(EMAC0_BASE, 0, (uint8_t*)args);
else
return -RT_ERROR;
break;
default :
break;
}
return RT_EOK;
}
/* Open the interface */
static rt_err_t eth_dev_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
/* Close the interface */
static rt_err_t eth_dev_close(rt_device_t dev)
{
return RT_EOK;
}
/* Read */
static rt_ssize_t eth_dev_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
rt_set_errno(-RT_ENOSYS);
return 0;
}
/* Write */
static rt_ssize_t eth_dev_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 eth_dev_tx(rt_device_t dev, struct pbuf *p)
{
return tivaif_transmit((net_device_t)dev, p);
}
static struct pbuf* eth_dev_rx(rt_device_t dev)
{
rt_err_t result;
rt_uint32_t temp =0;
net_device_t net_dev = (net_device_t)dev;
result = rt_mb_recv(net_dev->rx_pbuf_mb, (rt_ubase_t *)&temp, RT_WAITING_NO);
return (result == RT_EOK)? (struct pbuf*)temp : RT_NULL;
}
int rt_hw_tiva_eth_init(void)
{
rt_err_t result;
/* Clock GPIO and etc */
tiva_eth_lowlevel_init();
tiva_eth_mac_addr_init();
/* init rt-thread device interface */
eth_dev->parent.parent.init = eth_dev_init;
eth_dev->parent.parent.open = eth_dev_open;
eth_dev->parent.parent.close = eth_dev_close;
eth_dev->parent.parent.read = eth_dev_read;
eth_dev->parent.parent.write = eth_dev_write;
eth_dev->parent.parent.control = eth_dev_control;
eth_dev->parent.eth_rx = eth_dev_rx;
eth_dev->parent.eth_tx = eth_dev_tx;
result = rt_mb_init(ð_rx_pbuf_mb, "epbuf",
&rx_pbuf_mb_pool[0], sizeof(rx_pbuf_mb_pool)/4,
RT_IPC_FLAG_FIFO);
RT_ASSERT(result == RT_EOK);
eth_dev->rx_pbuf_mb = ð_rx_pbuf_mb;
result = eth_device_init(&(eth_dev->parent), "e0");
return result;
}
// eth_device_init using malloc
// We use INIT_COMPONENT_EXPORT insted of INIT_BOARD_EXPORT
INIT_COMPONENT_EXPORT(rt_hw_tiva_eth_init);
#if 0
#ifdef RT_USING_FINSH
#include "finsh.h"
void PHY_Read(uint8_t addr)
{
uint16_t data = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, addr);
rt_kprintf("R PHY_REG[0x%02X] = 0x%04X\n", addr, data);
}
FINSH_FUNCTION_EXPORT(PHY_Read, (add));
void PHY_Write(uint8_t addr , uint16_t data)
{
EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, addr, data);
rt_kprintf("W PHY_REG[0x%02X] = 0x%04X\n", addr, data);
}
FINSH_FUNCTION_EXPORT(PHY_Write, (add, data));
void PHY_SetAdd(uint8_t addr0, uint8_t addr1, uint8_t addr2,
uint8_t addr3, uint8_t addr4, uint8_t addr5)
{
uint32_t ulUser[2];
ulUser[0] = (((addr2<<8)|addr1)<<8)|addr0;
ulUser[1] = (((addr5<<8)|addr4)<<8)|addr3;
MAP_FlashUserSet(ulUser[0], ulUser[1]);
MAP_FlashUserSave();
rt_kprintf("Save to EEPROM. please reboot.");
}
FINSH_FUNCTION_EXPORT(PHY_SetAdd, (add0-add5));
#endif //RT_USING_FINSH
#endif
|