Skip to content

Commit a56c001

Browse files
authored
Merge pull request #4 from espreng/api-avr4809-master
Fix for Serial to use the corrected CPU frequency calculated in init …
2 parents 7862dbf + edab352 commit a56c001

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

Diff for: cores/arduino/UART.cpp

+8-28
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
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

Diff for: variants/uno2018/pins_arduino.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
5656
// USART1 on mega4809 (alternative pins)
5757
// Mapped to HWSERIAL0 in Serial library
5858
#define HWSERIAL0 (&USART1)
59-
//!!BUG in device header file. The RXC and DRE vectors are swapped!!
6059
#define HWSERIAL0_DRE_VECTOR (USART1_DRE_vect)
60+
#define HWSERIAL0_DRE_VECTOR_NUM (USART1_DRE_vect_num)
6161
#define HWSERIAL0_RXC_VECTOR (USART1_RXC_vect)
6262
#ifdef REV_A_ENGINEERING_SAMPLE
6363
#define PIN_WIRE_HWSERIAL0_RX (12)
@@ -71,8 +71,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
7171
// USART3 on mega4809 (alternative pins)
7272
// Mapped to HWSERIAL1 in Serial library
7373
#define HWSERIAL1 (&USART3)
74-
//!!BUG in device header file. The RXC and DRE vectors are swapped!!
7574
#define HWSERIAL1_DRE_VECTOR (USART3_DRE_vect)
75+
#define HWSERIAL1_DRE_VECTOR_NUM (USART3_DRE_vect_num)
7676
#define HWSERIAL1_RXC_VECTOR (USART3_RXC_vect)
7777
#define PIN_WIRE_HWSERIAL1_RX (26)
7878
#define PIN_WIRE_HWSERIAL1_TX (27)
@@ -81,8 +81,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
8181
// USART0 on mega4809 (alternative pins)
8282
// Mapped to HWSERIAL2 in Serial library
8383
#define HWSERIAL2 (&USART0)
84-
//!!BUG in device header file. The RXC and DRE vectors are swapped!!
8584
#define HWSERIAL2_DRE_VECTOR (USART0_DRE_vect)
85+
#define HWSERIAL2_DRE_VECTOR_NUM (USART0_DRE_vect_num)
8686
#define HWSERIAL2_RXC_VECTOR (USART0_RXC_vect)
8787
#define PIN_WIRE_HWSERIAL2_RX (23)
8888
#define PIN_WIRE_HWSERIAL2_TX (24)

0 commit comments

Comments
 (0)