From 945695075af673936dd94c8f199b08479cfe0196 Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 18 Feb 2019 21:17:07 +0100 Subject: [PATCH 1/2] Add missing 20 MHz clock option ATmega4809 have an internal 20 MHz clock that can be selected by changing the OSCCFG fuse byte. Why not make it available for the user? --- cores/arduino/wiring.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index 2dd956cb..d30d9022 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -274,6 +274,12 @@ void init() int64_t cpu_freq; + #elif (F_CPU == 20000000) + cpu_freq = 20000000; + + /* No division on clock */ + _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0x00); + #if (F_CPU == 16000000) cpu_freq = 16000000; @@ -321,19 +327,21 @@ void init() #if defined(ADC0) - /* ADC clock between 50-200KHz */ + /* ADC clock between 50-200 kHz */ - #if F_CPU >= 16000000 // 16 MHz / 128 = 125 KHz + #if F_CPU >= 20000000 // 20 MHz / 128 = 156.250 kHz + ADC0.CTRLC |= ADC_PRESC_DIV128_gc; + #elif F_CPU >= 16000000 // 16 MHz / 128 = 125 kHz ADC0.CTRLC |= ADC_PRESC_DIV128_gc; - #elif F_CPU >= 8000000 // 8 MHz / 64 = 125 KHz + #elif F_CPU >= 8000000 // 8 MHz / 64 = 125 kHz ADC0.CTRLC |= ADC_PRESC_DIV64_gc; - #elif F_CPU >= 4000000 // 4 MHz / 32 = 125 KHz + #elif F_CPU >= 4000000 // 4 MHz / 32 = 125 kHz ADC0.CTRLC |= ADC_PRESC_DIV32_gc; - #elif F_CPU >= 2000000 // 2 MHz / 16 = 125 KHz + #elif F_CPU >= 2000000 // 2 MHz / 16 = 125 kHz ADC0.CTRLC |= ADC_PRESC_DIV16_gc; - #elif F_CPU >= 1000000 // 1 MHz / 8 = 125 KHz + #elif F_CPU >= 1000000 // 1 MHz / 8 = 125 kHz ADC0.CTRLC |= ADC_PRESC_DIV8_gc; - #else // 128 kHz / 2 = 64 KHz -> This is the closest you can get, the prescaler is 2 + #else // 128 kHz / 2 = 64 kHz -> This is the closest you can get, the prescaler is 2 ADC0.CTRLC |= ADC_PRESC_DIV2_gc; #endif @@ -374,4 +382,4 @@ void init() sei(); } -void setup_timers(void) __attribute__((weak)); \ No newline at end of file +void setup_timers(void) __attribute__((weak)); From 9e3d38b104bf6e32e19999cd24acaa40b007db79 Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 18 Feb 2019 21:38:03 +0100 Subject: [PATCH 2/2] Fix incorrect if elif --- cores/arduino/wiring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index d30d9022..3e61056d 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -274,13 +274,13 @@ void init() int64_t cpu_freq; - #elif (F_CPU == 20000000) + #if (F_CPU == 20000000) cpu_freq = 20000000; /* No division on clock */ _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0x00); - #if (F_CPU == 16000000) + #elif (F_CPU == 16000000) cpu_freq = 16000000; /* No division on clock */