43
43
// - The SPI functions herein were developed for the AVR910_ARD programmer
44
44
// - More information at http://code.google.com/p/mega-isp
45
45
46
- #include " pins_arduino.h"
46
+ #include " Arduino.h"
47
+ #undef SERIAL
48
+
47
49
// Use pin 10 to reset the target
48
50
#define RESET 10
49
51
63
65
64
66
#endif
65
67
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
+
66
88
#define HWVER 2
67
89
#define SWMAJ 1
68
90
#define SWMIN 18
@@ -118,7 +140,7 @@ static BitBangedSPI SPI;
118
140
#endif
119
141
120
142
void setup () {
121
- Serial .begin (19200 );
143
+ SERIAL .begin (19200 );
122
144
SPI.setDataMode (0 );
123
145
SPI.setBitOrder (MSBFIRST);
124
146
// Select the slowest possible clock
@@ -190,14 +212,14 @@ void loop(void) {
190
212
191
213
// light the heartbeat LED
192
214
heartbeat ();
193
- if (Serial .available ()) {
215
+ if (SERIAL .available ()) {
194
216
avrisp ();
195
217
}
196
218
}
197
219
198
220
uint8_t getch () {
199
- while (!Serial .available ());
200
- return Serial .read ();
221
+ while (!SERIAL .available ());
222
+ return SERIAL .read ();
201
223
}
202
224
void fill (int n) {
203
225
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) {
232
254
233
255
void empty_reply () {
234
256
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);
237
259
}
238
260
else {
239
261
error++;
240
- Serial .print ((char )STK_NOSYNC);
262
+ SERIAL .print ((char )STK_NOSYNC);
241
263
}
242
264
}
243
265
244
266
void breply (uint8_t b) {
245
267
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);
249
271
}
250
272
else {
251
273
error++;
252
- Serial .print ((char )STK_NOSYNC);
274
+ SERIAL .print ((char )STK_NOSYNC);
253
275
}
254
276
}
255
277
@@ -366,12 +388,12 @@ int current_page(int addr) {
366
388
void write_flash (int length) {
367
389
fill (length);
368
390
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));
371
393
}
372
394
else {
373
395
error++;
374
- Serial .print ((char ) STK_NOSYNC);
396
+ SERIAL .print ((char ) STK_NOSYNC);
375
397
}
376
398
}
377
399
@@ -438,16 +460,16 @@ void program_page() {
438
460
if (memtype == ' E' ) {
439
461
result = (char )write_eeprom (length);
440
462
if (CRC_EOP == getch ()) {
441
- Serial .print ((char ) STK_INSYNC);
442
- Serial .print (result);
463
+ SERIAL .print ((char ) STK_INSYNC);
464
+ SERIAL .print (result);
443
465
}
444
466
else {
445
467
error++;
446
- Serial .print ((char ) STK_NOSYNC);
468
+ SERIAL .print ((char ) STK_NOSYNC);
447
469
}
448
470
return ;
449
471
}
450
- Serial .print ((char )STK_FAILED);
472
+ SERIAL .print ((char )STK_FAILED);
451
473
return ;
452
474
}
453
475
@@ -461,9 +483,9 @@ uint8_t flash_read(uint8_t hilo, int addr) {
461
483
char flash_read_page (int length) {
462
484
for (int x = 0 ; x < length; x += 2 ) {
463
485
uint8_t low = flash_read (LOW, here);
464
- Serial .print ((char ) low);
486
+ SERIAL .print ((char ) low);
465
487
uint8_t high = flash_read (HIGH, here);
466
- Serial .print ((char ) high);
488
+ SERIAL .print ((char ) high);
467
489
here++;
468
490
}
469
491
return STK_OK;
@@ -475,7 +497,7 @@ char eeprom_read_page(int length) {
475
497
for (int x = 0 ; x < length; x++) {
476
498
int addr = start + x;
477
499
uint8_t ee = spi_transaction (0xA0 , (addr >> 8 ) & 0xFF , addr & 0xFF , 0xFF );
478
- Serial .print ((char ) ee);
500
+ SERIAL .print ((char ) ee);
479
501
}
480
502
return STK_OK;
481
503
}
@@ -487,30 +509,30 @@ void read_page() {
487
509
char memtype = getch ();
488
510
if (CRC_EOP != getch ()) {
489
511
error++;
490
- Serial .print ((char ) STK_NOSYNC);
512
+ SERIAL .print ((char ) STK_NOSYNC);
491
513
return ;
492
514
}
493
- Serial .print ((char ) STK_INSYNC);
515
+ SERIAL .print ((char ) STK_INSYNC);
494
516
if (memtype == ' F' ) result = flash_read_page (length);
495
517
if (memtype == ' E' ) result = eeprom_read_page (length);
496
- Serial .print (result);
518
+ SERIAL .print (result);
497
519
return ;
498
520
}
499
521
500
522
void read_signature () {
501
523
if (CRC_EOP != getch ()) {
502
524
error++;
503
- Serial .print ((char ) STK_NOSYNC);
525
+ SERIAL .print ((char ) STK_NOSYNC);
504
526
return ;
505
527
}
506
- Serial .print ((char ) STK_INSYNC);
528
+ SERIAL .print ((char ) STK_INSYNC);
507
529
uint8_t high = spi_transaction (0x30 , 0x00 , 0x00 , 0x00 );
508
- Serial .print ((char ) high);
530
+ SERIAL .print ((char ) high);
509
531
uint8_t middle = spi_transaction (0x30 , 0x00 , 0x01 , 0x00 );
510
- Serial .print ((char ) middle);
532
+ SERIAL .print ((char ) middle);
511
533
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);
514
536
}
515
537
// ////////////////////////////////////////
516
538
// ////////////////////////////////////////
@@ -528,13 +550,13 @@ int avrisp() {
528
550
break ;
529
551
case ' 1' :
530
552
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);
534
556
}
535
557
else {
536
558
error++;
537
- Serial .print ((char ) STK_NOSYNC);
559
+ SERIAL .print ((char ) STK_NOSYNC);
538
560
}
539
561
break ;
540
562
case ' A' :
@@ -599,16 +621,16 @@ int avrisp() {
599
621
// this is how we can get back in sync
600
622
case CRC_EOP:
601
623
error++;
602
- Serial .print ((char ) STK_NOSYNC);
624
+ SERIAL .print ((char ) STK_NOSYNC);
603
625
break ;
604
626
605
627
// anything else we will return STK_UNKNOWN
606
628
default :
607
629
error++;
608
630
if (CRC_EOP == getch ())
609
- Serial .print ((char )STK_UNKNOWN);
631
+ SERIAL .print ((char )STK_UNKNOWN);
610
632
else
611
- Serial .print ((char )STK_NOSYNC);
633
+ SERIAL .print ((char )STK_NOSYNC);
612
634
}
613
635
}
614
636
0 commit comments