Skip to content

Commit 1c90558

Browse files
committed
Add an option on Uno boards to enable a 4 MHz clock signal on pin 3
This clock signal can be used to reprogram the fuses of an AVR that has been set wrong (ie. when an external clock has been configured but the board/chip does not have one). This is disabled by default. We check that we compile for known chips before enabling the clock generation. Maybe there is other chips that support this code.
1 parent 72330aa commit 1c90558

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

build/shared/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

+33
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@
5353
#define SPI_CLOCK (1000000/6)
5454

5555

56+
// With an Genuino/Uno board, uncomment following line to generate a 4 MHz clock
57+
// signal on pin 3 (only available on Uno / ATmega*8)
58+
//
59+
// Can be useful to recover an AVR chip with fuses setup for an external clock
60+
61+
// #define ENABLE_CLOCK_GEN
62+
63+
#ifdef ENABLE_CLOCK_GEN
64+
65+
#if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega88__)
66+
#define CLOCK_GEN_ENABLED
67+
#endif
68+
69+
#endif
70+
71+
5672
// Select hardware or software SPI, depending on SPI clock.
5773
// Currently only for AVR, for other architectures (Due, Zero,...), hardware SPI
5874
// is probably too fast anyway.
@@ -216,6 +232,19 @@ static BitBangedSPI SPI;
216232

217233
#endif
218234

235+
#ifdef CLOCK_GEN_ENABLED
236+
237+
// ouput a 4MHz clock on pin 3 (Uno) using fast PWM
238+
void setup_clock_gen(){
239+
pinMode(3, OUTPUT);
240+
TCCR2A = 0x23;
241+
TCCR2B = 0x09;
242+
OCR2A = 3;
243+
OCR2B = 1;
244+
}
245+
246+
#endif
247+
219248
void setup() {
220249
SERIAL.begin(BAUDRATE);
221250

@@ -226,6 +255,10 @@ void setup() {
226255
pinMode(LED_HB, OUTPUT);
227256
pulse(LED_HB, 2);
228257

258+
#ifdef CLOCK_GEN_ENABLED
259+
setup_clock_gen();
260+
#endif
261+
229262
}
230263

231264
int error = 0;

0 commit comments

Comments
 (0)