1
- // ArduinoISP version 04m2
1
+ // ArduinoISP version 04m3
2
2
// Copyright (c) 2008-2011 Randall Bohn
3
3
// If you require a license, see
4
4
// http://www.opensource.org/licenses/bsd-license.php
5
5
//
6
- // this sketch turns the Arduino into a AVRISP
7
- // using the following pins:
8
- // 10: slave reset
9
- // 11: MOSI
10
- // 12: MISO
11
- // 13: SCK
12
- // (This is standard Arduino. Mega and others use different pins.)
13
-
6
+ // This sketch turns the Arduino into a AVRISP
7
+ // using the following arduino pins:
8
+ //
9
+ // pin name: not-mega: mega(1280 and 2560)
10
+ // slave reset: 10: 53
11
+ // MOSI: 11: 51
12
+ // MISO: 12: 50
13
+ // SCK: 13: 52
14
+ //
14
15
// Put an LED (with resistor) on the following pins:
15
- // 9: Heartbeat - shows the programmer is running
16
- // 8: Error - Lights up if something goes wrong (use red if that makes sense)
16
+ // 9: Heartbeat - shows the programmer is running
17
+ // 8: Error - Lights up if something goes wrong (use red if that makes sense)
17
18
// 7: Programming - In communication with the slave
18
19
//
19
20
// 23 July 2011 Randall Bohn
43
44
// - More information at http://code.google.com/p/mega-isp
44
45
45
46
#include " pins_arduino.h"
46
- #define RESET SS
47
+ #define RESET SS
47
48
48
- #define LED_HB 9
49
- #define LED_ERR 8
49
+ #define LED_HB 9
50
+ #define LED_ERR 8
50
51
#define LED_PMODE 7
51
52
#define PROG_FLICKER true
52
53
55
56
#define SWMIN 18
56
57
57
58
// STK Definitions
58
- #define STK_OK 0x10
59
- #define STK_FAILED 0x11
59
+ #define STK_OK 0x10
60
+ #define STK_FAILED 0x11
60
61
#define STK_UNKNOWN 0x12
61
- #define STK_INSYNC 0x14
62
- #define STK_NOSYNC 0x15
63
- #define CRC_EOP 0x20 // ok it is a space...
62
+ #define STK_INSYNC 0x14
63
+ #define STK_NOSYNC 0x15
64
+ #define CRC_EOP 0x20 // ok it is a space...
64
65
65
66
void pulse (int pin, int times);
66
67
@@ -110,7 +111,7 @@ void heartbeat() {
110
111
analogWrite (LED_HB, hbval);
111
112
delay (40 );
112
113
}
113
-
114
+
114
115
115
116
void loop (void ) {
116
117
// is pmode active?
@@ -119,7 +120,7 @@ void loop(void) {
119
120
// is there an error?
120
121
if (error) digitalWrite (LED_ERR, HIGH);
121
122
else digitalWrite (LED_ERR, LOW);
122
-
123
+
123
124
// light the heartbeat LED
124
125
heartbeat ();
125
126
if (Serial.available ()) {
@@ -187,7 +188,8 @@ void empty_reply() {
187
188
if (CRC_EOP == getch ()) {
188
189
Serial.print ((char )STK_INSYNC);
189
190
Serial.print ((char )STK_OK);
190
- } else {
191
+ }
192
+ else {
191
193
error++;
192
194
Serial.print ((char )STK_NOSYNC);
193
195
}
@@ -227,18 +229,18 @@ void get_version(uint8_t c) {
227
229
void set_parameters () {
228
230
// call this after reading paramter packet into buff[]
229
231
param.devicecode = buff[0 ];
230
- param. revision = buff[1 ];
231
- param. progtype = buff[2 ];
232
- param. parmode = buff[3 ];
233
- param. polling = buff[4 ];
234
- param. selftimed = buff[5 ];
235
- param. lockbytes = buff[6 ];
236
- param. fusebytes = buff[7 ];
237
- param. flashpoll = buff[8 ];
232
+ param.revision = buff[1 ];
233
+ param.progtype = buff[2 ];
234
+ param.parmode = buff[3 ];
235
+ param.polling = buff[4 ];
236
+ param.selftimed = buff[5 ];
237
+ param.lockbytes = buff[6 ];
238
+ param.fusebytes = buff[7 ];
239
+ param.flashpoll = buff[8 ];
238
240
// ignore buff[9] (= buff[8])
239
241
// following are 16 bits (big endian)
240
242
param.eeprompoll = beget16 (&buff[10 ]);
241
- param. pagesize = beget16(& buff[12 ]);
243
+ param.pagesize = beget16 (&buff[12 ]);
242
244
param.eepromsize = beget16 (&buff[14 ]);
243
245
244
246
// 32 bits flashsize (big endian)
@@ -299,8 +301,8 @@ void commit(int addr) {
299
301
300
302
// #define _current_page(x) (here & 0xFFFFE0)
301
303
int current_page (int addr) {
302
- if (param. pagesize == 32 ) return here & 0xFFFFFFF0 ;
303
- if (param. pagesize == 64 ) return here & 0xFFFFFFE0 ;
304
+ if (param.pagesize == 32 ) return here & 0xFFFFFFF0 ;
305
+ if (param.pagesize == 64 ) return here & 0xFFFFFFE0 ;
304
306
if (param.pagesize == 128 ) return here & 0xFFFFFFC0 ;
305
307
if (param.pagesize == 256 ) return here & 0xFFFFFF80 ;
306
308
return here;
@@ -312,7 +314,8 @@ void write_flash(int length) {
312
314
if (CRC_EOP == getch ()) {
313
315
Serial.print ((char ) STK_INSYNC);
314
316
Serial.print ((char ) write_flash_pages (length));
315
- } else {
317
+ }
318
+ else {
316
319
error++;
317
320
Serial.print ((char ) STK_NOSYNC);
318
321
}
@@ -383,7 +386,8 @@ void program_page() {
383
386
if (CRC_EOP == getch ()) {
384
387
Serial.print ((char ) STK_INSYNC);
385
388
Serial.print (result);
386
- } else {
389
+ }
390
+ else {
387
391
error++;
388
392
Serial.print ((char ) STK_NOSYNC);
389
393
}
@@ -395,9 +399,9 @@ void program_page() {
395
399
396
400
uint8_t flash_read (uint8_t hilo, int addr) {
397
401
return spi_transaction (0x20 + hilo * 8 ,
398
- (addr >> 8 ) & 0xFF ,
399
- addr & 0xFF ,
400
- 0 );
402
+ (addr >> 8 ) & 0xFF ,
403
+ addr & 0xFF ,
404
+ 0 );
401
405
}
402
406
403
407
char flash_read_page (int length) {
@@ -511,7 +515,7 @@ int avrisp() {
511
515
case 0x64 : // STK_PROG_PAGE
512
516
program_page ();
513
517
break ;
514
-
518
+
515
519
case 0x74 : // STK_READ_PAGE 't'
516
520
read_page ();
517
521
break ;
@@ -524,19 +528,19 @@ int avrisp() {
524
528
end_pmode ();
525
529
empty_reply ();
526
530
break ;
527
-
531
+
528
532
case 0x75 : // STK_READ_SIGN 'u'
529
533
read_signature ();
530
534
break ;
531
535
532
- // expecting a command, not CRC_EOP
533
- // this is how we can get back in sync
536
+ // expecting a command, not CRC_EOP
537
+ // this is how we can get back in sync
534
538
case CRC_EOP:
535
539
error++;
536
540
Serial.print ((char ) STK_NOSYNC);
537
541
break ;
538
-
539
- // anything else we will return STK_UNKNOWN
542
+
543
+ // anything else we will return STK_UNKNOWN
540
544
default :
541
545
error++;
542
546
if (CRC_EOP == getch ())
@@ -546,3 +550,5 @@ int avrisp() {
546
550
}
547
551
}
548
552
553
+
554
+
0 commit comments