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.

228 satır
5.4KB

  1. /* UART (hardware serial) for Teensy & Teensy++
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/io.h>
  24. #include <avr/interrupt.h>
  25. #include "core_pins.h"
  26. #include "HardwareSerial.h"
  27. #include "wiring_private.h"
  28. #define RX_BUFFER_SIZE 64
  29. static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
  30. static volatile uint8_t rx_buffer_head = 0;
  31. static volatile uint8_t rx_buffer_tail = 0;
  32. #define TX_BUFFER_SIZE 40
  33. static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
  34. static volatile uint8_t tx_buffer_head = 0;
  35. static volatile uint8_t tx_buffer_tail = 0;
  36. static volatile uint8_t transmitting = 0;
  37. static volatile uint8_t tx_enable_pin = 255;
  38. // Public Methods //////////////////////////////////////////////////////////////
  39. void HardwareSerial::_begin(uint16_t baud_count, uint8_t txen_pin)
  40. {
  41. tx_enable_pin = txen_pin;
  42. if (txen_pin < 255) {
  43. pinMode(txen_pin, OUTPUT);
  44. digitalWrite(txen_pin, LOW);
  45. }
  46. if ((baud_count & 1) && baud_count <= 4096) {
  47. UCSR1A = (1<<U2X1);
  48. UBRR1 = baud_count - 1;
  49. } else {
  50. UCSR1A = 0;
  51. UBRR1 = (baud_count >> 1) - 1;
  52. }
  53. if (!(UCSR1B & (1<<TXEN1))) {
  54. rx_buffer_head = 0;
  55. rx_buffer_tail = 0;
  56. tx_buffer_head = 0;
  57. tx_buffer_tail = 0;
  58. transmitting = 0;
  59. UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
  60. UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1);
  61. }
  62. }
  63. void HardwareSerial::end(void)
  64. {
  65. while (transmitting) ; // wait for buffered data to send
  66. UCSR1B = 0;
  67. rx_buffer_head = 0;
  68. rx_buffer_tail = 0;
  69. }
  70. void HardwareSerial::transmitterEnable(uint8_t pin)
  71. {
  72. while (transmitting) ;
  73. pinMode(pin, OUTPUT);
  74. digitalWrite(pin, LOW);
  75. tx_enable_pin = pin;
  76. }
  77. int HardwareSerial::available(void)
  78. {
  79. uint8_t head, tail;
  80. head = rx_buffer_head;
  81. tail = rx_buffer_tail;
  82. if (head >= tail) return head - tail;
  83. return RX_BUFFER_SIZE + head - tail;
  84. }
  85. int HardwareSerial::availableForWrite(void)
  86. {
  87. uint8_t head, tail;
  88. head = rx_buffer_head;
  89. tail = rx_buffer_tail;
  90. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  91. return tail - head - 1;
  92. }
  93. int HardwareSerial::peek(void)
  94. {
  95. uint8_t head, tail;
  96. head = rx_buffer_head;
  97. tail = rx_buffer_tail;
  98. if (head == tail) return -1;
  99. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  100. return rx_buffer[tail];
  101. }
  102. int HardwareSerial::read(void)
  103. {
  104. uint8_t c, i;
  105. if (rx_buffer_head == rx_buffer_tail) return -1;
  106. i = rx_buffer_tail + 1;
  107. if (i >= RX_BUFFER_SIZE) i = 0;
  108. c = rx_buffer[i];
  109. rx_buffer_tail = i;
  110. return c;
  111. }
  112. void HardwareSerial::flush()
  113. {
  114. #if ARDUINO >= 100
  115. while (transmitting) ; // wait for buffered data to send
  116. #else
  117. rx_buffer_head = rx_buffer_tail;
  118. #endif
  119. }
  120. void HardwareSerial::clear()
  121. {
  122. rx_buffer_head = rx_buffer_tail;
  123. }
  124. #if ARDUINO >= 100
  125. size_t HardwareSerial::write(uint8_t c)
  126. #else
  127. void HardwareSerial::write(uint8_t c)
  128. #endif
  129. {
  130. uint8_t i;
  131. if (!(UCSR1B & (1<<TXEN1))) {
  132. #if ARDUINO >= 100
  133. setWriteError();
  134. return 0;
  135. #else
  136. return;
  137. #endif
  138. }
  139. if (tx_enable_pin < 255 && !transmitting) {
  140. digitalWrite(tx_enable_pin, HIGH);
  141. }
  142. // If the buffer and the data register is empty, just write the byte
  143. // to the data register and be done. This shortcut helps
  144. // significantly improve the effective datarate at high (>
  145. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  146. if (tx_buffer_head == tx_buffer_tail && bit_is_set(UCSR1A, UDRE1)) {
  147. UDR1 = c;
  148. transmitting = 1;
  149. return 1;
  150. }
  151. i = tx_buffer_head + 1;
  152. if (i >= TX_BUFFER_SIZE) i = 0;
  153. while (tx_buffer_tail == i) ; // wait until space in buffer
  154. tx_buffer[i] = c;
  155. transmitting = 1;
  156. tx_buffer_head = i;
  157. //UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1) | (1<<UDRIE1);
  158. sbi(UCSR1B, UDRIE1);
  159. #if ARDUINO >= 100
  160. return 1;
  161. #endif
  162. }
  163. ISR(USART1_RX_vect)
  164. {
  165. uint8_t c, i;
  166. c = UDR1;
  167. i = rx_buffer_head + 1;
  168. if (i >= RX_BUFFER_SIZE) i = 0;
  169. if (i != rx_buffer_tail) {
  170. rx_buffer[i] = c;
  171. rx_buffer_head = i;
  172. }
  173. }
  174. ISR(USART1_UDRE_vect)
  175. {
  176. uint8_t i;
  177. if (tx_buffer_head == tx_buffer_tail) {
  178. // buffer is empty, disable transmit interrupt
  179. //UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1);
  180. cbi(UCSR1B, UDRIE1);
  181. } else {
  182. i = tx_buffer_tail + 1;
  183. if (i >= TX_BUFFER_SIZE) i = 0;
  184. UDR1 = tx_buffer[i];
  185. tx_buffer_tail = i;
  186. }
  187. }
  188. ISR(USART1_TX_vect)
  189. {
  190. transmitting = 0;
  191. if (tx_enable_pin < 255) {
  192. digitalWrite(tx_enable_pin, LOW);
  193. }
  194. }
  195. // Preinstantiate Objects //////////////////////////////////////////////////////
  196. HardwareSerial Serial1;