Skip to content

Commit fe2b7e7

Browse files
committed
Always use normal RX clock (no 2x)
1 parent d60f5a9 commit fe2b7e7

File tree

1 file changed

+20
-60
lines changed

1 file changed

+20
-60
lines changed

cores/arduino/UART.cpp

+20-60
Original file line numberDiff line numberDiff line change
@@ -142,74 +142,34 @@ void UartClass::begin(unsigned long baud, uint16_t config)
142142
uint8_t oldSREG = SREG;
143143
cli();
144144

145-
// ********Check if desired baud rate is within the acceptable range for using CLK2X RX-mode********
146-
// Condition from datasheet
147-
// This limits the minimum baud_setting value to 64 (0x0040)
148-
if((8 * baud) <= F_CPU_CORRECTED) {
149-
150-
// Check that the desired baud rate is not so low that it will
151-
// cause the BAUD register to overflow (1024 * 64 = 2^16)
152-
if(baud > (F_CPU_CORRECTED / (8 * 1024))) {
153-
// Datasheet formula for calculating the baud setting including trick to reduce rounding error ((2*(X/Y))+1)/2
154-
// baud_setting = ( ( (2 * (64 * F_CPU_CORRECTED) / (8 * baud) ) + 1 ) / 2;
155-
baud_setting = (((16 * F_CPU_CORRECTED) / baud) + 1 ) / 2;
156-
// Enable CLK2X
157-
(*_hwserial_module).CTRLB |= USART_RXMODE_CLK2X_gc;
158-
} else {
159-
// Invalid baud rate requested.
160-
error = 1;
161-
}
162-
163-
// ********Check if desired baud rate is within the acceptable range for using normal RX-mode********
164-
// Condition from datasheet
165-
// This limits the minimum baud_setting value to 64 (0x0040)
166-
} else if ((16 * baud <= F_CPU_CORRECTED)) {
167-
168-
// Check that the desired baud rate is not so low that it will
169-
// cause the BAUD register to overflow (1024 * 64 = 2^16)
170-
if(baud > (F_CPU_CORRECTED / (16 * 1024))) {
171-
// Datasheet formula for calculating the baud setting including trick to reduce rounding error
172-
// baud_setting = ( ( (2 * (64 * F_CPU_CORRECTED) / (16 * baud) ) + 1 ) / 2;
173-
baud_setting = (((8 * F_CPU_CORRECTED) / baud) + 1 ) / 2;
174-
// Make sure CLK2X is disabled
175-
(*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc);
176-
} else {
177-
// Invalid baud rate requested.
178-
error = 1;
179-
}
145+
baud_setting = (((8 * F_CPU_CORRECTED) / baud) ) / 2;
146+
// Disable CLK2X
147+
(*_hwserial_module).CTRLB &= (~USART_RXMODE_CLK2X_gc);
148+
(*_hwserial_module).CTRLB |= USART_RXMODE_NORMAL_gc;
180149

181-
} else {
182-
// Invalid baud rate requested.
183-
error = 1;
184-
}
185-
186-
// Do nothing if an invalid baud rate is requested
187-
if(!error) {
188-
189-
_written = false;
150+
_written = false;
190151

191-
//Set up the rx pin
192-
pinMode(_hwserial_rx_pin, INPUT_PULLUP);
152+
//Set up the rx pin
153+
pinMode(_hwserial_rx_pin, INPUT_PULLUP);
193154

194-
//Set up the tx pin
195-
digitalWrite(_hwserial_tx_pin, HIGH);
196-
pinMode(_hwserial_tx_pin, OUTPUT);
155+
//Set up the tx pin
156+
digitalWrite(_hwserial_tx_pin, HIGH);
157+
pinMode(_hwserial_tx_pin, OUTPUT);
197158

198-
int8_t sigrow_val = SIGROW.OSC16ERR5V;
199-
baud_setting *= (1024 + sigrow_val);
200-
baud_setting /= (1024 - abs(sigrow_val));
159+
int8_t sigrow_val = SIGROW.OSC16ERR5V;
160+
baud_setting *= (1024 + sigrow_val);
161+
baud_setting /= (1024 - abs(sigrow_val));
201162

202-
// assign the baud_setting, a.k.a. BAUD (USART Baud Rate Register)
203-
(*_hwserial_module).BAUD = (int16_t) baud_setting;
163+
// assign the baud_setting, a.k.a. BAUD (USART Baud Rate Register)
164+
(*_hwserial_module).BAUD = (int16_t) baud_setting;
204165

205-
// Set USART mode of operation
206-
(*_hwserial_module).CTRLC = config;
166+
// Set USART mode of operation
167+
(*_hwserial_module).CTRLC = config;
207168

208-
// Enable transmitter and receiver
209-
(*_hwserial_module).CTRLB |= (USART_RXEN_bm | USART_TXEN_bm);
169+
// Enable transmitter and receiver
170+
(*_hwserial_module).CTRLB |= (USART_RXEN_bm | USART_TXEN_bm);
210171

211-
(*_hwserial_module).CTRLA |= USART_RXCIE_bm;
212-
}
172+
(*_hwserial_module).CTRLA |= USART_RXCIE_bm;
213173

214174
// Restore SREG content
215175
SREG = oldSREG;

0 commit comments

Comments
 (0)