PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. uint8_t status;
  132. if (!(UCSR1B & (1<<TXEN1))) {
  133. #if ARDUINO >= 100
  134. setWriteError();
  135. return 0;
  136. #else
  137. return;
  138. #endif
  139. }
  140. if (tx_enable_pin < 255 && !transmitting) {
  141. digitalWrite(tx_enable_pin, HIGH);
  142. }
  143. // If the buffer and the data register is empty, just write the byte
  144. // to the data register and be done. This shortcut helps
  145. // significantly improve the effective datarate at high (>
  146. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  147. if ((tx_buffer_head == tx_buffer_tail) && (UCSR1A & (1<<UDRE1))) {
  148. status = SREG;
  149. cli();
  150. UDR1 = c;
  151. transmitting = 1;
  152. SREG = status;
  153. return 1;
  154. }
  155. i = tx_buffer_head + 1;
  156. if (i >= TX_BUFFER_SIZE) i = 0;
  157. while (tx_buffer_tail == i) ; // wait until space in buffer
  158. tx_buffer[i] = c;
  159. transmitting = 1;
  160. tx_buffer_head = i;
  161. //UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1) | (1<<UDRIE1);
  162. sbi(UCSR1B, UDRIE1);
  163. #if ARDUINO >= 100
  164. return 1;
  165. #endif
  166. }
  167. ISR(USART1_RX_vect)
  168. {
  169. uint8_t c, i;
  170. c = UDR1;
  171. i = rx_buffer_head + 1;
  172. if (i >= RX_BUFFER_SIZE) i = 0;
  173. if (i != rx_buffer_tail) {
  174. rx_buffer[i] = c;
  175. rx_buffer_head = i;
  176. }
  177. }
  178. ISR(USART1_UDRE_vect)
  179. {
  180. uint8_t i;
  181. if (tx_buffer_head == tx_buffer_tail) {
  182. // buffer is empty, disable transmit interrupt
  183. //UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1);
  184. cbi(UCSR1B, UDRIE1);
  185. } else {
  186. i = tx_buffer_tail + 1;
  187. if (i >= TX_BUFFER_SIZE) i = 0;
  188. UDR1 = tx_buffer[i];
  189. tx_buffer_tail = i;
  190. }
  191. }
  192. ISR(USART1_TX_vect)
  193. {
  194. transmitting = 0;
  195. if (tx_enable_pin < 255) {
  196. digitalWrite(tx_enable_pin, LOW);
  197. }
  198. }
  199. // Preinstantiate Objects //////////////////////////////////////////////////////
  200. HardwareSerial Serial1;