-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
SpdReaderWriter.ino
1445 lines (1124 loc) · 33.4 KB
/
SpdReaderWriter.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
/*
Arduino based EEPROM SPD reader and writer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For overclockers and PC hardware enthusiasts
Repos: https://github.com/1a2m3/SPD-Reader-Writer
Support: https://forums.evga.com/FindPost/3053544
Donate: https://paypal.me/mik4rt3m
PS: DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!
CONFIGURABLE SETTINGS ARE IN "SpdReaderWriterSettings.h" FILE
*/
#include <Wire.h>
#include <EEPROM.h>
#include "SpdReaderWriterSettings.h" // Settings
#define FW_VER 20231207 // Firmware version number (YYYYMMDD)
// RAM RSWP support bitmasks
#define DDR5 _BV(5) // Offline mode
#define DDR4 _BV(4) // VHV control
#define DDR3 _BV(3) // VHV+SA1 controls
// SPD5 hub registers
#define MR0 0x00 // Device Type; Most Significant Byte
#define MR1 0x01 // Device Type; Least Significant Byte
#define MR6 0x06 // Device Write Recovery Time Capability
#define MR11 0x0B // I2C Legacy Mode Device Configuration
#define MR12 0x0C // Write Protection For NVM Blocks [7:0]
#define MR13 0x0D // Write Protection for NVM Blocks [15:8]
#define MR14 0x0E // Device Configuration - Host and Local Interface IO
#define MR18 0x12 // Device Configuration
#define MR20 0x14 // Clear Register MR52 Error Status Command
#define MR48 0x30 // Device Status
#define MR52 0x34 // Hub, Thermal and NVM Error Status
#define PMIC 0b1001 << 3 // PMIC local device type ID
// SPD5 hub device type data
#define SPD5_NO 0x5108 // SPD5 Hub Device
#define SPD5_TS 0x5118 // SPD5 Hub Device w/ Temp Sensor
// DDR4 EEPROM page commands
#define SPA0 0x6C // Set Page Address 0
#define SPA1 0x6E // Set Page Address 1
#define RPA 0x6D // Read Page Address
// EEPROM RSWP commands
#define RPS0 0x63 // Read SWP0 status (offsets 0-127) (DDR4/DDR3/DDR2)
#define RPS1 0x69 // Read SWP1 status (offsets 128-255) (DDR4)
#define RPS2 0x6B // Read SWP2 status (offsets 256-383) (DDR4)
#define RPS3 0x61 // Read SWP3 status (offsets 384-511) (DDR4)
#define SWP0 0x62 // Set RSWP for block 0 (offsets 0-127) (DDR4/DDR3/DDR2) *
#define SWP1 0x68 // Set RSWP for block 1 (offsets 128-255) (DDR4)
#define SWP2 0x6A // Set RSWP for block 2 (offsets 256-383) (DDR4)
#define SWP3 0x60 // Set RSWP for block 3 (offsets 384-511) (DDR4) *
#define CWP 0x66 // Clear RSWP (DDR4/DDR3/DDR2) *
// EEPROM PSWP commands
#define PWPB 0b0110 // PSWP Device Type Identifier Control Code (bits 7-4) (DDR3/DDR2)
// EEPROM data
#define DNC 0x00 // "Do not care" byte
// Device responses
#define RESPONSE '&'
#define ALERT '@'
#define UNKNOWN '?'
// Templates
#define A1_MASK 0b11001100 // ScanBus() bitmask response when SA1 is high: 82-83, 86-87
// Device alerts
#define SLAVEINC '+'
#define SLAVEDEC '-'
#define CLOCKINC '/'
#define CLOCKDEC '\\'
// Device name settings
#define NAMELENGTH 16
char deviceName[NAMELENGTH];
// Device settings
#define DEVICESETTINGS 0x20 // EEPROM location to store device settings
#define CLOCKMODE 0 // Bit position for I2C clock settings
#define FASTMODE true
#define STDMODE false
// I2C clock frequencies
int32_t clock[] = { 100000, 400000 };
// Global variables
uint32_t i2cClock = clock[0]; // Initial I2C clock
uint8_t eepromPageAddress; // Initial EEPROM page address
uint8_t slaveCountCurrent; // Current number of slave addresses on I2C bus
uint8_t slaveCountLast; // Last number of slave addresses on I2C bus
bool i2cClockCurrent; // Current I2C clock mode
bool i2cClockLast; // Last I2C clock mode
bool cmdExecuting; // Indicates an input command is being executed
uint8_t responseBuffer[32]; // Response body buffer
uint8_t responseLength; // Output response body length and index
// Device commands
enum Command : uint8_t {
Get = ((uint8_t)-1), // Gets current value
Disable = 0, // Resets variable value to default
Enable, // Modifies variable value
ReadByte, // Read byte
WriteByte, // Write byte
WritePage, // Write page
WriteTest, // Write protection test
Ddr4Detect, // DDR4 detection
Ddr5Detect, // DDR5 detection
Spd5HubReg, // Access SPD5 Hub register space
Size, // Get EEPROM size
ScanBus, // Scan I2C bus
BusClock, // I2C clock control
ProbeAddress, // Probe I2C address
PinControl, // Config pin control
PinReset, // Reset config pins state to defaults
Rswp, // RSWP operation
Pswp, // PSWP operation
RswpReport, // Report current RSWP capabilities
Version, // Get Firmware version
Test, // Device Communication Test
Name, // Name controls
FactoryReset, // Restore device settings to default
};
// Config pin enum
enum pin {
HV_FEEDBACK = -1, // VHV feedback pin
HV_SWITCH, // Pin to toggle VHV on SA0 pin
SA1_SWITCH, // Pin to toggle SA1 state
};
// Pin data struct
typedef struct {
pin number;
const int name;
bool defaultState;
uint8_t mode;
} pinData;
// Configuration pins array
pinData ConfigPin[] = {
{ HV_SWITCH, HV_EN, false, OUTPUT }, // HV control
{ SA1_SWITCH, SA1_EN, true, OUTPUT }, // SA1 control
{ HV_FEEDBACK, HV_FB, false, INPUT }, // HV feedback
};
size_t pinCount = sizeof(ConfigPin) / sizeof(ConfigPin[0]);
void setup() {
// Set up config pins
for (uint8_t i = 0; i < pinCount; i++) {
pinMode(ConfigPin[i].name, ConfigPin[i].mode);
}
// Reset config pins
resetPins();
// Initiate and join the I2C bus as a master
Wire.begin();
// Set I2C timeout to 0.01 sec.
Wire.setWireTimeout(10000, true);
// Setup I2C clock
Wire.setClock(clock[getI2cClockMode()]);
i2cClockCurrent = clock[getI2cClockMode()];
i2cClockLast = i2cClockCurrent;
// Scan I2C bus
slaveCountCurrent = getQuantity();
slaveCountLast = slaveCountCurrent;
// Reset DDR4 EEPROM page address
setDdr4PageAddress(0);
// Start serial data transmission
PORT.begin(BAUD_RATE);
PORT.setTimeout(100); // Input timeout in ms
// Wait for serial port connection or initialization
while (!PORT) {}
// Check hardware
#ifndef __AVR__
PORT.write(UNKNOWN);
while (true) {}
#endif
// Send a true response when the device is ready
Respond(true);
OutputResponse();
}
void loop() {
resetPinsInternal();
// Wait for input data
if (PORT.available()) {
parseCommand();
}
// Monitor I2C bus
i2cMonitor();
}
// Process input commands and data
void parseCommand() {
if (!PORT.available()) {
cmdExecuting = false;
return;
}
cmdExecuting = true;
switch ((uint8_t)PORT.read()) {
// Read byte
case Command::ReadByte:
cmdRead();
break;
// Write byte
case Command::WriteByte:
cmdWriteByte();
break;
// Write page
case Command::WritePage:
cmdWritePage();
break;
// Scan I2C bus for addresses
case Command::ScanBus:
cmdScanBus();
break;
// Probe if I2C address is present
case Command::ProbeAddress:
cmdProbeBusAddress();
break;
// I2C bus settings
case Command::BusClock:
cmdBusClock();
break;
// Control digital pins
case Command::PinControl:
cmdPinControl();
break;
case Command::PinReset:
cmdPinReset();
break;
// RSWP controls
case Command::Rswp:
cmdRSWP();
break;
// PSWP controls
case Command::Pswp:
cmdPSWP();
break;
case Command::WriteTest:
cmdWriteTest();
break;
// Get Firmware version
case Command::Version:
cmdVersion();
break;
// Device Communication Test
case Command::Test:
cmdTest();
break;
// Report supported RSWP capabilities
case Command::RswpReport:
cmdRswpRespond();
break;
// DDR4 detection test
case Command::Ddr4Detect:
cmdDdr4Detect();
break;
// DDR5 detection test
case Command::Ddr5Detect:
cmdDdr5Detect();
break;
// DDR5 Hub register
case Command::Spd5HubReg:
cmdSpd5Hub();
break;
// Get EEPROM size
case Command::Size:
cmdSize();
break;
// Device name controls
case Command::Name:
cmdName();
break;
// Factory defaults restore
case Command::FactoryReset:
cmdFactoryReset();
break;
}
// Output response
OutputResponse();
cmdExecuting = false;
}
/* -= Response handlers =- */
// Put single byte into response
void Respond(uint8_t inputData) {
responseBuffer[responseLength] = inputData;
responseLength++;
}
// Put byte array into response
void Respond(uint8_t* inputData, size_t length) {
for (uint8_t i = 0; i < length; i++) {
Respond(inputData[i]);
}
}
// Put string into response
void Respond(String inputData) {
for (uint8_t i = 0; i < inputData.length(); i++) {
Respond(inputData[i]);
}
}
// Output response header, size, contents, and checksum
void OutputResponse() {
if (responseLength > 0) {
// Calculate checksum
uint8_t checkSum = 0;
for (uint8_t i = 0; i < responseLength; i++) {
checkSum += responseBuffer[i];
}
PORT.write(RESPONSE);
PORT.write(responseLength);
PORT.write(responseBuffer, responseLength);
PORT.write(checkSum);
// Wait for output to complete
PORT.flush();
// Reset response data buffer index
responseLength = 0;
// Clear response array
memset(responseBuffer, 0x00, sizeof(responseBuffer));
}
}
/* -= Command handlers =- */
void cmdRead() {
// Input buffer
uint8_t buffer[4] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// EEPROM address
uint8_t address = buffer[0];
// Offset address
uint16_t offset = buffer[1] << 8 | buffer[2];
// Byte count
uint8_t length = buffer[3];
// Output buffer
uint8_t data[length];
// Fill the data buffer
readByte(address, offset, length, data);
Respond(data, sizeof(data));
}
void cmdWriteByte() {
// Input buffer
uint8_t buffer[4] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// EEPROM address
uint8_t address = buffer[0];
// Offset address
uint16_t offset = buffer[1] << 8 | buffer[2];
// Input byte value
uint8_t data = buffer[3];
Respond(writeByte(address, offset, data));
}
void cmdWritePage() {
// Input buffer
uint8_t buffer[4] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// EEPROM address
uint8_t address = buffer[0];
// Offset address
uint16_t offset = buffer[1] << 8 | buffer[2];
// Bytes count
uint8_t length = buffer[3];
// Validate input length
if (length == 0) {
Respond(0);
return;
}
// Input data buffer
uint8_t data[length];
PORT.readBytes(data, sizeof(data));
if (length > 16) {
Respond(false);
return;
}
Respond(writePage(address, offset, length, data));
}
void cmdScanBus() {
Respond(scanBus());
}
void cmdTest() {
Respond(true);
}
void cmdRswpRespond() {
Respond(rswpSupportTest());
}
void cmdDdr4Detect() {
// Input buffer
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
uint8_t address = buffer[0]; // I2C address
Respond(ddr4Detect(address));
}
void cmdDdr5Detect() {
// Input buffer
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
uint8_t address = buffer[0]; // I2C address
Respond(ddr5Detect(address));
}
void cmdSpd5Hub() {
// Input buffer
uint8_t buffer[3] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
uint8_t address = buffer[0]; // I2C address
uint8_t memReg = buffer[1]; // register
uint8_t command = buffer[2]; // command
if (!ddr5Detect(address)) {
Respond(false);
}
// Write to register
if (command == Command::Enable) {
// Data buffer
uint8_t data[1] = { 0 }; // Byte value
PORT.readBytes(data, sizeof(data));
Respond(writeReg(address, memReg, data[0]));
}
// Read from register
else if (command == Command::Get) {
Respond(readReg(address, memReg));
}
// Unrecognized command
else {
Respond(false);
}
}
void cmdSize() {
// Input buffer
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
uint8_t address = buffer[0]; // I2C address
uint16_t size = 0; // 0
if (probeBusAddress(address)) {
if (validateEepromAddress(address)) { // EEPROM
if (ddr5Detect(address)) {
size = 1024; // 3
}
else {
if (getQuantity() == 1) {
if (ddr4Detect()) {
size = 512; // 2
}
else {
size = 256; // 1
}
}
else if (getQuantity() > 1) {
if (!ddr4Detect()) {
size = 256; // 1
}
}
}
}
else if (validatePmicAddress(address)) { // PMIC
size = 256; // 1
}
}
if (!size) {
// Read byte 0x02
uint8_t keyByte[1] = { 0 };
readByte(address, 0x02, 1, keyByte);
if (0x0C <= keyByte[0] && keyByte[0] <= 0x11) {
// DDR4, DDR4E, LPDDR3, LPDDR4, and LPDDR4X
size = 512; // 2
}
}
// Return bit position matching SpdReaderWriterDll.Spd.DataLength.Length array index
for (uint8_t i = 0; i <= 3; i++) {
if(bitRead(highByte(size), i)) {
Respond(i + 1);
return;
}
}
Respond(0);
}
void cmdVersion() {
uint8_t verLength = sizeof(FW_VER);
uint8_t data[verLength];
for (int8_t i = verLength; i > 0; i--) {
data[i - 1] = FW_VER >> (8 * (i - 1));
}
Respond(data, verLength);
}
void cmdName() {
// Data buffer for command byte
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// Get name
if (buffer[0] == Command::Get) {
Respond(getName());
}
// Set name
else if (buffer[0] > 0 && buffer[0] <= NAMELENGTH) {
// prepare name buffer
char name[buffer[0] + 1];
// read name and put it into buffer
PORT.readBytes(name, buffer[0]);
// set last byte to \0 where the string ends
name[buffer[0]] = 0;
Respond(setName(name));
}
// Invalid command
else {
Respond(false);
}
}
void cmdProbeBusAddress() {
// Data buffer for address
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
uint8_t address = buffer[0]; // I2C address
Respond(probeBusAddress(address));
}
void cmdBusClock() {
// Data buffer for clock mode
uint8_t buffer[1] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// Set I2C clock
if (buffer[0] == FASTMODE || buffer[0] == STDMODE) {
setI2cClockMode(buffer[0]);
Respond(getI2cClockMode() == buffer[0]);
}
// Get current I2C clock
else if (buffer[0] == Command::Get) {
Respond(getI2cClockMode());
}
// Unrecognized command
else {
Respond(false);
}
}
void cmdFactoryReset() {
Respond(factoryReset());
}
void cmdRSWP() {
// Data buffer
uint8_t buffer[3] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// I2C address
uint8_t address = buffer[0];
// Block number
uint8_t block = buffer[1];
// Block state
char state = buffer[2];
// enable RSWP
if (state == Command::Enable) {
Respond(setRswp(address, block));
}
// clear RSWP (all blocks)
else if (state == Command::Disable) {
Respond(clearRswp(address));
}
// get RSWP status
else if (state == Command::Get) {
Respond(getRswp(address, block));
}
// unrecognized RSWP command
else {
Respond(false);
}
}
void cmdPSWP() {
// Data buffer
uint8_t buffer[2] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// EEPROM address
uint8_t address = buffer[0];
// PSWP state
char state = buffer[1];
// enable PSWP
if (state == Command::Enable) {
Respond(setPswp(address));
}
// read PSWP
else if (state == Command::Get) {
Respond(getPswp(address));
}
// unknown state
else {
Respond(false);
}
}
void cmdWriteTest() {
// Data buffer
uint8_t buffer[3] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// EEPROM address
uint8_t address = buffer[0];
// Offset address
uint16_t offset = buffer[1] << 8 | buffer[2];
uint8_t data[1];
Respond(readByte(address, offset, 1, data) && writeByte(address, offset, data[0]));
}
void cmdPinControl() {
// Data buffer
uint8_t buffer[2] = { 0 };
PORT.readBytes(buffer, sizeof(buffer));
// Pin number
uint8_t pin = buffer[0];
// Pin state
uint8_t state = buffer[1];
// SA1 controls
if (pin == SA1_SWITCH) {
// Toggle SA1 state
if (state == Command::Enable || state == Command::Disable) {
Respond(setConfigPin(SA1_EN, state));
}
// Get SA1 state
else if (state == Command::Get) {
Respond(getConfigPin(SA1_EN));
}
// Unknown state
else {
Respond(false);
}
}
// VHV 9V controls
else if (pin == HV_SWITCH) {
// Toggle HV state
if (state == Command::Enable || state == Command::Disable) {
Respond(setHighVoltage(state));
}
// Get HV state
else if (state == Command::Get) {
Respond(getHighVoltage());
}
// Unknown state
else {
Respond(0);
}
}
// Unknown pin
else {
Respond(false);
}
}
void cmdPinReset() {
Respond(resetPins());
}
/* -= Read/Write functions =- */
// Reads bytes from EEPROM into data buffer
bool readByte(uint8_t address, uint16_t offset, uint8_t length, uint8_t* data) {
uint8_t _offset = lowByte(offset);
if (ddr5Detect(address)) {
_offset |= 0x80;
}
adjustPageAddress(address, offset);
Wire.beginTransmission(address);
Wire.write(_offset);
if (Wire.endTransmission(false) != 0) {
return false;
}
Wire.requestFrom(address, length);
while (Wire.available() < length) {}
// Fill data buffer
for (uint8_t i = 0; i < length; i++) {
while (!Wire.available()) {}
data[i] = Wire.read();
}
return true;
}
// Writes a single byte to EEPROM
bool writeByte(uint8_t address, uint16_t offset, uint8_t data) {
uint8_t input[1] = { data };
return writePage(address, offset, 1, input);
}
// Writes a page (multiple bytes) to EEPROM
bool writePage(uint8_t address, uint16_t offset, uint8_t length, uint8_t* data) {
// Check offset and length to avoid page or block boundary overlapping
if ((offset % 16 + length) > 16) {
return false;
}
// Check if the block is write protected
if (ddr5Detect(address) && ddr5GetOfflineMode()) {
uint8_t block = offset / 64;
if (getRswp(address, block)) {
return false;
}
}
uint16_t _offset = offset;
if (ddr5Detect(address)) {
_offset |= 0x80;
// Wait for write completion
while (bitRead(readReg(address, MR48), 3)) {}
}
adjustPageAddress(address, offset);
Wire.beginTransmission(address);
Wire.write((uint8_t)(_offset));
Wire.write(data, length);
uint8_t status = Wire.endTransmission();
delay(10);
return status == 0;
}
// Reads data from SPD5 hub register (/w reset page fix)
uint8_t readReg(uint8_t address, uint8_t memReg) {
return readReg(address, memReg, true);
}
// Reads data from SPD5 hub register
uint8_t readReg(uint8_t address, uint8_t memReg, bool resetPage) {
if (memReg >= 128) {
return false;
}
if (resetPage) {
// Reset page to 0
adjustPageAddress(address, 0);
}
Wire.beginTransmission(address);
Wire.write(memReg & 0x7F);
uint8_t status = Wire.endTransmission(false);
if (status != 0) {
return false;
}
Wire.requestFrom(address, (uint8_t)1);
// Fill data buffer
while (!Wire.available()) {}
uint8_t output = Wire.read();
return output;
}
// Writes data to SPD5 hub register
bool writeReg(uint8_t address, uint8_t memReg, uint8_t value) {
if ( memReg >= 128 || !(MR11 <= memReg && memReg <= MR13)) {
return false;
}
Wire.beginTransmission(address);
Wire.write(memReg);
Wire.write(value);
// Writing to MR12/MR13 registers must be followed by Stop operation to allow SPD hub to update
uint8_t status = Wire.endTransmission();
// The SPD5 Hub device does not incur any delay to switch from one page to another page
delay(memReg == MR11 ? 0 : 10);
return status == 0;
}
/* -= RSWP functions =- */
// Sets reversible write protection on specified block
bool setRswp(uint8_t address, uint8_t block) {
if (block > 15) {
return false;
}
// DDR5 RSWP
if (ddr5Detect(address)) {
// Select register
uint8_t memReg = MR12 + bitRead(block, 3);
// Existing RSWP value
uint8_t currentValue = readReg(address, memReg);
// Updated RSWP value
uint8_t updatedValue = 1 << (block & 0b111);
return writeReg(address, memReg, currentValue | updatedValue);
}
// DDR4 & older RSWP
uint8_t commands[] = { SWP0, SWP1, SWP2, SWP3 };
uint8_t cmd = commands[(0 < block && block <= 3) ? block : 0];
bool result = false;
if (setHighVoltage(true)) {
if (block == 0) {
setConfigPin(SA1_EN, false); // Required for pre-DDR4
}
if (block > 0 && !ddr4Detect()) {
result = false;
}
else {
result = probeDeviceTypeId(cmd);
}
resetPins();
}
return result;
}
// Reads reversible write protection status
bool getRswp(uint8_t address, uint8_t block) {
if (ddr5Detect(address)) {
return (block <= 15)
? readReg(address, MR12 + bitRead(block, 3)) & (1 << (block & 0b111))
: false;
}
uint8_t commands[] = { RPS0, RPS1, RPS2, RPS3 };
uint8_t cmd = (0 < block && block <= 3) ? commands[block] : commands[0];
// Jedec EE1002(A), TSE2002av compliance
if (block == 0 && !ddr4Detect()) {
setHighVoltage(true);
}
bool status = probeDeviceTypeId(cmd); // true/ack = not protected
resetPins();
return !status; // true = protected or rswp not supported; false = unprotected
}
// Clears reversible software write protection
bool clearRswp(uint8_t address) {
if (ddr5Detect(address)) {
return ddr5GetOfflineMode()
? writeReg(address, MR12, 0) && readReg(address, MR12) == 0 &&
writeReg(address, MR13, 0) && readReg(address, MR13) == 0
: false;
}
if (!ddr4Detect(address)) {
setConfigPin(SA1_EN, true); // Required for pre-DDR4
}
if (setHighVoltage(true)) {
bool result = probeDeviceTypeId(CWP);
resetPins();
return result;
}
return false;
}
// Test RSWP support capabilities
uint8_t rswpSupportTest() {
// Reset config pins and HV state
resetPins();
// Scan I2C bus
if (!scanBus()) {
return 0;
}
// Supported RAM value
uint8_t rswpSupport = 0;
// RSWP DDR5 test
if (ddr5GetOfflineMode()) {
rswpSupport |= DDR5;
}
// RSWP VHV test
if (setHighVoltage(true)) {