Skip to content

Commit 9806d58

Browse files
committed
Use distinctive names for SPI pin macros to avoid collisions
The "ArduinoISP" example allows the user to define arbitrary pins for use as the signal pins on the programmer board. This is done via a set of macros. Previously, very generic names were used for these macros. This resulted in a name collision. The core for the Renesas boards also defines macros of the same name. Despite what the names might lead us to believe, the values of these macros in the core do not match the Arduino pin numbers of the SPI bus. The result was that, with the default configuration of the sketch where the USE_OLD_STYLE_WIRING macro is not defined, the sketch uses the values of the macros defined by the core as the SPI signal pins instead of the standard SPI signal pins as the user would expect when the sketch is running on an UNO R4 Minima, UNO R4 WiFi, or Portenta C33 board. This causes operations using the programmer to fail with an error like: avrdude: stk500_recv(): programmer is not responding avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x03 [...] The name collision is avoided by adding a "namespace" prefix to the macro names.
1 parent fdbc6ba commit 9806d58

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

+27-27
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
// using an Uno. (On an Uno this is not needed).
2323
//
2424
// Alternatively you can use any other digital pin by configuring
25-
// software ('BitBanged') SPI and having appropriate defines for PIN_MOSI,
26-
// PIN_MISO and PIN_SCK.
25+
// software ('BitBanged') SPI and having appropriate defines for ARDUINOISP_PIN_MOSI,
26+
// ARDUINOISP_PIN_MISO and ARDUINOISP_PIN_SCK.
2727
//
2828
// IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...) as
2929
// the programmer, make sure to not expose any of the programmer's pins to 5V.
@@ -82,9 +82,9 @@
8282

8383
#ifdef USE_OLD_STYLE_WIRING
8484

85-
#define PIN_MOSI 11
86-
#define PIN_MISO 12
87-
#define PIN_SCK 13
85+
#define ARDUINOISP_PIN_MOSI 11
86+
#define ARDUINOISP_PIN_MISO 12
87+
#define ARDUINOISP_PIN_SCK 13
8888

8989
#endif
9090

@@ -100,20 +100,20 @@
100100
#endif
101101

102102
// By default, use hardware SPI pins:
103-
#ifndef PIN_MOSI
104-
#define PIN_MOSI MOSI
103+
#ifndef ARDUINOISP_PIN_MOSI
104+
#define ARDUINOISP_PIN_MOSI MOSI
105105
#endif
106106

107-
#ifndef PIN_MISO
108-
#define PIN_MISO MISO
107+
#ifndef ARDUINOISP_PIN_MISO
108+
#define ARDUINOISP_PIN_MISO MISO
109109
#endif
110110

111-
#ifndef PIN_SCK
112-
#define PIN_SCK SCK
111+
#ifndef ARDUINOISP_PIN_SCK
112+
#define ARDUINOISP_PIN_SCK SCK
113113
#endif
114114

115115
// Force bitbanged SPI if not using the hardware SPI pins:
116-
#if (PIN_MISO != MISO) || (PIN_MOSI != MOSI) || (PIN_SCK != SCK)
116+
#if (ARDUINOISP_PIN_MISO != MISO) || (ARDUINOISP_PIN_MOSI != MOSI) || (ARDUINOISP_PIN_SCK != SCK)
117117
#undef USE_HARDWARE_SPI
118118
#endif
119119

@@ -186,11 +186,11 @@ private:
186186
class BitBangedSPI {
187187
public:
188188
void begin() {
189-
digitalWrite(PIN_SCK, LOW);
190-
digitalWrite(PIN_MOSI, LOW);
191-
pinMode(PIN_SCK, OUTPUT);
192-
pinMode(PIN_MOSI, OUTPUT);
193-
pinMode(PIN_MISO, INPUT);
189+
digitalWrite(ARDUINOISP_PIN_SCK, LOW);
190+
digitalWrite(ARDUINOISP_PIN_MOSI, LOW);
191+
pinMode(ARDUINOISP_PIN_SCK, OUTPUT);
192+
pinMode(ARDUINOISP_PIN_MOSI, OUTPUT);
193+
pinMode(ARDUINOISP_PIN_MISO, INPUT);
194194
}
195195

196196
void beginTransaction(SPISettings settings) {
@@ -204,11 +204,11 @@ public:
204204

205205
uint8_t transfer(uint8_t b) {
206206
for (unsigned int i = 0; i < 8; ++i) {
207-
digitalWrite(PIN_MOSI, (b & 0x80) ? HIGH : LOW);
208-
digitalWrite(PIN_SCK, HIGH);
207+
digitalWrite(ARDUINOISP_PIN_MOSI, (b & 0x80) ? HIGH : LOW);
208+
digitalWrite(ARDUINOISP_PIN_SCK, HIGH);
209209
delayMicroseconds(pulseWidth);
210-
b = (b << 1) | digitalRead(PIN_MISO);
211-
digitalWrite(PIN_SCK, LOW); // slow pulse
210+
b = (b << 1) | digitalRead(ARDUINOISP_PIN_MISO);
211+
digitalWrite(ARDUINOISP_PIN_SCK, LOW); // slow pulse
212212
delayMicroseconds(pulseWidth);
213213
}
214214
return b;
@@ -408,7 +408,7 @@ void set_parameters() {
408408

409409
void start_pmode() {
410410

411-
// Reset target before driving PIN_SCK or PIN_MOSI
411+
// Reset target before driving ARDUINOISP_PIN_SCK or ARDUINOISP_PIN_MOSI
412412

413413
// SPI.begin() will configure SS as output, so SPI master mode is selected.
414414
// We have defined RESET as pin 10, which for many Arduinos is not the SS pin.
@@ -421,9 +421,9 @@ void start_pmode() {
421421

422422
// See AVR datasheets, chapter "SERIAL_PRG Programming Algorithm":
423423

424-
// Pulse RESET after PIN_SCK is low:
425-
digitalWrite(PIN_SCK, LOW);
426-
delay(20); // discharge PIN_SCK, value arbitrarily chosen
424+
// Pulse RESET after ARDUINOISP_PIN_SCK is low:
425+
digitalWrite(ARDUINOISP_PIN_SCK, LOW);
426+
delay(20); // discharge ARDUINOISP_PIN_SCK, value arbitrarily chosen
427427
reset_target(false);
428428
// Pulse must be minimum 2 target CPU clock cycles so 100 usec is ok for CPU
429429
// speeds above 20 KHz
@@ -439,8 +439,8 @@ void start_pmode() {
439439
void end_pmode() {
440440
SPI.end();
441441
// We're about to take the target out of reset so configure SPI pins as input
442-
pinMode(PIN_MOSI, INPUT);
443-
pinMode(PIN_SCK, INPUT);
442+
pinMode(ARDUINOISP_PIN_MOSI, INPUT);
443+
pinMode(ARDUINOISP_PIN_SCK, INPUT);
444444
reset_target(false);
445445
pinMode(RESET, INPUT);
446446
pmode = 0;

0 commit comments

Comments
 (0)