PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

usb_api.cpp 19KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* USB API for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/teensyduino.html
  3. * Copyright (c) 2011 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/pgmspace.h>
  25. #include <stdint.h>
  26. #include "usb_common.h"
  27. #include "usb_private.h"
  28. #include "usb_api.h"
  29. #include "wiring.h"
  30. int usb_rawhid_class::available(void)
  31. {
  32. uint8_t n=0, intr_state;
  33. intr_state = SREG;
  34. cli();
  35. if (usb_configuration) {
  36. UENUM = RAWHID_RX_ENDPOINT;
  37. n = UEBCLX;
  38. }
  39. SREG = intr_state;
  40. return n;
  41. }
  42. // receive a packet, with timeout
  43. int usb_rawhid_class::recv(void *ptr, uint16_t timeout)
  44. {
  45. uint8_t *buffer = (uint8_t *)ptr;
  46. uint8_t intr_state;
  47. // if we're not online (enumerated and configured), error
  48. if (!usb_configuration) return -1;
  49. intr_state = SREG;
  50. cli();
  51. rawhid_rx_timeout_count = timeout;
  52. UENUM = RAWHID_RX_ENDPOINT;
  53. // wait for data to be available in the FIFO
  54. while (1) {
  55. if (UEINTX & (1<<RWAL)) break;
  56. SREG = intr_state;
  57. if (rawhid_rx_timeout_count == 0) return 0;
  58. if (!usb_configuration) return -1;
  59. intr_state = SREG;
  60. cli();
  61. UENUM = RAWHID_RX_ENDPOINT;
  62. }
  63. // read bytes from the FIFO
  64. #if (RAWHID_RX_SIZE >= 64)
  65. *buffer++ = UEDATX;
  66. #endif
  67. #if (RAWHID_RX_SIZE >= 63)
  68. *buffer++ = UEDATX;
  69. #endif
  70. #if (RAWHID_RX_SIZE >= 62)
  71. *buffer++ = UEDATX;
  72. #endif
  73. #if (RAWHID_RX_SIZE >= 61)
  74. *buffer++ = UEDATX;
  75. #endif
  76. #if (RAWHID_RX_SIZE >= 60)
  77. *buffer++ = UEDATX;
  78. #endif
  79. #if (RAWHID_RX_SIZE >= 59)
  80. *buffer++ = UEDATX;
  81. #endif
  82. #if (RAWHID_RX_SIZE >= 58)
  83. *buffer++ = UEDATX;
  84. #endif
  85. #if (RAWHID_RX_SIZE >= 57)
  86. *buffer++ = UEDATX;
  87. #endif
  88. #if (RAWHID_RX_SIZE >= 56)
  89. *buffer++ = UEDATX;
  90. #endif
  91. #if (RAWHID_RX_SIZE >= 55)
  92. *buffer++ = UEDATX;
  93. #endif
  94. #if (RAWHID_RX_SIZE >= 54)
  95. *buffer++ = UEDATX;
  96. #endif
  97. #if (RAWHID_RX_SIZE >= 53)
  98. *buffer++ = UEDATX;
  99. #endif
  100. #if (RAWHID_RX_SIZE >= 52)
  101. *buffer++ = UEDATX;
  102. #endif
  103. #if (RAWHID_RX_SIZE >= 51)
  104. *buffer++ = UEDATX;
  105. #endif
  106. #if (RAWHID_RX_SIZE >= 50)
  107. *buffer++ = UEDATX;
  108. #endif
  109. #if (RAWHID_RX_SIZE >= 49)
  110. *buffer++ = UEDATX;
  111. #endif
  112. #if (RAWHID_RX_SIZE >= 48)
  113. *buffer++ = UEDATX;
  114. #endif
  115. #if (RAWHID_RX_SIZE >= 47)
  116. *buffer++ = UEDATX;
  117. #endif
  118. #if (RAWHID_RX_SIZE >= 46)
  119. *buffer++ = UEDATX;
  120. #endif
  121. #if (RAWHID_RX_SIZE >= 45)
  122. *buffer++ = UEDATX;
  123. #endif
  124. #if (RAWHID_RX_SIZE >= 44)
  125. *buffer++ = UEDATX;
  126. #endif
  127. #if (RAWHID_RX_SIZE >= 43)
  128. *buffer++ = UEDATX;
  129. #endif
  130. #if (RAWHID_RX_SIZE >= 42)
  131. *buffer++ = UEDATX;
  132. #endif
  133. #if (RAWHID_RX_SIZE >= 41)
  134. *buffer++ = UEDATX;
  135. #endif
  136. #if (RAWHID_RX_SIZE >= 40)
  137. *buffer++ = UEDATX;
  138. #endif
  139. #if (RAWHID_RX_SIZE >= 39)
  140. *buffer++ = UEDATX;
  141. #endif
  142. #if (RAWHID_RX_SIZE >= 38)
  143. *buffer++ = UEDATX;
  144. #endif
  145. #if (RAWHID_RX_SIZE >= 37)
  146. *buffer++ = UEDATX;
  147. #endif
  148. #if (RAWHID_RX_SIZE >= 36)
  149. *buffer++ = UEDATX;
  150. #endif
  151. #if (RAWHID_RX_SIZE >= 35)
  152. *buffer++ = UEDATX;
  153. #endif
  154. #if (RAWHID_RX_SIZE >= 34)
  155. *buffer++ = UEDATX;
  156. #endif
  157. #if (RAWHID_RX_SIZE >= 33)
  158. *buffer++ = UEDATX;
  159. #endif
  160. #if (RAWHID_RX_SIZE >= 32)
  161. *buffer++ = UEDATX;
  162. #endif
  163. #if (RAWHID_RX_SIZE >= 31)
  164. *buffer++ = UEDATX;
  165. #endif
  166. #if (RAWHID_RX_SIZE >= 30)
  167. *buffer++ = UEDATX;
  168. #endif
  169. #if (RAWHID_RX_SIZE >= 29)
  170. *buffer++ = UEDATX;
  171. #endif
  172. #if (RAWHID_RX_SIZE >= 28)
  173. *buffer++ = UEDATX;
  174. #endif
  175. #if (RAWHID_RX_SIZE >= 27)
  176. *buffer++ = UEDATX;
  177. #endif
  178. #if (RAWHID_RX_SIZE >= 26)
  179. *buffer++ = UEDATX;
  180. #endif
  181. #if (RAWHID_RX_SIZE >= 25)
  182. *buffer++ = UEDATX;
  183. #endif
  184. #if (RAWHID_RX_SIZE >= 24)
  185. *buffer++ = UEDATX;
  186. #endif
  187. #if (RAWHID_RX_SIZE >= 23)
  188. *buffer++ = UEDATX;
  189. #endif
  190. #if (RAWHID_RX_SIZE >= 22)
  191. *buffer++ = UEDATX;
  192. #endif
  193. #if (RAWHID_RX_SIZE >= 21)
  194. *buffer++ = UEDATX;
  195. #endif
  196. #if (RAWHID_RX_SIZE >= 20)
  197. *buffer++ = UEDATX;
  198. #endif
  199. #if (RAWHID_RX_SIZE >= 19)
  200. *buffer++ = UEDATX;
  201. #endif
  202. #if (RAWHID_RX_SIZE >= 18)
  203. *buffer++ = UEDATX;
  204. #endif
  205. #if (RAWHID_RX_SIZE >= 17)
  206. *buffer++ = UEDATX;
  207. #endif
  208. #if (RAWHID_RX_SIZE >= 16)
  209. *buffer++ = UEDATX;
  210. #endif
  211. #if (RAWHID_RX_SIZE >= 15)
  212. *buffer++ = UEDATX;
  213. #endif
  214. #if (RAWHID_RX_SIZE >= 14)
  215. *buffer++ = UEDATX;
  216. #endif
  217. #if (RAWHID_RX_SIZE >= 13)
  218. *buffer++ = UEDATX;
  219. #endif
  220. #if (RAWHID_RX_SIZE >= 12)
  221. *buffer++ = UEDATX;
  222. #endif
  223. #if (RAWHID_RX_SIZE >= 11)
  224. *buffer++ = UEDATX;
  225. #endif
  226. #if (RAWHID_RX_SIZE >= 10)
  227. *buffer++ = UEDATX;
  228. #endif
  229. #if (RAWHID_RX_SIZE >= 9)
  230. *buffer++ = UEDATX;
  231. #endif
  232. #if (RAWHID_RX_SIZE >= 8)
  233. *buffer++ = UEDATX;
  234. #endif
  235. #if (RAWHID_RX_SIZE >= 7)
  236. *buffer++ = UEDATX;
  237. #endif
  238. #if (RAWHID_RX_SIZE >= 6)
  239. *buffer++ = UEDATX;
  240. #endif
  241. #if (RAWHID_RX_SIZE >= 5)
  242. *buffer++ = UEDATX;
  243. #endif
  244. #if (RAWHID_RX_SIZE >= 4)
  245. *buffer++ = UEDATX;
  246. #endif
  247. #if (RAWHID_RX_SIZE >= 3)
  248. *buffer++ = UEDATX;
  249. #endif
  250. #if (RAWHID_RX_SIZE >= 2)
  251. *buffer++ = UEDATX;
  252. #endif
  253. #if (RAWHID_RX_SIZE >= 1)
  254. *buffer++ = UEDATX;
  255. #endif
  256. // release the buffer
  257. UEINTX = 0x6B;
  258. SREG = intr_state;
  259. return RAWHID_RX_SIZE;
  260. }
  261. // send a packet, with timeout
  262. int usb_rawhid_class::send(const void *ptr, uint16_t timeout)
  263. {
  264. const uint8_t *buffer = (const uint8_t *)ptr;
  265. uint8_t intr_state;
  266. // if we're not online (enumerated and configured), error
  267. if (!usb_configuration) return -1;
  268. intr_state = SREG;
  269. cli();
  270. rawhid_tx_timeout_count = timeout;
  271. UENUM = RAWHID_TX_ENDPOINT;
  272. // wait for the FIFO to be ready to accept data
  273. while (1) {
  274. if (UEINTX & (1<<RWAL)) break;
  275. SREG = intr_state;
  276. if (rawhid_tx_timeout_count == 0) return 0;
  277. if (!usb_configuration) return -1;
  278. intr_state = SREG;
  279. cli();
  280. UENUM = RAWHID_TX_ENDPOINT;
  281. }
  282. // write bytes from the FIFO
  283. #if (RAWHID_TX_SIZE >= 64)
  284. UEDATX = *buffer++;
  285. #endif
  286. #if (RAWHID_TX_SIZE >= 63)
  287. UEDATX = *buffer++;
  288. #endif
  289. #if (RAWHID_TX_SIZE >= 62)
  290. UEDATX = *buffer++;
  291. #endif
  292. #if (RAWHID_TX_SIZE >= 61)
  293. UEDATX = *buffer++;
  294. #endif
  295. #if (RAWHID_TX_SIZE >= 60)
  296. UEDATX = *buffer++;
  297. #endif
  298. #if (RAWHID_TX_SIZE >= 59)
  299. UEDATX = *buffer++;
  300. #endif
  301. #if (RAWHID_TX_SIZE >= 58)
  302. UEDATX = *buffer++;
  303. #endif
  304. #if (RAWHID_TX_SIZE >= 57)
  305. UEDATX = *buffer++;
  306. #endif
  307. #if (RAWHID_TX_SIZE >= 56)
  308. UEDATX = *buffer++;
  309. #endif
  310. #if (RAWHID_TX_SIZE >= 55)
  311. UEDATX = *buffer++;
  312. #endif
  313. #if (RAWHID_TX_SIZE >= 54)
  314. UEDATX = *buffer++;
  315. #endif
  316. #if (RAWHID_TX_SIZE >= 53)
  317. UEDATX = *buffer++;
  318. #endif
  319. #if (RAWHID_TX_SIZE >= 52)
  320. UEDATX = *buffer++;
  321. #endif
  322. #if (RAWHID_TX_SIZE >= 51)
  323. UEDATX = *buffer++;
  324. #endif
  325. #if (RAWHID_TX_SIZE >= 50)
  326. UEDATX = *buffer++;
  327. #endif
  328. #if (RAWHID_TX_SIZE >= 49)
  329. UEDATX = *buffer++;
  330. #endif
  331. #if (RAWHID_TX_SIZE >= 48)
  332. UEDATX = *buffer++;
  333. #endif
  334. #if (RAWHID_TX_SIZE >= 47)
  335. UEDATX = *buffer++;
  336. #endif
  337. #if (RAWHID_TX_SIZE >= 46)
  338. UEDATX = *buffer++;
  339. #endif
  340. #if (RAWHID_TX_SIZE >= 45)
  341. UEDATX = *buffer++;
  342. #endif
  343. #if (RAWHID_TX_SIZE >= 44)
  344. UEDATX = *buffer++;
  345. #endif
  346. #if (RAWHID_TX_SIZE >= 43)
  347. UEDATX = *buffer++;
  348. #endif
  349. #if (RAWHID_TX_SIZE >= 42)
  350. UEDATX = *buffer++;
  351. #endif
  352. #if (RAWHID_TX_SIZE >= 41)
  353. UEDATX = *buffer++;
  354. #endif
  355. #if (RAWHID_TX_SIZE >= 40)
  356. UEDATX = *buffer++;
  357. #endif
  358. #if (RAWHID_TX_SIZE >= 39)
  359. UEDATX = *buffer++;
  360. #endif
  361. #if (RAWHID_TX_SIZE >= 38)
  362. UEDATX = *buffer++;
  363. #endif
  364. #if (RAWHID_TX_SIZE >= 37)
  365. UEDATX = *buffer++;
  366. #endif
  367. #if (RAWHID_TX_SIZE >= 36)
  368. UEDATX = *buffer++;
  369. #endif
  370. #if (RAWHID_TX_SIZE >= 35)
  371. UEDATX = *buffer++;
  372. #endif
  373. #if (RAWHID_TX_SIZE >= 34)
  374. UEDATX = *buffer++;
  375. #endif
  376. #if (RAWHID_TX_SIZE >= 33)
  377. UEDATX = *buffer++;
  378. #endif
  379. #if (RAWHID_TX_SIZE >= 32)
  380. UEDATX = *buffer++;
  381. #endif
  382. #if (RAWHID_TX_SIZE >= 31)
  383. UEDATX = *buffer++;
  384. #endif
  385. #if (RAWHID_TX_SIZE >= 30)
  386. UEDATX = *buffer++;
  387. #endif
  388. #if (RAWHID_TX_SIZE >= 29)
  389. UEDATX = *buffer++;
  390. #endif
  391. #if (RAWHID_TX_SIZE >= 28)
  392. UEDATX = *buffer++;
  393. #endif
  394. #if (RAWHID_TX_SIZE >= 27)
  395. UEDATX = *buffer++;
  396. #endif
  397. #if (RAWHID_TX_SIZE >= 26)
  398. UEDATX = *buffer++;
  399. #endif
  400. #if (RAWHID_TX_SIZE >= 25)
  401. UEDATX = *buffer++;
  402. #endif
  403. #if (RAWHID_TX_SIZE >= 24)
  404. UEDATX = *buffer++;
  405. #endif
  406. #if (RAWHID_TX_SIZE >= 23)
  407. UEDATX = *buffer++;
  408. #endif
  409. #if (RAWHID_TX_SIZE >= 22)
  410. UEDATX = *buffer++;
  411. #endif
  412. #if (RAWHID_TX_SIZE >= 21)
  413. UEDATX = *buffer++;
  414. #endif
  415. #if (RAWHID_TX_SIZE >= 20)
  416. UEDATX = *buffer++;
  417. #endif
  418. #if (RAWHID_TX_SIZE >= 19)
  419. UEDATX = *buffer++;
  420. #endif
  421. #if (RAWHID_TX_SIZE >= 18)
  422. UEDATX = *buffer++;
  423. #endif
  424. #if (RAWHID_TX_SIZE >= 17)
  425. UEDATX = *buffer++;
  426. #endif
  427. #if (RAWHID_TX_SIZE >= 16)
  428. UEDATX = *buffer++;
  429. #endif
  430. #if (RAWHID_TX_SIZE >= 15)
  431. UEDATX = *buffer++;
  432. #endif
  433. #if (RAWHID_TX_SIZE >= 14)
  434. UEDATX = *buffer++;
  435. #endif
  436. #if (RAWHID_TX_SIZE >= 13)
  437. UEDATX = *buffer++;
  438. #endif
  439. #if (RAWHID_TX_SIZE >= 12)
  440. UEDATX = *buffer++;
  441. #endif
  442. #if (RAWHID_TX_SIZE >= 11)
  443. UEDATX = *buffer++;
  444. #endif
  445. #if (RAWHID_TX_SIZE >= 10)
  446. UEDATX = *buffer++;
  447. #endif
  448. #if (RAWHID_TX_SIZE >= 9)
  449. UEDATX = *buffer++;
  450. #endif
  451. #if (RAWHID_TX_SIZE >= 8)
  452. UEDATX = *buffer++;
  453. #endif
  454. #if (RAWHID_TX_SIZE >= 7)
  455. UEDATX = *buffer++;
  456. #endif
  457. #if (RAWHID_TX_SIZE >= 6)
  458. UEDATX = *buffer++;
  459. #endif
  460. #if (RAWHID_TX_SIZE >= 5)
  461. UEDATX = *buffer++;
  462. #endif
  463. #if (RAWHID_TX_SIZE >= 4)
  464. UEDATX = *buffer++;
  465. #endif
  466. #if (RAWHID_TX_SIZE >= 3)
  467. UEDATX = *buffer++;
  468. #endif
  469. #if (RAWHID_TX_SIZE >= 2)
  470. UEDATX = *buffer++;
  471. #endif
  472. #if (RAWHID_TX_SIZE >= 1)
  473. UEDATX = *buffer++;
  474. #endif
  475. // transmit it now
  476. UEINTX = 0x3A;
  477. SREG = intr_state;
  478. return RAWHID_TX_SIZE;
  479. }
  480. static volatile uint8_t prev_byte=0;
  481. void usb_serial_class::begin(long speed)
  482. {
  483. // make sure USB is initialized
  484. usb_init();
  485. uint16_t begin_wait = (uint16_t)millis();
  486. while (1) {
  487. if (usb_configuration) {
  488. delay(200); // a little time for host to load a driver
  489. return;
  490. }
  491. if (usb_suspended) {
  492. uint16_t begin_suspend = (uint16_t)millis();
  493. while (usb_suspended) {
  494. // must remain suspended for a while, because
  495. // normal USB enumeration causes brief suspend
  496. // states, typically under 0.1 second
  497. if ((uint16_t)millis() - begin_suspend > 250) {
  498. return;
  499. }
  500. }
  501. }
  502. // ... or a timout (powered by a USB power adaptor that
  503. // wiggles the data lines to keep a USB device charging)
  504. if ((uint16_t)millis() - begin_wait > 2500) return;
  505. }
  506. prev_byte = 0;
  507. }
  508. void usb_serial_class::end()
  509. {
  510. usb_shutdown();
  511. delay(25);
  512. }
  513. // number of bytes available in the receive buffer
  514. int usb_serial_class::available()
  515. {
  516. uint8_t c;
  517. c = prev_byte; // assume 1 byte static volatile access is atomic
  518. if (c) return 1;
  519. c = readnext();
  520. if (c) {
  521. prev_byte = c;
  522. return 1;
  523. }
  524. return 0;
  525. }
  526. // get the next character, or -1 if nothing received
  527. int usb_serial_class::read()
  528. {
  529. uint8_t c;
  530. c = prev_byte;
  531. if (c) {
  532. prev_byte = 0;
  533. return c;
  534. }
  535. c = readnext();
  536. if (c) return c;
  537. return -1;
  538. }
  539. int usb_serial_class::peek()
  540. {
  541. uint8_t c;
  542. c = prev_byte;
  543. if (c) return c;
  544. c = readnext();
  545. if (c) {
  546. prev_byte = c;
  547. return c;
  548. }
  549. return -1;
  550. }
  551. // get the next character, or 0 if nothing
  552. uint8_t usb_serial_class::readnext(void)
  553. {
  554. uint8_t c, intr_state;
  555. // interrupts are disabled so these functions can be
  556. // used from the main program or interrupt context,
  557. // even both in the same program!
  558. intr_state = SREG;
  559. cli();
  560. if (!usb_configuration) {
  561. SREG = intr_state;
  562. return 0;
  563. }
  564. UENUM = DEBUG_RX_ENDPOINT;
  565. try_again:
  566. if (!(UEINTX & (1<<RWAL))) {
  567. // no packet in buffer
  568. SREG = intr_state;
  569. return 0;
  570. }
  571. // take one byte out of the buffer
  572. c = UEDATX;
  573. if (c == 0) {
  574. // if we see a zero, discard it and
  575. // discard the rest of this packet
  576. UEINTX = 0x6B;
  577. goto try_again;
  578. }
  579. // if this drained the buffer, release it
  580. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  581. SREG = intr_state;
  582. return c;
  583. }
  584. // discard any buffered input
  585. void usb_serial_class::flush()
  586. {
  587. uint8_t intr_state;
  588. if (usb_configuration) {
  589. intr_state = SREG;
  590. cli();
  591. UENUM = DEBUG_RX_ENDPOINT;
  592. while ((UEINTX & (1<<RWAL))) {
  593. UEINTX = 0x6B;
  594. }
  595. SREG = intr_state;
  596. }
  597. prev_byte = 0;
  598. }
  599. // transmit a character.
  600. #if ARDUINO >= 100
  601. size_t usb_serial_class::write(uint8_t c)
  602. #else
  603. void usb_serial_class::write(uint8_t c)
  604. #endif
  605. {
  606. //static uint8_t previous_timeout=0;
  607. uint8_t timeout, intr_state;
  608. // if we're not online (enumerated and configured), error
  609. if (!usb_configuration) goto error;
  610. // interrupts are disabled so these functions can be
  611. // used from the main program or interrupt context,
  612. // even both in the same program!
  613. intr_state = SREG;
  614. cli();
  615. UENUM = DEBUG_TX_ENDPOINT;
  616. // if we gave up due to timeout before, don't wait again
  617. #if 0
  618. // this seems to be causig a lockup... why????
  619. if (previous_timeout) {
  620. if (!(UEINTX & (1<<RWAL))) {
  621. SREG = intr_state;
  622. return;
  623. }
  624. previous_timeout = 0;
  625. }
  626. #endif
  627. // wait for the FIFO to be ready to accept data
  628. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  629. while (1) {
  630. // are we ready to transmit?
  631. if (UEINTX & (1<<RWAL)) break;
  632. SREG = intr_state;
  633. // have we waited too long? This happens if the user
  634. // is not running an application that is listening
  635. if (UDFNUML == timeout) {
  636. //previous_timeout = 1;
  637. goto error;
  638. }
  639. // has the USB gone offline?
  640. if (!usb_configuration) goto error;
  641. // get ready to try checking again
  642. intr_state = SREG;
  643. cli();
  644. UENUM = DEBUG_TX_ENDPOINT;
  645. }
  646. // actually write the byte into the FIFO
  647. UEDATX = c;
  648. // if this completed a packet, transmit it now!
  649. if (!(UEINTX & (1<<RWAL))) {
  650. UEINTX = 0x3A;
  651. debug_flush_timer = 0;
  652. } else {
  653. debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  654. }
  655. SREG = intr_state;
  656. #if ARDUINO >= 100
  657. return 1;
  658. #endif
  659. error:
  660. #if ARDUINO >= 100
  661. setWriteError();
  662. return 0;
  663. #else
  664. return;
  665. #endif
  666. }
  667. // These are Teensy-specific extensions to the Serial object
  668. // immediately transmit any buffered output.
  669. // This doesn't actually transmit the data - that is impossible!
  670. // USB devices only transmit when the host allows, so the best
  671. // we can do is release the FIFO buffer for when the host wants it
  672. void usb_serial_class::send_now(void)
  673. {
  674. uint8_t intr_state;
  675. intr_state = SREG;
  676. cli();
  677. if (debug_flush_timer) {
  678. UENUM = DEBUG_TX_ENDPOINT;
  679. while ((UEINTX & (1<<RWAL))) {
  680. UEDATX = 0;
  681. }
  682. UEINTX = 0x3A;
  683. debug_flush_timer = 0;
  684. }
  685. SREG = intr_state;
  686. }
  687. uint32_t usb_serial_class::baud(void)
  688. {
  689. return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
  690. }
  691. uint8_t usb_serial_class::stopbits(void)
  692. {
  693. return 1;
  694. }
  695. uint8_t usb_serial_class::paritytype(void)
  696. {
  697. return 0;
  698. }
  699. uint8_t usb_serial_class::numbits(void)
  700. {
  701. return 8;
  702. }
  703. uint8_t usb_serial_class::dtr(void)
  704. {
  705. return 1;
  706. }
  707. uint8_t usb_serial_class::rts(void)
  708. {
  709. return 1;
  710. }
  711. usb_serial_class::operator bool()
  712. {
  713. if (usb_configuration) return true;
  714. return false;
  715. }
  716. // Preinstantiate Objects //////////////////////////////////////////////////////
  717. usb_serial_class Serial = usb_serial_class();
  718. usb_rawhid_class RawHID = usb_rawhid_class();