2011-11-27 20:12:55 +00:00
|
|
|
/*
|
|
|
|
HardwareSerial.cpp - Hardware serial library for Wiring
|
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2015-10-03 06:08:58 +00:00
|
|
|
|
2011-11-27 20:12:55 +00:00
|
|
|
Modified 23 November 2006 by David A. Mellis
|
|
|
|
Modified 28 September 2010 by Mark Sproul
|
|
|
|
*/
|
|
|
|
|
2011-12-22 13:55:45 +00:00
|
|
|
#include "Marlin.h"
|
|
|
|
#include "MarlinSerial.h"
|
2011-11-27 20:12:55 +00:00
|
|
|
|
2015-05-24 16:32:34 +00:00
|
|
|
#ifndef USBCON
|
2015-10-03 06:08:58 +00:00
|
|
|
// this next line disables the entire HardwareSerial.cpp,
|
2014-02-17 10:58:36 +00:00
|
|
|
// this is so I can support Attiny series and any other chip without a UART
|
2011-11-27 20:12:55 +00:00
|
|
|
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
|
|
|
|
|
2012-11-05 10:34:27 +00:00
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
2011-11-27 20:12:55 +00:00
|
|
|
ring_buffer rx_buffer = { { 0 }, 0, 0 };
|
|
|
|
#endif
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
FORCE_INLINE void store_char(unsigned char c) {
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
uint8_t h = rx_buffer.head;
|
|
|
|
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
// if we should be storing the received character into the location
|
|
|
|
// just before the tail (meaning that the head would advance to the
|
|
|
|
// current location of the tail), we're about to overflow the buffer
|
|
|
|
// and so we don't write the character or advance the head.
|
|
|
|
if (i != rx_buffer.tail) {
|
|
|
|
rx_buffer.buffer[h] = c;
|
|
|
|
rx_buffer.head = i;
|
|
|
|
}
|
|
|
|
CRITICAL_SECTION_END;
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#elif defined(SIG_USART_RECV)
|
2012-11-05 10:34:27 +00:00
|
|
|
#if defined(M_USARTx_RX_vect)
|
2011-11-27 20:12:55 +00:00
|
|
|
// fixed by Mark Sproul this is on the 644/644p
|
|
|
|
//SIGNAL(SIG_USART_RECV)
|
2015-01-24 05:11:50 +00:00
|
|
|
SIGNAL(M_USARTx_RX_vect) {
|
2012-11-05 10:34:27 +00:00
|
|
|
unsigned char c = M_UDRx;
|
2011-11-28 18:13:40 +00:00
|
|
|
store_char(c);
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
MarlinSerial::MarlinSerial() { }
|
2011-11-27 20:12:55 +00:00
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::begin(long baud) {
|
2011-11-27 20:12:55 +00:00
|
|
|
uint16_t baud_setting;
|
2012-11-05 10:34:27 +00:00
|
|
|
bool useU2X = true;
|
2011-11-27 20:12:55 +00:00
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
#if F_CPU == 16000000UL && SERIAL_PORT == 0
|
|
|
|
// hard-coded exception for compatibility with the bootloader shipped
|
|
|
|
// with the Duemilanove and previous boards and the firmware on the 8U2
|
|
|
|
// on the Uno and Mega 2560.
|
|
|
|
if (baud == 57600) {
|
|
|
|
useU2X = false;
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-03 06:08:58 +00:00
|
|
|
|
2012-11-05 10:34:27 +00:00
|
|
|
if (useU2X) {
|
2015-03-14 11:28:22 +00:00
|
|
|
M_UCSRxA = BIT(M_U2Xx);
|
2011-11-27 20:12:55 +00:00
|
|
|
baud_setting = (F_CPU / 4 / baud - 1) / 2;
|
2015-10-13 10:51:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-11-05 10:34:27 +00:00
|
|
|
M_UCSRxA = 0;
|
2011-11-27 20:12:55 +00:00
|
|
|
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
|
2012-11-05 10:34:27 +00:00
|
|
|
M_UBRRxH = baud_setting >> 8;
|
|
|
|
M_UBRRxL = baud_setting;
|
2011-11-27 20:12:55 +00:00
|
|
|
|
2012-11-05 10:34:27 +00:00
|
|
|
sbi(M_UCSRxB, M_RXENx);
|
|
|
|
sbi(M_UCSRxB, M_TXENx);
|
|
|
|
sbi(M_UCSRxB, M_RXCIEx);
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::end() {
|
2012-11-05 10:34:27 +00:00
|
|
|
cbi(M_UCSRxB, M_RXENx);
|
|
|
|
cbi(M_UCSRxB, M_TXENx);
|
2015-10-03 06:08:58 +00:00
|
|
|
cbi(M_UCSRxB, M_RXCIEx);
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 18:13:40 +00:00
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
int MarlinSerial::peek(void) {
|
2016-02-24 16:18:12 +00:00
|
|
|
int v;
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
uint8_t t = rx_buffer.tail;
|
|
|
|
if (rx_buffer.head == t) {
|
|
|
|
v = -1;
|
2015-10-03 06:08:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-02-24 16:18:12 +00:00
|
|
|
v = rx_buffer.buffer[t];
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
return v;
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
int MarlinSerial::read(void) {
|
2016-02-24 16:18:12 +00:00
|
|
|
int v;
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
uint8_t t = rx_buffer.tail;
|
|
|
|
if (rx_buffer.head == t) {
|
|
|
|
v = -1;
|
2015-01-24 05:11:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-02-24 16:18:12 +00:00
|
|
|
v = rx_buffer.buffer[t];
|
|
|
|
rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
return v;
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::flush() {
|
2011-11-27 20:12:55 +00:00
|
|
|
// don't reverse this or there may be problems if the RX interrupt
|
|
|
|
// occurs after reading the value of rx_buffer_head but before writing
|
|
|
|
// the value to rx_buffer_tail; the previous value of rx_buffer_head
|
|
|
|
// may be written to rx_buffer_tail, making it appear as if the buffer
|
|
|
|
// don't reverse this or there may be problems if the RX interrupt
|
|
|
|
// occurs after reading the value of rx_buffer_head but before writing
|
|
|
|
// the value to rx_buffer_tail; the previous value of rx_buffer_head
|
|
|
|
// may be written to rx_buffer_tail, making it appear as if the buffer
|
|
|
|
// were full, not empty.
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
rx_buffer.head = rx_buffer.tail;
|
|
|
|
CRITICAL_SECTION_END;
|
2011-11-27 20:12:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 18:13:40 +00:00
|
|
|
|
|
|
|
/// imports from print.h
|
2011-11-27 20:12:55 +00:00
|
|
|
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(char c, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print((long) c, base);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(unsigned char b, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print((unsigned long) b, base);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(int n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print((long) n, base);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(unsigned int n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print((unsigned long) n, base);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(long n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
if (base == 0) {
|
|
|
|
write(n);
|
2015-01-24 05:11:50 +00:00
|
|
|
}
|
|
|
|
else if (base == 10) {
|
2011-11-28 18:13:40 +00:00
|
|
|
if (n < 0) {
|
|
|
|
print('-');
|
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
printNumber(n, 10);
|
2015-10-03 06:08:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-11-28 18:13:40 +00:00
|
|
|
printNumber(n, base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(unsigned long n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
if (base == 0) write(n);
|
|
|
|
else printNumber(n, base);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::print(double n, int digits) {
|
2011-11-28 18:13:40 +00:00
|
|
|
printFloat(n, digits);
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(void) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print('\r');
|
2015-10-03 06:08:58 +00:00
|
|
|
print('\n');
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 06:08:58 +00:00
|
|
|
void MarlinSerial::println(const String& s) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(s);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(const char c[]) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(c);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(char c, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(c, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(unsigned char b, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(b, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(int n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(unsigned int n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(long n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(unsigned long n, int base) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::println(double n, int digits) {
|
2011-11-28 18:13:40 +00:00
|
|
|
print(n, digits);
|
|
|
|
println();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private Methods /////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
2015-10-03 06:08:58 +00:00
|
|
|
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
2011-11-28 18:13:40 +00:00
|
|
|
unsigned long i = 0;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
print('0');
|
|
|
|
return;
|
2015-10-03 06:08:58 +00:00
|
|
|
}
|
2011-11-28 18:13:40 +00:00
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
buf[i++] = n % base;
|
|
|
|
n /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i > 0; i--)
|
2015-10-03 06:08:58 +00:00
|
|
|
print((char)(buf[i - 1] < 10 ?
|
|
|
|
'0' + buf[i - 1] :
|
|
|
|
'A' + buf[i - 1] - 10));
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
void MarlinSerial::printFloat(double number, uint8_t digits) {
|
2011-11-28 18:13:40 +00:00
|
|
|
// Handle negative numbers
|
2015-01-24 05:11:50 +00:00
|
|
|
if (number < 0.0) {
|
2015-10-03 06:08:58 +00:00
|
|
|
print('-');
|
|
|
|
number = -number;
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Round correctly so that print(1.999, 2) prints as "2.00"
|
|
|
|
double rounding = 0.5;
|
2015-01-24 05:11:50 +00:00
|
|
|
for (uint8_t i = 0; i < digits; ++i)
|
2011-11-28 18:13:40 +00:00
|
|
|
rounding /= 10.0;
|
2015-10-03 06:08:58 +00:00
|
|
|
|
2011-11-28 18:13:40 +00:00
|
|
|
number += rounding;
|
|
|
|
|
|
|
|
// Extract the integer part of the number and print it
|
|
|
|
unsigned long int_part = (unsigned long)number;
|
|
|
|
double remainder = number - (double)int_part;
|
|
|
|
print(int_part);
|
|
|
|
|
|
|
|
// Print the decimal point, but only if there are digits beyond
|
2015-04-04 04:43:30 +00:00
|
|
|
if (digits > 0) print('.');
|
2011-11-28 18:13:40 +00:00
|
|
|
|
|
|
|
// Extract digits from the remainder one at a time
|
2015-01-24 05:11:50 +00:00
|
|
|
while (digits-- > 0) {
|
2011-11-28 18:13:40 +00:00
|
|
|
remainder *= 10.0;
|
|
|
|
int toPrint = int(remainder);
|
|
|
|
print(toPrint);
|
2015-10-03 06:08:58 +00:00
|
|
|
remainder -= toPrint;
|
|
|
|
}
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
2011-11-27 20:12:55 +00:00
|
|
|
// Preinstantiate Objects //////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2015-08-01 03:31:53 +00:00
|
|
|
MarlinSerial customizedSerial;
|
2011-11-27 20:12:55 +00:00
|
|
|
|
|
|
|
#endif // whole file
|
2015-05-24 16:32:34 +00:00
|
|
|
#endif // !USBCON
|
2013-10-30 10:45:32 +00:00
|
|
|
|
|
|
|
// For AT90USB targets use the UART for BT interfacing
|
2015-07-31 23:55:47 +00:00
|
|
|
#if defined(USBCON) && ENABLED(BLUETOOTH)
|
2015-08-01 03:31:53 +00:00
|
|
|
HardwareSerial bluetoothSerial;
|
2013-10-30 10:45:32 +00:00
|
|
|
#endif
|