Skip to content

Commit fbd95eb

Browse files
authored
Fixed PSWP and writeByte
Fixed PSWP and writeByte function which would incorrectly return true when writing to PSWP-protected area
1 parent 3c0f7bf commit fbd95eb

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

arduino/SpdReaderWriter/SpdReaderWriter.ino

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ bool setWriteProtection(uint8_t block) {
8787
// Sets permanent write protection on supported EEPROMs
8888
bool setPermanentWriteProtection(uint8_t deviceAddress) {
8989

90-
uint8_t cmd = (deviceAddress & 0b111) + (PWPB << 4); // Keep address bits (SA0-SA2) intact and change bits 7-4 to '0110'
90+
uint8_t cmd = (deviceAddress & 0b111) + (PWPB << 3); // Keep address bits (SA0-SA2) intact and change bits 7-4 to '0110'
91+
9192
Wire.beginTransmission(cmd);
9293
Wire.write(DNC);
9394
Wire.write(DNC);
@@ -97,6 +98,19 @@ bool setPermanentWriteProtection(uint8_t deviceAddress) {
9798
return status == 0;
9899
}
99100

101+
// Read permanent write protection status
102+
bool readPermanentWriteProtection(uint8_t deviceAddress) {
103+
104+
uint8_t cmd = (deviceAddress & 0b111) + (PWPB << 3); // Keep address bits (SA0-SA2) intact and change bits 7-4 to '0110'
105+
106+
Wire.beginTransmission(cmd);
107+
Wire.write(DNC);
108+
int status = Wire.endTransmission();
109+
delay(10);
110+
111+
return status == 0; // returns true if PSWP is not set
112+
}
113+
100114
// Reads a byte
101115
byte readByte(uint8_t deviceAddress, uint16_t offset) {
102116

@@ -113,7 +127,7 @@ byte readByte(uint8_t deviceAddress, uint16_t offset) {
113127
}
114128

115129
// Writes a byte
116-
bool writeByte(uint8_t deviceAddress, uint16_t offset, byte data) {
130+
bool writeByte(uint8_t deviceAddress, uint16_t offset, uint16_t data) {
117131

118132
adjustPageAddress(offset);
119133

@@ -124,7 +138,8 @@ bool writeByte(uint8_t deviceAddress, uint16_t offset, byte data) {
124138

125139
delay(10);
126140

127-
return status == 0;
141+
//return status == 0;
142+
return readByte(deviceAddress, offset) == data;
128143
}
129144

130145
// Sets page address to access lower or upper 256 bytes of DDR4 SPD
@@ -162,8 +177,8 @@ bool probe(uint8_t address) {
162177
//Command handlers
163178

164179
void cmdScan() {
165-
int startAddress = PORT.parseInt(); //First address
166-
int endAddress = PORT.parseInt(); //Last address
180+
int startAddress = PORT.parseInt(); // First address
181+
int endAddress = PORT.parseInt(); // Last address
167182

168183
if (startAddress > endAddress) {
169184
PORT.write(NULL); // Send 0 when start and end addresses are incorrectly specified
@@ -219,12 +234,10 @@ void cmdProbe() {
219234
void cmdClearWP() {
220235

221236
if (clearWriteProtection()) {
222-
PORT.write(SUCCESS);
223-
//PORT.println("RSWP cleared");
237+
PORT.write(SUCCESS); // RSWP cleared
224238
return;
225239
}
226-
PORT.write(ERROR);
227-
//PORT.println("RSWP NOT cleared");
240+
PORT.write(ERROR); // RSWP NOT cleared
228241
}
229242

230243
void cmdEnableWP() {
@@ -235,7 +248,7 @@ void cmdEnableWP() {
235248
if (setWriteProtection(block)) {
236249
//Serial.print("Protection enabled on block ");
237250
//Serial.println(block, HEX);
238-
PORT.write(SUCCESS);
251+
PORT.write(SUCCESS);
239252
return;
240253
}
241254

@@ -249,7 +262,16 @@ void cmdEnablePSWP() {
249262

250263
if (setPermanentWriteProtection(address)) {
251264
PORT.write(SUCCESS);
252-
//PORT.println("PSWP is set");
265+
return;
266+
}
267+
PORT.write(ERROR);
268+
}
269+
270+
void cmdReadPSWP() {
271+
int address = PORT.parseInt(); // Device address
272+
273+
if (readPermanentWriteProtection(address)) {
274+
PORT.write(SUCCESS);
253275
return;
254276
}
255277
PORT.write(ERROR);
@@ -293,6 +315,8 @@ void parseCommand() {
293315
break;
294316
case 'x': cmdEnablePSWP(); // Enable permanent write protection
295317
break;
318+
case 'o': cmdReadPSWP(); // Read permanent write protection status
319+
break;
296320
case 'a': cmdSetAddress(); // Set EEPROM address
297321
break;
298322
}

0 commit comments

Comments
 (0)