Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

703 lines
16KB

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