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.

HardwareSerial.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. HardwareSerial.cpp - Hardware serial library for Wiring
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 23 November 2006 by David A. Mellis
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <inttypes.h>
  20. #include "wiring.h"
  21. #include "wiring_private.h"
  22. #include "HardwareSerial.h"
  23. // Define constants and variables for buffering incoming serial data. We're
  24. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  25. // location to which to write the next incoming character and rx_buffer_tail
  26. // is the index of the location from which to read.
  27. #define RX_BUFFER_SIZE 128
  28. struct ring_buffer {
  29. unsigned char buffer[RX_BUFFER_SIZE];
  30. int head;
  31. int tail;
  32. };
  33. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  34. #ifdef UDR1
  35. ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
  36. #endif
  37. #ifdef UDR2
  38. ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
  39. #endif
  40. #ifdef UDR3
  41. ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
  42. #endif
  43. inline void store_char(unsigned char c, ring_buffer *rx_buffer)
  44. {
  45. int i = (rx_buffer->head + 1) % RX_BUFFER_SIZE;
  46. // if we should be storing the received character into the location
  47. // just before the tail (meaning that the head would advance to the
  48. // current location of the tail), we're about to overflow the buffer
  49. // and so we don't write the character or advance the head.
  50. if (i != rx_buffer->tail) {
  51. rx_buffer->buffer[rx_buffer->head] = c;
  52. rx_buffer->head = i;
  53. }
  54. }
  55. ISR(USART0_RX_vect)
  56. {
  57. unsigned char c = UDR0;
  58. store_char(c, &rx_buffer);
  59. }
  60. #ifdef UDR1
  61. ISR(USART1_RX_vect)
  62. {
  63. unsigned char c = UDR1;
  64. store_char(c, &rx_buffer1);
  65. }
  66. #ifdef UDR2
  67. ISR(USART2_RX_vect)
  68. {
  69. unsigned char c = UDR2;
  70. store_char(c, &rx_buffer2);
  71. }
  72. #ifdef UDR2
  73. ISR(USART3_RX_vect)
  74. {
  75. unsigned char c = UDR3;
  76. store_char(c, &rx_buffer3);
  77. }
  78. #endif
  79. #endif
  80. #else
  81. #if defined(__AVR_ATmega8__)
  82. SIGNAL(SIG_UART_RECV)
  83. #else
  84. SIGNAL(USART_RX_vect)
  85. #endif
  86. {
  87. #if defined(__AVR_ATmega8__)
  88. unsigned char c = UDR;
  89. #else
  90. unsigned char c = UDR0;
  91. #endif
  92. store_char(c, &rx_buffer);
  93. }
  94. #endif
  95. // Constructors ////////////////////////////////////////////////////////////////
  96. HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
  97. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  98. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  99. volatile uint8_t *udr,
  100. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
  101. {
  102. _rx_buffer = rx_buffer;
  103. _ubrrh = ubrrh;
  104. _ubrrl = ubrrl;
  105. _ucsra = ucsra;
  106. _ucsrb = ucsrb;
  107. _udr = udr;
  108. _rxen = rxen;
  109. _txen = txen;
  110. _rxcie = rxcie;
  111. _udre = udre;
  112. _u2x = u2x;
  113. }
  114. // Public Methods //////////////////////////////////////////////////////////////
  115. void HardwareSerial::begin(long baud)
  116. {
  117. uint16_t baud_setting;
  118. bool use_u2x;
  119. // U2X mode is needed for baud rates higher than (CPU Hz / 16)
  120. if (baud > F_CPU / 16) {
  121. use_u2x = true;
  122. } else {
  123. // figure out if U2X mode would allow for a better connection
  124. // calculate the percent difference between the baud-rate specified and
  125. // the real baud rate for both U2X and non-U2X mode (0-255 error percent)
  126. uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud)));
  127. uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud)));
  128. // prefer non-U2X mode because it handles clock skew better
  129. use_u2x = (nonu2x_baud_error > u2x_baud_error);
  130. }
  131. if (use_u2x) {
  132. *_ucsra = 1 << _u2x;
  133. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  134. } else {
  135. *_ucsra = 0;
  136. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  137. }
  138. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  139. *_ubrrh = baud_setting >> 8;
  140. *_ubrrl = baud_setting;
  141. sbi(*_ucsrb, _rxen);
  142. sbi(*_ucsrb, _txen);
  143. sbi(*_ucsrb, _rxcie);
  144. }
  145. void HardwareSerial::end()
  146. {
  147. cbi(*_ucsrb, _rxen);
  148. cbi(*_ucsrb, _txen);
  149. cbi(*_ucsrb, _rxcie);
  150. }
  151. uint8_t HardwareSerial::available(void)
  152. {
  153. return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
  154. }
  155. int HardwareSerial::read(void)
  156. {
  157. // if the head isn't ahead of the tail, we don't have any characters
  158. if (_rx_buffer->head == _rx_buffer->tail) {
  159. return -1;
  160. } else {
  161. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  162. _rx_buffer->tail = (_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
  163. return c;
  164. }
  165. }
  166. void HardwareSerial::flush()
  167. {
  168. // don't reverse this or there may be problems if the RX interrupt
  169. // occurs after reading the value of rx_buffer_head but before writing
  170. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  171. // may be written to rx_buffer_tail, making it appear as if the buffer
  172. // don't reverse this or there may be problems if the RX interrupt
  173. // occurs after reading the value of rx_buffer_head but before writing
  174. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  175. // may be written to rx_buffer_tail, making it appear as if the buffer
  176. // were full, not empty.
  177. _rx_buffer->head = _rx_buffer->tail;
  178. }
  179. void HardwareSerial::write(uint8_t c)
  180. {
  181. while (!((*_ucsra) & (1 << _udre)))
  182. ;
  183. *_udr = c;
  184. }
  185. // Preinstantiate Objects //////////////////////////////////////////////////////
  186. #if defined(__AVR_ATmega8__)
  187. HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
  188. #else
  189. HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
  190. #endif
  191. #ifdef UDR1
  192. HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
  193. #endif
  194. #ifdef UDR2
  195. HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
  196. #endif
  197. #ifdef UDR3
  198. HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
  199. #endif