-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrtl8367c.ino
3201 lines (2671 loc) · 82.5 KB
/
rtl8367c.ino
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
/*
* Sketch for Arduino Nano 3.0 (CH340 - China) and board STM32F103C8T6 (Blue Pill)
*
* rtl8367c.ino: VLAN switch setup based on Realtek RTL8367C(S) chips.
* Used TP-Link TL-SG1005D rev.7 (RTL8367S) after remove EEPROM chip from board switch!
*
* Copyright (C) 2017 McMCC <mcmcc_at_mail_ru>
*
* For ARDUINO AVR:
*
* Remove EEPROM 24cXX on switch board!!!!
* Pin SDA - D6 (convertor 5v-to-3.3v pin SDA on EEPROM switch(pin 5))
* Pin SCK - D5 (convertor 5v-to-3.3v pin SCK on EEPROM switch(pin 6))
* Arduino Vin - get from power connector +12V
* +5V - get from Arduino 5V
* +3.3V - get from EEPROM pin 8 on switch board
*
* Logic Level Convertor 5v-to-3.3v
*
* +3.3V +5V SOT-23
* EEPROM Vcc(8) o-----*-------- -------o D __
* | | | ||
* | R1 | | R2 ----------
* --- 10K | --- 10K | BSS138 |
* | | | | | ----------
* | | Q1 |G | | || ||
* EEPROM --- ------- --- G -- -- S
* SDA(5),SCK(6) | - ^ - |
* (RTL8367S TTL 3.3v) | | | | | D5,D6(Arduino TTL 5v)
* o-----*----*--- *----*------o
* S| |D Q1 - MOSFET N-Channel
* |_|\|_| BSS138(diode built-in)
* |/| [ or 2N7000/2N7002 ]
*
*
* For ARDUINO STM32:
*
* No need Logic Level Convertor, use pin-to-pin conection.
* Remove EEPROM 24cXX on switch board!!!!
* Cut on the board STM32 the track from power USB!!!!
* +3.3V get from EEPROM pin 8 on switch board.
* GND get from EEPROM pin 1-4 on switch board.
*
* Pin SDA - PB10 (to EEPROM pin 5 on switch board)
* Pin SCK - PB11 (to EEPROM pin 6 on switch board)
*
* NEW! Added support write configuration on external I2C EEPROM 24cXX.
* !!!!!!!!!!Uncomment USE_I2C_EEPROM for using!!!!!!!!!!!!!!
* Pin 5 SDA EEPROM connect to PB7 STM32 board (between pullup resitor 4.7-6.8K)
* Pin 6 SCK EEPROM connect to PB6 STM32 board (between pullup resitor 4.7-6.8K)
* Pin 1-4 and 7 EEPROM connect to G STM32 board
* Pin 8 EEPROM connect to 3.3 STM32 board
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* RTK API */
#define _LITTLE_ENDIAN 1
#define RTL8367C_CVIDXNO 32
typedef struct rtk_portmask_e
{
uint32_t bits[1];
} rtk_portmask_t;
typedef enum rtk_led_group_e
{
LED_GROUP_0 = 0,
LED_GROUP_1,
LED_GROUP_2,
LED_GROUP_END,
} rtk_led_group_t;
typedef enum rtk_led_config_e
{
LED_CONFIG_LEDOFF = 0,
LED_CONFIG_DUPCOL,
LED_CONFIG_LINK_ACT,
LED_CONFIG_SPD1000,
LED_CONFIG_SPD100,
LED_CONFIG_SPD10,
LED_CONFIG_SPD1000ACT,
LED_CONFIG_SPD100ACT,
LED_CONFIG_SPD10ACT,
LED_CONFIG_SPD10010ACT,
LED_CONFIG_LOOPDETECT,
LED_CONFIG_EEE,
LED_CONFIG_LINKRX,
LED_CONFIG_LINKTX,
LED_CONFIG_MASTER,
LED_CONFIG_ACT,
LED_CONFIG_END,
} rtk_led_config_t;
typedef enum rtk_led_operation_e
{
LED_OP_SCAN = 0,
LED_OP_PARALLEL,
LED_OP_SERIAL,
LED_OP_END,
} rtk_led_operation_t;
typedef enum rtl8367c_accframetype_e
{
FRAME_TYPE_BOTH = 0,
FRAME_TYPE_TAGGED_ONLY,
FRAME_TYPE_UNTAGGED_ONLY,
FRAME_TYPE_MAX_BOUND
} rtl8367c_accframetype;
typedef enum rtk_vlan_acceptFrameType_e
{
ACCEPT_FRAME_TYPE_ALL = 0, /* untagged, priority-tagged and tagged */
ACCEPT_FRAME_TYPE_TAG_ONLY, /* tagged */
ACCEPT_FRAME_TYPE_UNTAG_ONLY, /* untagged and priority-tagged */
ACCEPT_FRAME_TYPE_END
} rtk_vlan_acceptFrameType_t;
enum RTL8367C_LEDOP
{
LEDOP_SCAN0 = 0,
LEDOP_SCAN1,
LEDOP_PARALLEL,
LEDOP_SERIAL,
LEDOP_END,
};
typedef struct VLANCONFIGSMI
{
#ifdef _LITTLE_ENDIAN
uint16_t mbr:11;
uint16_t reserved:5;
uint16_t fid_msti:4;
uint16_t reserved2:12;
uint16_t vbpen:1;
uint16_t vbpri:3;
uint16_t envlanpol:1;
uint16_t meteridx:6;
uint16_t reserved3:5;
uint16_t evid:13;
uint16_t reserved4:3;
#else
uint16_t reserved:5;
uint16_t mbr:11;
uint16_t reserved2:12;
uint16_t fid_msti:4;
uint16_t reserved3:5;
uint16_t meteridx:6;
uint16_t envlanpol:1;
uint16_t vbpri:3;
uint16_t vbpen:1;
uint16_t reserved4:3;
uint16_t evid:13;
#endif
} rtl8367c_vlanconfigsmi;
typedef struct VLANCONFIGUSER
{
uint16_t evid;
uint16_t mbr;
uint16_t fid_msti;
uint16_t envlanpol;
uint16_t meteridx;
uint16_t vbpen;
uint16_t vbpri;
} rtl8367c_vlanconfiguser;
typedef struct VLANTABLE
{
#ifdef _LITTLE_ENDIAN
uint16_t mbr:8;
uint16_t untag:8;
uint16_t fid_msti:4;
uint16_t vbpen:1;
uint16_t vbpri:3;
uint16_t envlanpol:1;
uint16_t meteridx:5;
uint16_t ivl_svl:1;
uint16_t mbr_ext_0_1:2;
uint16_t mbr_ext_2:1;
uint16_t untagset_ext:3;
uint16_t mtr_idx_ext:1;
uint16_t reserved:11;
#else
uint16_t untag:8;
uint16_t mbr:8;
uint16_t mbr_ext_0_1:2;
uint16_t ivl_svl:1;
uint16_t meteridx:5;
uint16_t envlanpol:1;
uint16_t vbpri:3;
uint16_t vbpen:1;
uint16_t fid_msti:4;
uint16_t reserved:11;
uint16_t mtr_idx_ext:1;
uint16_t untagset_ext:3;
uint16_t mbr_ext_2:1;
#endif
} rtl8367c_vlan4kentrysmi;
typedef struct USER_VLANTABLE
{
uint16_t vid;
uint16_t mbr;
uint16_t untag;
uint16_t fid_msti;
uint16_t envlanpol;
uint16_t meteridx;
uint16_t vbpen;
uint16_t vbpri;
uint16_t ivl_svl;
} rtl8367c_user_vlan4kentry;
typedef enum rtl8367c_egtagmode_e
{
EG_TAG_MODE_ORI = 0,
EG_TAG_MODE_KEEP,
EG_TAG_MODE_PRI_TAG,
EG_TAG_MODE_REAL_KEEP,
EG_TAG_MODE_END
} rtl8367c_egtagmode;
enum RTL8367C_TABLE_ACCESS_OP
{
TB_OP_READ = 0,
TB_OP_WRITE
};
enum RTL8367C_TABLE_ACCESS_TARGET
{
TB_TARGET_ACLRULE = 1,
TB_TARGET_ACLACT,
TB_TARGET_CVLAN,
TB_TARGET_L2,
TB_TARGET_IGMP_GROUP
};
enum PRIDECISION
{
PRIDEC_PORT = 0,
PRIDEC_ACL,
PRIDEC_DSCP,
PRIDEC_1Q,
PRIDEC_1AD,
PRIDEC_CVLAN,
PRIDEC_DA,
PRIDEC_SA,
PRIDEC_END,
};
enum RTL8367C_PRIDEC_TABLE
{
PRIDEC_IDX0 = 0,
PRIDEC_IDX1,
PRIDEC_IDX_END,
};
enum FLOW_CONTROL_TYPE
{
FC_EGRESS = 0,
FC_INGRESS,
};
typedef struct rtk_vlan_cfg_s
{
rtk_portmask_t mbr;
rtk_portmask_t untag;
uint16_t ivl_en;
uint16_t fid_msti;
uint16_t envlanpol;
uint16_t meteridx;
uint16_t vbpen;
uint16_t vbpri;
} rtk_vlan_cfg_t;
typedef enum vlan_mbrCfgType_e
{
MBRCFG_UNUSED = 0,
MBRCFG_USED_BY_VLAN,
MBRCFG_END
} vlan_mbrCfgType_t;
typedef enum rtk_qos_priDecTbl_e
{
PRIDECTBL_IDX0 = 0,
PRIDECTBL_IDX1,
PRIDECTBL_END,
} rtk_qos_priDecTbl_t;
typedef uint32_t rtk_vlan_t;
rtk_vlan_t vlan_mbrCfgVid[RTL8367C_CVIDXNO];
vlan_mbrCfgType_t vlan_mbrCfgUsage[RTL8367C_CVIDXNO];
/* End RTK API */
#if !defined(__AVR__) /* if STM32 Arduino */
/*
#define USE_I2C_EEPROM 1
*/
#define PIN_SDA PB10
#define PIN_SCK PB11
#include <inttypes.h>
#ifndef USE_I2C_EEPROM
#include <EEPROM.h>
#else
#include <Wire.h>
/* Set I2C1, 400KHz, PB7 - SDA1, PB6 - SCL1 */
HardWire HWire(1, I2C_FAST_MODE);
const byte DEVADDR = 0x50;
char i2c_eeprom_read_byte(unsigned eeaddr)
{
byte rdata = -1;
/* Three lsb of Device address byte are bits 8-10 of eeaddress */
byte devaddr = DEVADDR | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
HWire.beginTransmission(devaddr);
HWire.write(int(addr));
HWire.endTransmission();
HWire.requestFrom(int(devaddr), 1);
if (HWire.available()) {
rdata = HWire.read();
}
return rdata;
}
int i2c_eeprom_write_byte(unsigned eeaddr, uint8_t data)
{
/* Three lsb of Device address byte are bits 8-10 of eeaddress */
byte devaddr = DEVADDR | ((eeaddr >> 8) & 0x07);
byte addr = eeaddr;
HWire.beginTransmission(devaddr);
HWire.write(int(addr));
HWire.write(char(data));
HWire.endTransmission();
delay(10);
return 0;
}
#endif
struct EERef {
EERef( const int index )
: index( index ) {}
/* Access/read members. */
#ifndef USE_I2C_EEPROM
uint8_t operator*() const { return EEPROM.read( (uint16_t) index ); }
#else
uint8_t operator*() const { return i2c_eeprom_read_byte( (unsigned) index ); }
#endif
operator const uint8_t() const { return **this; }
/* Assignment/write members. */
EERef &operator=( const EERef &ref ) { return *this = *ref; }
#ifndef USE_I2C_EEPROM
EERef &operator=( uint8_t in ) { return EEPROM.write( (uint16_t) index, in ), *this; }
#else
EERef &operator=( uint8_t in ) { return i2c_eeprom_write_byte( (unsigned) index, in ), *this; }
#endif
EERef &operator +=( uint8_t in ) { return *this = **this + in; }
EERef &operator -=( uint8_t in ) { return *this = **this - in; }
EERef &operator *=( uint8_t in ) { return *this = **this * in; }
EERef &operator /=( uint8_t in ) { return *this = **this / in; }
EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; }
EERef &operator %=( uint8_t in ) { return *this = **this % in; }
EERef &operator &=( uint8_t in ) { return *this = **this & in; }
EERef &operator |=( uint8_t in ) { return *this = **this | in; }
EERef &operator <<=( uint8_t in ) { return *this = **this << in; }
EERef &operator >>=( uint8_t in ) { return *this = **this >> in; }
EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
/* Prefix increment/decrement */
EERef& operator++() { return *this += 1; }
EERef& operator--() { return *this -= 1; }
/* Postfix increment/decrement */
uint8_t operator++ (int) {
uint8_t ret = **this;
return ++(*this), ret;
}
uint8_t operator-- (int){
uint8_t ret = **this;
return --(*this), ret;
}
int index; /* Index of current EEPROM cell. */
};
struct EEPtr {
EEPtr( const int index )
: index( index ) {}
operator const int() const { return index; }
EEPtr &operator=( int in ) { return index = in, *this; }
/* Iterator functionality. */
bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
EERef operator*() { return index; }
/* Prefix & Postfix increment/decrement */
EEPtr& operator++() { return ++index, *this; }
EEPtr& operator--() { return --index, *this; }
EEPtr operator++ (int) { return index++; }
EEPtr operator-- (int) { return index--; }
int index; /* Index of current EEPROM cell. */
};
struct EEPROMClass_EMU {
/* Basic user access methods. */
EERef operator[]( const int idx ) { return idx; }
uint8_t read( int idx ) { return EERef( idx ); }
void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
/* Functionality to 'get' and 'put' objects to and from EEPROM. */
template< typename T > T &get( int idx, T &t ) {
EEPtr e = idx;
uint8_t *ptr = (uint8_t*) &t;
for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
return t;
}
template< typename T > const T &put( int idx, const T &t ) {
EEPtr e = idx;
const uint8_t *ptr = (const uint8_t*) &t;
for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
return t;
}
};
static EEPROMClass_EMU EEPROM_EMU;
#ifndef USE_I2C_EEPROM
#define TYPE_EEPROM "_EMU"
#define EEPROM_update(a, b) EEPROM.update(a, b)
#define EEPROM_read(a) EEPROM.read(a)
#define EEPROM_write(a, b) EEPROM.write(a, b)
#else
#define TYPE_EEPROM "_I2C"
#define EEPROM_update(a, b) EEPROM_EMU.update(a, b)
#define EEPROM_read(a) EEPROM_EMU.read(a)
#define EEPROM_write(a, b) EEPROM_EMU.write(a, b)
#endif
#define EEPROM_get(a, b) EEPROM_EMU.get(a, b)
#define EEPROM_put(a, b) EEPROM_EMU.put(a, b)
#define BOARD "_STM32" TYPE_EEPROM
#else /* if AVR Arduino */
#include <EEPROM.h>
#define PIN_SDA 6
#define PIN_SCK 5
#define EEPROM_update(a, b) EEPROM.update(a, b)
#define EEPROM_read(a) EEPROM.read(a)
#define EEPROM_write(a, b) EEPROM.write(a, b)
#define EEPROM_get(a, b) EEPROM.get(a, b)
#define EEPROM_put(a, b) EEPROM.put(a, b)
#define BOARD "_AVR"
#endif /* End support board */
#define VERFW "v.1.0.04_RTL8367C"
#define MAGIC_EEPROM_START 0x8367
#define MAX_VLAN_GROUP 8
#define MAX_PORTS 5
/* If Port1 = P0, Port2 = P1...etc, please disable this define */
#define PORTS_INVERSION 1
#ifdef PORTS_INVERSION
#define VERSION VERFW BOARD "_INV"
#define PORT_INV(x) (4 - x)
#else
#define VERSION VERFW BOARD
#define PORT_INV(x) (x)
#endif
struct eeprom_vlan_record {
uint16_t vid;
uint8_t ports_mask;
uint8_t prio;
};
/* Magic EEPROM start + tag/untag masks group + idx + vlan record structure * 8 */
#define CFG_SIZE (2 + 2 + 1 + (sizeof(eeprom_vlan_record) * 8))
String readString;
#define CLK_DURATION(clk) delayMicroseconds(clk)
#define DELAY 3
#define ack_timer 10
/* smi */
void _smi_start()
{
/* change GPIO pin to Output only */
pinMode(PIN_SCK, OUTPUT);
pinMode(PIN_SDA, OUTPUT);
/* Initial state: SCK: 0, SDA: 1 */
digitalWrite(PIN_SCK, LOW);
digitalWrite(PIN_SDA, HIGH);
CLK_DURATION(DELAY);
/* CLK 1: 0 -> 1, 1 -> 0 */
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, LOW);
CLK_DURATION(DELAY);
/* CLK 2: */
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SDA, LOW);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, LOW);
CLK_DURATION(DELAY);
digitalWrite(PIN_SDA, HIGH);
}
void _smi_writeBit(uint16_t signal, uint32_t bitLen)
{
for ( ; bitLen > 0; bitLen--) {
CLK_DURATION(DELAY);
/* prepare data */
if (signal & (1UL << (bitLen - 1)))
digitalWrite(PIN_SDA, HIGH);
else
digitalWrite(PIN_SDA, LOW);
CLK_DURATION(DELAY);
/* clocking */
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, LOW);
}
}
void _smi_readBit(uint32_t bitLen, uint32_t *rData)
{
unsigned long u;
/* change GPIO pin to Input only */
pinMode(PIN_SDA, INPUT);
for (*rData = 0; bitLen > 0; bitLen--) {
CLK_DURATION(DELAY);
/* clocking */
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
u = (digitalRead(PIN_SDA) == HIGH) ? 1 : 0;
digitalWrite(PIN_SCK, LOW);
*rData |= (u << (bitLen - 1));
}
/* change GPIO pin to Output only */
pinMode(PIN_SDA, OUTPUT);
}
void _smi_stop()
{
CLK_DURATION(DELAY);
digitalWrite(PIN_SDA, LOW);
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SDA, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, HIGH);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, LOW);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, HIGH);
/* add a click */
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, LOW);
CLK_DURATION(DELAY);
digitalWrite(PIN_SCK, HIGH);
/* change GPIO pin to Input only */
pinMode(PIN_SDA, INPUT);
pinMode(PIN_SCK, INPUT);
}
uint32_t smi_read(uint32_t mAddrs)
{
uint32_t rawData = 0, rData = 0, ACK = 0;
uint8_t con;
_smi_start(); /* Start SMI */
_smi_writeBit(0x0b, 4); /* CTRL code: 4'b1011 for RTL8370 */
_smi_writeBit(0x4, 3); /* CTRL code: 3'b100 */
_smi_writeBit(0x1, 1); /* 1: issue READ command */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for issuing READ command */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_read_0...timeout!"));
_smi_writeBit((mAddrs & 0xff), 8); /* Set reg_addr[7:0] */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for setting reg_addr[7:0] */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_read_1...timeout!"));
_smi_writeBit((mAddrs >> 8), 8); /* Set reg_addr[15:8] */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK by RTL836x */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_read_2...timeout!"));
_smi_readBit(8, &rawData); /* Read DATA [7:0] */
rData = rawData & 0xff;
_smi_writeBit(0x00, 1); /* ACK by CPU */
_smi_readBit(8, &rawData); /* Read DATA [15: 8] */
_smi_writeBit(0x01, 1); /* ACK by CPU */
rData |= (rawData << 8);
_smi_stop();
return rData;
}
void smi_write(uint32_t mAddrs, uint32_t rData)
{
int8_t con;
uint32_t ACK = 0;
_smi_start(); /* Start SMI */
_smi_writeBit(0x0b, 4); /* CTRL code: 4'b1011 for RTL836x */
_smi_writeBit(0x4, 3); /* CTRL code: 3'b100 */
_smi_writeBit(0x0, 1); /* 0: issue WRITE command */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for issuing WRITE command */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_write_0...timeout!"));
_smi_writeBit((mAddrs & 0xff), 8); /* Set reg_addr[7:0] */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for setting reg_addr[7:0] */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_write_1...timeout!"));
_smi_writeBit((mAddrs >> 8), 8); /* Set reg_addr[15:8] */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK or setting reg_addr[15:8] */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_write_2...timeout!"));
_smi_writeBit(rData & 0xff, 8); /* Write Data [7:0] out */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for writting data [7:0] */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_write_3...timeout!"));
_smi_writeBit(rData >> 8, 8); /* Write Data [15:8] out */
con = 0;
do {
con++;
_smi_readBit(1, &ACK); /* ACK for writting data [15:8] */
} while ((ACK != 0) && (con < ack_timer));
if (ACK != 0) Serial.println(F("smi_write_4...timeout!"));
_smi_stop();
}
/* End smi */
/* rtl8367c_asicdrv */
#define RTL8367C_REGBITLENGTH 16
#define RTL8367C_REGDATAMAX 0xffff
int rtl8367c_setAsicRegBit(uint32_t reg, uint32_t bit, uint32_t value)
{
if(bit >= RTL8367C_REGBITLENGTH)
return -1;
uint32_t regData = smi_read(reg);
if(value)
regData = regData | (1UL << bit);
else
regData = regData & (~(1UL << bit));
smi_write(reg, regData);
return 0;
}
int rtl8367c_getAsicRegBit(uint32_t reg, uint32_t bit, uint32_t *pValue)
{
if(bit >= RTL8367C_REGBITLENGTH)
return -1;
uint32_t regData = smi_read(reg);
*pValue = (regData & (1UL << bit)) >> bit;
return 0;
}
int rtl8367c_setAsicRegBits(uint32_t reg, uint32_t bits, uint32_t value)
{
if(bits >= (1UL << RTL8367C_REGBITLENGTH))
return -1;
uint32_t bitsShift = 0;
while(!(bits & (1UL << bitsShift)))
{
bitsShift++;
if(bitsShift >= RTL8367C_REGBITLENGTH)
return -1;
}
uint32_t valueShifted = (unsigned long)value << bitsShift;
if(valueShifted > RTL8367C_REGDATAMAX)
return -1;
uint32_t regData = smi_read(reg);
regData = regData & (~bits);
regData = regData | (valueShifted & bits);
smi_write(reg, regData);
return 0;
}
int rtl8367c_getAsicRegBits(uint32_t reg, uint32_t bits, uint32_t *pValue)
{
if(bits >= (1UL << RTL8367C_REGBITLENGTH))
return -1;
uint32_t bitsShift = 0;
while(!(bits & (1UL << bitsShift)))
{
bitsShift++;
if(bitsShift >= RTL8367C_REGBITLENGTH)
return -1;
}
uint32_t regData = smi_read(reg);
*pValue = (regData & bits) >> bitsShift;
return 0;
}
int rtl8367c_setAsicReg(uint32_t reg, uint32_t value)
{
smi_write(reg, value);
return 0;
}
int rtl8367c_getAsicReg(uint32_t reg, uint32_t *pValue)
{
*pValue = smi_read(reg);
return 0;
}
/* End rtl8367c_asicdrv */
/* rtl8367c_asicdrv_phy */
#define RTL8367C_CMD_MASK 1
#define RTL8367C_RW_MASK 2
#define RTL8367C_PHY_INTERNALNOMAX 4
#define RTL8367C_PHY_OFFSET 5
#define RTL8367C_PHY_EXTERNALMAX 7
#define RTL8367C_PHY_REGNOMAX 0x1f
#define RTL8367C_CFG_CPU_OCPADR_MSB_MASK 0xfc0
#define RTL8367C_REG_GPHY_OCP_MSB_0 0x1d15
#define RTL8367C_REG_INDRECT_ACCESS_CTRL 0x1f00
#define RTL8367C_REG_INDRECT_ACCESS_STATUS 0x1f01
#define RTL8367C_REG_INDRECT_ACCESS_ADDRESS 0x1f02
#define RTL8367C_REG_INDRECT_ACCESS_WRITE_DATA 0x1f03
#define RTL8367C_REG_INDRECT_ACCESS_READ_DATA 0x1f04
#define RTL8367C_PHY_BASE 0x2000
int rtl8367c_setAsicPHYReg(uint32_t phyNo, uint32_t phyAddr, uint32_t value)
{
uint32_t busyFlag = 0, checkCounter = 100, regData;
if(phyNo > RTL8367C_PHY_INTERNALNOMAX)
return -1;
if(phyAddr > RTL8367C_PHY_REGNOMAX)
return -1;
/* Check internal phy access busy or not */
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag)
return -2;
/* Default OCP Address */
rtl8367c_setAsicRegBits(RTL8367C_REG_GPHY_OCP_MSB_0, RTL8367C_CFG_CPU_OCPADR_MSB_MASK, 0x29);
/* prepare access data */
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_WRITE_DATA, value);
/* prepare access address */
regData = RTL8367C_PHY_BASE | ((unsigned long)phyNo << RTL8367C_PHY_OFFSET) | phyAddr;
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_ADDRESS, regData);
/* Set WRITE Command */
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_CTRL, RTL8367C_CMD_MASK | RTL8367C_RW_MASK);
while(checkCounter) {
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag) {
checkCounter--;
if(!checkCounter) {
Serial.println(F("rtl8367c_setAsicPHYReg...timeout!"));
return -2;
}
} else
checkCounter = 0;
}
return 0;
}
int rtl8367c_getAsicPHYReg(uint32_t phyNo, uint32_t phyAddr, uint32_t *value)
{
uint32_t busyFlag = 0, checkCounter = 100, regData;
if(phyNo > RTL8367C_PHY_INTERNALNOMAX)
return -1;
if(phyAddr > RTL8367C_PHY_REGNOMAX)
return -1;
/* Check internal phy access busy or not */
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag)
return -2;
/* Default OCP Address */
rtl8367c_setAsicRegBits(RTL8367C_REG_GPHY_OCP_MSB_0, RTL8367C_CFG_CPU_OCPADR_MSB_MASK, 0x29);
/* prepare access address */
regData = RTL8367C_PHY_BASE | ((unsigned long)phyNo << RTL8367C_PHY_OFFSET) | phyAddr;
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_ADDRESS, regData);
/* Set READ Command */
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_CTRL, RTL8367C_CMD_MASK);
while(checkCounter) {
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag) {
checkCounter--;
if(!checkCounter) {
Serial.println(F("rtl8367c_getAsicPHYReg...timeout!"));
return -2;
}
} else
checkCounter = 0;
}
/* get PHY register */
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_READ_DATA, ®Data);
*value = regData;
return 0;
}
int rtl8367c_setAsicPHYOCPReg(uint32_t phyNo, uint32_t ocpAddr, uint32_t ocpData)
{
uint32_t busyFlag = 0, checkCounter = 100, regData;
uint32_t ocpAddrPrefix, ocpAddr9_6, ocpAddr5_1;
if(phyNo > RTL8367C_PHY_INTERNALNOMAX)
return -1;
if(ocpAddr > RTL8367C_PHY_REGNOMAX)
return -1;
/* Check internal phy access busy or not */
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag)
return -2;
/* OCP prefix */
ocpAddrPrefix = ((ocpAddr & 0xFC00) >> 10);
rtl8367c_setAsicRegBits(RTL8367C_REG_GPHY_OCP_MSB_0, RTL8367C_CFG_CPU_OCPADR_MSB_MASK, ocpAddrPrefix);
/* prepare access data */
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_WRITE_DATA, ocpData);
/* prepare access address */
ocpAddr9_6 = ((ocpAddr >> 6) & 0x000F);
ocpAddr5_1 = ((ocpAddr >> 1) & 0x001F);
regData = RTL8367C_PHY_BASE | (ocpAddr9_6 << 8) | (phyNo << RTL8367C_PHY_OFFSET) | ocpAddr5_1;
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_ADDRESS, regData);
/* Set WRITE Command */
rtl8367c_setAsicReg(RTL8367C_REG_INDRECT_ACCESS_CTRL, RTL8367C_CMD_MASK | RTL8367C_RW_MASK);
while(checkCounter) {
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag) {
checkCounter--;
if(!checkCounter) {
Serial.println(F("rtl8367c_setAsicPHYOCPReg...timeout!"));
return -2;
}
} else
checkCounter = 0;
}
return 0;
}
int rtl8367c_getAsicPHYOCPReg(uint32_t phyNo, uint32_t ocpAddr, uint32_t *pRegData)
{
uint32_t busyFlag = 0, checkCounter = 100, regData;
uint32_t ocpAddrPrefix, ocpAddr9_6, ocpAddr5_1;
if(phyNo > RTL8367C_PHY_INTERNALNOMAX)
return -1;
if(ocpAddr > RTL8367C_PHY_REGNOMAX)
return -1;
/* Check internal phy access busy or not */
rtl8367c_getAsicReg(RTL8367C_REG_INDRECT_ACCESS_STATUS, &busyFlag);
if(busyFlag)
return -2;
/* OCP prefix */
ocpAddrPrefix = ((ocpAddr & 0xFC00) >> 10);
rtl8367c_setAsicRegBits(RTL8367C_REG_GPHY_OCP_MSB_0, RTL8367C_CFG_CPU_OCPADR_MSB_MASK, ocpAddrPrefix);
/* prepare access address */
ocpAddr9_6 = ((ocpAddr >> 6) & 0x000F);
ocpAddr5_1 = ((ocpAddr >> 1) & 0x001F);