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 all commits
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
42 changes: 23 additions & 19 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 Expand Up @@ -229,7 +233,7 @@ void setup() {

}

int error = 0;
int ISPError = 0;
int pmode = 0;
// address for reading and writing, set by 'U' command
unsigned int here;
Expand Down Expand Up @@ -289,7 +293,7 @@ void loop(void) {
digitalWrite(LED_PMODE, LOW);
}
// is there an error?
if (error) {
if (ISPError) {
digitalWrite(LED_ERR, HIGH);
} else {
digitalWrite(LED_ERR, LOW);
Expand Down Expand Up @@ -340,7 +344,7 @@ void empty_reply() {
SERIAL.print((char)STK_INSYNC);
SERIAL.print((char)STK_OK);
} else {
error++;
ISPError++;
SERIAL.print((char)STK_NOSYNC);
}
}
Expand All @@ -351,7 +355,7 @@ void breply(uint8_t b) {
SERIAL.print((char)b);
SERIAL.print((char)STK_OK);
} else {
error++;
ISPError++;
SERIAL.print((char)STK_NOSYNC);
}
}
Expand Down Expand Up @@ -490,7 +494,7 @@ void write_flash(int length) {
SERIAL.print((char) STK_INSYNC);
SERIAL.print((char) write_flash_pages(length));
} else {
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
}
}
Expand Down Expand Up @@ -519,7 +523,7 @@ uint8_t write_eeprom(unsigned int length) {
unsigned int start = here * 2;
unsigned int remaining = length;
if (length > param.eepromsize) {
error++;
ISPError++;
return STK_FAILED;
}
while (remaining > EECHUNK) {
Expand Down Expand Up @@ -560,7 +564,7 @@ void program_page() {
SERIAL.print((char) STK_INSYNC);
SERIAL.print(result);
} else {
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
}
return;
Expand Down Expand Up @@ -604,7 +608,7 @@ void read_page() {
length += getch();
char memtype = getch();
if (CRC_EOP != getch()) {
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
return;
}
Expand All @@ -620,7 +624,7 @@ void read_page() {

void read_signature() {
if (CRC_EOP != getch()) {
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
return;
}
Expand All @@ -643,7 +647,7 @@ void avrisp() {
uint8_t ch = getch();
switch (ch) {
case '0': // signon
error = 0;
ISPError = 0;
empty_reply();
break;
case '1':
Expand All @@ -652,7 +656,7 @@ void avrisp() {
SERIAL.print("AVR ISP");
SERIAL.print((char) STK_OK);
} else {
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
}
break;
Expand Down Expand Up @@ -702,7 +706,7 @@ void avrisp() {
universal();
break;
case 'Q': //0x51
error = 0;
ISPError = 0;
end_pmode();
empty_reply();
break;
Expand All @@ -714,13 +718,13 @@ void avrisp() {
// expecting a command, not CRC_EOP
// this is how we can get back in sync
case CRC_EOP:
error++;
ISPError++;
SERIAL.print((char) STK_NOSYNC);
break;

// anything else we will return STK_UNKNOWN
default:
error++;
ISPError++;
if (CRC_EOP == getch()) {
SERIAL.print((char)STK_UNKNOWN);
} else {
Expand Down