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
|
/**************************************************************************//**
* @file emac.c
* @version V1.00
* @brief M480 EMAC driver source file
*
* SPDX-License-Identifier: Apache-2.0
* @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "NuMicro.h"
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup EMAC_Driver EMAC Driver
@{
*/
/* Below are structure, definitions, static variables used locally by EMAC driver and does not want to parse by doxygen unless HIDDEN_SYMBOLS is defined */
/** @cond HIDDEN_SYMBOLS */
/** @addtogroup EMAC_EXPORTED_CONSTANTS EMAC Exported Constants
@{
*/
/* PHY Register Description */
#define PHY_CNTL_REG 0x00UL /*!< PHY control register address */
#define PHY_STATUS_REG 0x01UL /*!< PHY status register address */
#define PHY_ID1_REG 0x02UL /*!< PHY ID1 register */
#define PHY_ID2_REG 0x03UL /*!< PHY ID2 register */
#define PHY_ANA_REG 0x04UL /*!< PHY auto-negotiation advertisement register */
#define PHY_ANLPA_REG 0x05UL /*!< PHY auto-negotiation link partner availability register */
#define PHY_ANE_REG 0x06UL /*!< PHY auto-negotiation expansion register */
/* PHY Control Register */
#define PHY_CNTL_RESET_PHY (1UL << 15UL)
#define PHY_CNTL_DR_100MB (1UL << 13UL)
#define PHY_CNTL_ENABLE_AN (1UL << 12UL)
#define PHY_CNTL_POWER_DOWN (1UL << 11UL)
#define PHY_CNTL_RESTART_AN (1UL << 9UL)
#define PHY_CNTL_FULLDUPLEX (1UL << 8UL)
/* PHY Status Register */
#define PHY_STATUS_AN_COMPLETE (1UL << 5UL)
#define PHY_STATUS_LINK_VALID (1UL << 2UL)
/* PHY Auto-negotiation Advertisement Register */
#define PHY_ANA_DR100_TX_FULL (1UL << 8UL)
#define PHY_ANA_DR100_TX_HALF (1UL << 7UL)
#define PHY_ANA_DR10_TX_FULL (1UL << 6UL)
#define PHY_ANA_DR10_TX_HALF (1UL << 5UL)
#define PHY_ANA_IEEE_802_3_CSMA_CD (1UL << 0UL)
/* PHY Auto-negotiation Link Partner Advertisement Register */
#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)
/* EMAC Tx/Rx descriptor's owner bit */
#define EMAC_DESC_OWN_EMAC 0x80000000UL /*!< Set owner to EMAC */
#define EMAC_DESC_OWN_CPU 0x00000000UL /*!< Set owner to CPU */
/* Rx Frame Descriptor Status */
#define EMAC_RXFD_RTSAS 0x0080UL /*!< Time Stamp Available */
#define EMAC_RXFD_RP 0x0040UL /*!< Runt Packet */
#define EMAC_RXFD_ALIE 0x0020UL /*!< Alignment Error */
#define EMAC_RXFD_RXGD 0x0010UL /*!< Receiving Good packet received */
#define EMAC_RXFD_PTLE 0x0008UL /*!< Packet Too Long Error */
#define EMAC_RXFD_CRCE 0x0002UL /*!< CRC Error */
#define EMAC_RXFD_RXINTR 0x0001UL /*!< Interrupt on receive */
/* Tx Frame Descriptor's Control bits */
#define EMAC_TXFD_TTSEN 0x08UL /*!< Tx time stamp enable */
#define EMAC_TXFD_INTEN 0x04UL /*!< Tx interrupt enable */
#define EMAC_TXFD_CRCAPP 0x02UL /*!< Append CRC */
#define EMAC_TXFD_PADEN 0x01UL /*!< Padding mode enable */
/* Tx Frame Descriptor Status */
#define EMAC_TXFD_TXINTR 0x0001UL /*!< Interrupt on Transmit */
#define EMAC_TXFD_DEF 0x0002UL /*!< Transmit deferred */
#define EMAC_TXFD_TXCP 0x0008UL /*!< Transmission Completion */
#define EMAC_TXFD_EXDEF 0x0010UL /*!< Exceed Deferral */
#define EMAC_TXFD_NCS 0x0020UL /*!< No Carrier Sense Error */
#define EMAC_TXFD_TXABT 0x0040UL /*!< Transmission Abort */
#define EMAC_TXFD_LC 0x0080UL /*!< Late Collision */
#define EMAC_TXFD_TXHA 0x0100UL /*!< Transmission halted */
#define EMAC_TXFD_PAU 0x0200UL /*!< Paused */
#define EMAC_TXFD_SQE 0x0400UL /*!< SQE error */
#define EMAC_TXFD_TTSAS 0x0800UL /*!< Time Stamp available */
/*@}*/ /* end of group EMAC_EXPORTED_CONSTANTS */
/** @addtogroup EMAC_EXPORTED_TYPEDEF EMAC Exported Type Defines
@{
*/
/** Tx/Rx buffer descriptor structure */
typedef struct
{
uint32_t u32Status1; /*!< Status word 1 */
uint32_t u32Data; /*!< Pointer to data buffer */
uint32_t u32Status2; /*!< Status word 2 */
uint32_t u32Next; /*!< Pointer to next descriptor */
uint32_t u32Backup1; /*!< For backup descriptor fields over written by time stamp */
uint32_t u32Backup2; /*!< For backup descriptor fields over written by time stamp */
} EMAC_DESCRIPTOR_T;
/** Tx/Rx buffer structure */
typedef struct
{
uint8_t au8Buf[EMAC_MAX_PKT_SIZE];
} EMAC_FRAME_T;
/*@}*/ /* end of group EMAC_EXPORTED_TYPEDEF */
/* local variables */
static volatile EMAC_DESCRIPTOR_T rx_desc[EMAC_RX_DESC_SIZE];
static volatile EMAC_FRAME_T rx_buf[EMAC_RX_DESC_SIZE];
static volatile EMAC_DESCRIPTOR_T tx_desc[EMAC_TX_DESC_SIZE];
static volatile EMAC_FRAME_T tx_buf[EMAC_TX_DESC_SIZE];
static uint32_t u32CurrentTxDesc, u32NextTxDesc, u32CurrentRxDesc;
static uint32_t s_u32EnableTs = 0UL;
static void EMAC_MdioWrite(uint32_t u32Reg, uint32_t u32Addr, uint32_t u32Data);
static uint32_t EMAC_MdioRead(uint32_t u32Reg, uint32_t u32Addr);
static void EMAC_TxDescInit(void);
static void EMAC_RxDescInit(void);
static uint32_t EMAC_Subsec2Nsec(uint32_t subsec);
static uint32_t EMAC_Nsec2Subsec(uint32_t nsec);
/** @addtogroup EMAC_EXPORTED_FUNCTIONS EMAC Exported Functions
@{
*/
/**
* @brief Write PHY register
* @param[in] u32Reg PHY register number
* @param[in] u32Addr PHY address, this address is board dependent
* @param[in] u32Data data to write to PHY register
* @return None
*/
static void EMAC_MdioWrite(uint32_t u32Reg, uint32_t u32Addr, uint32_t u32Data)
{
/* Set data register */
EMAC->MIIMDAT = u32Data ;
/* Set PHY address, PHY register address, busy bit and write bit */
EMAC->MIIMCTL = u32Reg | (u32Addr << 8) | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_WRITE_Msk | EMAC_MIIMCTL_MDCON_Msk;
/* Wait write complete by polling busy bit. */
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk)
{
;
}
}
/**
* @brief Read PHY register
* @param[in] u32Reg PHY register number
* @param[in] u32Addr PHY address, this address is board dependent
* @return Value read from PHY register
*/
static uint32_t EMAC_MdioRead(uint32_t u32Reg, uint32_t u32Addr)
{
/* Set PHY address, PHY register address, busy bit */
EMAC->MIIMCTL = u32Reg | (u32Addr << EMAC_MIIMCTL_PHYADDR_Pos) | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_MDCON_Msk;
/* Wait read complete by polling busy bit */
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk)
{
;
}
/* Get return data */
return EMAC->MIIMDAT;
}
/**
* @brief Initialize PHY chip, check for the auto-negotiation result.
* @param None
* @return None
*/
void EMAC_PhyInit(void)
{
uint32_t reg;
uint32_t i = 0UL;
/* Reset Phy Chip */
EMAC_MdioWrite(PHY_CNTL_REG, EMAC_PHY_ADDR, PHY_CNTL_RESET_PHY);
/* Wait until reset complete */
while (1)
{
reg = EMAC_MdioRead(PHY_CNTL_REG, EMAC_PHY_ADDR) ;
if ((reg & PHY_CNTL_RESET_PHY) == 0UL)
{
break;
}
}
while (!(EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR) & PHY_STATUS_LINK_VALID))
{
if (i++ > 10000UL) /* Cable not connected */
{
EMAC->CTL &= ~EMAC_CTL_OPMODE_Msk;
EMAC->CTL &= ~EMAC_CTL_FUDUP_Msk;
break;
}
}
if (i <= 10000UL)
{
/* Configure auto negotiation capability */
EMAC_MdioWrite(PHY_ANA_REG, EMAC_PHY_ADDR, PHY_ANA_DR100_TX_FULL |
PHY_ANA_DR100_TX_HALF |
PHY_ANA_DR10_TX_FULL |
PHY_ANA_DR10_TX_HALF |
PHY_ANA_IEEE_802_3_CSMA_CD);
/* Restart auto negotiation */
EMAC_MdioWrite(PHY_CNTL_REG, EMAC_PHY_ADDR, EMAC_MdioRead(PHY_CNTL_REG, EMAC_PHY_ADDR) | PHY_CNTL_RESTART_AN);
/* Wait for auto-negotiation complete */
while (!(EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR) & PHY_STATUS_AN_COMPLETE))
{
;
}
/* Check link valid again. Some PHYs needs to check result after link valid bit set */
while (!(EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR) & PHY_STATUS_LINK_VALID))
{
;
}
/* Check link partner capability */
reg = EMAC_MdioRead(PHY_ANLPA_REG, EMAC_PHY_ADDR) ;
if (reg & PHY_ANLPA_DR100_TX_FULL)
{
EMAC->CTL |= EMAC_CTL_OPMODE_Msk;
EMAC->CTL |= EMAC_CTL_FUDUP_Msk;
}
else if (reg & PHY_ANLPA_DR100_TX_HALF)
{
EMAC->CTL |= EMAC_CTL_OPMODE_Msk;
EMAC->CTL &= ~EMAC_CTL_FUDUP_Msk;
}
else if (reg & PHY_ANLPA_DR10_TX_FULL)
{
EMAC->CTL &= ~EMAC_CTL_OPMODE_Msk;
EMAC->CTL |= EMAC_CTL_FUDUP_Msk;
}
else
{
EMAC->CTL &= ~EMAC_CTL_OPMODE_Msk;
EMAC->CTL &= ~EMAC_CTL_FUDUP_Msk;
}
}
}
/**
* @brief Initial EMAC Tx descriptors and get Tx descriptor base address
* @param None
* @return None
*/
static void EMAC_TxDescInit(void)
{
uint32_t i;
/* Get Frame descriptor's base address. */
EMAC->TXDSA = (uint32_t)&tx_desc[0];
u32NextTxDesc = u32CurrentTxDesc = (uint32_t)&tx_desc[0];
for (i = 0UL; i < EMAC_TX_DESC_SIZE; i++)
{
if (s_u32EnableTs)
{
tx_desc[i].u32Status1 = EMAC_TXFD_PADEN | EMAC_TXFD_CRCAPP | EMAC_TXFD_INTEN;
}
else
{
tx_desc[i].u32Status1 = EMAC_TXFD_PADEN | EMAC_TXFD_CRCAPP | EMAC_TXFD_INTEN | EMAC_TXFD_TTSEN;
}
tx_desc[i].u32Data = (uint32_t)((uint32_t)&tx_buf[i]);
tx_desc[i].u32Backup1 = tx_desc[i].u32Data;
tx_desc[i].u32Status2 = 0UL;
tx_desc[i].u32Next = (uint32_t)&tx_desc[(i + 1UL) % EMAC_TX_DESC_SIZE];
tx_desc[i].u32Backup2 = tx_desc[i].u32Next;
}
}
/**
* @brief Initial EMAC Rx descriptors and get Rx descriptor base address
* @param None
* @return None
*/
static void EMAC_RxDescInit(void)
{
uint32_t i;
/* Get Frame descriptor's base address. */
EMAC->RXDSA = (uint32_t)&rx_desc[0];
u32CurrentRxDesc = (uint32_t)&rx_desc[0];
for (i = 0UL; i < EMAC_RX_DESC_SIZE; i++)
{
rx_desc[i].u32Status1 = EMAC_DESC_OWN_EMAC;
rx_desc[i].u32Data = (uint32_t)((uint32_t)&rx_buf[i]);
rx_desc[i].u32Backup1 = rx_desc[i].u32Data;
rx_desc[i].u32Status2 = 0UL;
rx_desc[i].u32Next = (uint32_t)&rx_desc[(i + 1UL) % EMAC_RX_DESC_SIZE];
rx_desc[i].u32Backup2 = rx_desc[i].u32Next;
}
}
/**
* @brief Convert subsecond value to nano second
* @param[in] subsec Subsecond value to be convert
* @return Nano second
*/
static uint32_t EMAC_Subsec2Nsec(uint32_t subsec)
{
/* 2^31 subsec == 10^9 ns */
uint64_t i;
i = 1000000000ull * (uint64_t)subsec;
i >>= 31;
return ((uint32_t)i);
}
/**
* @brief Convert nano second to subsecond value
* @param[in] nsec Nano second to be convert
* @return Subsecond
*/
static uint32_t EMAC_Nsec2Subsec(uint32_t nsec)
{
/* 10^9 ns = 2^31 subsec */
uint64_t i;
i = (1ull << 31) * nsec;
i /= 1000000000ull;
return ((uint32_t)i);
}
/*@}*/ /* end of group EMAC_EXPORTED_FUNCTIONS */
/** @endcond HIDDEN_SYMBOLS */
/** @addtogroup EMAC_EXPORTED_FUNCTIONS EMAC Exported Functions
@{
*/
/**
* @brief Initialize EMAC interface, including descriptors, MAC address, and PHY.
* @param[in] pu8MacAddr Pointer to uint8_t array holds MAC address
* @return None
* @note This API configures EMAC to receive all broadcast and multicast packets, but could configure to other settings with
* \ref EMAC_ENABLE_RECV_BCASTPKT, \ref EMAC_DISABLE_RECV_BCASTPKT, \ref EMAC_ENABLE_RECV_MCASTPKT, and \ref EMAC_DISABLE_RECV_MCASTPKT
* @note Receive(RX) and transmit(TX) are not enabled yet, application must call \ref EMAC_ENABLE_RX and \ref EMAC_ENABLE_TX to
* enable receive and transmit function.
*/
void EMAC_Open(uint8_t *pu8MacAddr)
{
/* Enable transmit and receive descriptor */
EMAC_TxDescInit();
EMAC_RxDescInit();
/* Set the CAM Control register and the MAC address value */
EMAC_SetMacAddr(pu8MacAddr);
/* Configure the MAC interrupt enable register. */
EMAC->INTEN = EMAC_INTEN_RXIEN_Msk |
EMAC_INTEN_TXIEN_Msk |
EMAC_INTEN_RXGDIEN_Msk |
EMAC_INTEN_TXCPIEN_Msk |
EMAC_INTEN_RXBEIEN_Msk |
EMAC_INTEN_TXBEIEN_Msk |
EMAC_INTEN_RDUIEN_Msk |
EMAC_INTEN_TSALMIEN_Msk |
EMAC_INTEN_WOLIEN_Msk;
/* Configure the MAC control register. */
EMAC->CTL = EMAC_CTL_STRIPCRC_Msk |
EMAC_CTL_RMIIEN_Msk;
/* Accept packets for us and all broadcast and multicast packets */
EMAC->CAMCTL = EMAC_CAMCTL_CMPEN_Msk |
EMAC_CAMCTL_AMP_Msk |
EMAC_CAMCTL_ABP_Msk;
/* Limit the max receive frame length to 1514 + 4 */
EMAC->MRFL = EMAC_MAX_PKT_SIZE;
}
/**
* @brief This function stop all receive and transmit activity and disable MAC interface
* @param None
* @return None
*/
void EMAC_Close(void)
{
EMAC->CTL |= EMAC_CTL_RST_Msk;
while (EMAC->CTL & EMAC_CTL_RST_Msk) {}
}
/**
* @brief Set the device MAC address
* @param[in] pu8MacAddr Pointer to uint8_t array holds MAC address
* @return None
*/
void EMAC_SetMacAddr(uint8_t *pu8MacAddr)
{
EMAC_EnableCamEntry(0UL, pu8MacAddr);
}
/**
* @brief Fill a CAM entry for MAC address comparison.
* @param[in] u32Entry MAC entry to fill. Entry 0 is used to store device MAC address, do not overwrite the setting in it.
* @param[in] pu8MacAddr Pointer to uint8_t array holds MAC address
* @return None
*/
void EMAC_EnableCamEntry(uint32_t u32Entry, uint8_t pu8MacAddr[])
{
uint32_t u32Lsw, u32Msw;
uint32_t reg;
u32Lsw = (uint32_t)(((uint32_t)pu8MacAddr[4] << 24) |
((uint32_t)pu8MacAddr[5] << 16));
u32Msw = (uint32_t)(((uint32_t)pu8MacAddr[0] << 24) |
((uint32_t)pu8MacAddr[1] << 16) |
((uint32_t)pu8MacAddr[2] << 8) |
(uint32_t)pu8MacAddr[3]);
reg = (uint32_t)&EMAC->CAM0M + u32Entry * 2UL * 4UL;
*(uint32_t volatile *)reg = u32Msw;
reg = (uint32_t)&EMAC->CAM0L + u32Entry * 2UL * 4UL;
*(uint32_t volatile *)reg = u32Lsw;
EMAC->CAMEN |= (1UL << u32Entry);
}
/**
* @brief Disable a specified CAM entry
* @param[in] u32Entry CAM entry to be disabled
* @return None
*/
void EMAC_DisableCamEntry(uint32_t u32Entry)
{
EMAC->CAMEN &= ~(1UL << u32Entry);
}
/**
* @brief Receive an Ethernet packet
* @param[in] pu8Data Pointer to a buffer to store received packet (4 byte CRC removed)
* @param[in] pu32Size Received packet size (without 4 byte CRC).
* @return Packet receive success or not
* @retval 0 No packet available for receive
* @retval 1 A packet is received
* @note Return 0 doesn't guarantee the packet will be sent and received successfully.
*/
uint32_t EMAC_RecvPkt(uint8_t *pu8Data, uint32_t *pu32Size)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status, reg;
uint32_t u32Count = 0UL;
/* Clear Rx interrupt flags */
reg = EMAC->INTSTS;
EMAC->INTSTS = reg & 0xFFFFUL; /* Clear all RX related interrupt status */
if (reg & EMAC_INTSTS_RXBEIF_Msk)
{
/* Bus error occurred, this is usually a bad sign about software bug and will occur again... */
while (1) {}
}
else
{
/* Get Rx Frame Descriptor */
desc = (EMAC_DESCRIPTOR_T *)u32CurrentRxDesc;
/* If we reach last recv Rx descriptor, leave the loop */
if ((desc->u32Status1 & EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC) /* ownership=CPU */
{
status = desc->u32Status1 >> 16;
/* If Rx frame is good, process received frame */
if (status & EMAC_RXFD_RXGD)
{
/* lower 16 bit in descriptor status1 stores the Rx packet length */
*pu32Size = desc->u32Status1 & 0xFFFFUL;
memcpy(pu8Data, (uint8_t *)desc->u32Backup1, *pu32Size);
u32Count = 1UL;
}
else
{
/* Save Error status if necessary */
if (status & EMAC_RXFD_RP) {}
if (status & EMAC_RXFD_ALIE) {}
if (status & EMAC_RXFD_PTLE) {}
if (status & EMAC_RXFD_CRCE) {}
}
}
}
return (u32Count);
}
/**
* @brief Receive an Ethernet packet and the time stamp while it's received
* @param[out] pu8Data Pointer to a buffer to store received packet (4 byte CRC removed)
* @param[out] pu32Size Received packet size (without 4 byte CRC).
* @param[out] pu32Sec Second value while packet received
* @param[out] pu32Nsec Nano second value while packet received
* @return Packet receive success or not
* @retval 0 No packet available for receive
* @retval 1 A packet is received
* @note Return 0 doesn't guarantee the packet will be sent and received successfully.
* @note Largest Ethernet packet is 1514 bytes after stripped CRC, application must give
* a buffer large enough to store such packet
*/
uint32_t EMAC_RecvPktTS(uint8_t *pu8Data, uint32_t *pu32Size, uint32_t *pu32Sec, uint32_t *pu32Nsec)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status, reg;
uint32_t u32Count = 0UL;
/* Clear Rx interrupt flags */
reg = EMAC->INTSTS;
EMAC->INTSTS = reg & 0xFFFFUL; /* Clear all Rx related interrupt status */
if (reg & EMAC_INTSTS_RXBEIF_Msk)
{
/* Bus error occurred, this is usually a bad sign about software bug and will occur again... */
while (1) {}
}
else
{
/* Get Rx Frame Descriptor */
desc = (EMAC_DESCRIPTOR_T *)u32CurrentRxDesc;
/* If we reach last recv Rx descriptor, leave the loop */
if (EMAC->CRXDSA != (uint32_t)desc)
{
if ((desc->u32Status1 | EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC) /* ownership=CPU */
{
status = desc->u32Status1 >> 16;
/* If Rx frame is good, process received frame */
if (status & EMAC_RXFD_RXGD)
{
/* lower 16 bit in descriptor status1 stores the Rx packet length */
*pu32Size = desc->u32Status1 & 0xFFFFUL;
memcpy(pu8Data, (uint8_t *)desc->u32Backup1, *pu32Size);
*pu32Sec = desc->u32Next; /* second stores in descriptor's NEXT field */
*pu32Nsec = EMAC_Subsec2Nsec(desc->u32Data); /* Sub nano second store in DATA field */
u32Count = 1UL;
}
else
{
/* Save Error status if necessary */
if (status & EMAC_RXFD_RP) {}
if (status & EMAC_RXFD_ALIE) {}
if (status & EMAC_RXFD_PTLE) {}
if (status & EMAC_RXFD_CRCE) {}
}
}
}
}
return (u32Count);
}
/**
* @brief Clean up process after a packet is received
* @param None
* @return None
* @details EMAC Rx interrupt service routine \b must call this API to release the resource use by receive process
* @note Application can only call this function once every time \ref EMAC_RecvPkt or \ref EMAC_RecvPktTS returns 1
*/
void EMAC_RecvPktDone(void)
{
EMAC_DESCRIPTOR_T *desc;
/* Get Rx Frame Descriptor */
desc = (EMAC_DESCRIPTOR_T *)u32CurrentRxDesc;
/* Restore descriptor link list and data pointer they will be overwrite if time stamp enabled */
desc->u32Data = desc->u32Backup1;
desc->u32Next = desc->u32Backup2;
/* Change ownership to DMA for next use */
desc->u32Status1 |= EMAC_DESC_OWN_EMAC;
/* Get Next Frame Descriptor pointer to process */
desc = (EMAC_DESCRIPTOR_T *)desc->u32Next;
/* Save last processed Rx descriptor */
u32CurrentRxDesc = (uint32_t)desc;
EMAC_TRIGGER_RX();
}
/**
* @brief Send an Ethernet packet
* @param[in] pu8Data Pointer to a buffer holds the packet to transmit
* @param[in] u32Size Packet size (without 4 byte CRC).
* @return Packet transmit success or not
* @retval 0 Transmit failed due to descriptor unavailable.
* @retval 1 Packet is copied to descriptor and triggered to transmit.
* @note Return 1 doesn't guarantee the packet will be sent and received successfully.
*/
uint32_t EMAC_SendPkt(uint8_t *pu8Data, uint32_t u32Size)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status;
uint32_t ret = 0UL;
/* Get Tx frame descriptor & data pointer */
desc = (EMAC_DESCRIPTOR_T *)u32NextTxDesc;
status = desc->u32Status1;
/* Check descriptor ownership */
if ((status & EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC)
{
memcpy((uint8_t *)desc->u32Data, pu8Data, u32Size);
/* Set Tx descriptor transmit byte count */
desc->u32Status2 = u32Size;
/* Change descriptor ownership to EMAC */
desc->u32Status1 |= EMAC_DESC_OWN_EMAC;
/* Get next Tx descriptor */
u32NextTxDesc = (uint32_t)(desc->u32Next);
/* Trigger EMAC to send the packet */
EMAC_TRIGGER_TX();
ret = 1UL;
}
return (ret);
}
/**
* @brief Clean up process after packet(s) are sent
* @param None
* @return Number of packet sent between two function calls
* @details EMAC Tx interrupt service routine \b must call this API or \ref EMAC_SendPktDoneTS to
* release the resource use by transmit process
*/
uint32_t EMAC_SendPktDone(void)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status, reg;
uint32_t last_tx_desc;
uint32_t u32Count = 0UL;
reg = EMAC->INTSTS;
/* Clear Tx interrupt flags */
EMAC->INTSTS = reg & (0xFFFF0000UL & ~EMAC_INTSTS_TSALMIF_Msk);
if (reg & EMAC_INTSTS_TXBEIF_Msk)
{
/* Bus error occurred, this is usually a bad sign about software bug and will occur again... */
while (1) {}
}
else
{
/* Process the descriptor(s). */
last_tx_desc = EMAC->CTXDSA ;
/* Get our first descriptor to process */
desc = (EMAC_DESCRIPTOR_T *) u32CurrentTxDesc;
do
{
/* Descriptor ownership is still EMAC, so this packet haven't been send. */
if (desc->u32Status1 & EMAC_DESC_OWN_EMAC)
{
break;
}
/* Get Tx status stored in descriptor */
status = desc->u32Status2 >> 16UL;
if (status & EMAC_TXFD_TXCP)
{
u32Count++;
}
else
{
/* Do nothing here on error. */
if (status & EMAC_TXFD_TXABT) {}
if (status & EMAC_TXFD_DEF) {}
if (status & EMAC_TXFD_PAU) {}
if (status & EMAC_TXFD_EXDEF) {}
if (status & EMAC_TXFD_NCS) {}
if (status & EMAC_TXFD_SQE) {}
if (status & EMAC_TXFD_LC) {}
if (status & EMAC_TXFD_TXHA) {}
}
/* restore descriptor link list and data pointer they will be overwrite if time stamp enabled */
desc->u32Data = desc->u32Backup1;
desc->u32Next = desc->u32Backup2;
/* go to next descriptor in link */
desc = (EMAC_DESCRIPTOR_T *)desc->u32Next;
}
while (last_tx_desc != (uint32_t)desc); /* If we reach last sent Tx descriptor, leave the loop */
/* Save last processed Tx descriptor */
u32CurrentTxDesc = (uint32_t)desc;
}
return (u32Count);
}
/**
* @brief Clean up process after a packet is sent, and get the time stamp while packet is sent
* @param[in] pu32Sec Second value while packet sent
* @param[in] pu32Nsec Nano second value while packet sent
* @return If a packet sent successfully
* @retval 0 No packet sent successfully, and the value in *pu32Sec and *pu32Nsec are meaningless
* @retval 1 A packet sent successfully, and the value in *pu32Sec and *pu32Nsec is the time stamp while packet sent
* @details EMAC Tx interrupt service routine \b must call this API or \ref EMAC_SendPktDone to
* release the resource use by transmit process
*/
uint32_t EMAC_SendPktDoneTS(uint32_t *pu32Sec, uint32_t *pu32Nsec)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status, reg;
uint32_t u32Count = 0UL;
reg = EMAC->INTSTS;
/* Clear Tx interrupt flags */
EMAC->INTSTS = reg & (0xFFFF0000UL & ~EMAC_INTSTS_TSALMIF_Msk);
if (reg & EMAC_INTSTS_TXBEIF_Msk)
{
/* Bus error occurred, this is usually a bad sign about software bug and will occur again... */
while (1) {}
}
else
{
/* Process the descriptor.
Get our first descriptor to process */
desc = (EMAC_DESCRIPTOR_T *) u32CurrentTxDesc;
/* Descriptor ownership is still EMAC, so this packet haven't been send. */
if ((desc->u32Status1 & EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC)
{
/* Get Tx status stored in descriptor */
status = desc->u32Status2 >> 16UL;
if (status & EMAC_TXFD_TXCP)
{
u32Count = 1UL;
*pu32Sec = desc->u32Next; /* second stores in descriptor's NEXT field */
*pu32Nsec = EMAC_Subsec2Nsec(desc->u32Data); /* Sub nano second store in DATA field */
}
else
{
/* Do nothing here on error. */
if (status & EMAC_TXFD_TXABT) {}
if (status & EMAC_TXFD_DEF) {}
if (status & EMAC_TXFD_PAU) {}
if (status & EMAC_TXFD_EXDEF) {}
if (status & EMAC_TXFD_NCS) {}
if (status & EMAC_TXFD_SQE) {}
if (status & EMAC_TXFD_LC) {}
if (status & EMAC_TXFD_TXHA) {}
}
/* restore descriptor link list and data pointer they will be overwrite if time stamp enabled */
desc->u32Data = desc->u32Backup1;
desc->u32Next = desc->u32Backup2;
/* go to next descriptor in link */
desc = (EMAC_DESCRIPTOR_T *)desc->u32Next;
/* Save last processed Tx descriptor */
u32CurrentTxDesc = (uint32_t)desc;
}
}
return (u32Count);
}
/**
* @brief Enable IEEE1588 time stamp function and set current time
* @param[in] u32Sec Second value
* @param[in] u32Nsec Nano second value
* @return None
*/
void EMAC_EnableTS(uint32_t u32Sec, uint32_t u32Nsec)
{
double f;
uint32_t reg;
EMAC->TSCTL = EMAC_TSCTL_TSEN_Msk;
EMAC->UPDSEC = u32Sec; /* Assume current time is 0 sec + 0 nano sec */
EMAC->UPDSUBSEC = EMAC_Nsec2Subsec(u32Nsec);
/* PTP source clock is 160MHz (Real chip using PLL). Each tick is 6.25ns
Assume we want to set each tick to 100ns.
Increase register = (100 * 2^31) / (10^9) = 214.71 =~ 215 = 0xD7
Addend register = 2^32 * tick_freq / (160MHz), where tick_freq = (2^31 / 215) MHz
From above equation, addend register = 2^63 / (160M * 215) ~= 268121280 = 0xFFB34C0
So:
EMAC->TSIR = 0xD7;
EMAC->TSAR = 0x1E70C600; */
f = (100.0 * 2147483648.0) / (1000000000.0) + 0.5;
EMAC->TSINC = (reg = (uint32_t)f);
f = (double)9223372036854775808.0 / ((double)(CLK_GetHCLKFreq()) * (double)reg);
EMAC->TSADDEND = (uint32_t)f;
EMAC->TSCTL |= (EMAC_TSCTL_TSUPDATE_Msk | EMAC_TSCTL_TSIEN_Msk | EMAC_TSCTL_TSMODE_Msk); /* Fine update */
}
/**
* @brief Disable IEEE1588 time stamp function
* @param None
* @return None
*/
void EMAC_DisableTS(void)
{
EMAC->TSCTL = 0UL;
}
/**
* @brief Get current time stamp
* @param[out] pu32Sec Current second value
* @param[out] pu32Nsec Current nano second value
* @return None
*/
void EMAC_GetTime(uint32_t *pu32Sec, uint32_t *pu32Nsec)
{
/* Must read TSLSR firstly. Hardware will preserve TSMSR value at the time TSLSR read. */
*pu32Nsec = EMAC_Subsec2Nsec(EMAC->TSSUBSEC);
*pu32Sec = EMAC->TSSEC;
}
/**
* @brief Set current time stamp
* @param[in] u32Sec Second value
* @param[in] u32Nsec Nano second value
* @return None
*/
void EMAC_SetTime(uint32_t u32Sec, uint32_t u32Nsec)
{
/* Disable time stamp counter before update time value (clear EMAC_TSCTL_TSIEN_Msk) */
EMAC->TSCTL = EMAC_TSCTL_TSEN_Msk;
EMAC->UPDSEC = u32Sec;
EMAC->UPDSUBSEC = EMAC_Nsec2Subsec(u32Nsec);
EMAC->TSCTL |= (EMAC_TSCTL_TSIEN_Msk | EMAC_TSCTL_TSMODE_Msk);
}
/**
* @brief Enable alarm function and set alarm time
* @param[in] u32Sec Second value to trigger alarm
* @param[in] u32Nsec Nano second value to trigger alarm
* @return None
*/
void EMAC_EnableAlarm(uint32_t u32Sec, uint32_t u32Nsec)
{
EMAC->ALMSEC = u32Sec;
EMAC->ALMSUBSEC = EMAC_Nsec2Subsec(u32Nsec);
EMAC->TSCTL |= EMAC_TSCTL_TSALMEN_Msk;
}
/**
* @brief Disable alarm function
* @param None
* @return None
*/
void EMAC_DisableAlarm(void)
{
EMAC->TSCTL &= ~EMAC_TSCTL_TSALMEN_Msk;
}
/**
* @brief Add a offset to current time
* @param[in] u32Neg Offset is negative value (u32Neg == 1) or positive value (u32Neg == 0).
* @param[in] u32Sec Second value to add to current time
* @param[in] u32Nsec Nano second value to add to current time
* @return None
*/
void EMAC_UpdateTime(uint32_t u32Neg, uint32_t u32Sec, uint32_t u32Nsec)
{
EMAC->UPDSEC = u32Sec;
EMAC->UPDSUBSEC = EMAC_Nsec2Subsec(u32Nsec);
if (u32Neg)
{
EMAC->UPDSUBSEC |= BIT31; /* Set bit 31 indicates this is a negative value */
}
EMAC->TSCTL |= EMAC_TSCTL_TSUPDATE_Msk;
}
/**
* @brief Check Ethernet link status
* @param None
* @return Current link status, could be one of following value.
* - \ref EMAC_LINK_DOWN
* - \ref EMAC_LINK_100F
* - \ref EMAC_LINK_100H
* - \ref EMAC_LINK_10F
* - \ref EMAC_LINK_10H
* @note This API should be called regularly to sync EMAC setting with real connection status
*/
uint32_t EMAC_CheckLinkStatus(void)
{
uint32_t reg, ret = EMAC_LINK_DOWN;
/* Check link valid again */
if (EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR) & PHY_STATUS_LINK_VALID)
{
/* Check link partner capability */
reg = EMAC_MdioRead(PHY_ANLPA_REG, EMAC_PHY_ADDR) ;
if (reg & PHY_ANLPA_DR100_TX_FULL)
{
EMAC->CTL |= EMAC_CTL_OPMODE_Msk;
EMAC->CTL |= EMAC_CTL_FUDUP_Msk;
ret = EMAC_LINK_100F;
}
else if (reg & PHY_ANLPA_DR100_TX_HALF)
{
EMAC->CTL |= EMAC_CTL_OPMODE_Msk;
EMAC->CTL &= ~EMAC_CTL_FUDUP_Msk;
ret = EMAC_LINK_100H;
}
else if (reg & PHY_ANLPA_DR10_TX_FULL)
{
EMAC->CTL &= ~EMAC_CTL_OPMODE_Msk;
EMAC->CTL |= EMAC_CTL_FUDUP_Msk;
ret = EMAC_LINK_10F;
}
else
{
EMAC->CTL &= ~EMAC_CTL_OPMODE_Msk;
EMAC->CTL &= ~EMAC_CTL_FUDUP_Msk;
ret = EMAC_LINK_10H;
}
}
return ret;
}
/**
* @brief Fill a MAC address to list and enable.
* @param A MAC address
* @return The CAM index
* @retval -1 Failed to fill the MAC address.
* @retval 0~(EMAC_CAMENTRY_NB-1) The index number of entry location.
*/
int32_t EMAC_FillCamEntry(uint8_t pu8MacAddr[])
{
uint32_t *EMAC_CAMxM;
uint32_t *EMAC_CAMxL;
int32_t index;
uint8_t mac[6];
for (index = 0; index < EMAC_CAMENTRY_NB; index ++)
{
EMAC_CAMxM = (uint32_t *)((uint32_t)&EMAC->CAM0M + (index * 8));
EMAC_CAMxL = (uint32_t *)((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, pu8MacAddr, sizeof(mac)) == 0)
{
goto exit_emac_fillcamentry;
}
if (*EMAC_CAMxM == 0 && *EMAC_CAMxL == 0)
{
break;
}
}
if (index < EMAC_CAMENTRY_NB)
{
EMAC_EnableCamEntry(index, pu8MacAddr);
goto exit_emac_fillcamentry;
}
return -1;
exit_emac_fillcamentry:
return index;
}
/**
* @brief Send an Ethernet packet
* @param[in] u32Size Packet size (without 4 byte CRC).
* @return Packet transmit success or not
* @retval 0 Transmit failed due to descriptor unavailable.
* @retval 1 Triggered to transmit.
* @note Return 1 doesn't guarantee the packet will be sent and received successfully.
*/
uint32_t EMAC_SendPktWoCopy(uint32_t u32Size)
{
EMAC_DESCRIPTOR_T *desc;
uint32_t status;
uint32_t ret = 0UL;
/* Get Tx frame descriptor & data pointer */
desc = (EMAC_DESCRIPTOR_T *)u32NextTxDesc;
status = desc->u32Status1;
/* Check descriptor ownership */
if ((status & EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC)
{
/* Set Tx descriptor transmit byte count */
desc->u32Status2 = u32Size;
/* Change descriptor ownership to EMAC */
desc->u32Status1 |= EMAC_DESC_OWN_EMAC;
/* Get next Tx descriptor */
u32NextTxDesc = (uint32_t)(desc->u32Next);
/* Trigger EMAC to send the packet */
EMAC_TRIGGER_TX();
ret = 1UL;
}
return (ret);
}
/**
* @brief Get avaiable TX buffer address
* @param None
* @return An avaiable TX buffer.
* @note This API should be called before EMAC_SendPkt_WoCopy calling. Caller will do data-copy.
*/
uint8_t *EMAC_ClaimFreeTXBuf(void)
{
EMAC_DESCRIPTOR_T *desc = (EMAC_DESCRIPTOR_T *)u32NextTxDesc;
if (desc->u32Status1 & EMAC_DESC_OWN_EMAC)
{
return (NULL);
}
else
{
return (uint8_t *)desc->u32Data;
}
}
/**
* @brief Get data length of avaiable RX buffer.
* @param None
* @return An data length of avaiable RX buffer.
* @note This API should be called before EMAC_RecvPktDone_WoTrigger calling. Caller will do data-copy.
*/
uint32_t EMAC_GetAvailRXBufSize(uint8_t **ppuDataBuf)
{
EMAC_DESCRIPTOR_T *desc = (EMAC_DESCRIPTOR_T *)u32CurrentRxDesc;
if ((desc->u32Status1 & EMAC_DESC_OWN_EMAC) != EMAC_DESC_OWN_EMAC) /* ownership=CPU */
{
uint32_t status = desc->u32Status1 >> 16;
/* It is good and no CRC error. */
if ((status & EMAC_RXFD_RXGD) && !(status & EMAC_RXFD_CRCE))
{
*ppuDataBuf = (uint8_t *)desc->u32Backup1;
return desc->u32Status1 & 0xFFFFUL;
}
else
{
// Drop it
EMAC_RecvPktDone();
}
}
return 0;
}
/**
* @brief Clean up process after a packet is received.
* @param None
* @return None
* @details Caller must call the function to release the resource.
* @note Application can only call this function once every time \ref EMAC_RecvPkt or \ref EMAC_RecvPktTS returns 1
* @note This function is without doing EMAC_TRIGGER_RX.
*/
void EMAC_RecvPktDoneWoRxTrigger(void)
{
EMAC_DESCRIPTOR_T *desc;
/* Get Rx Frame Descriptor */
desc = (EMAC_DESCRIPTOR_T *)u32CurrentRxDesc;
/* Restore descriptor link list and data pointer they will be overwrite if time stamp enabled */
desc->u32Data = desc->u32Backup1;
desc->u32Next = desc->u32Backup2;
/* Change ownership to DMA for next use */
desc->u32Status1 |= EMAC_DESC_OWN_EMAC;
/* Get Next Frame Descriptor pointer to process */
desc = (EMAC_DESCRIPTOR_T *)desc->u32Next;
/* Save last processed Rx descriptor */
u32CurrentRxDesc = (uint32_t)desc;
}
/*@}*/ /* end of group EMAC_EXPORTED_FUNCTIONS */
/*@}*/ /* end of group EMAC_Driver */
/*@}*/ /* end of group Standard_Driver */
/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
|