3131#include " UART.h"
3232#include " UART_private.h"
3333
34- // #define PERFORM_BAUD_CORRECTION
35-
3634// this next line disables the entire UART.cpp,
3735// this is so I can support Attiny series and any other chip without a uart
3836#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
@@ -144,14 +142,14 @@ void UartClass::begin(unsigned long baud, uint16_t config)
144142 // ********Check if desired baud rate is within the acceptable range for using CLK2X RX-mode********
145143 // Condition from datasheet
146144 // This limits the minimum baud_setting value to 64 (0x0040)
147- if ((8 * baud) <= F_CPU ) {
145+ if ((8 * baud) <= F_CPU_CORRECTED ) {
148146
149147 // Check that the desired baud rate is not so low that it will
150148 // cause the BAUD register to overflow (1024 * 64 = 2^16)
151- if (baud > (F_CPU / (8 * 1024 ))) {
149+ if (baud > (F_CPU_CORRECTED / (8 * 1024 ))) {
152150 // Datasheet formula for calculating the baud setting including trick to reduce rounding error ((2*(X/Y))+1)/2
153- // baud_setting = ( ( (2 * (64 * F_CPU ) / (8 * baud) ) + 1 ) / 2;
154- baud_setting = (((16 * F_CPU ) / baud) + 1 ) / 2 ;
151+ // baud_setting = ( ( (2 * (64 * F_CPU_CORRECTED ) / (8 * baud) ) + 1 ) / 2;
152+ baud_setting = (((16 * F_CPU_CORRECTED ) / baud) + 1 ) / 2 ;
155153 // Enable CLK2X
156154 (*_hwserial_module).CTRLB |= USART_RXMODE_CLK2X_gc;
157155 } else {
@@ -162,14 +160,14 @@ void UartClass::begin(unsigned long baud, uint16_t config)
162160 // ********Check if desired baud rate is within the acceptable range for using normal RX-mode********
163161 // Condition from datasheet
164162 // This limits the minimum baud_setting value to 64 (0x0040)
165- } else if ((16 * baud <= F_CPU )) {
163+ } else if ((16 * baud <= F_CPU_CORRECTED )) {
166164
167165 // Check that the desired baud rate is not so low that it will
168166 // cause the BAUD register to overflow (1024 * 64 = 2^16)
169- if (baud > (F_CPU / (16 * 1024 ))) {
167+ if (baud > (F_CPU_CORRECTED / (16 * 1024 ))) {
170168 // Datasheet formula for calculating the baud setting including trick to reduce rounding error
171- // baud_setting = ( ( (2 * (64 * F_CPU ) / (16 * baud) ) + 1 ) / 2;
172- baud_setting = (((8 * F_CPU ) / baud) + 1 ) / 2 ;
169+ // baud_setting = ( ( (2 * (64 * F_CPU_CORRECTED ) / (16 * baud) ) + 1 ) / 2;
170+ baud_setting = (((8 * F_CPU_CORRECTED ) / baud) + 1 ) / 2 ;
173171 // Make sure CLK2X is disabled
174172 (*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc);
175173 } else {
@@ -185,24 +183,6 @@ void UartClass::begin(unsigned long baud, uint16_t config)
185183// Do nothing if an invalid baud rate is requested
186184 if (!error) {
187185
188- #ifdef PERFORM_BAUD_CORRECTION
189- // Compensate baud rate register value with factory stored frequency error
190- // Routine assumes Vcc to be 5V
191- // Verify that the desired baud setting is large enough
192- // (taking into account maximum negative compensation value)
193- if ( baud_setting >= 0x4A ){
194-
195- int8_t sigrow_val = 0 ;
196- if (FUSE.OSCCFG & FREQSEL_16MHZ_gc){
197- sigrow_val = SIGROW.OSC16ERR5V ;
198- } else if (FUSE.OSCCFG & FREQSEL_20MHZ_gc){
199- sigrow_val = SIGROW.OSC20ERR5V ;
200- }
201- baud_setting *= (1024 + sigrow_val);
202- baud_setting /= 1024 ;
203- }
204- #endif
205-
206186 _written = false ;
207187
208188 // Set up the rx pin
0 commit comments