Skip to content

Commit 6c7c4ac

Browse files
authored
Update SpdReaderWriter.ino
Native USB Arduinos support enabled
1 parent 9f5480d commit 6c7c4ac

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

arduino/SpdReaderWriter/SpdReaderWriter.ino

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@
1111
#define SPA1 0x37 // Set EE Page Address to 1
1212

1313
// RSWP commands
14-
#define SWP0 0x31 // Set Write Protection for block 0 (addresses 00h to 7Fh) ( 0-127)
15-
#define SWP1 0x34 // Set Write Protection for block 1 (addresses 80h to FFh) (128-255)
16-
#define SWP2 0x35 // Set Write Protection for block 2 (addresses 100h to 17Fh) (256-383)
17-
#define SWP3 0x30 // Set Write Protection for block 3 (addresses 180h to 1FFh) (384-511)
18-
#define CWP 0x33 // Clear Write Protection for all 4 blocks
14+
#define SWP0 0x31 // Set Write Protection for block 0 (addresses 00h to 7Fh) ( 0-127)
15+
#define SWP1 0x34 // Set Write Protection for block 1 (addresses 80h to FFh) (128-255)
16+
#define SWP2 0x35 // Set Write Protection for block 2 (addresses 100h to 17Fh) (256-383)
17+
#define SWP3 0x30 // Set Write Protection for block 3 (addresses 180h to 1FFh) (384-511)
18+
#define CWP 0x33 // Clear Write Protection for all 4 blocks
1919

20-
#define HVSW 6 // High Voltage (Optocoupler) switch pin
20+
#define HVSW 6 // High Voltage (Optocoupler) switch pin
21+
#define PORT Serial // Communications Port, change to "SerialUSB" for native USB Arduinos (Leonardo, Micro, Due, Yun, etc)
2122

22-
int eeAddress = 0; // EE Page address
23+
int eeAddress = 0; // EE Page address
2324

2425
void setup() {
2526

2627
pinMode(HVSW, OUTPUT);
2728

2829
Wire.begin();
29-
Serial.begin(SERIAL_BAUD_RATE);
30-
Serial.setTimeout(10000); // 10 sec. timeout
31-
3230
setPageAddress(eeAddress); // Reset eeprom page address
3331

34-
while (!Serial) {} // Wait for serial monitor
32+
PORT.begin(SERIAL_BAUD_RATE);
33+
PORT.setTimeout(10000); // 10 sec. timeout
34+
35+
while (!PORT) {} // Wait for serial monitor
3536
}
3637

3738
// Reverses software write protection
@@ -136,11 +137,11 @@ bool probe(uint8_t address) {
136137
//Command handlers
137138

138139
void cmdScan() {
139-
int startAddress = Serial.parseInt(); //First address
140-
int endAddress = Serial.parseInt(); //Last address
140+
int startAddress = PORT.parseInt(); //First address
141+
int endAddress = PORT.parseInt(); //Last address
141142

142143
if (startAddress > endAddress) {
143-
Serial.write((byte)0);
144+
PORT.write((byte)0);
144145
return;
145146
}
146147

@@ -155,87 +156,79 @@ void cmdScan() {
155156

156157
for (int i = startAddress; i <= endAddress; i++) {
157158
if (probe(i)) {
158-
Serial.write((byte)i & 0xFF);
159+
PORT.write((byte)i);
159160
}
160161
}
161-
Serial.write((byte)0); // Send 0 to prevent application from waiting in case no devices are present
162-
//return;
162+
PORT.write((byte)0); // Send 0 to prevent application from waiting in case no devices are present
163163
}
164164

165165
void cmdRead() {
166-
int address = Serial.parseInt(); // Device address
167-
int offset = Serial.parseInt(); // Offset address
166+
int address = PORT.parseInt(); // Device address
167+
int offset = PORT.parseInt(); // Offset address
168168

169-
Serial.write((byte)readByte(address, offset));
170-
//return;
169+
PORT.write((byte)readByte(address, offset));
171170
}
172171

173172
void cmdWrite() {
174-
int address = Serial.parseInt(); // Device address
175-
int offset = Serial.parseInt(); // Offset address
176-
byte data = Serial.parseInt(); // Byte value
173+
int address = PORT.parseInt(); // Device address
174+
int offset = PORT.parseInt(); // Offset address
175+
byte data = PORT.parseInt(); // Byte value
177176

178177
if (writeByte(address, offset, data)) {
179-
Serial.write((byte)0); // Success
180-
return;
178+
PORT.write((byte)0); // Success
181179
}
182180

183-
Serial.write((byte)1); // Error
184-
//return;
181+
PORT.write((byte)1); // Error
185182
}
186183

187184
void cmdTest() {
188-
Serial.write((byte)'!');
189-
return;
185+
PORT.write((byte)'!');
190186
}
191187

192188
void cmdProbe() {
193-
int address = Serial.parseInt(); // Device address
189+
int address = PORT.parseInt(); // Device address
194190

195191
if (probe(address)) {
196-
Serial.write((byte)address); // Success
192+
PORT.write((byte)address); // Success
197193
return;
198194
}
199-
Serial.write((byte)0); // Error
200-
//return;
195+
PORT.write((byte)0); // Error
201196
}
202197

203198
void cmdClearWP() {
204199

205200
if (clearWriteProtection()) {
206-
Serial.write((byte)0); // Success
207-
//Serial.println("WP cleared");
201+
PORT.write((byte)0); // Success
202+
//PORT.println("WP cleared");
208203
return;
209204
}
210-
Serial.write((byte)1); // Error
211-
//Serial.println("WP NOT cleared");
212-
//return;
205+
PORT.write((byte)1); // Error
206+
//PORT.println("WP NOT cleared");
213207
}
214208

215209
void cmdEnableWP() {
216210

217-
int block = Serial.parseInt(); // Block number
211+
int block = PORT.parseInt(); // Block number
218212
block = (block >= 0 && block <= 3) ? block : 0; // Block number can't be outside of 0-3 range
219213

220214
if (setWriteProtection(block)) {
221-
//Serial.print("Protection enabled on block ");
222-
//Serial.println(block, HEX);
223-
Serial.write((byte)0); // Success
215+
//PORT.print("Protection enabled on block ");
216+
//PORT.println(block, HEX);
217+
PORT.write((byte)0); // Success
224218
return;
225219
}
226220

227-
//Serial.print("Nothing changed with block ");
228-
//Serial.println(block, HEX);
229-
Serial.write((byte)1); // Error
230-
//return;
221+
//PORT.print("Nothing changed with block ");
222+
//PORT.println(block, HEX);
223+
PORT.write((byte)1); // Error
231224
}
232225

233226
void parseCommand() {
234-
if (!Serial.available()) {
227+
if (!PORT.available()) {
235228
return;
236229
}
237230

238-
char cmd = Serial.read();
231+
char cmd = PORT.read();
239232

240233
switch (cmd) {
241234
case 't': cmdTest(); // Device Communication Test

0 commit comments

Comments
 (0)