Skip to content

Commit 9ed12a1

Browse files
committed
Fix SPISettings name collision in ArduinoISP sketch when compiled in bitbanged SPI mode for board using ArduinoCore-API
When using bit banged SPI, which the sketch did when compiled for any architecture other than AVR, a `SPISettings` class was declared by the sketch. At the time the sketch was written, it was reasonable to expect this would not cause a name collision, since SPI.h is not `#include`d when doing bit banged SPI. However, since then a `SPISettings` class has been declared in [ArduinoCore-API's HardwareSPI.h](https://github.com/arduino/ArduinoCore-API/blob/932c7c7d4d4d334b10484284cc846672ad59607c/api/HardwareSPI.h#L37), causing the ArduinoISP sketch to not compile for any board whose core uses ArduinoCoreAPI (currently Arduino Mega AVR Boards, "Arduino nRF528x Boards (Mbed OS]", and "Arduino Mbed OS Boards (nRF52840 / STM32H747)"): ``` /github/workspace/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino:191:27: error: reference to 'SPISettings' is ambiguous void beginTransaction(SPISettings settings) { ^~~~~~~~~~~ /github/workspace/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino:167:7: note: candidates are: class SPISettings class SPISettings { ^~~~~~~~~~~ In file included from /github/home/.arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/ArduinoAPI.h:31:0, from /github/home/.arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/Arduino.h:23, from /github/workspace/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino:39: /github/home/.arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/HardwareSPI.h:37:7: note: class arduino::SPISettings class SPISettings { ^~~~~~~~~~~ ``` The fix is to use the `ARDUINO_API_VERSION` macro defined by ArduinoCore-API to detect when it is in use and make the bitbanged SPI code use the `SPISettings` class from ArduinoCore-API in this case.
1 parent 35dab59 commit 9ed12a1

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Diff for: examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

+9-5
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,23 @@ void pulse(int pin, int times);
164164

165165
#define SPI_MODE0 0x00
166166

167+
#if !defined(ARDUINO_API_VERSION) // A SPISettings class is declared by ArduinoCore-API
167168
class SPISettings {
168169
public:
169170
// clock is in Hz
170-
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock) {
171+
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clockFreq(clock) {
171172
(void) bitOrder;
172173
(void) dataMode;
173174
};
174175

175-
private:
176-
uint32_t clock;
176+
uint32_t getClockFreq() const {
177+
return clockFreq;
178+
}
177179

178-
friend class BitBangedSPI;
180+
private:
181+
uint32_t clockFreq;
179182
};
183+
#endif // !defined(ARDUINO_API_VERSION)
180184

181185
class BitBangedSPI {
182186
public:
@@ -189,7 +193,7 @@ class BitBangedSPI {
189193
}
190194

191195
void beginTransaction(SPISettings settings) {
192-
pulseWidth = (500000 + settings.clock - 1) / settings.clock;
196+
pulseWidth = (500000 + settings.getClockFreq() - 1) / settings.getClockFreq();
193197
if (pulseWidth == 0) {
194198
pulseWidth = 1;
195199
}

0 commit comments

Comments
 (0)