HardwareSerial.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. Modified 14 August 2012 by Alarus
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <inttypes.h>
  23. #include "Arduino.h"
  24. #include "wiring_private.h"
  25. // this next line disables the entire HardwareSerial.cpp,
  26. // this is so I can support Attiny series and any other chip without a uart
  27. #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
  28. #include "HardwareSerial.h"
  29. /*
  30. * on ATmega8, the uart and its bits are not numbered, so there is no "TXC0"
  31. * definition.
  32. */
  33. #if !defined(TXC0)
  34. #if defined(TXC)
  35. #define TXC0 TXC
  36. #elif defined(TXC1)
  37. // Some devices have uart1 but no uart0
  38. #define TXC0 TXC1
  39. #else
  40. #error TXC0 not definable in HardwareSerial.h
  41. #endif
  42. #endif
  43. // Define constants and variables for buffering incoming serial data. We're
  44. // using a ring buffer (I think), in which head is the index of the location
  45. // to which to write the next incoming character and tail is the index of the
  46. // location from which to read.
  47. #if (RAMEND < 1000)
  48. #define SERIAL_BUFFER_SIZE 16
  49. #else
  50. #define SERIAL_BUFFER_SIZE 64
  51. #endif
  52. struct ring_buffer
  53. {
  54. unsigned char buffer[SERIAL_BUFFER_SIZE];
  55. volatile unsigned int head;
  56. volatile unsigned int tail;
  57. };
  58. #if defined(USBCON)
  59. ring_buffer rx_buffer = { { 0 }, 0, 0};
  60. ring_buffer tx_buffer = { { 0 }, 0, 0};
  61. #endif
  62. #if defined(UBRRH) || defined(UBRR0H)
  63. ring_buffer rx_buffer = { { 0 }, 0, 0 };
  64. ring_buffer tx_buffer = { { 0 }, 0, 0 };
  65. #endif
  66. #if defined(UBRR1H)
  67. ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
  68. ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
  69. #endif
  70. #if defined(UBRR2H)
  71. ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
  72. ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
  73. #endif
  74. #if defined(UBRR3H)
  75. ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
  76. ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
  77. #endif
  78. inline void store_char(unsigned char c, ring_buffer *buffer)
  79. {
  80. int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
  81. // if we should be storing the received character into the location
  82. // just before the tail (meaning that the head would advance to the
  83. // current location of the tail), we're about to overflow the buffer
  84. // and so we don't write the character or advance the head.
  85. if (i != buffer->tail) {
  86. buffer->buffer[buffer->head] = c;
  87. buffer->head = i;
  88. }
  89. }
  90. #if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
  91. // do nothing - on the 32u4 the first USART is USART1
  92. #else
  93. #if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
  94. !defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
  95. !defined(SIG_UART_RECV)
  96. #error "Don't know what the Data Received vector is called for the first UART"
  97. #else
  98. void serialEvent() __attribute__((weak));
  99. void serialEvent() {}
  100. #define serialEvent_implemented
  101. #if defined(USART_RX_vect)
  102. SIGNAL(USART_RX_vect)
  103. #elif defined(SIG_USART0_RECV)
  104. SIGNAL(SIG_USART0_RECV)
  105. #elif defined(SIG_UART0_RECV)
  106. SIGNAL(SIG_UART0_RECV)
  107. #elif defined(USART0_RX_vect)
  108. SIGNAL(USART0_RX_vect)
  109. #elif defined(SIG_UART_RECV)
  110. SIGNAL(SIG_UART_RECV)
  111. #endif
  112. {
  113. #if defined(UDR0)
  114. if (bit_is_clear(UCSR0A, UPE0)) {
  115. unsigned char c = UDR0;
  116. store_char(c, &rx_buffer);
  117. } else {
  118. unsigned char c = UDR0;
  119. };
  120. #elif defined(UDR)
  121. if (bit_is_clear(UCSRA, PE)) {
  122. unsigned char c = UDR;
  123. store_char(c, &rx_buffer);
  124. } else {
  125. unsigned char c = UDR;
  126. };
  127. #else
  128. #error UDR not defined
  129. #endif
  130. }
  131. #endif
  132. #endif
  133. #if defined(USART1_RX_vect)
  134. void serialEvent1() __attribute__((weak));
  135. void serialEvent1() {}
  136. #define serialEvent1_implemented
  137. SIGNAL(USART1_RX_vect)
  138. {
  139. if (bit_is_clear(UCSR1A, UPE1)) {
  140. unsigned char c = UDR1;
  141. store_char(c, &rx_buffer1);
  142. } else {
  143. unsigned char c = UDR1;
  144. };
  145. }
  146. #elif defined(SIG_USART1_RECV)
  147. #error SIG_USART1_RECV
  148. #endif
  149. #if defined(USART2_RX_vect) && defined(UDR2)
  150. void serialEvent2() __attribute__((weak));
  151. void serialEvent2() {}
  152. #define serialEvent2_implemented
  153. SIGNAL(USART2_RX_vect)
  154. {
  155. if (bit_is_clear(UCSR2A, UPE2)) {
  156. unsigned char c = UDR2;
  157. store_char(c, &rx_buffer2);
  158. } else {
  159. unsigned char c = UDR2;
  160. };
  161. }
  162. #elif defined(SIG_USART2_RECV)
  163. #error SIG_USART2_RECV
  164. #endif
  165. #if defined(USART3_RX_vect) && defined(UDR3)
  166. void serialEvent3() __attribute__((weak));
  167. void serialEvent3() {}
  168. #define serialEvent3_implemented
  169. SIGNAL(USART3_RX_vect)
  170. {
  171. if (bit_is_clear(UCSR3A, UPE3)) {
  172. unsigned char c = UDR3;
  173. store_char(c, &rx_buffer3);
  174. } else {
  175. unsigned char c = UDR3;
  176. };
  177. }
  178. #elif defined(SIG_USART3_RECV)
  179. #error SIG_USART3_RECV
  180. #endif
  181. void serialEventRun(void)
  182. {
  183. #ifdef serialEvent_implemented
  184. if (Serial.available()) serialEvent();
  185. #endif
  186. #ifdef serialEvent1_implemented
  187. if (Serial1.available()) serialEvent1();
  188. #endif
  189. #ifdef serialEvent2_implemented
  190. if (Serial2.available()) serialEvent2();
  191. #endif
  192. #ifdef serialEvent3_implemented
  193. if (Serial3.available()) serialEvent3();
  194. #endif
  195. }
  196. #if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
  197. // do nothing - on the 32u4 the first USART is USART1
  198. #else
  199. #if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
  200. #error "Don't know what the Data Register Empty vector is called for the first UART"
  201. #else
  202. #if defined(UART0_UDRE_vect)
  203. ISR(UART0_UDRE_vect)
  204. #elif defined(UART_UDRE_vect)
  205. ISR(UART_UDRE_vect)
  206. #elif defined(USART0_UDRE_vect)
  207. ISR(USART0_UDRE_vect)
  208. #elif defined(USART_UDRE_vect)
  209. ISR(USART_UDRE_vect)
  210. #endif
  211. {
  212. if (tx_buffer.head == tx_buffer.tail) {
  213. // Buffer empty, so disable interrupts
  214. #if defined(UCSR0B)
  215. cbi(UCSR0B, UDRIE0);
  216. #else
  217. cbi(UCSRB, UDRIE);
  218. #endif
  219. }
  220. else {
  221. // There is more data in the output buffer. Send the next byte
  222. unsigned char c = tx_buffer.buffer[tx_buffer.tail];
  223. tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
  224. #if defined(UDR0)
  225. UDR0 = c;
  226. #elif defined(UDR)
  227. UDR = c;
  228. #else
  229. #error UDR not defined
  230. #endif
  231. }
  232. }
  233. #endif
  234. #endif
  235. #ifdef USART1_UDRE_vect
  236. ISR(USART1_UDRE_vect)
  237. {
  238. if (tx_buffer1.head == tx_buffer1.tail) {
  239. // Buffer empty, so disable interrupts
  240. cbi(UCSR1B, UDRIE1);
  241. }
  242. else {
  243. // There is more data in the output buffer. Send the next byte
  244. unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
  245. tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
  246. UDR1 = c;
  247. }
  248. }
  249. #endif
  250. #ifdef USART2_UDRE_vect
  251. ISR(USART2_UDRE_vect)
  252. {
  253. if (tx_buffer2.head == tx_buffer2.tail) {
  254. // Buffer empty, so disable interrupts
  255. cbi(UCSR2B, UDRIE2);
  256. }
  257. else {
  258. // There is more data in the output buffer. Send the next byte
  259. unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
  260. tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
  261. UDR2 = c;
  262. }
  263. }
  264. #endif
  265. #ifdef USART3_UDRE_vect
  266. ISR(USART3_UDRE_vect)
  267. {
  268. if (tx_buffer3.head == tx_buffer3.tail) {
  269. // Buffer empty, so disable interrupts
  270. cbi(UCSR3B, UDRIE3);
  271. }
  272. else {
  273. // There is more data in the output buffer. Send the next byte
  274. unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
  275. tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
  276. UDR3 = c;
  277. }
  278. }
  279. #endif
  280. // Constructors ////////////////////////////////////////////////////////////////
  281. HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
  282. volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  283. volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  284. volatile uint8_t *ucsrc, volatile uint8_t *udr,
  285. uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
  286. {
  287. _rx_buffer = rx_buffer;
  288. _tx_buffer = tx_buffer;
  289. _ubrrh = ubrrh;
  290. _ubrrl = ubrrl;
  291. _ucsra = ucsra;
  292. _ucsrb = ucsrb;
  293. _ucsrc = ucsrc;
  294. _udr = udr;
  295. _rxen = rxen;
  296. _txen = txen;
  297. _rxcie = rxcie;
  298. _udrie = udrie;
  299. _u2x = u2x;
  300. }
  301. // Public Methods //////////////////////////////////////////////////////////////
  302. void HardwareSerial::begin(unsigned long baud)
  303. {
  304. uint16_t baud_setting;
  305. bool use_u2x = true;
  306. #if F_CPU == 16000000UL
  307. // hardcoded exception for compatibility with the bootloader shipped
  308. // with the Duemilanove and previous boards and the firmware on the 8U2
  309. // on the Uno and Mega 2560.
  310. if (baud == 57600) {
  311. use_u2x = false;
  312. }
  313. #endif
  314. try_again:
  315. if (use_u2x) {
  316. *_ucsra = 1 << _u2x;
  317. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  318. } else {
  319. *_ucsra = 0;
  320. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  321. }
  322. if ((baud_setting > 4095) && use_u2x)
  323. {
  324. use_u2x = false;
  325. goto try_again;
  326. }
  327. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  328. *_ubrrh = baud_setting >> 8;
  329. *_ubrrl = baud_setting;
  330. transmitting = false;
  331. sbi(*_ucsrb, _rxen);
  332. sbi(*_ucsrb, _txen);
  333. sbi(*_ucsrb, _rxcie);
  334. cbi(*_ucsrb, _udrie);
  335. }
  336. void HardwareSerial::begin(unsigned long baud, byte config)
  337. {
  338. uint16_t baud_setting;
  339. uint8_t current_config;
  340. bool use_u2x = true;
  341. #if F_CPU == 16000000UL
  342. // hardcoded exception for compatibility with the bootloader shipped
  343. // with the Duemilanove and previous boards and the firmware on the 8U2
  344. // on the Uno and Mega 2560.
  345. if (baud == 57600) {
  346. use_u2x = false;
  347. }
  348. #endif
  349. try_again:
  350. if (use_u2x) {
  351. *_ucsra = 1 << _u2x;
  352. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  353. } else {
  354. *_ucsra = 0;
  355. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  356. }
  357. if ((baud_setting > 4095) && use_u2x)
  358. {
  359. use_u2x = false;
  360. goto try_again;
  361. }
  362. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  363. *_ubrrh = baud_setting >> 8;
  364. *_ubrrl = baud_setting;
  365. //set the data bits, parity, and stop bits
  366. #if defined(__AVR_ATmega8__)
  367. config |= 0x80; // select UCSRC register (shared with UBRRH)
  368. #endif
  369. *_ucsrc = config;
  370. sbi(*_ucsrb, _rxen);
  371. sbi(*_ucsrb, _txen);
  372. sbi(*_ucsrb, _rxcie);
  373. cbi(*_ucsrb, _udrie);
  374. }
  375. void HardwareSerial::end()
  376. {
  377. // wait for transmission of outgoing data
  378. while (_tx_buffer->head != _tx_buffer->tail)
  379. ;
  380. cbi(*_ucsrb, _rxen);
  381. cbi(*_ucsrb, _txen);
  382. cbi(*_ucsrb, _rxcie);
  383. cbi(*_ucsrb, _udrie);
  384. // clear any received data
  385. _rx_buffer->head = _rx_buffer->tail;
  386. }
  387. int HardwareSerial::available(void)
  388. {
  389. return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
  390. }
  391. int HardwareSerial::peek(void)
  392. {
  393. if (_rx_buffer->head == _rx_buffer->tail) {
  394. return -1;
  395. } else {
  396. return _rx_buffer->buffer[_rx_buffer->tail];
  397. }
  398. }
  399. int HardwareSerial::read(void)
  400. {
  401. // if the head isn't ahead of the tail, we don't have any characters
  402. if (_rx_buffer->head == _rx_buffer->tail) {
  403. return -1;
  404. } else {
  405. unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
  406. _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
  407. return c;
  408. }
  409. }
  410. void HardwareSerial::flush()
  411. {
  412. // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
  413. while (transmitting && ! (*_ucsra & _BV(TXC0)));
  414. transmitting = false;
  415. }
  416. size_t HardwareSerial::write(uint8_t c)
  417. {
  418. int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
  419. // If the output buffer is full, there's nothing for it other than to
  420. // wait for the interrupt handler to empty it a bit
  421. // ???: return 0 here instead?
  422. while (i == _tx_buffer->tail)
  423. ;
  424. _tx_buffer->buffer[_tx_buffer->head] = c;
  425. _tx_buffer->head = i;
  426. sbi(*_ucsrb, _udrie);
  427. // clear the TXC bit -- "can be cleared by writing a one to its bit location"
  428. transmitting = true;
  429. sbi(*_ucsra, TXC0);
  430. return 1;
  431. }
  432. HardwareSerial::operator bool() {
  433. return true;
  434. }
  435. // Preinstantiate Objects //////////////////////////////////////////////////////
  436. #if defined(UBRRH) && defined(UBRRL)
  437. HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
  438. #elif defined(UBRR0H) && defined(UBRR0L)
  439. HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
  440. #elif defined(USBCON)
  441. // do nothing - Serial object and buffers are initialized in CDC code
  442. #else
  443. #error no serial port defined (port 0)
  444. #endif
  445. #if defined(UBRR1H)
  446. HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
  447. #endif
  448. #if defined(UBRR2H)
  449. HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
  450. #endif
  451. #if defined(UBRR3H)
  452. HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
  453. #endif
  454. #endif // whole file