Skip to content

Commit 5c47f87

Browse files
pr1csynthper1234
authored andcommitted
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 b3da224 commit 5c47f87

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: 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.
@@ -221,6 +237,19 @@ static BitBangedSPI SPI;
221237

222238
#endif
223239

240+
#ifdef CLOCK_GEN_ENABLED
241+
242+
// ouput a 4MHz clock on pin 3 (Uno) using fast PWM
243+
void setup_clock_gen(){
244+
pinMode(3, OUTPUT);
245+
TCCR2A = 0x23;
246+
TCCR2B = 0x09;
247+
OCR2A = 3;
248+
OCR2B = 1;
249+
}
250+
251+
#endif
252+
224253
void setup() {
225254
SERIAL.begin(BAUDRATE);
226255

@@ -231,6 +260,10 @@ void setup() {
231260
pinMode(LED_HB, OUTPUT);
232261
pulse(LED_HB, 2);
233262

263+
#ifdef CLOCK_GEN_ENABLED
264+
setup_clock_gen();
265+
#endif
266+
234267
}
235268

236269
int ISPError = 0;

0 commit comments

Comments
 (0)