-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenc28j60.c
1448 lines (1185 loc) · 43.7 KB
/
enc28j60.c
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
/**
******************************************************************************
* @file enc28j60.c
* @author Christian Schoffit, portions from Gregory Nutt:
* Copyright (C) 2010-2012, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* @version V1.0.0
* @date 02-June-2015
* @brief This file provides a set of functions needed to manage the ENC28J60
* Stand-Alone Ethernet Controller with SPI Interface.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 Christian Schoffit</center></h2>
*
* 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. Neither the name of Christian Schoffit nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*
******************************************************************************
*/
/*
Module Feature Issue Issue Summary Affected Revisions
B1 B4 B5 B7
MAC Interface - 1. MAC registers unreliable with slow asynchronous SPI clock X X
Reset - 2. CLKRDY set early X X X X
Core Operating 3. Industrial (-40°C to +85°C) temperature range unsupported X X
Specifications
Oscillator CLKOUT pin 4. CLKOUT unavailable in Power Save mode X X X X
Memory Ethernet 5. Receive buffer must start at 0000h X X X X
Buffer
Interrupts - 6. Receive Packet Pending Interrupt Flag (PKTIF) unreliable X X X X
PHY - 7. TPIN+/- automatic polarity detection and correction
unreliable X X X X
PHY - 8. RBIAS resistor value differs between silicon revisions X X
PHY - 9. Internal loopback in half-duplex unreliable X X X X
PHY - 10. Internal loopback in full-duplex unreliable X X X X
PHY LEDs - 11. Combined Collision and Duplex Status mode unavailable X X X X
Transmit - 12. Transmit abort may stall transmit logic X X X X
Logic
PHY - 13. Received link pulses potentially cause collisions X X
Memory Ethernet 14. Even values in ERXRDPT may corrupt receive buffer X X X X
Buffer
Transmit - 15. LATECOL Status bit unreliable X X X X
Logic
PHY LEDs - 16. LED auto-polarity detection unreliable X X X X
DMA - 17. DMA checksum calculations will abort receive packets X X X X
Receive - 18. Pattern match filter allows reception of extra packets X X X X
Filter
SPI - 19. Reset command unavailable in Power Save mode X X X X
Interface
Only workaround relative to issues affecting B7 silicon revision are implemented. Therefore, issues
specific to Ethernet conformance are not addressed, since they only affect B1 and B3 silicon revisions.
Erratas 7, 8, 16... have workaround implemented by hardware
Errata 18 is implemented in lwip stack
*/
/* Includes ------------------------------------------------------------------*/
#include "enc28j60.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup Components
* @{
*/
/** @defgroup ENC28J60
* @{
*/
/** @defgroup ENC28J60_Private_Types_Definitions
* @{
*/
/** @defgroup ENC28J60_Private_Defines
* @{
*/
/* Poll timeout */
#define ENC_POLLTIMEOUT 50
/**
* @}
*/
/** @defgroup ENC28J60_Private_Macros
* @{
*/
/* Packet Memory ************************************************************/
/* Packet memory layout */
#define ALIGNED_BUFSIZE ((CONFIG_NET_ETH_MTU + 255) & ~255)
/* Work around Errata #5 (spurious reset of ERXWRPT to 0) by placing the RX
* FIFO at the beginning of packet memory.
*/
# define PKTMEM_RX_START 0x0000 /* RX buffer must be at addr 0 for errata 5 */
# define PKTMEM_RX_END (PKTMEM_END-ALIGNED_BUFSIZE) /* RX buffer length is total SRAM minus TX buffer */
# define PKTMEM_TX_START (PKTMEM_RX_END+1) /* Start TX buffer after */
# define PKTMEM_TX_ENDP1 (PKTMEM_TX_START+ALIGNED_BUFSIZE) /* Allow TX buffer for two frames */
/* Misc. Helper Macros ******************************************************/
#define enc_rdgreg(ctrlreg) \
enc_rdgreg2(ENC_RCR | GETADDR(ctrlreg))
#define enc_wrgreg(ctrlreg, wrdata) \
enc_wrgreg2(ENC_WCR | GETADDR(ctrlreg), wrdata)
#define enc_bfcgreg(ctrlreg,clrbits) \
enc_wrgreg2(ENC_BFC | GETADDR(ctrlreg), clrbits)
#define enc_bfsgreg(ctrlreg,setbits) \
enc_wrgreg2(ENC_BFS | GETADDR(ctrlreg), setbits)
/**
* @}
*/
/** @defgroup ENC28J60_Private_Variables
* @{
*/
/* Stores how many iterations the microcontroller can do in 1 µs */
static uint32_t iter_per_us=0;
/**
* @}
*/
/** @defgroup ENC28J60_Private_Function_Prototypes
* @{
*/
/**
* @}
*/
/** @defgroup ENC28J60_Private_Functions
* @{
*/
/**
Calibrate the constant time
**/
static void calibrate(void)
{
uint32_t time;
volatile uint32_t i;
iter_per_us = 1000000;
time = HAL_GetTick();
/* Wait for next tick */
while (HAL_GetTick() == time) {
/* wait */
}
for (i=0; i<iter_per_us; i++) {
}
iter_per_us /= ((HAL_GetTick()-time)*1000);
}
/**
* Software delay in µs
* us: the number of µs to wait
**/
__inline void up_udelay(uint32_t us)
{
volatile uint32_t i;
for (i=0; i<us*iter_per_us; i++) {
}
}
/****************************************************************************
* Function: enc_rdgreg2
*
* Description:
* Read a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
* include the CMD 'OR'd with the global address register.
*
* Parameters:
* cmd - The full command to received (cmd | address)
*
* Returned Value:
* The value read from the register
*
* Assumptions:
*
****************************************************************************/
static uint8_t enc_rdgreg2(uint8_t cmd)
{
uint8_t cmdpdata[2];
cmdpdata[0] = cmd;
/* Send the read command and collect the data. The sequence requires
* 16-clocks: 8 to clock out the cmd + 8 to clock in the data.
*/
ENC_SPI_SendBuf(cmdpdata, cmdpdata, 2);
return cmdpdata[1];
}
/****************************************************************************
* Function: enc_wrgreg2
*
* Description:
* Write to a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
* include the CMD 'OR'd with the global address register.
*
* Parameters:
* cmd - The full command to received (cmd | address)
* wrdata - The data to send
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void enc_wrgreg2(uint8_t cmd, uint8_t wrdata)
{
uint8_t cmdpdata[2];
cmdpdata[0] = cmd;
cmdpdata[1] = wrdata;
/* Send the write command and data. The sequence requires 16-clocks:
* 8 to clock out the cmd + 8 to clock out the data.
*/
ENC_SPI_SendBuf(cmdpdata, NULL, 2);
}
/****************************************************************************
* Function: enc_waitgreg
*
* Description:
* Wait until grouped register bit(s) take a specific value (or a timeout
* occurs).
*
* Parameters:
* ctrlreg - Bit encoded address of banked register to check
* bits - The bits to check (a mask)
* value - The value of the bits to return (value under mask)
*
* Returned Value:
* OK on success, negated errno on failure
*
* Assumptions:
*
****************************************************************************/
static bool enc_waitgreg(uint8_t ctrlreg,
uint8_t bits, uint8_t value)
{
uint32_t start = HAL_GetTick();
uint32_t elapsed;
uint8_t rddata;
/* Loop until the exit condition is met */
do
{
/* Read the byte from the requested banked register */
rddata = enc_rdgreg(ctrlreg);
elapsed = HAL_GetTick() - start;
}
while ((rddata & bits) != value && elapsed < ENC_POLLTIMEOUT);
return (rddata & bits) == value;
}
/****************************************************************************
* Function: enc_waitwhilegreg
*
* Description:
* Wait while grouped register bit(s) have a specific value (or a timeout
* occurs).
*
* Parameters:
* ctrlreg - Bit encoded address of banked register to check
* bits - The bits to check (a mask)
* value - The value of the bits to return (value under mask)
*
* Returned Value:
* OK on success, negated errno on failure
*
* Assumptions:
*
****************************************************************************/
#ifndef USE_PROTOTHREADS
static bool enc_waitwhilegreg(uint8_t ctrlreg,
uint8_t bits, uint8_t value)
{
uint32_t start = HAL_GetTick();
uint32_t elapsed;
uint8_t rddata;
/* Loop until the exit condition is met */
do
{
/* Read the byte from the requested banked register */
rddata = enc_rdgreg(ctrlreg);
elapsed = HAL_GetTick() - start;
}
while ((rddata & bits) == value && elapsed < ENC_POLLTIMEOUT);
return (rddata & bits) != value;
}
#endif
/**
* @brief Perform a soft reset on enc28j60
* Description:
* Send the single byte system reset command (SRC).
*
* "The System Reset Command (SRC) allows the host controller to issue a
* System Soft Reset command. Unlike other SPI commands, the SRC is
* only a single byte command and does not operate on any register. The
* command is started by pulling the CS pin low. The SRC opcode is the
* sent, followed by a 5-bit Soft Reset command constant of 1Fh. The
* SRC operation is terminated by raising the CS pin."
*
* @param None
* @retval None
*/
void enc_reset(ENC_HandleTypeDef *handle) {
/* Send the system reset command. */
ENC_SPI_Send(ENC_SRC);
/* Check CLKRDY bit to see when the reset is complete. There is an errata
* that says the CLKRDY may be invalid. We'll wait a couple of msec to
* workaround this condition.
*
* Also, "After a System Reset, all PHY registers should not be read or
* written to until at least 50 µs have passed since the Reset has ended.
* All registers will revert to their Reset default values. The dual
* port buffer memory will maintain state throughout the System Reset."
*/
handle->bank = 0; /* Initialize the trace on the current selected bank */
//up_mdelay(2);
HAL_Delay(2); /* >1000 µs, conforms to errata #2 */
}
/****************************************************************************
* Function: enc_setbank
*
* Description:
* Set the bank for these next control register access.
*
* Assumption:
* The caller has exclusive access to the SPI bus
*
* Parameters:
* handle - Reference to the driver state structure
* bank - The bank to select (0-3)
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
void enc_setbank(ENC_HandleTypeDef *handle, uint8_t bank) {
if (bank != handle->bank) {
/* Select bank 0 (just so that all of the bits are cleared) */
enc_bfcgreg(ENC_ECON1, ECON1_BSEL_MASK);
/* Then OR in bits to get the correct bank */
if (bank != 0)
{
enc_bfsgreg(ENC_ECON1, (bank << ECON1_BSEL_SHIFT));
}
/* Then remember the bank setting */
handle->bank = bank;
}
}
/****************************************************************************
* Function: enc_rdbreg
*
* Description:
* Read from a banked control register using the RCR command.
*
* Parameters:
* handle - Reference to the driver state structure
* ctrlreg - Bit encoded address of banked register to read
*
* Returned Value:
* The byte read from the banked register
*
* Assumptions:
*
****************************************************************************/
static uint8_t enc_rdbreg(ENC_HandleTypeDef *handle, uint8_t ctrlreg)
{
uint8_t data[3];
/* Set the bank */
enc_setbank(handle, GETBANK(ctrlreg));
/* Send the RCR command and collect the data. How we collect the data
* depends on if this is a PHY/CAN or not. The normal sequence requires
* 16-clocks: 8 to clock out the cmd and 8 to clock in the data.
*/
data[0] = ENC_RCR | GETADDR(ctrlreg);
/* The PHY/MAC sequence requires 24-clocks: 8 to clock out the cmd,
* 8 dummy bits, and 8 to clock in the PHY/MAC data.
*/
ENC_SPI_SendBuf(data, data, (ISPHYMAC(ctrlreg))?3:2);
return (ISPHYMAC(ctrlreg))?data[2]:data[1];
}
/****************************************************************************
* Function: enc_wrbreg
*
* Description:
* Write to a banked control register using the WCR command. Unlike
* reading, this same SPI sequence works for normal, MAC, and PHY
* registers.
*
* Parameters:
* handle - Reference to the driver state structure
* ctrlreg - Bit encoded address of banked register to write
* wrdata - The data to send
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void enc_wrbreg(ENC_HandleTypeDef *handle, uint8_t ctrlreg,
uint8_t wrdata)
{
uint8_t data[2];
/* Set the bank */
enc_setbank(handle, GETBANK(ctrlreg));
/* Send the WCR command and data. The sequence requires 16-clocks:
* 8 to clock out the cmd + 8 to clock out the data.
*/
data[0] = ENC_WCR | GETADDR(ctrlreg);
data[1] = wrdata;
ENC_SPI_SendBuf(data, NULL, 2);
}
/****************************************************************************
* Function: enc_waitbreg
*
* Description:
* Wait until banked register bit(s) take a specific value (or a timeout
* occurs).
*
* Parameters:
* handle - Reference to the driver state structure
* ctrlreg - Bit encoded address of banked register to check
* bits - The bits to check (a mask)
* value - The value of the bits to return (value under mask)
*
* Returned Value:
* OK on success, negated errno on failure
*
* Assumptions:
*
****************************************************************************/
static bool enc_waitbreg(ENC_HandleTypeDef *handle, uint8_t ctrlreg,
uint8_t bits, uint8_t value)
{
uint32_t start = HAL_GetTick();
uint32_t elapsed;
uint8_t rddata;
/* Loop until the exit condition is met */
do
{
/* Read the byte from the requested banked register */
rddata = enc_rdbreg(handle, ctrlreg);
elapsed = HAL_GetTick() - start;
}
while ((rddata & bits) != value && elapsed < ENC_POLLTIMEOUT);
return (rddata & bits) == value;
}
/****************************************************************************
* Function: enc_rdphy
*
* Description:
* Read 16-bits of PHY data.
*
* Parameters:
* priv - Reference to the driver state structure
* phyaddr - The PHY register address
*
* Returned Value:
* 16-bit value read from the PHY
*
* Assumptions:
*
****************************************************************************/
static uint16_t enc_rdphy(ENC_HandleTypeDef *handle, uint8_t phyaddr)
{
uint16_t data = 0;
/* "To read from a PHY register:
*
* 1. Write the address of the PHY register to read from into the MIREGADR
* register.
*/
enc_wrbreg(handle, ENC_MIREGADR, phyaddr);
/* 2. Set the MICMD.MIIRD bit. The read operation begins and the
* MISTAT.BUSY bit is set.
*/
enc_wrbreg(handle, ENC_MICMD, MICMD_MIIRD);
/* 3. Wait 10.24 µs. Poll the MISTAT.BUSY bit to be certain that the
* operation is complete. While busy, the host controller should not
* start any MIISCAN operations or write to the MIWRH register.
*
* When the MAC has obtained the register contents, the BUSY bit will
* clear itself.
*/
// volatile int i;
// for (i=0; i<12*17; i++) {
// }
up_udelay(12);
if (enc_waitbreg(handle, ENC_MISTAT, MISTAT_BUSY, 0x00))
{
/* 4. Clear the MICMD.MIIRD bit. */
enc_wrbreg(handle, ENC_MICMD, 0x00);
/* 5. Read the desired data from the MIRDL and MIRDH registers. The
* order that these bytes are accessed is unimportant."
*/
data = (uint16_t)enc_rdbreg(handle, ENC_MIRDL);
data |= (uint16_t)enc_rdbreg(handle, ENC_MIRDH) << 8;
}
return data;
}
/****************************************************************************
* Function: enc_wrphy
*
* Description:
* write 16-bits of PHY data.
*
* Parameters:
* handle - Reference to the driver state structure
* phyaddr - The PHY register address
* phydata - 16-bit data to write to the PHY
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void enc_wrphy(ENC_HandleTypeDef *handle, uint8_t phyaddr,
uint16_t phydata)
{
/* "To write to a PHY register:
*
* 1. Write the address of the PHY register to write to into the
* MIREGADR register.
*/
enc_wrbreg(handle, ENC_MIREGADR, phyaddr);
/* 2. Write the lower 8 bits of data to write into the MIWRL register. */
enc_wrbreg(handle, ENC_MIWRL, phydata);
/* 3. Write the upper 8 bits of data to write into the MIWRH register.
* Writing to this register automatically begins the MIIM transaction,
* so it must be written to after MIWRL. The MISTAT.BUSY bit becomes
* set.
*/
enc_wrbreg(handle, ENC_MIWRH, phydata >> 8);
/* The PHY register will be written after the MIIM operation completes,
* which takes 10.24 µs. When the write operation has completed, the BUSY
* bit will clear itself.
*
* The host controller should not start any MIISCAN or MIIRD operations
* while busy."
*/
/* wait for approx 12 µs */
// volatile int i;
// for (i=0; i<12*17; i++) {
// }
up_udelay(12);
enc_waitbreg(handle, ENC_MISTAT, MISTAT_BUSY, 0x00);
}
/****************************************************************************
* Function: enc_pwrfull
*
* Description:
* When normal operation is desired, the host controller must perform
* a slightly modified procedure:
*
* 1. Wake-up by clearing ECON2.PWRSV.
* 2. Wait at least 300 µs for the PHY to stabilize. To accomplish the
* delay, the host controller may poll ESTAT.CLKRDY and wait for it
* to become set.
* 3. Restore receive capability by setting ECON1.RXEN.
*
* After leaving Sleep mode, there is a delay of many milliseconds
* before a new link is established (assuming an appropriate link
* partner is present). The host controller may wish to wait until
* the link is established before attempting to transmit any packets.
* The link status can be determined by polling the PHSTAT2.LSTAT bit.
* Alternatively, the link change interrupt may be used if it is
* enabled.
*
* Parameters:
* handle - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
/* Power save mode not used (fix errata 4 and 19) */
#if 0
static void enc_pwrfull(ENC_HandleTypeDef *handle)
{
/* 1. Wake-up by clearing ECON2.PWRSV. */
enc_bfcgreg(ENC_ECON2, ECON2_PWRSV);
/* 2. Wait at least 300 µs for the PHY to stabilize. To accomplish the
* delay, the host controller may poll ESTAT.CLKRDY and wait for it to
* become set.
*/
/* wait for approx 350 µs */
// volatile int i;
// for (i=0; i<350*17; i++) {
// }
up_udelay(350);
enc_waitbreg(handle, ENC_ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
/* 3. Restore receive capability by setting ECON1.RXEN.
*
* The caller will do this when it is ready to receive packets
*/
}
#endif
/**
* @brief Initialize the enc28j60 and configure the needed hardware resources
* @param handle: Handle on data configuration.
* @retval None
*/
bool ENC_Start(ENC_HandleTypeDef *handle)
{
/* register value */
uint8_t regval;
/* Calibrate time constant */
calibrate();
/* System reset */
enc_reset(handle);
/* Use bank 0 */
enc_setbank(handle, 0);
/* Check if we are actually communicating with the ENC28J60. If its
* 0x00 or 0xff, then we are probably not communicating correctly
* via SPI.
*/
regval = enc_rdbreg(handle, ENC_EREVID);
if (regval == 0x00 || regval == 0xff) {
return false;
}
/* Initialize ECON2: Enable address auto increment.
*/
enc_wrgreg(ENC_ECON2, ECON2_AUTOINC /* | ECON2_VRPS*/);
/* Initialize receive buffer.
* First, set the receive buffer start address.
*/
handle->nextpkt = PKTMEM_RX_START;
enc_wrbreg(handle, ENC_ERXSTL, PKTMEM_RX_START & 0xff);
enc_wrbreg(handle, ENC_ERXSTH, PKTMEM_RX_START >> 8);
/* Set the receive data pointer */
/* Errata 14 */
enc_wrbreg(handle, ENC_ERXRDPTL, PKTMEM_RX_END & 0xff);
enc_wrbreg(handle, ENC_ERXRDPTH, PKTMEM_RX_END >> 8);
/*
enc_wrbreg(handle, ENC_ERXRDPTL, PKTMEM_RX_START & 0xff);
enc_wrbreg(handle, ENC_ERXRDPTH, PKTMEM_RX_START >> 8);
*/
/* Set the receive buffer end. */
enc_wrbreg(handle, ENC_ERXNDL, PKTMEM_RX_END & 0xff);
enc_wrbreg(handle, ENC_ERXNDH, PKTMEM_RX_END >> 8);
/* Set transmit buffer start. */
handle->transmitLength = 0;
enc_wrbreg(handle, ENC_ETXSTL, PKTMEM_TX_START & 0xff);
enc_wrbreg(handle, ENC_ETXSTH, PKTMEM_TX_START >> 8);
/* Set filter mode: unicast OR broadcast AND crc valid */
enc_wrbreg(handle, ENC_ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN);
do {
HAL_Delay(10); /* Wait for 10 ms to let the clock be ready */
regval = enc_rdbreg(handle, ENC_ESTAT);
} while ((regval & ESTAT_CLKRDY) == 0);
/* Enable MAC receive */
enc_wrbreg(handle, ENC_MACON1, MACON1_MARXEN | MACON1_TXPAUS | MACON1_RXPAUS);
/* Enable automatic padding and CRC operations */
if (handle->Init.DuplexMode == ETH_MODE_HALFDUPLEX) {
enc_wrbreg(handle, ENC_MACON3,
((handle->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)?MACON3_PADCFG0 | MACON3_TXCRCEN:0) |
MACON3_FRMLNEN);
enc_wrbreg(handle, ENC_MACON4, MACON4_DEFER); /* Defer transmission enable */
/* Set Non-Back-to-Back Inter-Packet Gap */
enc_wrbreg(handle, ENC_MAIPGL, 0x12);
enc_wrbreg(handle, ENC_MAIPGH, 0x0c);
/* Set Back-to-Back Inter-Packet Gap */
enc_wrbreg(handle, ENC_MABBIPG, 0x12);
} else {
/* Set filter mode: unicast OR broadcast AND crc valid AND Full Duplex */
enc_wrbreg(handle, ENC_MACON3,
((handle->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)?MACON3_PADCFG0 | MACON3_TXCRCEN:0) |
MACON3_FRMLNEN | MACON3_FULDPX);
/* Set Non-Back-to-Back Inter-Packet Gap */
enc_wrbreg(handle, ENC_MAIPGL, 0x12);
/* Set Back-to-Back Inter-Packet Gap */
enc_wrbreg(handle, ENC_MABBIPG, 0x15);
}
/* Set the maximum packet size which the controller will accept */
enc_wrbreg(handle, ENC_MAMXFLL, (CONFIG_NET_ETH_MTU+18) & 0xff);
enc_wrbreg(handle, ENC_MAMXFLH, (CONFIG_NET_ETH_MTU+18) >> 8);
/* Configure LEDs (No, just use the defaults for now) */
/* enc_wrphy(priv, ENC_PHLCON, ??); */
/* Setup up PHCON1 & 2 */
if (handle->Init.DuplexMode == ETH_MODE_HALFDUPLEX) {
enc_wrphy(handle, ENC_PHCON1, 0x00);
enc_wrphy(handle, ENC_PHCON2, PHCON2_HDLDIS); /* errata 9 workaround */
} else {
enc_wrphy(handle, ENC_PHCON1, PHCON1_PDPXMD); /* errata 10 workaround */
enc_wrphy(handle, ENC_PHCON2, 0x00);
}
/* Not used Restore normal operation mode
enc_pwrfull(handle); */
/* Process interrupt settings */
if (handle->Init.InterruptEnableBits & EIE_LINKIE) {
/* Enable link change interrupt in PHY module */
enc_wrphy(handle, ENC_PHIE, PHIE_PGEIE | PHIE_PLNKIE);
}
/* Since we not modify PHLCON register, we don't fall in errata 11 case */
/* Reset all interrupt flags */
enc_bfcgreg(ENC_EIR, EIR_ALLINTS);
regval = handle->Init.InterruptEnableBits;
if (regval) {
/* Ensure INTIE is set when at least an interruption is selected */
regval |= EIE_INTIE;
}
/* Enable selected interrupts in ethernet controller module */
enc_bfsgreg(ENC_EIE, regval);
/* Enable the receiver */
enc_bfsgreg(ENC_ECON1, ECON1_RXEN);
return true;
}
/**
* @}
*/
/****************************************************************************
* Function: ENC_SetMacAddr
*
* Description:
* Set the MAC address to the configured value. This is done after ifup
* or after a TX timeout. Note that this means that the interface must
* be down before configuring the MAC addr.
*
* Parameters:
* handle - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
void ENC_SetMacAddr(ENC_HandleTypeDef *handle)
{
/* Program the hardware with it's MAC address (for filtering).
* MAADR1 MAC Address Byte 1 (MAADR<47:40>), OUI Byte 1
* MAADR2 MAC Address Byte 2 (MAADR<39:32>), OUI Byte 2
* MAADR3 MAC Address Byte 3 (MAADR<31:24>), OUI Byte 3
* MAADR4 MAC Address Byte 4 (MAADR<23:16>)
* MAADR5 MAC Address Byte 5 (MAADR<15:8>)
* MAADR6 MAC Address Byte 6 (MAADR<7:0>)
*/
enc_wrbreg(handle, ENC_MAADR1, handle->Init.MACAddr[0]);
enc_wrbreg(handle, ENC_MAADR2, handle->Init.MACAddr[1]);
enc_wrbreg(handle, ENC_MAADR3, handle->Init.MACAddr[2]);
enc_wrbreg(handle, ENC_MAADR4, handle->Init.MACAddr[3]);
enc_wrbreg(handle, ENC_MAADR5, handle->Init.MACAddr[4]);
enc_wrbreg(handle, ENC_MAADR6, handle->Init.MACAddr[5]);
}
/****************************************************************************
* Function: ENC_WriteBuffer
*
* Description:
* Write a buffer of data.
*
* Parameters:
* buffer - A pointer to the buffer to write from
* buflen - The number of bytes to write
*
* Returned Value:
* None
*
* Assumptions:
* Read pointer is set to the correct address
*
****************************************************************************/
void ENC_WriteBuffer(void *buffer, uint16_t buflen)
{
/* Send the WBM command and copy the packet itself into the transmit
* buffer at the position of the EWRPT register.
*/
/* Select ENC28J60 chip
*
* "The WBM command is started by lowering the CS pin. ..."
* We explicitly select the ENC28J60 chip because we have to transmits several pieces of
* information while keeping CS low
*
*/
ENC_SPI_Select(true);
/* Send the write buffer memory command (ignoring the response)
*
* "...The [3-bit]WBM opcode should then be sent to the ENC28J60,
* followed by the 5-bit constant, 1Ah."
*/
ENC_SPI_SendWithoutSelection(ENC_WBM);
/* Send the buffer
*
* "... After the WBM command and constant are sent, the data to
* be stored in the memory pointed to by EWRPT should be shifted
* out MSb first to the ENC28J60. After 8 data bits are received,
* the Write Pointer will automatically increment if AUTOINC is
* set. The host controller can continue to provide clocks on the
* SCK pin and send data on the SI pin, without raising CS, to
* keep writing to the memory. In this manner, with AUTOINC
* enabled, it is possible to continuously write sequential bytes
* to the buffer memory without any extra SPI command
* overhead.
*/
ENC_SPI_SendBuf(buffer, NULL, buflen);
/* De-select ENC28J60 chip
*
* "The WBM command is terminated by bringing up the CS pin. ..."
* done in ENC_SPI_SendBuf callback
*/
}
/****************************************************************************
* Function: enc_rdbuffer
*
* Description:
* Read a buffer of data.
*
* Parameters:
* buffer - A pointer to the buffer to read into
* buflen - The number of bytes to read
*