8
8
//
9
9
// Pin 10 is used to reset the target microcontroller.
10
10
//
11
- // The MISO, MOSI and SCK pins are used to communicate with the target,
12
- // on all Arduinos, these pins can be found on the ICSP header:
11
+ // By default, the hardware SPI pins MISO, MOSI and SCK pins are used
12
+ // to communicate with the target. On all Arduinos, these pins can be found
13
+ // on the ICSP/SPI header:
13
14
//
14
15
// MISO °. . 5V (!) Avoid this pin on Due, Zero...
15
16
// SCK . . MOSI
20
21
// instruct you to hook up the target to these pins. If you find this wiring
21
22
// more practical, have a define USE_OLD_STYLE_WIRING. This will work even
22
23
// even when not using an Uno. (On an Uno this is not needed).
24
+ //
25
+ // Alternatively you can use any other digital pin by configuring software ('BitBanged')
26
+ // SPI and having appropriate defines for PIN_MOSI, PIN_MISO and PIN_SCK.
23
27
//
24
28
// IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...)
25
29
// as the programmer, make sure to not expose any of the programmer's pins to 5V.
61
65
62
66
#endif
63
67
64
- // Configure which pins to use
68
+ // Configure which pins to use:
65
69
66
70
// The standard pin configuration.
67
71
#ifndef ARDUINO_HOODLOADER2
71
75
#define LED_ERR 8
72
76
#define LED_PMODE 7
73
77
74
- // Uncomment following line to use the old uno style wiring
78
+ // Uncomment following line to use the old Uno style wiring
75
79
// (using pin 11, 12 and 13 instead of the SPI header) on Leonardo, Due...
76
80
77
81
// #define USE_OLD_STYLE_WIRING
78
82
79
83
#ifdef USE_OLD_STYLE_WIRING
80
- #undef USE_HARDWARE_SPI
81
- #undef MOSI
82
- #undef MISO
83
- #undef SCK
84
- #define MOSI 11
85
- #define MISO 12
86
- #define SCK 13
84
+
85
+ #define PIN_MOSI 11
86
+ #define PIN_MISO 12
87
+ #define PIN_SCK 13
88
+
87
89
#endif
88
90
89
91
// HOODLOADER2 means running sketches on the atmega16u2
90
92
// serial converter chips on Uno or Mega boards.
91
93
// We must use pins that are broken out:
92
94
#else
93
95
94
- #define RESET 4
95
- #define LED_HB 7
96
- #define LED_ERR 6
97
- #define LED_PMODE 5
96
+ #define RESET 4
97
+ #define LED_HB 7
98
+ #define LED_ERR 6
99
+ #define LED_PMODE 5
98
100
99
101
#endif
100
102
103
+ // By default, use hardware SPI pins:
104
+ #ifndef PIN_MOSI
105
+ #define PIN_MOSI MOSI
106
+ #endif
107
+
108
+ #ifndef PIN_MISO
109
+ #define PIN_MISO MISO
110
+ #endif
111
+
112
+ #ifndef PIN_SCK
113
+ #define PIN_SCK SCK
114
+ #endif
115
+
116
+ // Force bitbanged SPI if not using the hardware SPI pins:
117
+ #if (PIN_MISO != MISO) || (PIN_MOSI != MOSI) || (PIN_SCK != SCK)
118
+ #undef USE_HARDWARE_SPI
119
+ #endif
120
+
101
121
102
122
// Configure the serial port to use.
103
123
//
@@ -155,11 +175,11 @@ friend class BitBangedSPI;
155
175
class BitBangedSPI {
156
176
public:
157
177
void begin () {
158
- digitalWrite (SCK , LOW);
159
- digitalWrite (MOSI , LOW);
160
- pinMode (SCK , OUTPUT);
161
- pinMode (MOSI , OUTPUT);
162
- pinMode (MISO , INPUT);
178
+ digitalWrite (PIN_SCK , LOW);
179
+ digitalWrite (PIN_MOSI , LOW);
180
+ pinMode (PIN_SCK , OUTPUT);
181
+ pinMode (PIN_MOSI , OUTPUT);
182
+ pinMode (PIN_MISO , INPUT);
163
183
}
164
184
165
185
void beginTransaction (SPISettings settings) {
@@ -172,11 +192,11 @@ public:
172
192
173
193
uint8_t transfer (uint8_t b) {
174
194
for (unsigned int i = 0 ; i < 8 ; ++i) {
175
- digitalWrite (MOSI , (b & 0x80 ) ? HIGH : LOW);
176
- digitalWrite (SCK , HIGH);
195
+ digitalWrite (PIN_MOSI , (b & 0x80 ) ? HIGH : LOW);
196
+ digitalWrite (PIN_SCK , HIGH);
177
197
delayMicroseconds (pulseWidth);
178
- b = (b << 1 ) | digitalRead (MISO );
179
- digitalWrite (SCK , LOW); // slow pulse
198
+ b = (b << 1 ) | digitalRead (PIN_MISO );
199
+ digitalWrite (PIN_SCK , LOW); // slow pulse
180
200
delayMicroseconds (pulseWidth);
181
201
}
182
202
return b;
@@ -372,7 +392,7 @@ void set_parameters() {
372
392
373
393
void start_pmode () {
374
394
375
- // Reset target before driving SCK or MOSI
395
+ // Reset target before driving PIN_SCK or PIN_MOSI
376
396
377
397
// SPI.begin() will configure SS as output,
378
398
// so SPI master mode is selected.
@@ -387,9 +407,9 @@ void start_pmode() {
387
407
388
408
// See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
389
409
390
- // Pulse RESET after SCK is low:
391
- digitalWrite (SCK , LOW);
392
- delay (20 ); // discharge SCK , value arbitrally chosen
410
+ // Pulse RESET after PIN_SCK is low:
411
+ digitalWrite (PIN_SCK , LOW);
412
+ delay (20 ); // discharge PIN_SCK , value arbitrally chosen
393
413
reset_target (false );
394
414
// Pulse must be minimum 2 target CPU clock cycles
395
415
// so 100 usec is ok for CPU speeds above 20KHz
@@ -406,8 +426,8 @@ void end_pmode() {
406
426
SPI.end ();
407
427
// We're about to take the target out of reset
408
428
// so configure SPI pins as input
409
- pinMode (MOSI , INPUT);
410
- pinMode (SCK , INPUT);
429
+ pinMode (PIN_MOSI , INPUT);
430
+ pinMode (PIN_SCK , INPUT);
411
431
reset_target (false );
412
432
pinMode (RESET, INPUT);
413
433
pmode = 0 ;
0 commit comments