My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Modified 28 September 2010 by Mark Sproul
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <inttypes.h>
  22. #include "wiring.h"
  23. #include "wiring_private.h"
  24. // this next line disables the entire HardwareSerial.cpp,
  25. // this is so I can support Attiny series and any other chip without a uart
  26. #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
  27. #include "MarlinSerial.h"
  28. // Define constants and variables for buffering incoming serial data. We're
  29. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  30. // location to which to write the next incoming character and rx_buffer_tail
  31. // is the index of the location from which to read.
  32. #define RX_BUFFER_SIZE 128
  33. struct ring_buffer
  34. {
  35. unsigned char buffer[RX_BUFFER_SIZE];
  36. int head;
  37. int tail;
  38. };
  39. #if defined(UBRRH) || defined(UBRR0H)
  40. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  41. #endif
  42. inline void store_char(unsigned char c, ring_buffer *rx_buffer)
  43. {
  44. int i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE;
  45. // if we should be storing the received character into the location
  46. // just before the tail (meaning that the head would advance to the
  47. // current location of the tail), we're about to overflow the buffer
  48. // and so we don't write the character or advance the head.
  49. if (i != rx_buffer->tail) {
  50. rx_buffer->buffer[rx_buffer->head] = c;
  51. rx_buffer->head = i;
  52. }
  53. }
  54. //#elif defined(SIG_USART_RECV)
  55. #if defined(USART0_RX_vect)
  56. // fixed by Mark Sproul this is on the 644/644p
  57. //SIGNAL(SIG_USART_RECV)
  58. SIGNAL(USART0_RX_vect)
  59. {
  60. #if defined(UDR0)
  61. unsigned char c = UDR0;
  62. #elif defined(UDR)
  63. unsigned char c = UDR; // atmega8, atmega32
  64. #else
  65. #error UDR not defined
  66. #endif
  67. store_char(c, &rx_buffer);
  68. }
  69. #endif
  70. // Constructors ////////////////////////////////////////////////////////////////
  71. MarlinSerial::MarlinSerial(ring_buffer *rx_buffer,
  72. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  73. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  74. volatile uint8_t *udr,
  75. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
  76. {
  77. _rx_buffer = rx_buffer;
  78. _ubrrh = ubrrh;
  79. _ubrrl = ubrrl;
  80. _ucsra = ucsra;
  81. _ucsrb = ucsrb;
  82. _udr = udr;
  83. _rxen = rxen;
  84. _txen = txen;
  85. _rxcie = rxcie;
  86. _udre = udre;
  87. _u2x = u2x;
  88. }
  89. // Public Methods //////////////////////////////////////////////////////////////
  90. void MarlinSerial::begin(long baud)
  91. {
  92. uint16_t baud_setting;
  93. bool use_u2x = true;
  94. #if F_CPU == 16000000UL
  95. // hardcoded exception for compatibility with the bootloader shipped
  96. // with the Duemilanove and previous boards and the firmware on the 8U2
  97. // on the Uno and Mega 2560.
  98. if (baud == 57600) {
  99. use_u2x = false;
  100. }
  101. #endif
  102. if (use_u2x) {
  103. *_ucsra = 1 << _u2x;
  104. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  105. } else {
  106. *_ucsra = 0;
  107. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  108. }
  109. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  110. *_ubrrh = baud_setting >> 8;
  111. *_ubrrl = baud_setting;
  112. sbi(*_ucsrb, _rxen);
  113. sbi(*_ucsrb, _txen);
  114. sbi(*_ucsrb, _rxcie);
  115. }
  116. void MarlinSerial::end()
  117. {
  118. cbi(*_ucsrb, _rxen);
  119. cbi(*_ucsrb, _txen);
  120. cbi(*_ucsrb, _rxcie);
  121. }
  122. int MarlinSerial::available(void)
  123. {
  124. return (unsigned int)(RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
  125. }
  126. int MarlinSerial::peek(void)
  127. {
  128. if (_rx_buffer->head == _rx_buffer->tail) {
  129. return -1;
  130. } else {
  131. return _rx_buffer->buffer[_rx_buffer->tail];
  132. }
  133. }
  134. int MarlinSerial::read(void)
  135. {
  136. // if the head isn't ahead of the tail, we don't have any characters
  137. if (_rx_buffer->head == _rx_buffer->tail) {
  138. return -1;
  139. } else {
  140. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  141. _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
  142. return c;
  143. }
  144. }
  145. void MarlinSerial::flush()
  146. {
  147. // don't reverse this or there may be problems if the RX interrupt
  148. // occurs after reading the value of rx_buffer_head but before writing
  149. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  150. // may be written to rx_buffer_tail, making it appear as if the buffer
  151. // don't reverse this or there may be problems if the RX interrupt
  152. // occurs after reading the value of rx_buffer_head but before writing
  153. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  154. // may be written to rx_buffer_tail, making it appear as if the buffer
  155. // were full, not empty.
  156. _rx_buffer->head = _rx_buffer->tail;
  157. }
  158. void MarlinSerial::write(uint8_t c)
  159. {
  160. while (!((*_ucsra) & (1 << _udre)))
  161. ;
  162. *_udr = c;
  163. }
  164. void MarlinSerial::checkRx()
  165. {
  166. if((UCSR0A & (1<<RXC0)) != 0) {
  167. unsigned char c = UDR0;
  168. store_char(c, &rx_buffer);
  169. }
  170. }
  171. // Preinstantiate Objects //////////////////////////////////////////////////////
  172. #if defined(UBRR0H) && defined(UBRR0L)
  173. MarlinSerial MSerial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
  174. #else
  175. #error no serial port defined (port 0)
  176. #endif
  177. #endif // whole file