From 103ddee889529da6da1a5b16d466e3f99df7677e Mon Sep 17 00:00:00 2001 From: Tom Keddie Date: Fri, 1 Jan 2016 15:42:33 -0800 Subject: [PATCH 1/4] =?UTF-8?q?Fix=20operation=20at=201200=20baud.=20=20As?= =?UTF-8?q?=20per=20Table=2024-2=20of=20Atmel-42181G=E2=80=93SAM-D21=5FDat?= =?UTF-8?q?asheet=E2=80=9309/2015,=20constant=20in=20formula=20should=20be?= =?UTF-8?q?=2065536,=20not=2065535.=20=20Also=20add=200.5=20to=20round=20c?= =?UTF-8?q?orrectly=20to=20closest=20integer.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/SERCOM.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/arduino/SERCOM.cpp b/cores/arduino/SERCOM.cpp index 34d3aa18b..e8fadfb19 100644 --- a/cores/arduino/SERCOM.cpp +++ b/cores/arduino/SERCOM.cpp @@ -62,9 +62,9 @@ void SERCOM::initUART(SercomUartMode mode, SercomUartSampleRate sampleRate, uint } // Asynchronous arithmetic mode - // 65535 * ( 1 - sampleRateValue * baudrate / SystemCoreClock); - // 65535 - 65535 * (sampleRateValue * baudrate / SystemCoreClock)); - sercom->USART.BAUD.reg = 65535.0f * ( 1.0f - (float)(sampleRateValue) * (float)(baudrate) / (float)(SystemCoreClock)); + // 65536 * ( 1 - sampleRateValue * baudrate / SystemCoreClock); + // add 0.5 to round up/down as appropriate + sercom->USART.BAUD.reg = 65536.0f * ( 1.0f - (float)(sampleRateValue) * (float)(baudrate) / (float)(SystemCoreClock)) + 0.5f; } } void SERCOM::initFrame(SercomUartCharSize charSize, SercomDataOrder dataOrder, SercomParityMode parityMode, SercomNumberStopBit nbStopBits) From 39a958a9f69f8e74898abfbca5ce7333dcdae382 Mon Sep 17 00:00:00 2001 From: Tom Keddie Date: Sat, 2 Jan 2016 16:33:31 -0800 Subject: [PATCH 2/4] Fix https://github.com/arduino/ArduinoCore-samd/issues/83 by changing to 16 bits --- cores/arduino/Uart.cpp | 8 ++++---- cores/arduino/Uart.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cores/arduino/Uart.cpp b/cores/arduino/Uart.cpp index 6f24bed9e..7a718e2c1 100644 --- a/cores/arduino/Uart.cpp +++ b/cores/arduino/Uart.cpp @@ -34,7 +34,7 @@ void Uart::begin(unsigned long baudrate) begin(baudrate, (uint8_t)SERIAL_8N1); } -void Uart::begin(unsigned long baudrate, uint8_t config) +void Uart::begin(unsigned long baudrate, uint16_t config) { pinPeripheral(uc_pinRX, g_APinDescription[uc_pinRX].ulPinType); pinPeripheral(uc_pinTX, g_APinDescription[uc_pinTX].ulPinType); @@ -93,7 +93,7 @@ size_t Uart::write(const uint8_t data) return 1; } -SercomNumberStopBit Uart::extractNbStopBit(uint8_t config) +SercomNumberStopBit Uart::extractNbStopBit(uint16_t config) { switch(config & HARDSER_STOP_BIT_MASK) { @@ -106,7 +106,7 @@ SercomNumberStopBit Uart::extractNbStopBit(uint8_t config) } } -SercomUartCharSize Uart::extractCharSize(uint8_t config) +SercomUartCharSize Uart::extractCharSize(uint16_t config) { switch(config & HARDSER_DATA_MASK) { @@ -126,7 +126,7 @@ SercomUartCharSize Uart::extractCharSize(uint8_t config) } } -SercomParityMode Uart::extractParity(uint8_t config) +SercomParityMode Uart::extractParity(uint16_t config) { switch(config & HARDSER_PARITY_MASK) { diff --git a/cores/arduino/Uart.h b/cores/arduino/Uart.h index 3c88d6711..7115da740 100644 --- a/cores/arduino/Uart.h +++ b/cores/arduino/Uart.h @@ -29,7 +29,7 @@ class Uart : public HardwareSerial public: Uart(SERCOM *_s, uint8_t _pinRX, uint8_t _pinTX, SercomRXPad _padRX, SercomUartTXPad _padTX); void begin(unsigned long baudRate); - void begin(unsigned long baudrate, uint8_t config); + void begin(unsigned long baudrate, uint16_t config); void end(); int available(); int peek(); @@ -51,7 +51,7 @@ class Uart : public HardwareSerial SercomRXPad uc_padRX; SercomUartTXPad uc_padTX; - SercomNumberStopBit extractNbStopBit(uint8_t config); - SercomUartCharSize extractCharSize(uint8_t config); - SercomParityMode extractParity(uint8_t config); + SercomNumberStopBit extractNbStopBit(uint16_t config); + SercomUartCharSize extractCharSize(uint16_t config); + SercomParityMode extractParity(uint16_t config); }; From 780d085cc7334e018733e570437c17753c275e73 Mon Sep 17 00:00:00 2001 From: Tom Keddie Date: Sat, 2 Jan 2016 16:45:42 -0800 Subject: [PATCH 3/4] Add missed file to fix of https://github.com/arduino/ArduinoCore-samd/issues/83 --- cores/arduino/HardwareSerial.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 7c3a76ad2..62508e786 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -68,7 +68,7 @@ class HardwareSerial : public Stream { public: virtual void begin(unsigned long); - virtual void begin(unsigned long baudrate, uint8_t config); + virtual void begin(unsigned long baudrate, uint16_t config); virtual void end(); virtual int available(void) = 0; virtual int peek(void) = 0; From a06eb6d9480c1318bdf4e0a68e8f0d8f1bece769 Mon Sep 17 00:00:00 2001 From: Tom Keddie Date: Sat, 2 Jan 2016 16:50:19 -0800 Subject: [PATCH 4/4] Change flush semantics to match AVR, fixes https://github.com/arduino/ArduinoCore-samd/issues/82 --- cores/arduino/SERCOM.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/arduino/SERCOM.cpp b/cores/arduino/SERCOM.cpp index e8fadfb19..5aa52e510 100644 --- a/cores/arduino/SERCOM.cpp +++ b/cores/arduino/SERCOM.cpp @@ -112,7 +112,7 @@ void SERCOM::enableUART() void SERCOM::flushUART() { // Wait for transmission to complete - while(sercom->USART.INTFLAG.bit.DRE != SERCOM_USART_INTFLAG_DRE); + while(!sercom->USART.INTFLAG.bit.TXC); } void SERCOM::clearStatusUART() @@ -168,8 +168,8 @@ uint8_t SERCOM::readDataUART() int SERCOM::writeDataUART(uint8_t data) { - //Flush UART buffer - flushUART(); + // Wait for data register to be empty + while(!isDataRegisterEmptyUART()); //Put data into DATA register sercom->USART.DATA.reg = (uint16_t)data;