|
|
|
@ -33,21 +33,111 @@
|
|
|
|
|
#include "stepper.h"
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
|
|
|
|
|
#ifndef USBCON
|
|
|
|
|
// this next line disables the entire HardwareSerial.cpp,
|
|
|
|
|
// this is so I can support Attiny series and any other chip without a UART
|
|
|
|
|
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
|
|
|
|
|
// Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
|
|
|
|
|
|
|
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
|
|
|
|
#if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
|
|
|
|
|
|
|
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
|
|
|
|
ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
|
|
|
|
|
static bool _written;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
|
|
|
|
|
|
#include "language.h"
|
|
|
|
|
|
|
|
|
|
// Currently looking for: M108, M112, M410
|
|
|
|
|
// If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE void emergency_parser(const unsigned char c) {
|
|
|
|
|
|
|
|
|
|
static e_parser_state state = state_RESET;
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case state_RESET:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ': break;
|
|
|
|
|
case 'N': state = state_N; break;
|
|
|
|
|
case 'M': state = state_M; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_N:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '0': case '1': case '2':
|
|
|
|
|
case '3': case '4': case '5':
|
|
|
|
|
case '6': case '7': case '8':
|
|
|
|
|
case '9': case '-': case ' ': break;
|
|
|
|
|
case 'M': state = state_M; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ': break;
|
|
|
|
|
case '1': state = state_M1; break;
|
|
|
|
|
case '4': state = state_M4; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M1:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '0': state = state_M10; break;
|
|
|
|
|
case '1': state = state_M11; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M10:
|
|
|
|
|
state = (c == '8') ? state_M108 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M11:
|
|
|
|
|
state = (c == '2') ? state_M112 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M4:
|
|
|
|
|
state = (c == '1') ? state_M41 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M41:
|
|
|
|
|
state = (c == '0') ? state_M410 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_IGNORE:
|
|
|
|
|
if (c == '\n') state = state_RESET;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (c == '\n') {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case state_M108:
|
|
|
|
|
wait_for_user = wait_for_heatup = false;
|
|
|
|
|
break;
|
|
|
|
|
case state_M112:
|
|
|
|
|
kill(PSTR(MSG_KILLED));
|
|
|
|
|
break;
|
|
|
|
|
case state_M410:
|
|
|
|
|
quickstop_stepper();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
state = state_RESET;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE void store_char(unsigned char c) {
|
|
|
|
|
FORCE_INLINE void store_char(unsigned char c) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
uint8_t h = rx_buffer.head;
|
|
|
|
|
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
|
|
|
|
@ -65,9 +155,9 @@ FORCE_INLINE void store_char(unsigned char c) {
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
|
emergency_parser(c);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE void _tx_udr_empty_irq(void) {
|
|
|
|
|
// If interrupts are enabled, there must be more data in the output
|
|
|
|
@ -95,22 +185,18 @@ FORCE_INLINE void store_char(unsigned char c) {
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // TX_BUFFER_SIZE
|
|
|
|
|
#endif // TX_BUFFER_SIZE
|
|
|
|
|
|
|
|
|
|
#ifdef M_USARTx_RX_vect
|
|
|
|
|
#ifdef M_USARTx_RX_vect
|
|
|
|
|
ISR(M_USARTx_RX_vect) {
|
|
|
|
|
unsigned char c = M_UDRx;
|
|
|
|
|
store_char(c);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
MarlinSerial::MarlinSerial() { }
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
// Public Methods
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::begin(long baud) {
|
|
|
|
|
void MarlinSerial::begin(long baud) {
|
|
|
|
|
uint16_t baud_setting;
|
|
|
|
|
bool useU2X = true;
|
|
|
|
|
|
|
|
|
@ -143,30 +229,30 @@ void MarlinSerial::begin(long baud) {
|
|
|
|
|
CBI(M_UCSRxB, M_UDRIEx);
|
|
|
|
|
_written = false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::end() {
|
|
|
|
|
void MarlinSerial::end() {
|
|
|
|
|
CBI(M_UCSRxB, M_RXENx);
|
|
|
|
|
CBI(M_UCSRxB, M_TXENx);
|
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
|
CBI(M_UCSRxB, M_UDRIEx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::checkRx(void) {
|
|
|
|
|
void MarlinSerial::checkRx(void) {
|
|
|
|
|
if (TEST(M_UCSRxA, M_RXCx)) {
|
|
|
|
|
uint8_t c = M_UDRx;
|
|
|
|
|
store_char(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MarlinSerial::peek(void) {
|
|
|
|
|
int MarlinSerial::peek(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MarlinSerial::read(void) {
|
|
|
|
|
int MarlinSerial::read(void) {
|
|
|
|
|
int v;
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
uint8_t t = rx_buffer.tail;
|
|
|
|
@ -179,17 +265,17 @@ int MarlinSerial::read(void) {
|
|
|
|
|
}
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t MarlinSerial::available(void) {
|
|
|
|
|
uint8_t MarlinSerial::available(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
uint8_t h = rx_buffer.head,
|
|
|
|
|
t = rx_buffer.tail;
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::flush(void) {
|
|
|
|
|
void MarlinSerial::flush(void) {
|
|
|
|
|
// RX
|
|
|
|
|
// 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
|
|
|
|
@ -199,9 +285,9 @@ void MarlinSerial::flush(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
rx_buffer.head = rx_buffer.tail;
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
uint8_t MarlinSerial::availableForWrite(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
uint8_t h = tx_buffer.head;
|
|
|
|
@ -270,38 +356,38 @@ void MarlinSerial::flush(void) {
|
|
|
|
|
}
|
|
|
|
|
// If we get here, nothing is queued anymore (DRIE is disabled) and
|
|
|
|
|
// the hardware finished tranmission (TXC is set).
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
void MarlinSerial::write(uint8_t c) {
|
|
|
|
|
while (!TEST(M_UCSRxA, M_UDREx))
|
|
|
|
|
;
|
|
|
|
|
M_UDRx = c;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// end NEW
|
|
|
|
|
// end NEW
|
|
|
|
|
|
|
|
|
|
/// imports from print.h
|
|
|
|
|
/// imports from print.h
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(char c, int base) {
|
|
|
|
|
void MarlinSerial::print(char c, int base) {
|
|
|
|
|
print((long) c, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(unsigned char b, int base) {
|
|
|
|
|
void MarlinSerial::print(unsigned char b, int base) {
|
|
|
|
|
print((unsigned long) b, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(int n, int base) {
|
|
|
|
|
void MarlinSerial::print(int n, int base) {
|
|
|
|
|
print((long) n, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(unsigned int n, int base) {
|
|
|
|
|
void MarlinSerial::print(unsigned int n, int base) {
|
|
|
|
|
print((unsigned long) n, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(long n, int base) {
|
|
|
|
|
void MarlinSerial::print(long n, int base) {
|
|
|
|
|
if (base == 0) {
|
|
|
|
|
write(n);
|
|
|
|
|
}
|
|
|
|
@ -315,70 +401,70 @@ void MarlinSerial::print(long n, int base) {
|
|
|
|
|
else {
|
|
|
|
|
printNumber(n, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(unsigned long n, int base) {
|
|
|
|
|
void MarlinSerial::print(unsigned long n, int base) {
|
|
|
|
|
if (base == 0) write(n);
|
|
|
|
|
else printNumber(n, base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(double n, int digits) {
|
|
|
|
|
void MarlinSerial::print(double n, int digits) {
|
|
|
|
|
printFloat(n, digits);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(void) {
|
|
|
|
|
void MarlinSerial::println(void) {
|
|
|
|
|
print('\r');
|
|
|
|
|
print('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(const String& s) {
|
|
|
|
|
void MarlinSerial::println(const String& s) {
|
|
|
|
|
print(s);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(const char c[]) {
|
|
|
|
|
void MarlinSerial::println(const char c[]) {
|
|
|
|
|
print(c);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(char c, int base) {
|
|
|
|
|
void MarlinSerial::println(char c, int base) {
|
|
|
|
|
print(c, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(unsigned char b, int base) {
|
|
|
|
|
void MarlinSerial::println(unsigned char b, int base) {
|
|
|
|
|
print(b, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(int n, int base) {
|
|
|
|
|
void MarlinSerial::println(int n, int base) {
|
|
|
|
|
print(n, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(unsigned int n, int base) {
|
|
|
|
|
void MarlinSerial::println(unsigned int n, int base) {
|
|
|
|
|
print(n, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(long n, int base) {
|
|
|
|
|
void MarlinSerial::println(long n, int base) {
|
|
|
|
|
print(n, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(unsigned long n, int base) {
|
|
|
|
|
void MarlinSerial::println(unsigned long n, int base) {
|
|
|
|
|
print(n, base);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::println(double n, int digits) {
|
|
|
|
|
void MarlinSerial::println(double n, int digits) {
|
|
|
|
|
print(n, digits);
|
|
|
|
|
println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private Methods /////////////////////////////////////////////////////////////
|
|
|
|
|
// Private Methods
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
|
|
|
|
void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
|
|
|
|
if (n) {
|
|
|
|
|
unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
|
|
|
|
|
int8_t i = 0;
|
|
|
|
@ -391,9 +477,9 @@ void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
print('0');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::printFloat(double number, uint8_t digits) {
|
|
|
|
|
void MarlinSerial::printFloat(double number, uint8_t digits) {
|
|
|
|
|
// Handle negative numbers
|
|
|
|
|
if (number < 0.0) {
|
|
|
|
|
print('-');
|
|
|
|
@ -423,105 +509,14 @@ void MarlinSerial::printFloat(double number, uint8_t digits) {
|
|
|
|
|
remainder -= toPrint;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Preinstantiate Objects //////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MarlinSerial customizedSerial;
|
|
|
|
|
// Preinstantiate
|
|
|
|
|
MarlinSerial customizedSerial;
|
|
|
|
|
|
|
|
|
|
#endif // whole file
|
|
|
|
|
#endif // !USBCON
|
|
|
|
|
#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
|
|
|
|
|
|
|
|
|
|
// For AT90USB targets use the UART for BT interfacing
|
|
|
|
|
#if defined(USBCON) && ENABLED(BLUETOOTH)
|
|
|
|
|
HardwareSerial bluetoothSerial;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
|
|
|
|
|
|
// Currently looking for: M108, M112, M410
|
|
|
|
|
// If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE void emergency_parser(unsigned char c) {
|
|
|
|
|
|
|
|
|
|
static e_parser_state state = state_RESET;
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case state_RESET:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ': break;
|
|
|
|
|
case 'N': state = state_N; break;
|
|
|
|
|
case 'M': state = state_M; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_N:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '0': case '1': case '2':
|
|
|
|
|
case '3': case '4': case '5':
|
|
|
|
|
case '6': case '7': case '8':
|
|
|
|
|
case '9': case '-': case ' ': break;
|
|
|
|
|
case 'M': state = state_M; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ': break;
|
|
|
|
|
case '1': state = state_M1; break;
|
|
|
|
|
case '4': state = state_M4; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M1:
|
|
|
|
|
switch (c) {
|
|
|
|
|
case '0': state = state_M10; break;
|
|
|
|
|
case '1': state = state_M11; break;
|
|
|
|
|
default: state = state_IGNORE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M10:
|
|
|
|
|
state = (c == '8') ? state_M108 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M11:
|
|
|
|
|
state = (c == '2') ? state_M112 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M4:
|
|
|
|
|
state = (c == '1') ? state_M41 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_M41:
|
|
|
|
|
state = (c == '0') ? state_M410 : state_IGNORE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case state_IGNORE:
|
|
|
|
|
if (c == '\n') state = state_RESET;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (c == '\n') {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case state_M108:
|
|
|
|
|
wait_for_user = wait_for_heatup = false;
|
|
|
|
|
break;
|
|
|
|
|
case state_M112:
|
|
|
|
|
kill(PSTR(MSG_KILLED));
|
|
|
|
|
break;
|
|
|
|
|
case state_M410:
|
|
|
|
|
quickstop_stepper();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
state = state_RESET;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|