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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #if (RAMEND < 1000)
  33. #define RX_BUFFER_SIZE 32
  34. #else
  35. #define RX_BUFFER_SIZE 128
  36. #endif
  37. struct ring_buffer
  38. {
  39. unsigned char buffer[RX_BUFFER_SIZE];
  40. int head;
  41. int tail;
  42. };
  43. #if defined(UBRRH) || defined(UBRR0H)
  44. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  45. #endif
  46. #if defined(UBRR1H)
  47. ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
  48. #endif
  49. #if defined(UBRR2H)
  50. ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
  51. #endif
  52. #if defined(UBRR3H)
  53. ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
  54. #endif
  55. inline void store_char(unsigned char c, ring_buffer *rx_buffer)
  56. {
  57. int i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE;
  58. // if we should be storing the received character into the location
  59. // just before the tail (meaning that the head would advance to the
  60. // current location of the tail), we're about to overflow the buffer
  61. // and so we don't write the character or advance the head.
  62. if (i != rx_buffer->tail) {
  63. rx_buffer->buffer[rx_buffer->head] = c;
  64. rx_buffer->head = i;
  65. }
  66. }
  67. #if defined(USART_RX_vect)
  68. SIGNAL(USART_RX_vect)
  69. {
  70. #if defined(UDR0)
  71. unsigned char c = UDR0;
  72. #elif defined(UDR)
  73. unsigned char c = UDR; // atmega8535
  74. #else
  75. #error UDR not defined
  76. #endif
  77. store_char(c, &rx_buffer);
  78. }
  79. #elif defined(SIG_USART0_RECV) && defined(UDR0)
  80. SIGNAL(SIG_USART0_RECV)
  81. {
  82. unsigned char c = UDR0;
  83. store_char(c, &rx_buffer);
  84. }
  85. #elif defined(SIG_UART0_RECV) && defined(UDR0)
  86. SIGNAL(SIG_UART0_RECV)
  87. {
  88. unsigned char c = UDR0;
  89. store_char(c, &rx_buffer);
  90. }
  91. //#elif defined(SIG_USART_RECV)
  92. #elif defined(USART0_RX_vect)
  93. // fixed by Mark Sproul this is on the 644/644p
  94. //SIGNAL(SIG_USART_RECV)
  95. SIGNAL(USART0_RX_vect)
  96. {
  97. #if defined(UDR0)
  98. unsigned char c = UDR0;
  99. #elif defined(UDR)
  100. unsigned char c = UDR; // atmega8, atmega32
  101. #else
  102. #error UDR not defined
  103. #endif
  104. store_char(c, &rx_buffer);
  105. }
  106. #elif defined(SIG_UART_RECV)
  107. // this is for atmega8
  108. SIGNAL(SIG_UART_RECV)
  109. {
  110. #if defined(UDR0)
  111. unsigned char c = UDR0; // atmega645
  112. #elif defined(UDR)
  113. unsigned char c = UDR; // atmega8
  114. #endif
  115. store_char(c, &rx_buffer);
  116. }
  117. #elif defined(USBCON)
  118. #warning No interrupt handler for usart 0
  119. #warning Serial(0) is on USB interface
  120. #else
  121. #error No interrupt handler for usart 0
  122. #endif
  123. //#if defined(SIG_USART1_RECV)
  124. #if defined(USART1_RX_vect)
  125. //SIGNAL(SIG_USART1_RECV)
  126. SIGNAL(USART1_RX_vect)
  127. {
  128. unsigned char c = UDR1;
  129. store_char(c, &rx_buffer1);
  130. }
  131. #elif defined(SIG_USART1_RECV)
  132. #error SIG_USART1_RECV
  133. #endif
  134. #if defined(USART2_RX_vect) && defined(UDR2)
  135. SIGNAL(USART2_RX_vect)
  136. {
  137. unsigned char c = UDR2;
  138. store_char(c, &rx_buffer2);
  139. }
  140. #elif defined(SIG_USART2_RECV)
  141. #error SIG_USART2_RECV
  142. #endif
  143. #if defined(USART3_RX_vect) && defined(UDR3)
  144. SIGNAL(USART3_RX_vect)
  145. {
  146. unsigned char c = UDR3;
  147. store_char(c, &rx_buffer3);
  148. }
  149. #elif defined(SIG_USART3_RECV)
  150. #error SIG_USART3_RECV
  151. #endif
  152. // Constructors ////////////////////////////////////////////////////////////////
  153. HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
  154. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  155. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  156. volatile uint8_t *udr,
  157. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
  158. {
  159. _rx_buffer = rx_buffer;
  160. _ubrrh = ubrrh;
  161. _ubrrl = ubrrl;
  162. _ucsra = ucsra;
  163. _ucsrb = ucsrb;
  164. _udr = udr;
  165. _rxen = rxen;
  166. _txen = txen;
  167. _rxcie = rxcie;
  168. _udre = udre;
  169. _u2x = u2x;
  170. }
  171. // Public Methods //////////////////////////////////////////////////////////////
  172. void HardwareSerial::begin(long baud)
  173. {
  174. uint16_t baud_setting;
  175. bool use_u2x = true;
  176. #if F_CPU == 16000000UL
  177. // hardcoded exception for compatibility with the bootloader shipped
  178. // with the Duemilanove and previous boards and the firmware on the 8U2
  179. // on the Uno and Mega 2560.
  180. if (baud == 57600) {
  181. use_u2x = false;
  182. }
  183. #endif
  184. if (use_u2x) {
  185. *_ucsra = 1 << _u2x;
  186. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  187. } else {
  188. *_ucsra = 0;
  189. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  190. }
  191. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  192. *_ubrrh = baud_setting >> 8;
  193. *_ubrrl = baud_setting;
  194. sbi(*_ucsrb, _rxen);
  195. sbi(*_ucsrb, _txen);
  196. sbi(*_ucsrb, _rxcie);
  197. }
  198. void HardwareSerial::end()
  199. {
  200. cbi(*_ucsrb, _rxen);
  201. cbi(*_ucsrb, _txen);
  202. cbi(*_ucsrb, _rxcie);
  203. }
  204. int HardwareSerial::available(void)
  205. {
  206. return (unsigned int)(RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
  207. }
  208. int HardwareSerial::peek(void)
  209. {
  210. if (_rx_buffer->head == _rx_buffer->tail) {
  211. return -1;
  212. } else {
  213. return _rx_buffer->buffer[_rx_buffer->tail];
  214. }
  215. }
  216. int HardwareSerial::read(void)
  217. {
  218. // if the head isn't ahead of the tail, we don't have any characters
  219. if (_rx_buffer->head == _rx_buffer->tail) {
  220. return -1;
  221. } else {
  222. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  223. _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
  224. return c;
  225. }
  226. }
  227. void HardwareSerial::flush()
  228. {
  229. // don't reverse this or there may be problems if the RX interrupt
  230. // occurs after reading the value of rx_buffer_head but before writing
  231. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  232. // may be written to rx_buffer_tail, making it appear as if the buffer
  233. // don't reverse this or there may be problems if the RX interrupt
  234. // occurs after reading the value of rx_buffer_head but before writing
  235. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  236. // may be written to rx_buffer_tail, making it appear as if the buffer
  237. // were full, not empty.
  238. _rx_buffer->head = _rx_buffer->tail;
  239. }
  240. void HardwareSerial::write(uint8_t c)
  241. {
  242. while (!((*_ucsra) & (1 << _udre)))
  243. ;
  244. *_udr = c;
  245. }
  246. // Preinstantiate Objects //////////////////////////////////////////////////////
  247. #if defined(UBRRH) && defined(UBRRL)
  248. HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
  249. #elif defined(UBRR0H) && defined(UBRR0L)
  250. HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
  251. #elif defined(USBCON)
  252. #warning no serial port defined (port 0)
  253. #else
  254. #error no serial port defined (port 0)
  255. #endif
  256. #if defined(UBRR1H)
  257. HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
  258. #endif
  259. #if defined(UBRR2H)
  260. HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
  261. #endif
  262. #if defined(UBRR3H)
  263. HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
  264. #endif
  265. #endif // whole file