Skip to content
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

ArduinoISP: Add an option to enable a 4 MHz clock signal on pin 3 #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
#define SPI_CLOCK (1000000/6)


// With an Genuino/Uno board, uncomment following line to generate a 4 MHz clock
// signal on pin 3 (only available on Uno / ATmega*8)
//
// Can be useful to recover an AVR chip with fuses setup for an external clock

// #define ENABLE_CLOCK_GEN

#ifdef ENABLE_CLOCK_GEN

#if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega88__)
Copy link
Contributor Author

@per1234 per1234 Sep 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facchinm requested changes on 2019-04-18

ArduinoISP sketch is meant to run on all Arduinos (also the ones with ARM chips), so it would be nice to keep the sketch functionalities in sync between all the targets.

#define CLOCK_GEN_ENABLED
#endif

#endif


// Select hardware or software SPI, depending on SPI clock.
// Currently only for AVR, for other architectures (Due, Zero,...), hardware SPI
// is probably too fast anyway.
Expand Down Expand Up @@ -221,6 +237,19 @@ static BitBangedSPI SPI;

#endif

#ifdef CLOCK_GEN_ENABLED

// ouput a 4MHz clock on pin 3 (Uno) using fast PWM
void setup_clock_gen(){
pinMode(3, OUTPUT);
TCCR2A = 0x23;
TCCR2B = 0x09;
OCR2A = 3;
OCR2B = 1;
}

#endif

void setup() {
SERIAL.begin(BAUDRATE);

Expand All @@ -231,6 +260,10 @@ void setup() {
pinMode(LED_HB, OUTPUT);
pulse(LED_HB, 2);

#ifdef CLOCK_GEN_ENABLED
setup_clock_gen();
#endif

}

int ISPError = 0;
Expand Down