Teensy 4.1 core updated for 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.

821 lines
20KB

  1. /* USB API for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/teensyduino.html
  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/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. // Step #1, decode UTF8 to Unicode code points
  31. //
  32. #if ARDUINO >= 100
  33. size_t usb_keyboard_class::write(uint8_t c)
  34. #else
  35. void usb_keyboard_class::write(uint8_t c)
  36. #endif
  37. {
  38. if (c < 0x80) {
  39. // single byte encoded, 0x00 to 0x7F
  40. utf8_state = 0;
  41. write_unicode(c);
  42. } else if (c < 0xC0) {
  43. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  44. c &= 0x3F;
  45. if (utf8_state == 1) {
  46. utf8_state = 0;
  47. write_unicode(unicode_wchar | c);
  48. } else if (utf8_state == 2) {
  49. unicode_wchar |= ((uint16_t)c << 6);
  50. utf8_state = 1;
  51. }
  52. } else if (c < 0xE0) {
  53. // begin 2 byte sequence, 0xC2 to 0xDF
  54. // or illegal 2 byte sequence, 0xC0 to 0xC1
  55. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  56. utf8_state = 1;
  57. } else if (c < 0xF0) {
  58. // begin 3 byte sequence, 0xE0 to 0xEF
  59. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  60. utf8_state = 2;
  61. } else {
  62. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  63. // or illegal, 0xF5 to 0xFF
  64. utf8_state = 255;
  65. }
  66. #if ARDUINO >= 100
  67. return 1;
  68. #endif
  69. }
  70. // Step #2: translate Unicode code point to keystroke sequence
  71. //
  72. KEYCODE_TYPE usb_keyboard_class::unicode_to_keycode(uint16_t cpoint)
  73. {
  74. // Unicode code points beyond U+FFFF are not supported
  75. // technically this input should probably be called UCS-2
  76. if (cpoint < 32) {
  77. if (cpoint == 10) return KEY_ENTER & 0x3FFF;
  78. return 0;
  79. }
  80. if (cpoint < 128) {
  81. if (sizeof(KEYCODE_TYPE) == 1) {
  82. return pgm_read_byte(keycodes_ascii + (cpoint - 0x20));
  83. } else if (sizeof(KEYCODE_TYPE) == 2) {
  84. return pgm_read_word(keycodes_ascii + (cpoint - 0x20));
  85. }
  86. return 0;
  87. }
  88. #ifdef ISO_8859_1_A0
  89. if (cpoint <= 0xA0) return 0;
  90. if (cpoint < 0x100) {
  91. if (sizeof(KEYCODE_TYPE) == 1) {
  92. return pgm_read_byte(keycodes_iso_8859_1 + (cpoint - 0xA0));
  93. } else if (sizeof(KEYCODE_TYPE) == 2) {
  94. return pgm_read_word(keycodes_iso_8859_1 + (cpoint - 0xA0));
  95. }
  96. return 0;
  97. }
  98. #endif
  99. //#ifdef UNICODE_20AC
  100. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  101. //#endif
  102. #ifdef KEYCODE_EXTRA00
  103. if (cpoint == UNICODE_EXTRA00) return KEYCODE_EXTRA00 & 0x3FFF;
  104. #endif
  105. #ifdef KEYCODE_EXTRA01
  106. if (cpoint == UNICODE_EXTRA01) return KEYCODE_EXTRA01 & 0x3FFF;
  107. #endif
  108. #ifdef KEYCODE_EXTRA02
  109. if (cpoint == UNICODE_EXTRA02) return KEYCODE_EXTRA02 & 0x3FFF;
  110. #endif
  111. #ifdef KEYCODE_EXTRA03
  112. if (cpoint == UNICODE_EXTRA03) return KEYCODE_EXTRA03 & 0x3FFF;
  113. #endif
  114. #ifdef KEYCODE_EXTRA04
  115. if (cpoint == UNICODE_EXTRA04) return KEYCODE_EXTRA04 & 0x3FFF;
  116. #endif
  117. #ifdef KEYCODE_EXTRA05
  118. if (cpoint == UNICODE_EXTRA05) return KEYCODE_EXTRA05 & 0x3FFF;
  119. #endif
  120. #ifdef KEYCODE_EXTRA06
  121. if (cpoint == UNICODE_EXTRA06) return KEYCODE_EXTRA06 & 0x3FFF;
  122. #endif
  123. #ifdef KEYCODE_EXTRA07
  124. if (cpoint == UNICODE_EXTRA07) return KEYCODE_EXTRA07 & 0x3FFF;
  125. #endif
  126. #ifdef KEYCODE_EXTRA08
  127. if (cpoint == UNICODE_EXTRA08) return KEYCODE_EXTRA08 & 0x3FFF;
  128. #endif
  129. #ifdef KEYCODE_EXTRA09
  130. if (cpoint == UNICODE_EXTRA09) return KEYCODE_EXTRA09 & 0x3FFF;
  131. #endif
  132. return 0;
  133. }
  134. // Step #3: execute keystroke sequence
  135. //
  136. void usb_keyboard_class::write_keycode(KEYCODE_TYPE keycode)
  137. {
  138. if (!keycode) return;
  139. #ifdef DEADKEYS_MASK
  140. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  141. if (deadkeycode) write_key(deadkeycode);
  142. #endif
  143. write_key(keycode);
  144. }
  145. KEYCODE_TYPE usb_keyboard_class::deadkey_to_keycode(KEYCODE_TYPE keycode)
  146. {
  147. #ifdef DEADKEYS_MASK
  148. keycode &= DEADKEYS_MASK;
  149. if (keycode == 0) return 0;
  150. #ifdef ACUTE_ACCENT_BITS
  151. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  152. #endif
  153. #ifdef CEDILLA_BITS
  154. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  155. #endif
  156. #ifdef CIRCUMFLEX_BITS
  157. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  158. #endif
  159. #ifdef DIAERESIS_BITS
  160. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  161. #endif
  162. #ifdef GRAVE_ACCENT_BITS
  163. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  164. #endif
  165. #ifdef TILDE_BITS
  166. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  167. #endif
  168. #ifdef RING_ABOVE_BITS
  169. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  170. #endif
  171. #endif // DEADKEYS_MASK
  172. return 0;
  173. }
  174. // Step #4: do each keystroke
  175. //
  176. void usb_keyboard_class::write_key(KEYCODE_TYPE keycode)
  177. {
  178. keyboard_report_data[0] = keycode_to_modifier(keycode);
  179. keyboard_report_data[1] = 0;
  180. keyboard_report_data[2] = keycode_to_key(keycode);
  181. keyboard_report_data[3] = 0;
  182. keyboard_report_data[4] = 0;
  183. keyboard_report_data[5] = 0;
  184. keyboard_report_data[6] = 0;
  185. keyboard_report_data[7] = 0;
  186. send_now();
  187. keyboard_report_data[0] = 0;
  188. keyboard_report_data[2] = 0;
  189. send_now();
  190. }
  191. uint8_t usb_keyboard_class::keycode_to_modifier(KEYCODE_TYPE keycode)
  192. {
  193. uint8_t modifier=0;
  194. #ifdef SHIFT_MASK
  195. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  196. #endif
  197. #ifdef ALTGR_MASK
  198. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  199. #endif
  200. #ifdef RCTRL_MASK
  201. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  202. #endif
  203. return modifier;
  204. }
  205. uint8_t usb_keyboard_class::keycode_to_key(KEYCODE_TYPE keycode)
  206. {
  207. uint8_t key = keycode & 0x3F;
  208. #ifdef KEY_NON_US_100
  209. if (key == KEY_NON_US_100) key = 100;
  210. #endif
  211. return key;
  212. }
  213. void usb_keyboard_class::set_modifier(uint8_t c)
  214. {
  215. keyboard_report_data[0] = c;
  216. }
  217. void usb_keyboard_class::set_key1(uint8_t c)
  218. {
  219. keyboard_report_data[2] = c;
  220. }
  221. void usb_keyboard_class::set_key2(uint8_t c)
  222. {
  223. keyboard_report_data[3] = c;
  224. }
  225. void usb_keyboard_class::set_key3(uint8_t c)
  226. {
  227. keyboard_report_data[4] = c;
  228. }
  229. void usb_keyboard_class::set_key4(uint8_t c)
  230. {
  231. keyboard_report_data[5] = c;
  232. }
  233. void usb_keyboard_class::set_key5(uint8_t c)
  234. {
  235. keyboard_report_data[6] = c;
  236. }
  237. void usb_keyboard_class::set_key6(uint8_t c)
  238. {
  239. keyboard_report_data[7] = c;
  240. }
  241. void usb_keyboard_class::set_media(uint8_t c)
  242. {
  243. keyboard_report_data[1] = c;
  244. }
  245. void usb_keyboard_class::send_now(void)
  246. {
  247. uint8_t intr_state, timeout;
  248. if (!usb_configuration) return;
  249. intr_state = SREG;
  250. cli();
  251. UENUM = KEYBOARD_ENDPOINT;
  252. timeout = UDFNUML + 50;
  253. while (1) {
  254. // are we ready to transmit?
  255. if (UEINTX & (1<<RWAL)) break;
  256. SREG = intr_state;
  257. // has the USB gone offline?
  258. if (!usb_configuration) return;
  259. // have we waited too long?
  260. if (UDFNUML == timeout) return;
  261. // get ready to try checking again
  262. intr_state = SREG;
  263. cli();
  264. UENUM = KEYBOARD_ENDPOINT;
  265. }
  266. UEDATX = keyboard_report_data[0];
  267. UEDATX = keyboard_report_data[1];
  268. UEDATX = keyboard_report_data[2];
  269. UEDATX = keyboard_report_data[3];
  270. UEDATX = keyboard_report_data[4];
  271. UEDATX = keyboard_report_data[5];
  272. UEDATX = keyboard_report_data[6];
  273. UEDATX = keyboard_report_data[7];
  274. UEINTX = 0x3A;
  275. keyboard_idle_count = 0;
  276. SREG = intr_state;
  277. }
  278. void usb_keyboard_class::press(uint16_t n)
  279. {
  280. uint8_t key, mod, msb, modrestore=0;
  281. msb = n >> 8;
  282. if (msb >= 0xC2 && msb <= 0xDF) {
  283. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  284. } else
  285. if (msb == 0x80) {
  286. presskey(0, n);
  287. return;
  288. } else
  289. if (msb == 0x40) {
  290. presskey(n, 0);
  291. return;
  292. }
  293. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  294. if (!keycode) return;
  295. #ifdef DEADKEYS_MASK
  296. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  297. if (deadkeycode) {
  298. modrestore = keyboard_report_data[0];
  299. if (modrestore) {
  300. keyboard_report_data[0] = 0;
  301. send_now();
  302. }
  303. // TODO: test if operating systems recognize
  304. // deadkey sequences when other keys are held
  305. mod = keycode_to_modifier(deadkeycode);
  306. key = keycode_to_key(deadkeycode);
  307. presskey(key, mod);
  308. releasekey(key, mod);
  309. }
  310. #endif
  311. mod = keycode_to_modifier(keycode);
  312. key = keycode_to_key(keycode);
  313. presskey(key, mod | modrestore);
  314. }
  315. void usb_keyboard_class::release(uint16_t n)
  316. {
  317. uint8_t key, mod, msb;
  318. msb = n >> 8;
  319. if (msb >= 0xC2 && msb <= 0xDF) {
  320. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  321. } else
  322. if (msb == 0x80) {
  323. releasekey(0, n);
  324. return;
  325. } else
  326. if (msb == 0x40) {
  327. releasekey(n, 0);
  328. return;
  329. }
  330. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  331. if (!keycode) return;
  332. mod = keycode_to_modifier(keycode);
  333. key = keycode_to_key(keycode);
  334. releasekey(key, mod);
  335. }
  336. void usb_keyboard_class::presskey(uint8_t key, uint8_t modifier)
  337. {
  338. bool send_required = false;
  339. uint8_t i;
  340. if (modifier) {
  341. if ((keyboard_report_data[0] & modifier) != modifier) {
  342. keyboard_report_data[0] |= modifier;
  343. send_required = true;
  344. }
  345. }
  346. if (key) {
  347. for (i=2; i < 8; i++) {
  348. if (keyboard_report_data[i] == key) goto end;
  349. }
  350. for (i=2; i < 8; i++) {
  351. if (keyboard_report_data[i] == 0) {
  352. keyboard_report_data[i] = key;
  353. send_required = true;
  354. goto end;
  355. }
  356. }
  357. }
  358. end:
  359. if (send_required) send_now();
  360. }
  361. void usb_keyboard_class::releasekey(uint8_t key, uint8_t modifier)
  362. {
  363. bool send_required = false;
  364. uint8_t i;
  365. if (modifier) {
  366. if ((keyboard_report_data[0] & modifier) != 0) {
  367. keyboard_report_data[0] &= ~modifier;
  368. send_required = true;
  369. }
  370. }
  371. if (key) {
  372. for (i=2; i < 8; i++) {
  373. if (keyboard_report_data[i] == key) {
  374. keyboard_report_data[i] = 0;
  375. send_required = true;
  376. }
  377. }
  378. }
  379. if (send_required) send_now();
  380. }
  381. void usb_keyboard_class::releaseAll(void)
  382. {
  383. uint8_t i, anybits;
  384. anybits = keyboard_report_data[0];
  385. for (i=2; i < 8; i++) {
  386. anybits |= keyboard_report_data[i];
  387. keyboard_report_data[i] = 0;
  388. }
  389. if (!anybits) return;
  390. keyboard_report_data[0] = 0;
  391. send_now();
  392. }
  393. void usb_mouse_class::move(int8_t x, int8_t y, int8_t wheel)
  394. {
  395. uint8_t intr_state, timeout;
  396. if (!usb_configuration) return;
  397. if (x == -128) x = -127;
  398. if (y == -128) y = -127;
  399. if (wheel == -128) wheel = -127;
  400. intr_state = SREG;
  401. cli();
  402. UENUM = MOUSE_ENDPOINT;
  403. timeout = UDFNUML + 50;
  404. while (1) {
  405. // are we ready to transmit?
  406. if (UEINTX & (1<<RWAL)) break;
  407. SREG = intr_state;
  408. // has the USB gone offline?
  409. if (!usb_configuration) return;
  410. // have we waited too long?
  411. if (UDFNUML == timeout) return;
  412. // get ready to try checking again
  413. intr_state = SREG;
  414. cli();
  415. UENUM = MOUSE_ENDPOINT;
  416. }
  417. UEDATX = mouse_buttons;
  418. UEDATX = x;
  419. UEDATX = y;
  420. UEDATX = wheel;
  421. UEINTX = 0x3A;
  422. SREG = intr_state;
  423. }
  424. void usb_mouse_class::click(uint8_t b)
  425. {
  426. mouse_buttons = (b & 7);
  427. move(0, 0);
  428. mouse_buttons = 0;
  429. move(0, 0);
  430. }
  431. void usb_mouse_class::scroll(int8_t wheel)
  432. {
  433. move(0, 0, wheel);
  434. }
  435. void usb_mouse_class::set_buttons(uint8_t left, uint8_t middle, uint8_t right)
  436. {
  437. uint8_t mask=0;
  438. if (left) mask |= 1;
  439. if (middle) mask |= 4;
  440. if (right) mask |= 2;
  441. mouse_buttons = mask;
  442. move(0, 0);
  443. }
  444. void usb_mouse_class::press(uint8_t b)
  445. {
  446. uint8_t prev = mouse_buttons;
  447. mouse_buttons |= (b & 7);
  448. if (mouse_buttons != prev) move(0, 0);
  449. }
  450. void usb_mouse_class::release(uint8_t b)
  451. {
  452. uint8_t prev = mouse_buttons;
  453. mouse_buttons &= ~(b & 7);
  454. if (mouse_buttons != prev) move(0, 0);
  455. }
  456. bool usb_mouse_class::isPressed(uint8_t b)
  457. {
  458. return ((mouse_buttons & (b & 7)) != 0);
  459. }
  460. #if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  461. void usb_joystick_class::send_now(void)
  462. {
  463. uint8_t intr_state, timeout;
  464. if (!usb_configuration) return;
  465. intr_state = SREG;
  466. cli();
  467. UENUM = JOYSTICK_ENDPOINT;
  468. timeout = UDFNUML + 50;
  469. while (1) {
  470. // are we ready to transmit?
  471. if (UEINTX & (1<<RWAL)) break;
  472. SREG = intr_state;
  473. // has the USB gone offline?
  474. if (!usb_configuration) return;
  475. // have we waited too long?
  476. if (UDFNUML == timeout) return;
  477. // get ready to try checking again
  478. intr_state = SREG;
  479. cli();
  480. UENUM = JOYSTICK_ENDPOINT;
  481. }
  482. UEDATX = joystick_report_data[0];
  483. UEDATX = joystick_report_data[1];
  484. UEDATX = joystick_report_data[2];
  485. UEDATX = joystick_report_data[3];
  486. UEDATX = joystick_report_data[4];
  487. UEDATX = joystick_report_data[5];
  488. UEDATX = joystick_report_data[6];
  489. UEDATX = joystick_report_data[7];
  490. UEDATX = joystick_report_data[8];
  491. UEDATX = joystick_report_data[9];
  492. UEDATX = joystick_report_data[10];
  493. UEDATX = joystick_report_data[11];
  494. UEINTX = 0x3A;
  495. SREG = intr_state;
  496. }
  497. #endif
  498. static volatile uint8_t prev_byte=0;
  499. void usb_serial_class::begin(long speed)
  500. {
  501. // make sure USB is initialized
  502. usb_init();
  503. uint16_t begin_wait = (uint16_t)millis();
  504. while (1) {
  505. if (usb_configuration) {
  506. delay(200); // a little time for host to load a driver
  507. return;
  508. }
  509. if (usb_suspended) {
  510. uint16_t begin_suspend = (uint16_t)millis();
  511. while (usb_suspended) {
  512. // must remain suspended for a while, because
  513. // normal USB enumeration causes brief suspend
  514. // states, typically under 0.1 second
  515. if ((uint16_t)millis() - begin_suspend > 250) {
  516. return;
  517. }
  518. }
  519. }
  520. // ... or a timout (powered by a USB power adaptor that
  521. // wiggles the data lines to keep a USB device charging)
  522. if ((uint16_t)millis() - begin_wait > 2500) return;
  523. }
  524. prev_byte = 0;
  525. }
  526. void usb_serial_class::end()
  527. {
  528. usb_shutdown();
  529. delay(25);
  530. }
  531. // number of bytes available in the receive buffer
  532. int usb_serial_class::available()
  533. {
  534. uint8_t c;
  535. c = prev_byte; // assume 1 byte static volatile access is atomic
  536. if (c) return 1;
  537. c = readnext();
  538. if (c) {
  539. prev_byte = c;
  540. return 1;
  541. }
  542. return 0;
  543. }
  544. // get the next character, or -1 if nothing received
  545. int usb_serial_class::read()
  546. {
  547. uint8_t c;
  548. c = prev_byte;
  549. if (c) {
  550. prev_byte = 0;
  551. return c;
  552. }
  553. c = readnext();
  554. if (c) return c;
  555. return -1;
  556. }
  557. int usb_serial_class::peek()
  558. {
  559. uint8_t c;
  560. c = prev_byte;
  561. if (c) return c;
  562. c = readnext();
  563. if (c) {
  564. prev_byte = c;
  565. return c;
  566. }
  567. return -1;
  568. }
  569. // get the next character, or 0 if nothing
  570. uint8_t usb_serial_class::readnext(void)
  571. {
  572. uint8_t c, intr_state;
  573. // interrupts are disabled so these functions can be
  574. // used from the main program or interrupt context,
  575. // even both in the same program!
  576. intr_state = SREG;
  577. cli();
  578. if (!usb_configuration) {
  579. SREG = intr_state;
  580. return 0;
  581. }
  582. UENUM = DEBUG_RX_ENDPOINT;
  583. try_again:
  584. if (!(UEINTX & (1<<RWAL))) {
  585. // no packet in buffer
  586. SREG = intr_state;
  587. return 0;
  588. }
  589. // take one byte out of the buffer
  590. c = UEDATX;
  591. if (c == 0) {
  592. // if we see a zero, discard it and
  593. // discard the rest of this packet
  594. UEINTX = 0x6B;
  595. goto try_again;
  596. }
  597. // if this drained the buffer, release it
  598. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  599. SREG = intr_state;
  600. return c;
  601. }
  602. // discard any buffered input
  603. void usb_serial_class::flush()
  604. {
  605. uint8_t intr_state;
  606. if (usb_configuration) {
  607. intr_state = SREG;
  608. cli();
  609. UENUM = DEBUG_RX_ENDPOINT;
  610. while ((UEINTX & (1<<RWAL))) {
  611. UEINTX = 0x6B;
  612. }
  613. SREG = intr_state;
  614. }
  615. prev_byte = 0;
  616. }
  617. // transmit a character.
  618. #if ARDUINO >= 100
  619. size_t usb_serial_class::write(uint8_t c)
  620. #else
  621. void usb_serial_class::write(uint8_t c)
  622. #endif
  623. {
  624. //static uint8_t previous_timeout=0;
  625. uint8_t timeout, intr_state;
  626. // if we're not online (enumerated and configured), error
  627. if (!usb_configuration) goto error;
  628. // interrupts are disabled so these functions can be
  629. // used from the main program or interrupt context,
  630. // even both in the same program!
  631. intr_state = SREG;
  632. cli();
  633. UENUM = DEBUG_TX_ENDPOINT;
  634. // if we gave up due to timeout before, don't wait again
  635. #if 0
  636. // this seems to be causig a lockup... why????
  637. if (previous_timeout) {
  638. if (!(UEINTX & (1<<RWAL))) {
  639. SREG = intr_state;
  640. return;
  641. }
  642. previous_timeout = 0;
  643. }
  644. #endif
  645. // wait for the FIFO to be ready to accept data
  646. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  647. while (1) {
  648. // are we ready to transmit?
  649. if (UEINTX & (1<<RWAL)) break;
  650. SREG = intr_state;
  651. // have we waited too long? This happens if the user
  652. // is not running an application that is listening
  653. if (UDFNUML == timeout) {
  654. //previous_timeout = 1;
  655. goto error;
  656. }
  657. // has the USB gone offline?
  658. if (!usb_configuration) goto error;
  659. // get ready to try checking again
  660. intr_state = SREG;
  661. cli();
  662. UENUM = DEBUG_TX_ENDPOINT;
  663. }
  664. // actually write the byte into the FIFO
  665. UEDATX = c;
  666. // if this completed a packet, transmit it now!
  667. if (!(UEINTX & (1<<RWAL))) {
  668. UEINTX = 0x3A;
  669. debug_flush_timer = 0;
  670. } else {
  671. debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  672. }
  673. SREG = intr_state;
  674. #if ARDUINO >= 100
  675. return 1;
  676. #endif
  677. error:
  678. #if ARDUINO >= 100
  679. setWriteError();
  680. return 0;
  681. #else
  682. return;
  683. #endif
  684. }
  685. // These are Teensy-specific extensions to the Serial object
  686. // immediately transmit any buffered output.
  687. // This doesn't actually transmit the data - that is impossible!
  688. // USB devices only transmit when the host allows, so the best
  689. // we can do is release the FIFO buffer for when the host wants it
  690. void usb_serial_class::send_now(void)
  691. {
  692. uint8_t intr_state;
  693. intr_state = SREG;
  694. cli();
  695. if (debug_flush_timer) {
  696. UENUM = DEBUG_TX_ENDPOINT;
  697. while ((UEINTX & (1<<RWAL))) {
  698. UEDATX = 0;
  699. }
  700. UEINTX = 0x3A;
  701. debug_flush_timer = 0;
  702. }
  703. SREG = intr_state;
  704. }
  705. uint32_t usb_serial_class::baud(void)
  706. {
  707. return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
  708. }
  709. uint8_t usb_serial_class::stopbits(void)
  710. {
  711. return 1;
  712. }
  713. uint8_t usb_serial_class::paritytype(void)
  714. {
  715. return 0;
  716. }
  717. uint8_t usb_serial_class::numbits(void)
  718. {
  719. return 8;
  720. }
  721. uint8_t usb_serial_class::dtr(void)
  722. {
  723. return 1;
  724. }
  725. uint8_t usb_serial_class::rts(void)
  726. {
  727. return 1;
  728. }
  729. usb_serial_class::operator bool()
  730. {
  731. if (usb_configuration) return true;
  732. return false;
  733. }
  734. // Preinstantiate Objects //////////////////////////////////////////////////////
  735. usb_serial_class Serial = usb_serial_class();
  736. usb_keyboard_class Keyboard = usb_keyboard_class();
  737. usb_mouse_class Mouse = usb_mouse_class();
  738. #if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  739. usb_joystick_class Joystick = usb_joystick_class();
  740. #endif