123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
-
-
-
-
- #include "MarlinSerial.h"
- #include "Marlin.h"
-
-
-
- #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
-
- #if ENABLED(EMERGENCY_PARSER)
-
- #include "stepper.h"
- #include "language.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) {
- CRITICAL_SECTION_START;
- const uint8_t h = rx_buffer.head,
- i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
-
-
-
-
-
- if (i != rx_buffer.tail) {
- rx_buffer.buffer[h] = c;
- rx_buffer.head = i;
- }
- CRITICAL_SECTION_END;
-
- #if ENABLED(EMERGENCY_PARSER)
- emergency_parser(c);
- #endif
- }
-
- #if TX_BUFFER_SIZE > 0
-
- FORCE_INLINE void _tx_udr_empty_irq(void) {
-
-
- const uint8_t t = tx_buffer.tail,
- c = tx_buffer.buffer[t];
- tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
-
- M_UDRx = c;
-
-
-
-
- SBI(M_UCSRxA, M_TXCx);
-
- if (tx_buffer.head == tx_buffer.tail) {
-
- CBI(M_UCSRxB, M_UDRIEx);
- }
- }
-
- #ifdef M_USARTx_UDRE_vect
- ISR(M_USARTx_UDRE_vect) {
- _tx_udr_empty_irq();
- }
- #endif
-
- #endif
-
- #ifdef M_USARTx_RX_vect
- ISR(M_USARTx_RX_vect) {
- const unsigned char c = M_UDRx;
- store_char(c);
- }
- #endif
-
-
-
- void MarlinSerial::begin(const long baud) {
- uint16_t baud_setting;
- bool useU2X = true;
-
- #if F_CPU == 16000000UL && SERIAL_PORT == 0
-
-
-
- if (baud == 57600) useU2X = false;
- #endif
-
- if (useU2X) {
- M_UCSRxA = _BV(M_U2Xx);
- baud_setting = (F_CPU / 4 / baud - 1) / 2;
- }
- else {
- M_UCSRxA = 0;
- baud_setting = (F_CPU / 8 / baud - 1) / 2;
- }
-
-
- M_UBRRxH = baud_setting >> 8;
- M_UBRRxL = baud_setting;
-
- SBI(M_UCSRxB, M_RXENx);
- SBI(M_UCSRxB, M_TXENx);
- SBI(M_UCSRxB, M_RXCIEx);
- #if TX_BUFFER_SIZE > 0
- CBI(M_UCSRxB, M_UDRIEx);
- _written = false;
- #endif
- }
-
- 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) {
- if (TEST(M_UCSRxA, M_RXCx)) {
- const uint8_t c = M_UDRx;
- store_char(c);
- }
- }
-
- int MarlinSerial::peek(void) {
- CRITICAL_SECTION_START;
- const 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 v;
- CRITICAL_SECTION_START;
- const uint8_t t = rx_buffer.tail;
- if (rx_buffer.head == t)
- v = -1;
- else {
- v = rx_buffer.buffer[t];
- rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
- }
- CRITICAL_SECTION_END;
- return v;
- }
-
- uint8_t MarlinSerial::available(void) {
- CRITICAL_SECTION_START;
- const 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) {
-
-
-
-
-
-
- CRITICAL_SECTION_START;
- rx_buffer.head = rx_buffer.tail;
- CRITICAL_SECTION_END;
- }
-
- #if TX_BUFFER_SIZE > 0
- uint8_t MarlinSerial::availableForWrite(void) {
- CRITICAL_SECTION_START;
- const uint8_t h = tx_buffer.head,
- t = tx_buffer.tail;
- CRITICAL_SECTION_END;
- return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
- }
-
- void MarlinSerial::write(const uint8_t c) {
- _written = true;
- CRITICAL_SECTION_START;
- bool emty = (tx_buffer.head == tx_buffer.tail);
- CRITICAL_SECTION_END;
-
-
-
-
- if (emty && TEST(M_UCSRxA, M_UDREx)) {
- CRITICAL_SECTION_START;
- M_UDRx = c;
- SBI(M_UCSRxA, M_TXCx);
- CRITICAL_SECTION_END;
- return;
- }
- const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
-
-
-
- while (i == tx_buffer.tail) {
- if (!TEST(SREG, SREG_I)) {
-
-
-
-
- if (TEST(M_UCSRxA, M_UDREx))
- _tx_udr_empty_irq();
- } else {
-
- }
- }
-
- tx_buffer.buffer[tx_buffer.head] = c;
- { CRITICAL_SECTION_START;
- tx_buffer.head = i;
- SBI(M_UCSRxB, M_UDRIEx);
- CRITICAL_SECTION_END;
- }
- return;
- }
-
- void MarlinSerial::flushTX(void) {
-
-
-
-
- if (!_written)
- return;
-
- while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
- if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
-
-
-
- if (TEST(M_UCSRxA, M_UDREx))
- _tx_udr_empty_irq();
- }
-
-
- }
-
- #else
- void MarlinSerial::write(uint8_t c) {
- while (!TEST(M_UCSRxA, M_UDREx))
- ;
- M_UDRx = c;
- }
- #endif
-
-
-
-
-
-
- void MarlinSerial::print(char c, int base) {
- print((long)c, base);
- }
-
- void MarlinSerial::print(unsigned char b, int base) {
- print((unsigned long)b, base);
- }
-
- void MarlinSerial::print(int n, int base) {
- print((long)n, base);
- }
-
- void MarlinSerial::print(unsigned int n, int base) {
- print((unsigned long)n, base);
- }
-
- void MarlinSerial::print(long n, int base) {
- if (base == 0)
- write(n);
- else if (base == 10) {
- if (n < 0) {
- print('-');
- n = -n;
- }
- printNumber(n, 10);
- }
- else
- printNumber(n, 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) {
- printFloat(n, digits);
- }
-
- void MarlinSerial::println(void) {
- print('\r');
- print('\n');
- }
-
- void MarlinSerial::println(const String& s) {
- print(s);
- println();
- }
-
- void MarlinSerial::println(const char c[]) {
- print(c);
- println();
- }
-
- void MarlinSerial::println(char c, int base) {
- print(c, base);
- println();
- }
-
- void MarlinSerial::println(unsigned char b, int base) {
- print(b, base);
- println();
- }
-
- void MarlinSerial::println(int n, int base) {
- print(n, base);
- println();
- }
-
- void MarlinSerial::println(unsigned int n, int base) {
- print(n, base);
- println();
- }
-
- void MarlinSerial::println(long n, int base) {
- print(n, base);
- println();
- }
-
- void MarlinSerial::println(unsigned long n, int base) {
- print(n, base);
- println();
- }
-
- void MarlinSerial::println(double n, int digits) {
- print(n, digits);
- println();
- }
-
-
-
- void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
- if (n) {
- unsigned char buf[8 * sizeof(long)];
- int8_t i = 0;
- while (n) {
- buf[i++] = n % base;
- n /= base;
- }
- while (i--)
- print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
- }
- else
- print('0');
- }
-
- void MarlinSerial::printFloat(double number, uint8_t digits) {
-
- if (number < 0.0) {
- print('-');
- number = -number;
- }
-
-
- double rounding = 0.5;
- for (uint8_t i = 0; i < digits; ++i)
- rounding *= 0.1;
-
- number += rounding;
-
-
- unsigned long int_part = (unsigned long)number;
- double remainder = number - (double)int_part;
- print(int_part);
-
-
- if (digits) {
- print('.');
-
- while (digits--) {
- remainder *= 10.0;
- int toPrint = int(remainder);
- print(toPrint);
- remainder -= toPrint;
- }
- }
- }
-
-
- MarlinSerial customizedSerial;
-
- #endif
-
-
- #if defined(USBCON) && ENABLED(BLUETOOTH)
- HardwareSerial bluetoothSerial;
- #endif
|