Skip to content

Commit d2a38f2

Browse files
Peter Van HoyweghenPeter Van Hoyweghen
Peter Van Hoyweghen
authored and
Peter Van Hoyweghen
committed
Rework pin configuration.
1 parent bfffb17 commit d2a38f2

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

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

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
//
99
// Pin 10 is used to reset the target microcontroller.
1010
//
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:
1314
//
1415
// MISO °. . 5V (!) Avoid this pin on Due, Zero...
1516
// SCK . . MOSI
@@ -20,6 +21,9 @@
2021
// instruct you to hook up the target to these pins. If you find this wiring
2122
// more practical, have a define USE_OLD_STYLE_WIRING. This will work even
2223
// 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.
2327
//
2428
// IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...)
2529
// as the programmer, make sure to not expose any of the programmer's pins to 5V.
@@ -61,7 +65,7 @@
6165

6266
#endif
6367

64-
// Configure which pins to use
68+
// Configure which pins to use:
6569

6670
// The standard pin configuration.
6771
#ifndef ARDUINO_HOODLOADER2
@@ -71,33 +75,49 @@
7175
#define LED_ERR 8
7276
#define LED_PMODE 7
7377

74-
// Uncomment following line to use the old uno style wiring
78+
// Uncomment following line to use the old Uno style wiring
7579
// (using pin 11, 12 and 13 instead of the SPI header) on Leonardo, Due...
7680

7781
// #define USE_OLD_STYLE_WIRING
7882

7983
#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+
8789
#endif
8890

8991
// HOODLOADER2 means running sketches on the atmega16u2
9092
// serial converter chips on Uno or Mega boards.
9193
// We must use pins that are broken out:
9294
#else
9395

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
98100

99101
#endif
100102

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+
101121

102122
// Configure the serial port to use.
103123
//
@@ -155,11 +175,11 @@ friend class BitBangedSPI;
155175
class BitBangedSPI {
156176
public:
157177
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);
163183
}
164184

165185
void beginTransaction(SPISettings settings) {
@@ -172,11 +192,11 @@ public:
172192

173193
uint8_t transfer (uint8_t b) {
174194
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);
177197
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
180200
delayMicroseconds(pulseWidth);
181201
}
182202
return b;
@@ -372,7 +392,7 @@ void set_parameters() {
372392

373393
void start_pmode() {
374394

375-
// Reset target before driving SCK or MOSI
395+
// Reset target before driving PIN_SCK or PIN_MOSI
376396

377397
// SPI.begin() will configure SS as output,
378398
// so SPI master mode is selected.
@@ -387,9 +407,9 @@ void start_pmode() {
387407

388408
// See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
389409

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
393413
reset_target(false);
394414
// Pulse must be minimum 2 target CPU clock cycles
395415
// so 100 usec is ok for CPU speeds above 20KHz
@@ -406,8 +426,8 @@ void end_pmode() {
406426
SPI.end();
407427
// We're about to take the target out of reset
408428
// so configure SPI pins as input
409-
pinMode(MOSI, INPUT);
410-
pinMode(SCK, INPUT);
429+
pinMode(PIN_MOSI, INPUT);
430+
pinMode(PIN_SCK, INPUT);
411431
reset_target(false);
412432
pinMode(RESET, INPUT);
413433
pmode = 0;

0 commit comments

Comments
 (0)