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.

928 lines
23KB

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