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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "HardwareSerial.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. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  40. inline void store_char(unsigned char c, ring_buffer *rx_buffer)
  41. {
  42. int i = (unsigned int)(rx_buffer->head + 1) & (RX_BUFFER_SIZE -1);
  43. // if we should be storing the received character into the location
  44. // just before the tail (meaning that the head would advance to the
  45. // current location of the tail), we're about to overflow the buffer
  46. // and so we don't write the character or advance the head.
  47. if (i != rx_buffer->tail) {
  48. rx_buffer->buffer[rx_buffer->head] = c;
  49. rx_buffer->head = i;
  50. }
  51. }
  52. // fixed by Mark Sproul this is on the 644/644p
  53. //SIGNAL(SIG_USART_RECV)
  54. SIGNAL(USART0_RX_vect)
  55. {
  56. unsigned char c = UDR0;
  57. store_char(c, &rx_buffer);
  58. }
  59. // Constructors ////////////////////////////////////////////////////////////////
  60. HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
  61. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  62. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  63. volatile uint8_t *udr,
  64. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
  65. {
  66. _rx_buffer = rx_buffer;
  67. _ubrrh = ubrrh;
  68. _ubrrl = ubrrl;
  69. _ucsra = ucsra;
  70. _ucsrb = ucsrb;
  71. _udr = udr;
  72. _rxen = rxen;
  73. _txen = txen;
  74. _rxcie = rxcie;
  75. _udre = udre;
  76. _u2x = u2x;
  77. }
  78. // Public Methods //////////////////////////////////////////////////////////////
  79. void HardwareSerial::begin(long baud)
  80. {
  81. uint16_t baud_setting;
  82. bool use_u2x = true;
  83. #if F_CPU == 16000000UL
  84. // hardcoded exception for compatibility with the bootloader shipped
  85. // with the Duemilanove and previous boards and the firmware on the 8U2
  86. // on the Uno and Mega 2560.
  87. if (baud == 57600) {
  88. use_u2x = false;
  89. }
  90. #endif
  91. if (use_u2x) {
  92. *_ucsra = 1 << _u2x;
  93. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  94. } else {
  95. *_ucsra = 0;
  96. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  97. }
  98. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  99. *_ubrrh = baud_setting >> 8;
  100. *_ubrrl = baud_setting;
  101. sbi(*_ucsrb, _rxen);
  102. sbi(*_ucsrb, _txen);
  103. sbi(*_ucsrb, _rxcie);
  104. }
  105. void HardwareSerial::end()
  106. {
  107. cbi(*_ucsrb, _rxen);
  108. cbi(*_ucsrb, _txen);
  109. cbi(*_ucsrb, _rxcie);
  110. }
  111. int HardwareSerial::available(void)
  112. {
  113. return (unsigned int)(RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) & (RX_BUFFER_SIZE-1);
  114. }
  115. int HardwareSerial::peek(void)
  116. {
  117. if (_rx_buffer->head == _rx_buffer->tail) {
  118. return -1;
  119. } else {
  120. return _rx_buffer->buffer[_rx_buffer->tail];
  121. }
  122. }
  123. int HardwareSerial::read(void)
  124. {
  125. // if the head isn't ahead of the tail, we don't have any characters
  126. if (_rx_buffer->head == _rx_buffer->tail) {
  127. return -1;
  128. } else {
  129. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  130. _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) & (RX_BUFFER_SIZE-1);
  131. return c;
  132. }
  133. }
  134. void HardwareSerial::flush()
  135. {
  136. // don't reverse this or there may be problems if the RX interrupt
  137. // occurs after reading the value of rx_buffer_head but before writing
  138. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  139. // may be written to rx_buffer_tail, making it appear as if the buffer
  140. // don't reverse this or there may be problems if the RX interrupt
  141. // occurs after reading the value of rx_buffer_head but before writing
  142. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  143. // may be written to rx_buffer_tail, making it appear as if the buffer
  144. // were full, not empty.
  145. _rx_buffer->head = _rx_buffer->tail;
  146. }
  147. void HardwareSerial::write(uint8_t c)
  148. {
  149. while (!((*_ucsra) & (1 << _udre)))
  150. ;
  151. *_udr = c;
  152. }
  153. // Preinstantiate Objects //////////////////////////////////////////////////////
  154. HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
  155. #endif // whole file