My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiring_serial.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. wiring_serial.c - serial functions.
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. Modified 29 January 2009, Marius Kintel for Sanguino - http://www.sanguino.cc/
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General
  15. Public License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  19. */
  20. #include "wiring_private.h"
  21. // Define constants and variables for buffering incoming serial data. We're
  22. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  23. // location to which to write the next incoming character and rx_buffer_tail
  24. // is the index of the location from which to read.
  25. #define RX_BUFFER_SIZE 128
  26. #define RX_BUFFER_MASK 0x7f
  27. #if defined(__AVR_ATmega644P__)
  28. unsigned char rx_buffer[2][RX_BUFFER_SIZE];
  29. int rx_buffer_head[2] = {0, 0};
  30. int rx_buffer_tail[2] = {0, 0};
  31. #else
  32. unsigned char rx_buffer[1][RX_BUFFER_SIZE];
  33. int rx_buffer_head[1] = {0};
  34. int rx_buffer_tail[1] = {0};
  35. #endif
  36. #define BEGIN_SERIAL(uart_, baud_) \
  37. { \
  38. UBRR##uart_##H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; \
  39. UBRR##uart_##L = ((F_CPU / 16 + baud / 2) / baud - 1); \
  40. \
  41. /* reset config for UART */ \
  42. UCSR##uart_##A = 0; \
  43. UCSR##uart_##B = 0; \
  44. UCSR##uart_##C = 0; \
  45. \
  46. /* enable rx and tx */ \
  47. sbi(UCSR##uart_##B, RXEN##uart_);\
  48. sbi(UCSR##uart_##B, TXEN##uart_);\
  49. \
  50. /* enable interrupt on complete reception of a byte */ \
  51. sbi(UCSR##uart_##B, RXCIE##uart_); \
  52. UCSR##uart_##C = _BV(UCSZ##uart_##1)|_BV(UCSZ##uart_##0); \
  53. /* defaults to 8-bit, no parity, 1 stop bit */ \
  54. }
  55. void beginSerial(uint8_t uart, long baud)
  56. {
  57. if (uart == 0) BEGIN_SERIAL(0, baud)
  58. #if defined(__AVR_ATmega644P__)
  59. else BEGIN_SERIAL(1, baud)
  60. #endif
  61. }
  62. #define SERIAL_WRITE(uart_, c_) \
  63. while (!(UCSR##uart_##A & (1 << UDRE##uart_))) \
  64. ; \
  65. UDR##uart_ = c
  66. void serialWrite(uint8_t uart, unsigned char c)
  67. {
  68. if (uart == 0) {
  69. SERIAL_WRITE(0, c);
  70. }
  71. #if defined(__AVR_ATmega644P__)
  72. else {
  73. SERIAL_WRITE(1, c);
  74. }
  75. #endif
  76. }
  77. int serialAvailable(uint8_t uart)
  78. {
  79. return (RX_BUFFER_SIZE + rx_buffer_head[uart] - rx_buffer_tail[uart]) & RX_BUFFER_MASK;
  80. }
  81. int serialRead(uint8_t uart)
  82. {
  83. // if the head isn't ahead of the tail, we don't have any characters
  84. if (rx_buffer_head[uart] == rx_buffer_tail[uart]) {
  85. return -1;
  86. } else {
  87. unsigned char c = rx_buffer[uart][rx_buffer_tail[uart]];
  88. rx_buffer_tail[uart] = (rx_buffer_tail[uart] + 1) & RX_BUFFER_MASK;
  89. return c;
  90. }
  91. }
  92. void serialFlush(uint8_t uart)
  93. {
  94. // don't reverse this or there may be problems if the RX interrupt
  95. // occurs after reading the value of rx_buffer_head but before writing
  96. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  97. // may be written to rx_buffer_tail, making it appear as if the buffer
  98. // were full, not empty.
  99. rx_buffer_head[uart] = rx_buffer_tail[uart];
  100. }
  101. #define UART_ISR(uart_) \
  102. ISR(USART##uart_##_RX_vect) \
  103. { \
  104. unsigned char c = UDR##uart_; \
  105. \
  106. int i = (rx_buffer_head[uart_] + 1) & RX_BUFFER_MASK; \
  107. \
  108. /* if we should be storing the received character into the location \
  109. just before the tail (meaning that the head would advance to the \
  110. current location of the tail), we're about to overflow the buffer \
  111. and so we don't write the character or advance the head. */ \
  112. if (i != rx_buffer_tail[uart_]) { \
  113. rx_buffer[uart_][rx_buffer_head[uart_]] = c; \
  114. rx_buffer_head[uart_] = i; \
  115. } \
  116. }
  117. UART_ISR(0)
  118. #if defined(__AVR_ATmega644P__)
  119. UART_ISR(1)
  120. #endif