8000 Fix name collisions in ArduinoISP sketch by per1234 · Pull Request #3 · arduino/arduino-examples · GitHub
[go: up one dir, main page]

Skip to content

Fix name collisions in ArduinoISP sketch #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
per1234 committed Sep 3, 2020
commit 97ae2c8c423f64de4ea4c1c64558f518608109a3
14 changes: 9 additions & 5 deletions examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,23 @@ void pulse(int pin, int times);

#define SPI_MODE0 0x00

#if !defined(ARDUINO_API_VERSION) // A SPISettings class is declared by ArduinoCore-API
class SPISettings {
public:
// clock is in Hz
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock) {
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clockFreq(clock) {
(void) bitOrder;
(void) dataMode;
};

private:
uint32_t clock;
uint32_t getClockFreq() const {
return clockFreq;
}

friend class BitBangedSPI;
private:
uint32_t clockFreq;
};
#endif // !defined(ARDUINO_API_VERSION)

class BitBangedSPI {
public:
Expand All @@ -189,7 +193,7 @@ class BitBangedSPI {
}

void beginTransaction(SPISettings settings) {
pulseWidth = (500000 + settings.clock - 1) / settings.clock;
pulseWidth = (500000 + settings.getClockFreq() - 1) / settings.getClockFreq();
if (pulseWidth == 0) {
pulseWidth = 1;
}
Expand Down
0