1616#include < EEPROM.h>
1717#include " SpdReaderWriterSettings.h" // Settings
1818
19- #define VERSION 20211111 // Version number (YYYYMMDD)
19+ #define VERSION 20211119 // Version number (YYYYMMDD)
2020
21- // RSWP RAM support bitmask
22- #define DDR2 (1 << 2 )
23- #define DDR3 (1 << 3 )
24- #define DDR4 (1 << 4 )
25- #define DDR5 (1 << 5 )
21+ // RSWP RAM support bitmasks
22+ #define DDR5 (1 << 5 ) // Offline mode control
23+ #define DDR4 (1 << 4 ) // VHV control
24+ #define DDR3 (1 << 3 ) // VHV+SA1 controls
2625
2726// SPD5 hub registers
2827#pragma region SPD5 hub registers
106105#define DDR4DETECT ' 4' // DDR4 detection test
107106#define DDR5DETECT ' 5' // DDR5 detection test
108107#define RETESTRSWP ' e' // Reevaluate RSWP capabilities
108+ #define FACTORYRESET ' -' // Factory reset device settings
109109#pragma endregion
110110
111111// Device pin names
121121#define UNKNOWN (char ) ' ?'
122122
123123// Pin states
124- #define ON HIGH
125- #define OFF LOW
126- #define ENABLE 1
127- #define SET 1
128- #define DISABLE 0
129- #define RESET 0
130- #define GET ' ?' // Suffix added to commands to return current state
124+ #define ON HIGH
125+ #define OFF LOW
126+ #define ENABLE (byte) 0x01
127+ #define SET (byte) 0x01
128+ #define DISABLE (byte) 0x00
129+ #define RESET (byte) 0x00
130+ #define GET ( char ) ' ?' // Suffix added to commands to return current state
131131
132132// Device name settings
133133#define NAMELENGTH 16
134134char deviceName[NAMELENGTH];
135135
136- // Device i2c clock settings
137- uint8_t clockSettings = 0x20 ; // EEPROM location to store I2C clock
138- uint32_t i2cClock = 100000L ; // Initial I2C clock
136+ // Device settings
137+ #define DEVICESETTINGS 0x20 // EEPROM location to store device settings
138+ #define CLOCKMODE 0 // Bit position for I2C clock settings
139+ #define FASTMODE true
140+ #define STDMODE false
139141
140142// Global variables
141- uint8_t rswpSupport = 0 ; // Bitmask representing RAM RSWP support
143+ uint32_t i2cClock = 100000L ; // Initial I2C clock
144+ uint8_t rswpSupport = 0 ; // Bitmask representing RAM RSWP support
142145uint8_t eepromPageAddress = 0 ; // Initial EEPROM page address
143146const int pins[] = { OFF_EN, SA1_EN, HV_EN }; // Configuration pins array
144147
@@ -156,7 +159,7 @@ void setup() {
156159 // Initiate and join the I2C bus as a master
157160 Wire.begin ();
158161
159- // Perform device features test
162+ // Perform initial device RSWP support test
160163 rswpSupport = rswpSupportTest ();
161164
162165 // Check saved i2c clock and set mode accordingly
@@ -196,7 +199,7 @@ void parseCommand() {
196199 return ;
197200 }
198201
199- char cmd = PORT.read ();
202+ char cmd = ( char ) PORT.read ();
200203
201204 switch (cmd) {
202205
@@ -277,7 +280,12 @@ void parseCommand() {
277280 // Device name controls
278281 case NAME:
279282 cmdName ();
280- break ;
283+ break ;
284+
285+ // Factory defaults restore
286+ case FACTORYRESET:
287+ cmdFactoryReset ();
288+ break ;
281289 }
282290}
283291
@@ -442,7 +450,7 @@ void cmdI2CClock() {
442450 PORT.readBytes (buffer, sizeof (buffer));
443451
444452 // Set I2C clock
445- if (buffer[0 ] == ENABLE || buffer[0 ] == DISABLE ) {
453+ if (buffer[0 ] == FASTMODE || buffer[0 ] == STDMODE ) {
446454 setI2cClockMode (buffer[0 ]);
447455 PORT.write (getI2cClockMode () == buffer[0 ] ? SUCCESS : ERROR);
448456 }
@@ -456,6 +464,10 @@ void cmdI2CClock() {
456464 }
457465}
458466
467+ bool cmdFactoryReset () {
468+ PORT.write (factoryReset () ? SUCCESS : ERROR);
469+ }
470+
459471void cmdRSWP () {
460472
461473 // Data buffer
@@ -709,7 +721,7 @@ byte rswpSupportTest() {
709721 if ((setConfigPin (SA1_EN, ON) && setConfigPin (SA1_EN, OFF)) &&
710722 (setConfigPin (SA1_EN, ON) ^ scanBus ()) !=
711723 (setConfigPin (SA1_EN, OFF) ^ scanBus ())) {
712- rswpSupport |= DDR3 | DDR2 ;
724+ rswpSupport |= DDR3;
713725 }
714726 }
715727
@@ -888,7 +900,7 @@ void setLegacyModeAddress(uint8_t address, bool twoByteAddressing) {
888900}
889901
890902
891- /* -= Device name functions =- */
903+ /* -= Device settings functions =- */
892904
893905// Assign a new name
894906bool setName (String name) {
@@ -915,20 +927,33 @@ String getName() {
915927 return deviceNameChar;
916928}
917929
930+ // Read device settings
931+ bool getSettings (byte name) {
932+ return bitRead (EEPROM.read (DEVICESETTINGS), name);
933+ }
934+
935+ // Save device settings
936+ bool saveSettings (byte name, byte value) {
937+
938+ byte currentSettings = EEPROM.read (DEVICESETTINGS);
939+ EEPROM.update (DEVICESETTINGS, bitWrite (currentSettings, name, value));
940+ }
941+
918942
919943/* -= I2C bus functions =- */
920944
921945// Set I2C bus clock mode
922- bool setI2cClockMode (bool fastMode ) {
923- EEPROM. update (clockSettings, (byte)(fastMode ? ENABLE : DISABLE) );
924- i2cClock = fastMode ? 400000 : 100000 ;
946+ bool setI2cClockMode (bool mode ) {
947+ saveSettings (CLOCKMODE, mode ? FASTMODE : STDMODE );
948+ i2cClock = mode ? 400000 : 100000 ;
925949 Wire.setClock (i2cClock);
926- return getI2cClockMode () == fastMode;
950+
951+ return getI2cClockMode ();
927952}
928953
929954// Gets saved I2C clock mode (true=fast mode, false=std mode)
930955bool getI2cClockMode () {
931- return EEPROM. read (clockSettings) == ENABLE ;
956+ return getSettings (CLOCKMODE) ;
932957}
933958
934959// Scans I2C bus range 80-87
@@ -1083,3 +1108,16 @@ bool ddr5Detect(uint8_t address) {
10831108
10841109 return result;
10851110}
1111+
1112+ // Restores device's default settings
1113+ bool factoryReset () {
1114+ for (uint8_t i = 0 ; i < 32 ; i++) {
1115+ EEPROM.update (i, ZERO);
1116+ }
1117+ for (uint8_t i = 0 ; i < 32 ; i++) {
1118+ if (EEPROM.read (i) != ZERO) {
1119+ return false ;
1120+ }
1121+ }
1122+ return true ;
1123+ }
0 commit comments