Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

usb_api.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. void usb_midi_class::sendSysEx_BufferHasTerm(uint16_t length, const uint8_t *data)
  31. {
  32. while (length > 3) {
  33. send_raw(0x04, data[0], data[1], data[2]);
  34. data += 3;
  35. length -= 3;
  36. }
  37. if (length == 3) {
  38. send_raw(0x07, data[0], data[1], data[2]);
  39. } else if (length == 2) {
  40. send_raw(0x06, data[0], data[1], 0);
  41. } else if (length == 1) {
  42. send_raw(0x05, data[0], 0, 0);
  43. }
  44. }
  45. void usb_midi_class::sendSysEx_AddTermBytes(uint16_t length, const uint8_t *data)
  46. {
  47. if (length == 0) {
  48. send_raw(0x06, 0xF0, 0xF7, 0);
  49. return;
  50. } else if (length == 1) {
  51. send_raw(0x07, 0xF0, data[0], 0xF7);
  52. return;
  53. } else {
  54. send_raw(0x04, 0xF0, data[0], data[1]);
  55. data += 2;
  56. length -= 2;
  57. }
  58. while (length >= 3) {
  59. send_raw(0x04, data[0], data[1], data[2]);
  60. data += 3;
  61. length -= 3;
  62. }
  63. if (length == 2) {
  64. send_raw(0x07, data[0], data[1], 0xF7);
  65. } else if (length == 1) {
  66. send_raw(0x06, data[0], 0xF7, 0);
  67. } else {
  68. send_raw(0x05, 0xF7, 0, 0);
  69. }
  70. }
  71. // This 4 byte input format is documented in the "Universal Serial Bus Device Class
  72. // Definition for MIDI Devices" specification, version 1.0, Nov 1, 1999. It can be
  73. // downloaded from www.usb.org. https://www.usb.org/sites/default/files/midi10.pdf
  74. // If the USB-IF reorganizes their website and this link no longer works, Google
  75. // search the name to find it. This data format is shown on page 16 in Figure #8.
  76. // Byte 0 (shown on the left hand side of Figure #8) is b0, Byte 1 is b1, etc.
  77. void usb_midi_class::send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
  78. {
  79. uint8_t intr_state, timeout;
  80. if (!usb_configuration) return;
  81. intr_state = SREG;
  82. cli();
  83. UENUM = MIDI_TX_ENDPOINT;
  84. timeout = UDFNUML + 2;
  85. while (1) {
  86. // are we ready to transmit?
  87. if (UEINTX & (1<<RWAL)) break;
  88. SREG = intr_state;
  89. if (UDFNUML == timeout) return;
  90. if (!usb_configuration) return;
  91. intr_state = SREG;
  92. cli();
  93. UENUM = MIDI_TX_ENDPOINT;
  94. }
  95. UEDATX = b0;
  96. UEDATX = b1;
  97. UEDATX = b2;
  98. UEDATX = b3;
  99. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  100. SREG = intr_state;
  101. }
  102. void usb_midi_class::send_now(void)
  103. {
  104. uint8_t intr_state;
  105. if (!usb_configuration) return;
  106. intr_state = SREG;
  107. cli();
  108. UENUM = MIDI_TX_ENDPOINT;
  109. if (UEBCLX != MIDI_TX_SIZE) {
  110. UEINTX = 0x3A;
  111. }
  112. SREG = intr_state;
  113. }
  114. // Convert 10 bit linear measurements to a logarthmic scale
  115. // suitable for sending as MIDI velocity numbers. The
  116. // "range" parameter should be probably be between 30 to 60,
  117. // with 36 probably a good default.
  118. //
  119. // This function uses fast 16 bit unsigned integer math. :-)
  120. //
  121. uint8_t usb_midi_class::analog2velocity(uint16_t val, uint8_t range)
  122. {
  123. #if 0
  124. if (val == 0) return 0;
  125. float scale = 1.0 + (20.0 / (float)range) * log10((float)val / 1023.0);
  126. if (scale < 0) return 0;
  127. return 127 * scale;
  128. #else
  129. uint8_t i, e, b;
  130. uint16_t s=0;
  131. static const uint8_t PROGMEM table[] = {225,124,65,34,17,9,4,2,1};
  132. if (val == 0) return 0;
  133. if (val >= 1023) return 127;
  134. for (e=0; (val & 512) == 0; e++) val <<= 1;
  135. for (i=0; i<9; i++) { // cordic algorithm
  136. uint16_t x = val + (val >> (i + 1));
  137. if (x < 1024) {
  138. val = x;
  139. s += pgm_read_byte(table + i);
  140. }
  141. }
  142. s += e * 385;
  143. s <<= 4;
  144. s += (range >> 1);
  145. s /= range;
  146. if (s >= 1024) return 0;
  147. s = 1024 - s;
  148. if (s > 511) {
  149. s -= 512;
  150. b = 64;
  151. } else if (s > 255) {
  152. s -= 256;
  153. b = 32;
  154. } else {
  155. b = 0;
  156. }
  157. return b + ((s * 127) >> 10);
  158. #endif
  159. }
  160. uint32_t usb_midi_class::midiusb_available()
  161. {
  162. uint8_t c, intr_state;
  163. intr_state = SREG;
  164. cli();
  165. if (!usb_configuration) {
  166. SREG = intr_state;
  167. return 0;
  168. }
  169. UENUM = MIDI_RX_ENDPOINT;
  170. retry:
  171. c = UEINTX;
  172. if (!(c & (1<<RWAL))) {
  173. if (c & (1<<RXOUTI)) {
  174. UEINTX = 0x6B;
  175. goto retry;
  176. }
  177. SREG = intr_state;
  178. return 0;
  179. }
  180. SREG = intr_state;
  181. return 4;
  182. }
  183. void usb_midi_class::midiusb_read(uint8_t *buf)
  184. {
  185. uint8_t c, intr_state;
  186. intr_state = SREG;
  187. cli();
  188. if (!usb_configuration) {
  189. SREG = intr_state;
  190. buf[0] = 0;
  191. buf[1] = 0;
  192. buf[2] = 0;
  193. buf[3] = 0;
  194. return;
  195. }
  196. UENUM = MIDI_RX_ENDPOINT;
  197. retry:
  198. c = UEINTX;
  199. if (!(c & (1<<RWAL))) {
  200. if (c & (1<<RXOUTI)) {
  201. UEINTX = 0x6B;
  202. goto retry;
  203. }
  204. SREG = intr_state;
  205. buf[0] = 0;
  206. buf[1] = 0;
  207. buf[2] = 0;
  208. buf[3] = 0;
  209. return;
  210. }
  211. buf[0] = UEDATX;
  212. buf[1] = UEDATX;
  213. buf[2] = UEDATX;
  214. buf[3] = UEDATX;
  215. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  216. SREG = intr_state;
  217. }
  218. bool usb_midi_class::read(uint8_t channel)
  219. {
  220. uint8_t c, intr_state;
  221. uint8_t b0, b1, b2, b3, type1, type2;
  222. intr_state = SREG;
  223. cli();
  224. if (!usb_configuration) {
  225. SREG = intr_state;
  226. return false;
  227. }
  228. UENUM = MIDI_RX_ENDPOINT;
  229. retry:
  230. c = UEINTX;
  231. if (!(c & (1<<RWAL))) {
  232. if (c & (1<<RXOUTI)) {
  233. UEINTX = 0x6B;
  234. goto retry;
  235. }
  236. SREG = intr_state;
  237. return false;
  238. }
  239. b0 = UEDATX;
  240. b1 = UEDATX;
  241. b2 = UEDATX;
  242. b3 = UEDATX;
  243. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  244. SREG = intr_state;
  245. type1 = b0 & 0x0F;
  246. type2 = b1 & 0xF0;
  247. c = (b1 & 0x0F) + 1;
  248. if (type1 >= 0x08 && type1 <= 0x0E) {
  249. if (channel && channel != c) {
  250. // ignore other channels when user wants single channel read
  251. return false;
  252. }
  253. if (type1 == 0x08 && type2 == 0x80) {
  254. msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  255. if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
  256. } else
  257. if (type1 == 0x09 && type2 == 0x90) {
  258. if (b3) {
  259. msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
  260. if (handleNoteOn) (*handleNoteOn)(c, b2, b3);
  261. } else {
  262. msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  263. if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
  264. }
  265. } else
  266. if (type1 == 0x0A && type2 == 0xA0) {
  267. msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
  268. if (handleVelocityChange) (*handleVelocityChange)(c, b2, b3);
  269. } else
  270. if (type1 == 0x0B && type2 == 0xB0) {
  271. msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
  272. if (handleControlChange) (*handleControlChange)(c, b2, b3);
  273. } else
  274. if (type1 == 0x0C && type2 == 0xC0) {
  275. msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
  276. if (handleProgramChange) (*handleProgramChange)(c, b2);
  277. } else
  278. if (type1 == 0x0D && type2 == 0xD0) {
  279. msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
  280. if (handleAfterTouch) (*handleAfterTouch)(c, b2);
  281. } else
  282. if (type1 == 0x0E && type2 == 0xE0) {
  283. msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
  284. if (handlePitchChange) {
  285. int value = (b2 & 0x7F) | ((int)(b3 & 0x7F) << 7);
  286. value -= 8192; // 0 to 16383 --> -8192 to +8191
  287. (*handlePitchChange)(c, value);
  288. }
  289. } else {
  290. return false;
  291. }
  292. return_message:
  293. // only update these when returning true for a parsed message
  294. // all other return cases will preserve these user-visible values
  295. msg_channel = c;
  296. msg_data1 = b2;
  297. msg_data2 = b3;
  298. return true;
  299. }
  300. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && type2 == 0x0F)) {
  301. // system common or system realtime message
  302. system_common_or_realtime:
  303. switch (b1) {
  304. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  305. if (handleTimeCodeQuarterFrame) {
  306. (*handleTimeCodeQuarterFrame)(b2);
  307. }
  308. break;
  309. case 0xF2: // usbMIDI.SongPosition
  310. if (handleSongPosition) {
  311. (*handleSongPosition)(
  312. (uint16_t)(b2 & 0x7F) | (uint16_t)(b3 & 0x7F) << 7);
  313. }
  314. break;
  315. case 0xF3: // usbMIDI.SongSelect
  316. if (handleSongSelect) {
  317. (*handleSongSelect)(b2);
  318. }
  319. break;
  320. case 0xF6: // usbMIDI.TuneRequest
  321. if (handleTuneRequest) {
  322. (*handleTuneRequest)();
  323. }
  324. break;
  325. case 0xF8: // usbMIDI.Clock
  326. if (handleClock) {
  327. (*handleClock)();
  328. } else if (handleRealTimeSystem) {
  329. (*handleRealTimeSystem)(0xF8);
  330. }
  331. break;
  332. case 0xFA: // usbMIDI.Start
  333. if (handleStart) {
  334. (*handleStart)();
  335. } else if (handleRealTimeSystem) {
  336. (*handleRealTimeSystem)(0xFA);
  337. }
  338. break;
  339. case 0xFB: // usbMIDI.Continue
  340. if (handleContinue) {
  341. (*handleContinue)();
  342. } else if (handleRealTimeSystem) {
  343. (*handleRealTimeSystem)(0xFB);
  344. }
  345. break;
  346. case 0xFC: // usbMIDI.Stop
  347. if (handleStop) {
  348. (*handleStop)();
  349. } else if (handleRealTimeSystem) {
  350. (*handleRealTimeSystem)(0xFC);
  351. }
  352. break;
  353. case 0xFE: // usbMIDI.ActiveSensing
  354. if (handleActiveSensing) {
  355. (*handleActiveSensing)();
  356. } else if (handleRealTimeSystem) {
  357. (*handleRealTimeSystem)(0xFE);
  358. }
  359. break;
  360. case 0xFF: // usbMIDI.SystemReset
  361. if (handleSystemReset) {
  362. (*handleSystemReset)();
  363. } else if (handleRealTimeSystem) {
  364. (*handleRealTimeSystem)(0xFF);
  365. }
  366. break;
  367. default:
  368. return false; // unknown message, ignore it
  369. }
  370. msg_type = b1;
  371. goto return_message;
  372. }
  373. if (type1 == 0x04) {
  374. read_sysex_byte(b1);
  375. read_sysex_byte(b2);
  376. read_sysex_byte(b3);
  377. return false;
  378. }
  379. if (type1 >= 0x05 && type1 <= 0x07) {
  380. read_sysex_byte(b1);
  381. if (type1 >= 0x06) read_sysex_byte(b2);
  382. if (type1 == 0x07) read_sysex_byte(b3);
  383. uint16_t len = msg_sysex_len;
  384. msg_data1 = len;
  385. msg_data2 = len >> 8;
  386. msg_sysex_len = 0;
  387. msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
  388. if (handleSysExPartial) {
  389. (*handleSysExPartial)(msg_sysex, len, 1);
  390. } else if (handleSysExComplete) {
  391. (*handleSysExComplete)(msg_sysex, len);
  392. }
  393. return true;
  394. }
  395. if (type1 == 0x0F) {
  396. if (b1 >= 0xF8) {
  397. // From Sebastian Tomczak, seb.tomczak at gmail.com
  398. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  399. goto system_common_or_realtime;
  400. }
  401. if (msg_sysex_len > 0) {
  402. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  403. // OSX sometimes uses Single Byte Unparsed to
  404. // send bytes in the middle of a SYSEX message.
  405. read_sysex_byte(b1);
  406. }
  407. }
  408. return false;
  409. }
  410. void usb_midi_class::read_sysex_byte(uint8_t b)
  411. {
  412. if (handleSysExPartial && msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
  413. // when buffer is full, send another chunk to partial handler.
  414. (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
  415. msg_sysex_len = 0;
  416. }
  417. if (msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  418. msg_sysex[msg_sysex_len++] = b;
  419. }
  420. }
  421. static volatile uint8_t prev_byte=0;
  422. void usb_serial_class::begin(long speed)
  423. {
  424. // make sure USB is initialized
  425. usb_init();
  426. uint16_t begin_wait = (uint16_t)millis();
  427. while (1) {
  428. if (usb_configuration) {
  429. delay(200); // a little time for host to load a driver
  430. return;
  431. }
  432. if (usb_suspended) {
  433. uint16_t begin_suspend = (uint16_t)millis();
  434. while (usb_suspended) {
  435. // must remain suspended for a while, because
  436. // normal USB enumeration causes brief suspend
  437. // states, typically under 0.1 second
  438. if ((uint16_t)millis() - begin_suspend > 250) {
  439. return;
  440. }
  441. }
  442. }
  443. // ... or a timout (powered by a USB power adaptor that
  444. // wiggles the data lines to keep a USB device charging)
  445. if ((uint16_t)millis() - begin_wait > 2500) return;
  446. }
  447. prev_byte = 0;
  448. }
  449. void usb_serial_class::end()
  450. {
  451. usb_shutdown();
  452. delay(25);
  453. }
  454. // number of bytes available in the receive buffer
  455. int usb_serial_class::available()
  456. {
  457. uint8_t c;
  458. c = prev_byte; // assume 1 byte static volatile access is atomic
  459. if (c) return 1;
  460. c = readnext();
  461. if (c) {
  462. prev_byte = c;
  463. return 1;
  464. }
  465. return 0;
  466. }
  467. // get the next character, or -1 if nothing received
  468. int usb_serial_class::read()
  469. {
  470. uint8_t c;
  471. c = prev_byte;
  472. if (c) {
  473. prev_byte = 0;
  474. return c;
  475. }
  476. c = readnext();
  477. if (c) return c;
  478. return -1;
  479. }
  480. int usb_serial_class::peek()
  481. {
  482. uint8_t c;
  483. c = prev_byte;
  484. if (c) return c;
  485. c = readnext();
  486. if (c) {
  487. prev_byte = c;
  488. return c;
  489. }
  490. return -1;
  491. }
  492. // get the next character, or 0 if nothing
  493. uint8_t usb_serial_class::readnext(void)
  494. {
  495. uint8_t c, intr_state;
  496. // interrupts are disabled so these functions can be
  497. // used from the main program or interrupt context,
  498. // even both in the same program!
  499. intr_state = SREG;
  500. cli();
  501. if (!usb_configuration) {
  502. SREG = intr_state;
  503. return 0;
  504. }
  505. UENUM = DEBUG_RX_ENDPOINT;
  506. try_again:
  507. if (!(UEINTX & (1<<RWAL))) {
  508. // no packet in buffer
  509. SREG = intr_state;
  510. return 0;
  511. }
  512. // take one byte out of the buffer
  513. c = UEDATX;
  514. if (c == 0) {
  515. // if we see a zero, discard it and
  516. // discard the rest of this packet
  517. UEINTX = 0x6B;
  518. goto try_again;
  519. }
  520. // if this drained the buffer, release it
  521. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  522. SREG = intr_state;
  523. return c;
  524. }
  525. // discard any buffered input
  526. void usb_serial_class::flush()
  527. {
  528. uint8_t intr_state;
  529. if (usb_configuration) {
  530. intr_state = SREG;
  531. cli();
  532. UENUM = DEBUG_RX_ENDPOINT;
  533. while ((UEINTX & (1<<RWAL))) {
  534. UEINTX = 0x6B;
  535. }
  536. SREG = intr_state;
  537. }
  538. prev_byte = 0;
  539. }
  540. // transmit a character.
  541. #if ARDUINO >= 100
  542. size_t usb_serial_class::write(uint8_t c)
  543. #else
  544. #define setWriteError()
  545. void usb_serial_class::write(uint8_t c)
  546. #endif
  547. {
  548. //static uint8_t previous_timeout=0;
  549. uint8_t timeout, intr_state;
  550. // if we're not online (enumerated and configured), error
  551. if (!usb_configuration) goto error;
  552. // interrupts are disabled so these functions can be
  553. // used from the main program or interrupt context,
  554. // even both in the same program!
  555. intr_state = SREG;
  556. cli();
  557. UENUM = DEBUG_TX_ENDPOINT;
  558. // if we gave up due to timeout before, don't wait again
  559. #if 0
  560. // this seems to be causig a lockup... why????
  561. if (previous_timeout) {
  562. if (!(UEINTX & (1<<RWAL))) {
  563. SREG = intr_state;
  564. return;
  565. }
  566. previous_timeout = 0;
  567. }
  568. #endif
  569. // wait for the FIFO to be ready to accept data
  570. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  571. while (1) {
  572. // are we ready to transmit?
  573. if (UEINTX & (1<<RWAL)) break;
  574. SREG = intr_state;
  575. // have we waited too long? This happens if the user
  576. // is not running an application that is listening
  577. if (UDFNUML == timeout) {
  578. //previous_timeout = 1;
  579. goto error;
  580. }
  581. // has the USB gone offline?
  582. if (!usb_configuration) goto error;
  583. // get ready to try checking again
  584. intr_state = SREG;
  585. cli();
  586. UENUM = DEBUG_TX_ENDPOINT;
  587. }
  588. // actually write the byte into the FIFO
  589. UEDATX = c;
  590. // if this completed a packet, transmit it now!
  591. if (!(UEINTX & (1<<RWAL))) {
  592. UEINTX = 0x3A;
  593. debug_flush_timer = 0;
  594. } else {
  595. debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  596. }
  597. SREG = intr_state;
  598. #if ARDUINO >= 100
  599. return 1;
  600. #endif
  601. error:
  602. #if ARDUINO >= 100
  603. setWriteError();
  604. return 0;
  605. #else
  606. return;
  607. #endif
  608. }
  609. // These are Teensy-specific extensions to the Serial object
  610. // immediately transmit any buffered output.
  611. // This doesn't actually transmit the data - that is impossible!
  612. // USB devices only transmit when the host allows, so the best
  613. // we can do is release the FIFO buffer for when the host wants it
  614. void usb_serial_class::send_now(void)
  615. {
  616. uint8_t intr_state;
  617. intr_state = SREG;
  618. cli();
  619. if (debug_flush_timer) {
  620. UENUM = DEBUG_TX_ENDPOINT;
  621. while ((UEINTX & (1<<RWAL))) {
  622. UEDATX = 0;
  623. }
  624. UEINTX = 0x3A;
  625. debug_flush_timer = 0;
  626. }
  627. SREG = intr_state;
  628. }
  629. uint32_t usb_serial_class::baud(void)
  630. {
  631. return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
  632. }
  633. uint8_t usb_serial_class::stopbits(void)
  634. {
  635. return 1;
  636. }
  637. uint8_t usb_serial_class::paritytype(void)
  638. {
  639. return 0;
  640. }
  641. uint8_t usb_serial_class::numbits(void)
  642. {
  643. return 8;
  644. }
  645. uint8_t usb_serial_class::dtr(void)
  646. {
  647. return 1;
  648. }
  649. uint8_t usb_serial_class::rts(void)
  650. {
  651. return 1;
  652. }
  653. usb_serial_class::operator bool()
  654. {
  655. if (usb_configuration) return true;
  656. return false;
  657. }
  658. // Preinstantiate Objects //////////////////////////////////////////////////////
  659. usb_serial_class Serial = usb_serial_class();
  660. usb_midi_class usbMIDI = usb_midi_class();