Skip to content

Commit c8b5e99

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 64e52d8 commit c8b5e99

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

build/shared/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

Lines changed: 64 additions & 48 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
@@ -196,14 +218,14 @@ void loop(void) {
196218

197219
// light the heartbeat LED
198220
heartbeat();
199-
if (Serial.available()) {
221+
if (SERIAL.available()) {
200222
avrisp();
201223
}
202224
}
203225

204226
uint8_t getch() {
205-
while (!Serial.available());
206-
return Serial.read();
227+
while (!SERIAL.available());
228+
return SERIAL.read();
207229
}
208230
void fill(int n) {
209231
for (int x = 0; x < n; x++) {
@@ -238,22 +260,22 @@ uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
238260

239261
void empty_reply() {
240262
if (CRC_EOP == getch()) {
241-
Serial.print((char)STK_INSYNC);
242-
Serial.print((char)STK_OK);
263+
SERIAL.print((char)STK_INSYNC);
264+
SERIAL.print((char)STK_OK);
243265
} else {
244266
error++;
245-
Serial.print((char)STK_NOSYNC);
267+
SERIAL.print((char)STK_NOSYNC);
246268
}
247269
}
248270

249271
void breply(uint8_t b) {
250272
if (CRC_EOP == getch()) {
251-
Serial.print((char)STK_INSYNC);
252-
Serial.print((char)b);
253-
Serial.print((char)STK_OK);
273+
SERIAL.print((char)STK_INSYNC);
274+
SERIAL.print((char)b);
275+
SERIAL.print((char)STK_OK);
254276
} else {
255277
error++;
256-
Serial.print((char)STK_NOSYNC);
278+
SERIAL.print((char)STK_NOSYNC);
257279
}
258280
}
259281

@@ -380,11 +402,11 @@ int current_page(int addr) {
380402
void write_flash(int length) {
381403
fill(length);
382404
if (CRC_EOP == getch()) {
383-
Serial.print((char) STK_INSYNC);
384-
Serial.print((char) write_flash_pages(length));
405+
SERIAL.print((char) STK_INSYNC);
406+
SERIAL.print((char) write_flash_pages(length));
385407
} else {
386408
error++;
387-
Serial.print((char) STK_NOSYNC);
409+
SERIAL.print((char) STK_NOSYNC);
388410
}
389411
}
390412

@@ -451,15 +473,15 @@ void program_page() {
451473
if (memtype == 'E') {
452474
result = (char)write_eeprom(length);
453475
if (CRC_EOP == getch()) {
454-
Serial.print((char) STK_INSYNC);
455-
Serial.print(result);
476+
SERIAL.print((char) STK_INSYNC);
477+
SERIAL.print(result);
456478
} else {
457479
error++;
458-
Serial.print((char) STK_NOSYNC);
480+
SERIAL.print((char) STK_NOSYNC);
459481
}
460482
return;
461483
}
462-
Serial.print((char)STK_FAILED);
484+
SERIAL.print((char)STK_FAILED);
463485
return;
464486
}
465487

@@ -473,9 +495,9 @@ uint8_t flash_read(uint8_t hilo, int addr) {
473495
char flash_read_page(int length) {
474496
for (int x = 0; x < length; x += 2) {
475497
uint8_t low = flash_read(LOW, here);
476-
Serial.print((char) low);
498+
SERIAL.print((char) low);
477499
uint8_t high = flash_read(HIGH, here);
478-
Serial.print((char) high);
500+
SERIAL.print((char) high);
479501
here++;
480502
}
481503
return STK_OK;
@@ -487,7 +509,7 @@ char eeprom_read_page(int length) {
487509
for (int x = 0; x < length; x++) {
488510
int addr = start + x;
489511
uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
490-
Serial.print((char) ee);
512+
SERIAL.print((char) ee);
491513
}
492514
return STK_OK;
493515
}
@@ -499,34 +521,29 @@ void read_page() {
499521
char memtype = getch();
500522
if (CRC_EOP != getch()) {
501523
error++;
502-
Serial.print((char) STK_NOSYNC);
524+
SERIAL.print((char) STK_NOSYNC);
503525
return;
504526
}
505-
Serial.print((char) STK_INSYNC);
506-
if (memtype == 'F') {
507-
result = flash_read_page(length);
508-
}
509-
if (memtype == 'E') {
510-
result = eeprom_read_page(length);
511-
}
512-
Serial.print(result);
513-
return;
527+
SERIAL.print((char) STK_INSYNC);
528+
if (memtype == 'F') result = flash_read_page(length);
529+
if (memtype == 'E') result = eeprom_read_page(length);
530+
SERIAL.print(result);
514531
}
515532

516533
void read_signature() {
517534
if (CRC_EOP != getch()) {
518535
error++;
519-
Serial.print((char) STK_NOSYNC);
536+
SERIAL.print((char) STK_NOSYNC);
520537
return;
521538
}
522-
Serial.print((char) STK_INSYNC);
539+
SERIAL.print((char) STK_INSYNC);
523540
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
524-
Serial.print((char) high);
541+
SERIAL.print((char) high);
525542
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
526-
Serial.print((char) middle);
543+
SERIAL.print((char) middle);
527544
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
528-
Serial.print((char) low);
529-
Serial.print((char) STK_OK);
545+
SERIAL.print((char) low);
546+
SERIAL.print((char) STK_OK);
530547
}
531548
//////////////////////////////////////////
532549
//////////////////////////////////////////
@@ -544,13 +561,13 @@ int avrisp() {
544561
break;
545562
case '1':
546563
if (getch() == CRC_EOP) {
547-
Serial.print((char) STK_INSYNC);
548-
Serial.print("AVR ISP");
549-
Serial.print((char) STK_OK);
564+
SERIAL.print((char) STK_INSYNC);
565+
SERIAL.print("AVR ISP");
566+
SERIAL.print((char) STK_OK);
550567
}
551568
else {
552569
error++;
553-
Serial.print((char) STK_NOSYNC);
570+
SERIAL.print((char) STK_NOSYNC);
554571
}
555572
break;
556573
case 'A':
@@ -615,17 +632,16 @@ int avrisp() {
615632
// this is how we can get back in sync
616633
case CRC_EOP:
617634
error++;
618-
Serial.print((char) STK_NOSYNC);
635+
SERIAL.print((char) STK_NOSYNC);
619636
break;
620637

621638
// anything else we will return STK_UNKNOWN
622639
default:
623640
error++;
624-
if (CRC_EOP == getch()) {
625-
Serial.print((char)STK_UNKNOWN);
626-
} else {
627-
Serial.print((char)STK_NOSYNC);
628-
}
641+
if (CRC_EOP == getch())
642+
SERIAL.print((char)STK_UNKNOWN);
643+
else
644+
SERIAL.print((char)STK_NOSYNC);
629645
}
630646
}
631647

0 commit comments

Comments
 (0)