選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

HardwareSerial.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "Arduino.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 head is the index of the location
  30. // to which to write the next incoming character and tail is the index of the
  31. // location from which to read.
  32. #if (RAMEND < 1000)
  33. #define SERIAL_BUFFER_SIZE 16
  34. #else
  35. #define SERIAL_BUFFER_SIZE 64
  36. #endif
  37. struct ring_buffer
  38. {
  39. unsigned char buffer[SERIAL_BUFFER_SIZE];
  40. volatile unsigned int head;
  41. volatile unsigned int tail;
  42. };
  43. #if defined(USBCON)
  44. ring_buffer rx_buffer = { { 0 }, 0, 0};
  45. ring_buffer tx_buffer = { { 0 }, 0, 0};
  46. #endif
  47. #if defined(UBRRH) || defined(UBRR0H)
  48. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  49. ring_buffer tx_buffer = { { 0 }, 0, 0 };
  50. #endif
  51. #if defined(UBRR1H)
  52. ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
  53. ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
  54. #endif
  55. #if defined(UBRR2H)
  56. ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
  57. ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
  58. #endif
  59. #if defined(UBRR3H)
  60. ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
  61. ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
  62. #endif
  63. inline void store_char(unsigned char c, ring_buffer *buffer)
  64. {
  65. int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
  66. // if we should be storing the received character into the location
  67. // just before the tail (meaning that the head would advance to the
  68. // current location of the tail), we're about to overflow the buffer
  69. // and so we don't write the character or advance the head.
  70. if (i != buffer->tail) {
  71. buffer->buffer[buffer->head] = c;
  72. buffer->head = i;
  73. }
  74. }
  75. #if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
  76. // do nothing - on the 32u4 the first USART is USART1
  77. #else
  78. #if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
  79. !defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
  80. !defined(SIG_UART_RECV)
  81. #error "Don't know what the Data Received vector is called for the first UART"
  82. #else
  83. void serialEvent() __attribute__((weak));
  84. void serialEvent() {}
  85. #define serialEvent_implemented
  86. #if defined(USART_RX_vect)
  87. SIGNAL(USART_RX_vect)
  88. #elif defined(SIG_USART0_RECV)
  89. SIGNAL(SIG_USART0_RECV)
  90. #elif defined(SIG_UART0_RECV)
  91. SIGNAL(SIG_UART0_RECV)
  92. #elif defined(USART0_RX_vect)
  93. SIGNAL(USART0_RX_vect)
  94. #elif defined(SIG_UART_RECV)
  95. SIGNAL(SIG_UART_RECV)
  96. #endif
  97. {
  98. #if defined(UDR0)
  99. unsigned char c = UDR0;
  100. #elif defined(UDR)
  101. unsigned char c = UDR;
  102. #else
  103. #error UDR not defined
  104. #endif
  105. store_char(c, &rx_buffer);
  106. }
  107. #endif
  108. #endif
  109. #if defined(USART1_RX_vect)
  110. void serialEvent1() __attribute__((weak));
  111. void serialEvent1() {}
  112. #define serialEvent1_implemented
  113. SIGNAL(USART1_RX_vect)
  114. {
  115. unsigned char c = UDR1;
  116. store_char(c, &rx_buffer1);
  117. }
  118. #elif defined(SIG_USART1_RECV)
  119. #error SIG_USART1_RECV
  120. #endif
  121. #if defined(USART2_RX_vect) && defined(UDR2)
  122. void serialEvent2() __attribute__((weak));
  123. void serialEvent2() {}
  124. #define serialEvent2_implemented
  125. SIGNAL(USART2_RX_vect)
  126. {
  127. unsigned char c = UDR2;
  128. store_char(c, &rx_buffer2);
  129. }
  130. #elif defined(SIG_USART2_RECV)
  131. #error SIG_USART2_RECV
  132. #endif
  133. #if defined(USART3_RX_vect) && defined(UDR3)
  134. void serialEvent3() __attribute__((weak));
  135. void serialEvent3() {}
  136. #define serialEvent3_implemented
  137. SIGNAL(USART3_RX_vect)
  138. {
  139. unsigned char c = UDR3;
  140. store_char(c, &rx_buffer3);
  141. }
  142. #elif defined(SIG_USART3_RECV)
  143. #error SIG_USART3_RECV
  144. #endif
  145. void serialEventRun(void)
  146. {
  147. #ifdef serialEvent_implemented
  148. if (Serial.available()) serialEvent();
  149. #endif
  150. #ifdef serialEvent1_implemented
  151. if (Serial1.available()) serialEvent1();
  152. #endif
  153. #ifdef serialEvent2_implemented
  154. if (Serial2.available()) serialEvent2();
  155. #endif
  156. #ifdef serialEvent3_implemented
  157. if (Serial3.available()) serialEvent3();
  158. #endif
  159. }
  160. #if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
  161. // do nothing - on the 32u4 the first USART is USART1
  162. #else
  163. #if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
  164. #error "Don't know what the Data Register Empty vector is called for the first UART"
  165. #else
  166. #if defined(UART0_UDRE_vect)
  167. ISR(UART0_UDRE_vect)
  168. #elif defined(UART_UDRE_vect)
  169. ISR(UART_UDRE_vect)
  170. #elif defined(USART0_UDRE_vect)
  171. ISR(USART0_UDRE_vect)
  172. #elif defined(USART_UDRE_vect)
  173. ISR(USART_UDRE_vect)
  174. #endif
  175. {
  176. if (tx_buffer.head == tx_buffer.tail) {
  177. // Buffer empty, so disable interrupts
  178. #if defined(UCSR0B)
  179. cbi(UCSR0B, UDRIE0);
  180. #else
  181. cbi(UCSRB, UDRIE);
  182. #endif
  183. }
  184. else {
  185. // There is more data in the output buffer. Send the next byte
  186. unsigned char c = tx_buffer.buffer[tx_buffer.tail];
  187. tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
  188. #if defined(UDR0)
  189. UDR0 = c;
  190. #elif defined(UDR)
  191. UDR = c;
  192. #else
  193. #error UDR not defined
  194. #endif
  195. }
  196. }
  197. #endif
  198. #endif
  199. #ifdef USART1_UDRE_vect
  200. ISR(USART1_UDRE_vect)
  201. {
  202. if (tx_buffer1.head == tx_buffer1.tail) {
  203. // Buffer empty, so disable interrupts
  204. cbi(UCSR1B, UDRIE1);
  205. }
  206. else {
  207. // There is more data in the output buffer. Send the next byte
  208. unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
  209. tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
  210. UDR1 = c;
  211. }
  212. }
  213. #endif
  214. #ifdef USART2_UDRE_vect
  215. ISR(USART2_UDRE_vect)
  216. {
  217. if (tx_buffer2.head == tx_buffer2.tail) {
  218. // Buffer empty, so disable interrupts
  219. cbi(UCSR2B, UDRIE2);
  220. }
  221. else {
  222. // There is more data in the output buffer. Send the next byte
  223. unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
  224. tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
  225. UDR2 = c;
  226. }
  227. }
  228. #endif
  229. #ifdef USART3_UDRE_vect
  230. ISR(USART3_UDRE_vect)
  231. {
  232. if (tx_buffer3.head == tx_buffer3.tail) {
  233. // Buffer empty, so disable interrupts
  234. cbi(UCSR3B, UDRIE3);
  235. }
  236. else {
  237. // There is more data in the output buffer. Send the next byte
  238. unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
  239. tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
  240. UDR3 = c;
  241. }
  242. }
  243. #endif
  244. // Constructors ////////////////////////////////////////////////////////////////
  245. HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
  246. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  247. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  248. volatile uint8_t *udr,
  249. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
  250. {
  251. _rx_buffer = rx_buffer;
  252. _tx_buffer = tx_buffer;
  253. _ubrrh = ubrrh;
  254. _ubrrl = ubrrl;
  255. _ucsra = ucsra;
  256. _ucsrb = ucsrb;
  257. _udr = udr;
  258. _rxen = rxen;
  259. _txen = txen;
  260. _rxcie = rxcie;
  261. _udrie = udrie;
  262. _u2x = u2x;
  263. }
  264. // Public Methods //////////////////////////////////////////////////////////////
  265. void HardwareSerial::begin(unsigned long baud)
  266. {
  267. uint16_t baud_setting;
  268. bool use_u2x = true;
  269. #if F_CPU == 16000000UL
  270. // hardcoded exception for compatibility with the bootloader shipped
  271. // with the Duemilanove and previous boards and the firmware on the 8U2
  272. // on the Uno and Mega 2560.
  273. if (baud == 57600) {
  274. use_u2x = false;
  275. }
  276. #endif
  277. try_again:
  278. if (use_u2x) {
  279. *_ucsra = 1 << _u2x;
  280. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  281. } else {
  282. *_ucsra = 0;
  283. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  284. }
  285. if ((baud_setting > 4095) && use_u2x)
  286. {
  287. use_u2x = false;
  288. goto try_again;
  289. }
  290. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  291. *_ubrrh = baud_setting >> 8;
  292. *_ubrrl = baud_setting;
  293. sbi(*_ucsrb, _rxen);
  294. sbi(*_ucsrb, _txen);
  295. sbi(*_ucsrb, _rxcie);
  296. cbi(*_ucsrb, _udrie);
  297. }
  298. void HardwareSerial::end()
  299. {
  300. // wait for transmission of outgoing data
  301. while (_tx_buffer->head != _tx_buffer->tail)
  302. ;
  303. cbi(*_ucsrb, _rxen);
  304. cbi(*_ucsrb, _txen);
  305. cbi(*_ucsrb, _rxcie);
  306. cbi(*_ucsrb, _udrie);
  307. // clear any received data
  308. _rx_buffer->head = _rx_buffer->tail;
  309. }
  310. int HardwareSerial::available(void)
  311. {
  312. return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
  313. }
  314. int HardwareSerial::peek(void)
  315. {
  316. if (_rx_buffer->head == _rx_buffer->tail) {
  317. return -1;
  318. } else {
  319. return _rx_buffer->buffer[_rx_buffer->tail];
  320. }
  321. }
  322. int HardwareSerial::read(void)
  323. {
  324. // if the head isn't ahead of the tail, we don't have any characters
  325. if (_rx_buffer->head == _rx_buffer->tail) {
  326. return -1;
  327. } else {
  328. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  329. _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
  330. return c;
  331. }
  332. }
  333. void HardwareSerial::flush()
  334. {
  335. while (_tx_buffer->head != _tx_buffer->tail)
  336. ;
  337. }
  338. size_t HardwareSerial::write(uint8_t c)
  339. {
  340. int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
  341. // If the output buffer is full, there's nothing for it other than to
  342. // wait for the interrupt handler to empty it a bit
  343. // ???: return 0 here instead?
  344. while (i == _tx_buffer->tail)
  345. ;
  346. _tx_buffer->buffer[_tx_buffer->head] = c;
  347. _tx_buffer->head = i;
  348. sbi(*_ucsrb, _udrie);
  349. return 1;
  350. }
  351. HardwareSerial::operator bool() {
  352. return true;
  353. }
  354. // Preinstantiate Objects //////////////////////////////////////////////////////
  355. #if defined(UBRRH) && defined(UBRRL)
  356. HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
  357. #elif defined(UBRR0H) && defined(UBRR0L)
  358. HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
  359. #elif defined(USBCON)
  360. // do nothing - Serial object and buffers are initialized in CDC code
  361. #else
  362. #error no serial port defined (port 0)
  363. #endif
  364. #if defined(UBRR1H)
  365. HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
  366. #endif
  367. #if defined(UBRR2H)
  368. HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
  369. #endif
  370. #if defined(UBRR3H)
  371. HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
  372. #endif
  373. #endif // whole file