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
@@ -196,14 +218,14 @@ void loop(void) {
196
218
197
219
// light the heartbeat LED
198
220
heartbeat ();
199
- if (Serial .available ()) {
221
+ if (SERIAL .available ()) {
200
222
avrisp ();
201
223
}
202
224
}
203
225
204
226
uint8_t getch () {
205
- while (!Serial .available ());
206
- return Serial .read ();
227
+ while (!SERIAL .available ());
228
+ return SERIAL .read ();
207
229
}
208
230
void fill (int n) {
209
231
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) {
238
260
239
261
void empty_reply () {
240
262
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);
243
265
} else {
244
266
error++;
245
- Serial .print ((char )STK_NOSYNC);
267
+ SERIAL .print ((char )STK_NOSYNC);
246
268
}
247
269
}
248
270
249
271
void breply (uint8_t b) {
250
272
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);
254
276
} else {
255
277
error++;
256
- Serial .print ((char )STK_NOSYNC);
278
+ SERIAL .print ((char )STK_NOSYNC);
257
279
}
258
280
}
259
281
@@ -380,11 +402,11 @@ int current_page(int addr) {
380
402
void write_flash (int length) {
381
403
fill (length);
382
404
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));
385
407
} else {
386
408
error++;
387
- Serial .print ((char ) STK_NOSYNC);
409
+ SERIAL .print ((char ) STK_NOSYNC);
388
410
}
389
411
}
390
412
@@ -451,15 +473,15 @@ void program_page() {
451
473
if (memtype == ' E' ) {
452
474
result = (char )write_eeprom (length);
453
475
if (CRC_EOP == getch ()) {
454
- Serial .print ((char ) STK_INSYNC);
455
- Serial .print (result);
476
+ SERIAL .print ((char ) STK_INSYNC);
477
+ SERIAL .print (result);
456
478
} else {
457
479
error++;
458
- Serial .print ((char ) STK_NOSYNC);
480
+ SERIAL .print ((char ) STK_NOSYNC);
459
481
}
460
482
return ;
461
483
}
462
- Serial .print ((char )STK_FAILED);
484
+ SERIAL .print ((char )STK_FAILED);
463
485
return ;
464
486
}
465
487
@@ -473,9 +495,9 @@ uint8_t flash_read(uint8_t hilo, int addr) {
473
495
char flash_read_page (int length) {
474
496
for (int x = 0 ; x < length; x += 2 ) {
475
497
uint8_t low = flash_read (LOW, here);
476
- Serial .print ((char ) low);
498
+ SERIAL .print ((char ) low);
477
499
uint8_t high = flash_read (HIGH, here);
478
- Serial .print ((char ) high);
500
+ SERIAL .print ((char ) high);
479
501
here++;
480
502
}
481
503
return STK_OK;
@@ -487,7 +509,7 @@ char eeprom_read_page(int length) {
487
509
for (int x = 0 ; x < length; x++) {
488
510
int addr = start + x;
489
511
uint8_t ee = spi_transaction (0xA0 , (addr >> 8 ) & 0xFF , addr & 0xFF , 0xFF );
490
- Serial .print ((char ) ee);
512
+ SERIAL .print ((char ) ee);
491
513
}
492
514
return STK_OK;
493
515
}
@@ -499,34 +521,29 @@ void read_page() {
499
521
char memtype = getch ();
500
522
if (CRC_EOP != getch ()) {
501
523
error++;
502
- Serial .print ((char ) STK_NOSYNC);
524
+ SERIAL .print ((char ) STK_NOSYNC);
503
525
return ;
504
526
}
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);
514
531
}
515
532
516
533
void read_signature () {
517
534
if (CRC_EOP != getch ()) {
518
535
error++;
519
- Serial .print ((char ) STK_NOSYNC);
536
+ SERIAL .print ((char ) STK_NOSYNC);
520
537
return ;
521
538
}
522
- Serial .print ((char ) STK_INSYNC);
539
+ SERIAL .print ((char ) STK_INSYNC);
523
540
uint8_t high = spi_transaction (0x30 , 0x00 , 0x00 , 0x00 );
524
- Serial .print ((char ) high);
541
+ SERIAL .print ((char ) high);
525
542
uint8_t middle = spi_transaction (0x30 , 0x00 , 0x01 , 0x00 );
526
- Serial .print ((char ) middle);
543
+ SERIAL .print ((char ) middle);
527
544
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);
530
547
}
531
548
// ////////////////////////////////////////
532
549
// ////////////////////////////////////////
@@ -544,13 +561,13 @@ int avrisp() {
544
561
break ;
545
562
case ' 1' :
546
563
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);
550
567
}
551
568
else {
552
569
error++;
553
- Serial .print ((char ) STK_NOSYNC);
570
+ SERIAL .print ((char ) STK_NOSYNC);
554
571
}
555
572
break ;
556
573
case ' A' :
@@ -615,17 +632,16 @@ int avrisp() {
615
632
// this is how we can get back in sync
616
633
case CRC_EOP:
617
634
error++;
618
- Serial .print ((char ) STK_NOSYNC);
635
+ SERIAL .print ((char ) STK_NOSYNC);
619
636
break ;
620
637
621
638
// anything else we will return STK_UNKNOWN
622
639
default :
623
640
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);
629
645
}
630
646
}
631
647
0 commit comments