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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #if defined(__AVR_ATmega644P__)
  27. unsigned char rx_buffer[2][RX_BUFFER_SIZE];
  28. int rx_buffer_head[2] = {0, 0};
  29. int rx_buffer_tail[2] = {0, 0};
  30. #else
  31. unsigned char rx_buffer[1][RX_BUFFER_SIZE];
  32. int rx_buffer_head[1] = {0};
  33. int rx_buffer_tail[1] = {0};
  34. #endif
  35. #define BEGIN_SERIAL(uart_, baud_) \
  36. { \
  37. UBRR##uart_##H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; \
  38. UBRR##uart_##L = ((F_CPU / 16 + baud / 2) / baud - 1); \
  39. \
  40. /* reset config for UART */ \
  41. UCSR##uart_##A = 0; \
  42. UCSR##uart_##B = 0; \
  43. UCSR##uart_##C = 0; \
  44. \
  45. /* enable rx and tx */ \
  46. sbi(UCSR##uart_##B, RXEN##uart_);\
  47. sbi(UCSR##uart_##B, TXEN##uart_);\
  48. \
  49. /* enable interrupt on complete reception of a byte */ \
  50. sbi(UCSR##uart_##B, RXCIE##uart_); \
  51. UCSR##uart_##C = _BV(UCSZ##uart_##1)|_BV(UCSZ##uart_##0); \
  52. /* defaults to 8-bit, no parity, 1 stop bit */ \
  53. }
  54. void beginSerial(uint8_t uart, long baud)
  55. {
  56. if (uart == 0) BEGIN_SERIAL(0, baud)
  57. #if defined(__AVR_ATmega644P__)
  58. else BEGIN_SERIAL(1, baud)
  59. #endif
  60. }
  61. #define SERIAL_WRITE(uart_, c_) \
  62. while (!(UCSR##uart_##A & (1 << UDRE##uart_))) \
  63. ; \
  64. UDR##uart_ = c
  65. void serialWrite(uint8_t uart, unsigned char c)
  66. {
  67. if (uart == 0) {
  68. SERIAL_WRITE(0, c);
  69. }
  70. #if defined(__AVR_ATmega644P__)
  71. else {
  72. SERIAL_WRITE(1, c);
  73. }
  74. #endif
  75. }
  76. int serialAvailable(uint8_t uart)
  77. {
  78. return (RX_BUFFER_SIZE + rx_buffer_head[uart] - rx_buffer_tail[uart]) % RX_BUFFER_SIZE;
  79. }
  80. int serialRead(uint8_t uart)
  81. {
  82. // if the head isn't ahead of the tail, we don't have any characters
  83. if (rx_buffer_head[uart] == rx_buffer_tail[uart]) {
  84. return -1;
  85. } else {
  86. unsigned char c = rx_buffer[uart][rx_buffer_tail[uart]];
  87. rx_buffer_tail[uart] = (rx_buffer_tail[uart] + 1) % RX_BUFFER_SIZE;
  88. return c;
  89. }
  90. }
  91. void serialFlush(uint8_t uart)
  92. {
  93. // don't reverse this or there may be problems if the RX interrupt
  94. // occurs after reading the value of rx_buffer_head but before writing
  95. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  96. // may be written to rx_buffer_tail, making it appear as if the buffer
  97. // were full, not empty.
  98. rx_buffer_head[uart] = rx_buffer_tail[uart];
  99. }
  100. #define UART_ISR(uart_) \
  101. ISR(USART##uart_##_RX_vect) \
  102. { \
  103. unsigned char c = UDR##uart_; \
  104. \
  105. int i = (rx_buffer_head[uart_] + 1) % RX_BUFFER_SIZE; \
  106. \
  107. /* if we should be storing the received character into the location \
  108. just before the tail (meaning that the head would advance to the \
  109. current location of the tail), we're about to overflow the buffer \
  110. and so we don't write the character or advance the head. */ \
  111. if (i != rx_buffer_tail[uart_]) { \
  112. rx_buffer[uart_][rx_buffer_head[uart_]] = c; \
  113. rx_buffer_head[uart_] = i; \
  114. } \
  115. }
  116. UART_ISR(0)
  117. #if defined(__AVR_ATmega644P__)
  118. UART_ISR(1)
  119. #endif