From 611a914b7a831c694653daf1ad2338a469ccdeb5 Mon Sep 17 00:00:00 2001 From: egil Date: Fri, 2 Aug 2019 16:18:59 +0200 Subject: [PATCH 1/3] Fix bug in baudrate compensation. The code is now in accordance to the data sheet. The previous code would in effect either apply twice the compensation, or apply no compensation, depending on the polarity of the compensation. --- cores/arduino/UART.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 8e0520bc..da5b8bb8 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -149,7 +149,7 @@ void UartClass::begin(unsigned long baud, uint16_t config) int8_t sigrow_val = SIGROW.OSC16ERR5V; baud_setting *= (1024 + sigrow_val); - baud_setting /= (1024 - abs(sigrow_val)); + baud_setting /= 1024; // assign the baud_setting, a.k.a. BAUD (USART Baud Rate Register) (*_hwserial_module).BAUD = (int16_t) baud_setting; From cb63a6cca4f4106404f497cf9ca0de7a7d591b94 Mon Sep 17 00:00:00 2001 From: egil Date: Fri, 2 Aug 2019 16:37:06 +0200 Subject: [PATCH 2/3] Improve readability of expression for baudrate compensation. It is now exactly the same as in the documentation. The net result is the same as the previous version. --- cores/arduino/UART.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index da5b8bb8..254a93b4 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -148,8 +148,7 @@ void UartClass::begin(unsigned long baud, uint16_t config) _written = false; int8_t sigrow_val = SIGROW.OSC16ERR5V; - baud_setting *= (1024 + sigrow_val); - baud_setting /= 1024; + baud_setting += (baud_setting * sigrow_val) / 1024; // assign the baud_setting, a.k.a. BAUD (USART Baud Rate Register) (*_hwserial_module).BAUD = (int16_t) baud_setting; From ab46691b5448313095cc30dc276d9931a4d03338 Mon Sep 17 00:00:00 2001 From: egil Date: Fri, 2 Aug 2019 20:19:50 +0200 Subject: [PATCH 3/3] Since we have code to correct for errors in F_CPU to generate the right baudrate, then we should use F_CPU, the specified CPU frequency as basis, not the F_CPU with whatever corrections. --- cores/arduino/UART.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 254a93b4..c6065825 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -140,7 +140,7 @@ void UartClass::begin(unsigned long baud, uint16_t config) uint8_t oldSREG = SREG; cli(); - baud_setting = (((8 * F_CPU_CORRECTED) / baud) + 1) / 2; + baud_setting = (((8 * F_CPU) / baud) + 1) / 2; // Disable CLK2X (*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc); (*_hwserial_module).CTRLB |= USART_RXMODE_NORMAL_gc;