Skip to content

Commit 24538bf

Browse files
Peter Van HoyweghenPeter Van Hoyweghen
authored andcommitted
Configure the serial port to use. Prefer native USB port if the Arduino has one.
1 parent 2c79f1c commit 24538bf

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

build/shared/examples/ArduinoISP/ArduinoISP.ino

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
// - The SPI functions herein were developed for the AVR910_ARD programmer
4444
// - More information at http://code.google.com/p/mega-isp
4545

46-
#include "pins_arduino.h"
46+
#include "Arduino.h"
47+
#undef SERIAL
48+
4749
// Use pin 10 to reset the target
4850
#define RESET 10
4951

@@ -63,6 +65,26 @@
6365

6466
#endif
6567

68+
69+
// Configure the serial port to use.
70+
//
71+
// Prefer the USB virtual serial port (aka. native USB port), if the Arduino has one:
72+
// - it does not autoreset (except for the magic baud rate of 1200).
73+
// - it is more reliable because of USB handshaking.
74+
//
75+
// Leonardo and similar have an USB virtual serial port: 'Serial'.
76+
// Due and Zero have an USB virtual serial port: 'SerialUSB'.
77+
//
78+
// On the Due and Zero, 'Serial' can be used too, provided you disable autoreset.
79+
// To use 'Serial': #define SERIAL Serial
80+
81+
#ifdef SERIAL_PORT_USBVIRTUAL
82+
#define SERIAL SERIAL_PORT_USBVIRTUAL
83+
#else
84+
#define SERIAL Serial
85+
#endif
86+
87+
6688
#define HWVER 2
6789
#define SWMAJ 1
6890
#define SWMIN 18
@@ -118,7 +140,7 @@ static BitBangedSPI SPI;
118140
#endif
119141

120142
void setup() {
121-
Serial.begin(19200);
143+
SERIAL.begin(19200);
122144
SPI.setDataMode(0);
123145
SPI.setBitOrder(MSBFIRST);
124146
// Select the slowest possible clock
@@ -190,14 +212,14 @@ void loop(void) {
190212

191213
// light the heartbeat LED
192214
heartbeat();
193-
if (Serial.available()) {
215+
if (SERIAL.available()) {
194216
avrisp();
195217
}
196218
}
197219

198220
uint8_t getch() {
199-
while (!Serial.available());
200-
return Serial.read();
221+
while (!SERIAL.available());
222+
return SERIAL.read();
201223
}
202224
void fill(int n) {
203225
for (int x = 0; x < n; x++) {
@@ -232,24 +254,24 @@ uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
232254

233255
void empty_reply() {
234256
if (CRC_EOP == getch()) {
235-
Serial.print((char)STK_INSYNC);
236-
Serial.print((char)STK_OK);
257+
SERIAL.print((char)STK_INSYNC);
258+
SERIAL.print((char)STK_OK);
237259
}
238260
else {
239261
error++;
240-
Serial.print((char)STK_NOSYNC);
262+
SERIAL.print((char)STK_NOSYNC);
241263
}
242264
}
243265

244266
void breply(uint8_t b) {
245267
if (CRC_EOP == getch()) {
246-
Serial.print((char)STK_INSYNC);
247-
Serial.print((char)b);
248-
Serial.print((char)STK_OK);
268+
SERIAL.print((char)STK_INSYNC);
269+
SERIAL.print((char)b);
270+
SERIAL.print((char)STK_OK);
249271
}
250272
else {
251273
error++;
252-
Serial.print((char)STK_NOSYNC);
274+
SERIAL.print((char)STK_NOSYNC);
253275
}
254276
}
255277

@@ -366,12 +388,12 @@ int current_page(int addr) {
366388
void write_flash(int length) {
367389
fill(length);
368390
if (CRC_EOP == getch()) {
369-
Serial.print((char) STK_INSYNC);
370-
Serial.print((char) write_flash_pages(length));
391+
SERIAL.print((char) STK_INSYNC);
392+
SERIAL.print((char) write_flash_pages(length));
371393
}
372394
else {
373395
error++;
374-
Serial.print((char) STK_NOSYNC);
396+
SERIAL.print((char) STK_NOSYNC);
375397
}
376398
}
377399

@@ -438,16 +460,16 @@ void program_page() {
438460
if (memtype == 'E') {
439461
result = (char)write_eeprom(length);
440462
if (CRC_EOP == getch()) {
441-
Serial.print((char) STK_INSYNC);
442-
Serial.print(result);
463+
SERIAL.print((char) STK_INSYNC);
464+
SERIAL.print(result);
443465
}
444466
else {
445467
error++;
446-
Serial.print((char) STK_NOSYNC);
468+
SERIAL.print((char) STK_NOSYNC);
447469
}
448470
return;
449471
}
450-
Serial.print((char)STK_FAILED);
472+
SERIAL.print((char)STK_FAILED);
451473
return;
452474
}
453475

@@ -461,9 +483,9 @@ uint8_t flash_read(uint8_t hilo, int addr) {
461483
char flash_read_page(int length) {
462484
for (int x = 0; x < length; x += 2) {
463485
uint8_t low = flash_read(LOW, here);
464-
Serial.print((char) low);
486+
SERIAL.print((char) low);
465487
uint8_t high = flash_read(HIGH, here);
466-
Serial.print((char) high);
488+
SERIAL.print((char) high);
467489
here++;
468490
}
469491
return STK_OK;
@@ -475,7 +497,7 @@ char eeprom_read_page(int length) {
475497
for (int x = 0; x < length; x++) {
476498
int addr = start + x;
477499
uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
478-
Serial.print((char) ee);
500+
SERIAL.print((char) ee);
479501
}
480502
return STK_OK;
481503
}
@@ -487,30 +509,30 @@ void read_page() {
487509
char memtype = getch();
488510
if (CRC_EOP != getch()) {
489511
error++;
490-
Serial.print((char) STK_NOSYNC);
512+
SERIAL.print((char) STK_NOSYNC);
491513
return;
492514
}
493-
Serial.print((char) STK_INSYNC);
515+
SERIAL.print((char) STK_INSYNC);
494516
if (memtype == 'F') result = flash_read_page(length);
495517
if (memtype == 'E') result = eeprom_read_page(length);
496-
Serial.print(result);
518+
SERIAL.print(result);
497519
return;
498520
}
499521

500522
void read_signature() {
501523
if (CRC_EOP != getch()) {
502524
error++;
503-
Serial.print((char) STK_NOSYNC);
525+
SERIAL.print((char) STK_NOSYNC);
504526
return;
505527
}
506-
Serial.print((char) STK_INSYNC);
528+
SERIAL.print((char) STK_INSYNC);
507529
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
508-
Serial.print((char) high);
530+
SERIAL.print((char) high);
509531
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
510-
Serial.print((char) middle);
532+
SERIAL.print((char) middle);
511533
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
512-
Serial.print((char) low);
513-
Serial.print((char) STK_OK);
534+
SERIAL.print((char) low);
535+
SERIAL.print((char) STK_OK);
514536
}
515537
//////////////////////////////////////////
516538
//////////////////////////////////////////
@@ -528,13 +550,13 @@ int avrisp() {
528550
break;
529551
case '1':
530552
if (getch() == CRC_EOP) {
531-
Serial.print((char) STK_INSYNC);
532-
Serial.print("AVR ISP");
533-
Serial.print((char) STK_OK);
553+
SERIAL.print((char) STK_INSYNC);
554+
SERIAL.print("AVR ISP");
555+
SERIAL.print((char) STK_OK);
534556
}
535557
else {
536558
error++;
537-
Serial.print((char) STK_NOSYNC);
559+
SERIAL.print((char) STK_NOSYNC);
538560
}
539561
break;
540562
case 'A':
@@ -599,16 +621,16 @@ int avrisp() {
599621
// this is how we can get back in sync
600622
case CRC_EOP:
601623
error++;
602-
Serial.print((char) STK_NOSYNC);
624+
SERIAL.print((char) STK_NOSYNC);
603625
break;
604626

605627
// anything else we will return STK_UNKNOWN
606628
default:
607629
error++;
608630
if (CRC_EOP == getch())
609-
Serial.print((char)STK_UNKNOWN);
631+
SERIAL.print((char)STK_UNKNOWN);
610632
else
611-
Serial.print((char)STK_NOSYNC);
633+
SERIAL.print((char)STK_NOSYNC);
612634
}
613635
}
614636

0 commit comments

Comments
 (0)