From 5c47f8712cc6faaf04d8d61e0cf315e54bbd49cf Mon Sep 17 00:00:00 2001 From: procsynth Date: Sun, 7 Apr 2019 21:55:25 +0200 Subject: [PATCH 1/3] 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. --- .../11.ArduinoISP/ArduinoISP/ArduinoISP.ino | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino index 43a91d3..66f3bf6 100644 --- a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino +++ b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino @@ -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__) +#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. @@ -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); @@ -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; From d8d24c7edffdf449eee63a2de542c7738650cd52 Mon Sep 17 00:00:00 2001 From: per1234 Date: Fri, 25 Sep 2020 20:42:41 -0700 Subject: [PATCH 2/3] Make ArduinoISP code formatting compliant with examples_formatter.conf --- examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino index 66f3bf6..5d5aba1 100644 --- a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino +++ b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino @@ -55,16 +55,16 @@ // 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__) -#define CLOCK_GEN_ENABLED -#endif + #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega88__) + #define CLOCK_GEN_ENABLED + #endif #endif @@ -240,7 +240,7 @@ static BitBangedSPI SPI; #ifdef CLOCK_GEN_ENABLED // ouput a 4MHz clock on pin 3 (Uno) using fast PWM -void setup_clock_gen(){ +void setup_clock_gen() { pinMode(3, OUTPUT); TCCR2A = 0x23; TCCR2B = 0x09; @@ -260,9 +260,9 @@ void setup() { pinMode(LED_HB, OUTPUT); pulse(LED_HB, 2); -#ifdef CLOCK_GEN_ENABLED + #ifdef CLOCK_GEN_ENABLED setup_clock_gen(); -#endif + #endif } From 96cd102e7466968a07f273687d815d61683140a1 Mon Sep 17 00:00:00 2001 From: per1234 Date: Fri, 25 Sep 2020 20:43:24 -0700 Subject: [PATCH 3/3] Correct misspelled word in ArduinoISP comment --- examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino index 5d5aba1..377857e 100644 --- a/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino +++ b/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino @@ -239,7 +239,7 @@ static BitBangedSPI SPI; #ifdef CLOCK_GEN_ENABLED -// ouput a 4MHz clock on pin 3 (Uno) using fast PWM +// output a 4MHz clock on pin 3 (Uno) using fast PWM void setup_clock_gen() { pinMode(3, OUTPUT); TCCR2A = 0x23;