arduino / arduino-examples

Arduino IDE bundled examples
Creative Commons Zero v1.0 Universal
92 stars 42 forks source link

Use distinctive names for SPI pin macros to avoid collisions #71

Closed per1234 closed 10 months ago

per1234 commented 10 months ago

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:

https://github.com/arduino/ArduinoCore-renesas/blob/1.0.5/cores/arduino/variant.h#L48-L50

#define PIN_MISO                    (6 << PIN_USE_POS)
#define PIN_MOSI                    (7 << PIN_USE_POS)
#define PIN_SCK                     (8 << PIN_USE_POS)

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:

https://github.com/arduino/arduino-examples/blob/fdbfc5b070623bbd560eecad7a387689f83053ce/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino#L81

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.

Additional Context

Originally reported at https://forum.arduino.cc/t/can-i-use-the-r4-minima-as-an-icsp/1170056