31
31
#include " UART.h"
32
32
#include " UART_private.h"
33
33
34
- // #define PERFORM_BAUD_CORRECTION
35
-
36
34
// this next line disables the entire UART.cpp,
37
35
// this is so I can support Attiny series and any other chip without a uart
38
36
#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)
144
142
// ********Check if desired baud rate is within the acceptable range for using CLK2X RX-mode********
145
143
// Condition from datasheet
146
144
// This limits the minimum baud_setting value to 64 (0x0040)
147
- if ((8 * baud) <= F_CPU ) {
145
+ if ((8 * baud) <= F_CPU_CORRECTED ) {
148
146
149
147
// Check that the desired baud rate is not so low that it will
150
148
// 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 ))) {
152
150
// 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 ;
155
153
// Enable CLK2X
156
154
(*_hwserial_module).CTRLB |= USART_RXMODE_CLK2X_gc;
157
155
} else {
@@ -162,14 +160,14 @@ void UartClass::begin(unsigned long baud, uint16_t config)
162
160
// ********Check if desired baud rate is within the acceptable range for using normal RX-mode********
163
161
// Condition from datasheet
164
162
// 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 )) {
166
164
167
165
// Check that the desired baud rate is not so low that it will
168
166
// 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 ))) {
170
168
// 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 ;
173
171
// Make sure CLK2X is disabled
174
172
(*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc);
175
173
} else {
@@ -185,24 +183,6 @@ void UartClass::begin(unsigned long baud, uint16_t config)
185
183
// Do nothing if an invalid baud rate is requested
186
184
if (!error) {
187
185
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
-
206
186
_written = false ;
207
187
208
188
// Set up the rx pin
0 commit comments