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.

580 lines
14KB

  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::sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel)
  31. {
  32. send_raw(0x08, 0x80 | ((channel - 1) & 0x0F), note & 0x7F, velocity & 0x7F);
  33. }
  34. void usb_midi_class::sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel)
  35. {
  36. send_raw(0x09, 0x90 | ((channel - 1) & 0x0F), note & 0x7F, velocity & 0x7F);
  37. }
  38. void usb_midi_class::sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel)
  39. {
  40. send_raw(0x0A, 0xA0 | ((channel - 1) & 0x0F), note & 0x7F, pressure & 0x7F);
  41. }
  42. void usb_midi_class::sendControlChange(uint8_t control, uint8_t value, uint8_t channel)
  43. {
  44. send_raw(0x0B, 0xB0 | ((channel - 1) & 0x0F), control & 0x7F, value & 0x7F);
  45. }
  46. void usb_midi_class::sendProgramChange(uint8_t program, uint8_t channel)
  47. {
  48. send_raw(0x0C, 0xC0 | ((channel - 1) & 0x0F), program & 0x7F, 0);
  49. }
  50. void usb_midi_class::sendAfterTouch(uint8_t pressure, uint8_t channel)
  51. {
  52. send_raw(0x0D, 0xD0 | ((channel - 1) & 0x0F), pressure & 0x7F, 0);
  53. }
  54. void usb_midi_class::sendPitchBend(uint16_t value, uint8_t channel)
  55. {
  56. send_raw(0x0E, 0xE0 | ((channel - 1) & 0x0F), value & 0x7F, (value >> 7) & 0x7F);
  57. }
  58. void usb_midi_class::sendSysEx(uint8_t length, const uint8_t *data)
  59. {
  60. // TODO: MIDI 2.5 lib automatically adds start and stop bytes
  61. while (length > 3) {
  62. send_raw(0x04, data[0], data[1], data[2]);
  63. data += 3;
  64. length -= 3;
  65. }
  66. if (length == 3) {
  67. send_raw(0x07, data[0], data[1], data[2]);
  68. } else if (length == 2) {
  69. send_raw(0x06, data[0], data[1], 0);
  70. } else if (length == 1) {
  71. send_raw(0x05, data[0], 0, 0);
  72. }
  73. }
  74. void usb_midi_class::send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
  75. {
  76. uint8_t intr_state, timeout;
  77. if (!usb_configuration) return;
  78. intr_state = SREG;
  79. cli();
  80. UENUM = MIDI_TX_ENDPOINT;
  81. timeout = UDFNUML + 2;
  82. while (1) {
  83. // are we ready to transmit?
  84. if (UEINTX & (1<<RWAL)) break;
  85. SREG = intr_state;
  86. if (UDFNUML == timeout) return;
  87. if (!usb_configuration) return;
  88. intr_state = SREG;
  89. cli();
  90. UENUM = MIDI_TX_ENDPOINT;
  91. }
  92. UEDATX = b0;
  93. UEDATX = b1;
  94. UEDATX = b2;
  95. UEDATX = b3;
  96. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  97. SREG = intr_state;
  98. }
  99. void usb_midi_class::send_now(void)
  100. {
  101. uint8_t intr_state;
  102. if (!usb_configuration) return;
  103. intr_state = SREG;
  104. cli();
  105. UENUM = MIDI_TX_ENDPOINT;
  106. if (UEBCLX != MIDI_TX_SIZE) {
  107. UEINTX = 0x3A;
  108. }
  109. SREG = intr_state;
  110. }
  111. // Convert 10 bit linear measurements to a logarthmic scale
  112. // suitable for sending as MIDI velocity numbers. The
  113. // "range" parameter should be probably be between 30 to 60,
  114. // with 36 probably a good default.
  115. //
  116. // This function uses fast 16 bit unsigned integer math. :-)
  117. //
  118. uint8_t usb_midi_class::analog2velocity(uint16_t val, uint8_t range)
  119. {
  120. #if 0
  121. if (val == 0) return 0;
  122. float scale = 1.0 + (20.0 / (float)range) * log10((float)val / 1023.0);
  123. if (scale < 0) return 0;
  124. return 127 * scale;
  125. #else
  126. uint8_t i, e, b;
  127. uint16_t s=0;
  128. static const prog_uint8_t table[] = {225,124,65,34,17,9,4,2,1};
  129. if (val == 0) return 0;
  130. if (val >= 1023) return 127;
  131. for (e=0; (val & 512) == 0; e++) val <<= 1;
  132. for (i=0; i<9; i++) { // cordic algorithm
  133. uint16_t x = val + (val >> (i + 1));
  134. if (x < 1024) {
  135. val = x;
  136. s += pgm_read_byte(table + i);
  137. }
  138. }
  139. s += e * 385;
  140. s <<= 4;
  141. s += (range >> 1);
  142. s /= range;
  143. if (s >= 1024) return 0;
  144. s = 1024 - s;
  145. if (s > 511) {
  146. s -= 512;
  147. b = 64;
  148. } else if (s > 255) {
  149. s -= 256;
  150. b = 32;
  151. } else {
  152. b = 0;
  153. }
  154. return b + ((s * 127) >> 10);
  155. #endif
  156. }
  157. bool usb_midi_class::read(uint8_t channel)
  158. {
  159. uint8_t c, intr_state;
  160. uint8_t b0, b1, b2, b3, type1, type2;
  161. intr_state = SREG;
  162. cli();
  163. if (!usb_configuration) {
  164. SREG = intr_state;
  165. return false;
  166. }
  167. UENUM = MIDI_RX_ENDPOINT;
  168. retry:
  169. c = UEINTX;
  170. if (!(c & (1<<RWAL))) {
  171. if (c & (1<<RXOUTI)) {
  172. UEINTX = 0x6B;
  173. goto retry;
  174. }
  175. SREG = intr_state;
  176. return false;
  177. }
  178. b0 = UEDATX;
  179. b1 = UEDATX;
  180. b2 = UEDATX;
  181. b3 = UEDATX;
  182. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  183. SREG = intr_state;
  184. type1 = b0 & 0x0F;
  185. type2 = b1 & 0xF0;
  186. c = (b1 & 0x0F) + 1;
  187. if (type1 >= 0x08 && type1 <= 0x0E) {
  188. if (channel && channel != c) {
  189. // ignore other channels when user wants single channel read
  190. return false;
  191. }
  192. if (type1 == 0x08 && type2 == 0x80) {
  193. msg_type = 0; // Note off
  194. if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
  195. goto return_message;
  196. }
  197. if (type1 == 0x09 && type2 == 0x90) {
  198. if (b3) {
  199. msg_type = 1; // Note on
  200. if (handleNoteOn) (*handleNoteOn)(c, b2, b3);
  201. } else {
  202. msg_type = 0; // Note off
  203. if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
  204. }
  205. goto return_message;
  206. }
  207. if (type1 == 0x0A && type2 == 0xA0) {
  208. msg_type = 2; // Poly Pressure
  209. if (handleVelocityChange) (*handleVelocityChange)(c, b2, b3);
  210. goto return_message;
  211. }
  212. if (type1 == 0x0B && type2 == 0xB0) {
  213. msg_type = 3; // Control Change
  214. if (handleControlChange) (*handleControlChange)(c, b2, b3);
  215. goto return_message;
  216. }
  217. if (type1 == 0x0C && type2 == 0xC0) {
  218. msg_type = 4; // Program Change
  219. if (handleProgramChange) (*handleProgramChange)(c, b2);
  220. goto return_message;
  221. }
  222. if (type1 == 0x0D && type2 == 0xD0) {
  223. msg_type = 5; // After Touch
  224. if (handleAfterTouch) (*handleAfterTouch)(c, b2);
  225. goto return_message;
  226. }
  227. if (type1 == 0x0E && type2 == 0xE0) {
  228. msg_type = 6; // Pitch Bend
  229. if (handlePitchChange) (*handlePitchChange)(c,
  230. (b2 & 0x7F) | ((b3 & 0x7F) << 7));
  231. goto return_message;
  232. }
  233. return false;
  234. return_message:
  235. // only update these when returning true for a parsed message
  236. // all other return cases will preserve these user-visible values
  237. msg_channel = c;
  238. msg_data1 = b2;
  239. msg_data2 = b3;
  240. return true;
  241. }
  242. if (type1 == 0x04) {
  243. read_sysex_byte(b1);
  244. read_sysex_byte(b2);
  245. read_sysex_byte(b3);
  246. return false;
  247. }
  248. if (type1 >= 0x05 && type1 <= 0x07) {
  249. read_sysex_byte(b1);
  250. if (type1 >= 0x06) read_sysex_byte(b2);
  251. if (type1 == 0x07) read_sysex_byte(b3);
  252. msg_data1 = msg_sysex_len;
  253. msg_sysex_len = 0;
  254. msg_type = 7;
  255. return true;
  256. }
  257. if (type1 == 0x0F) {
  258. // TODO: does this need to be a full MIDI parser?
  259. // What software actually uses this message type in practice?
  260. if (msg_sysex_len > 0) {
  261. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  262. // OSX sometimes uses Single Byte Unparsed to
  263. // send bytes in the middle of a SYSEX message.
  264. read_sysex_byte(b1);
  265. } else {
  266. // From Sebastian Tomczak, seb.tomczak at gmail.com
  267. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  268. msg_type = 8;
  269. if (handleRealTimeSystem) (*handleRealTimeSystem)(b1);
  270. goto return_message;
  271. }
  272. }
  273. return false;
  274. }
  275. void usb_midi_class::read_sysex_byte(uint8_t b)
  276. {
  277. if (msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  278. msg_sysex[msg_sysex_len++] = b;
  279. }
  280. }
  281. static volatile uint8_t prev_byte=0;
  282. void usb_serial_class::begin(long speed)
  283. {
  284. // make sure USB is initialized
  285. usb_init();
  286. uint16_t begin_wait = (uint16_t)millis();
  287. while (1) {
  288. if (usb_configuration) {
  289. delay(200); // a little time for host to load a driver
  290. return;
  291. }
  292. if (usb_suspended) {
  293. uint16_t begin_suspend = (uint16_t)millis();
  294. while (usb_suspended) {
  295. // must remain suspended for a while, because
  296. // normal USB enumeration causes brief suspend
  297. // states, typically under 0.1 second
  298. if ((uint16_t)millis() - begin_suspend > 250) {
  299. return;
  300. }
  301. }
  302. }
  303. // ... or a timout (powered by a USB power adaptor that
  304. // wiggles the data lines to keep a USB device charging)
  305. if ((uint16_t)millis() - begin_wait > 2500) return;
  306. }
  307. prev_byte = 0;
  308. }
  309. void usb_serial_class::end()
  310. {
  311. usb_shutdown();
  312. delay(25);
  313. }
  314. // number of bytes available in the receive buffer
  315. int usb_serial_class::available()
  316. {
  317. uint8_t c;
  318. c = prev_byte; // assume 1 byte static volatile access is atomic
  319. if (c) return 1;
  320. c = readnext();
  321. if (c) {
  322. prev_byte = c;
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. // get the next character, or -1 if nothing received
  328. int usb_serial_class::read()
  329. {
  330. uint8_t c;
  331. c = prev_byte;
  332. if (c) {
  333. prev_byte = 0;
  334. return c;
  335. }
  336. c = readnext();
  337. if (c) return c;
  338. return -1;
  339. }
  340. int usb_serial_class::peek()
  341. {
  342. uint8_t c;
  343. c = prev_byte;
  344. if (c) return c;
  345. c = readnext();
  346. if (c) {
  347. prev_byte = c;
  348. return c;
  349. }
  350. return -1;
  351. }
  352. // get the next character, or 0 if nothing
  353. uint8_t usb_serial_class::readnext(void)
  354. {
  355. uint8_t c, intr_state;
  356. // interrupts are disabled so these functions can be
  357. // used from the main program or interrupt context,
  358. // even both in the same program!
  359. intr_state = SREG;
  360. cli();
  361. if (!usb_configuration) {
  362. SREG = intr_state;
  363. return 0;
  364. }
  365. UENUM = DEBUG_RX_ENDPOINT;
  366. try_again:
  367. if (!(UEINTX & (1<<RWAL))) {
  368. // no packet in buffer
  369. SREG = intr_state;
  370. return 0;
  371. }
  372. // take one byte out of the buffer
  373. c = UEDATX;
  374. if (c == 0) {
  375. // if we see a zero, discard it and
  376. // discard the rest of this packet
  377. UEINTX = 0x6B;
  378. goto try_again;
  379. }
  380. // if this drained the buffer, release it
  381. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  382. SREG = intr_state;
  383. return c;
  384. }
  385. // discard any buffered input
  386. void usb_serial_class::flush()
  387. {
  388. uint8_t intr_state;
  389. if (usb_configuration) {
  390. intr_state = SREG;
  391. cli();
  392. UENUM = DEBUG_RX_ENDPOINT;
  393. while ((UEINTX & (1<<RWAL))) {
  394. UEINTX = 0x6B;
  395. }
  396. SREG = intr_state;
  397. }
  398. prev_byte = 0;
  399. }
  400. // transmit a character.
  401. #if ARDUINO >= 100
  402. size_t usb_serial_class::write(uint8_t c)
  403. #else
  404. #define setWriteError()
  405. void usb_serial_class::write(uint8_t c)
  406. #endif
  407. {
  408. //static uint8_t previous_timeout=0;
  409. uint8_t timeout, intr_state;
  410. // if we're not online (enumerated and configured), error
  411. if (!usb_configuration) goto error;
  412. // interrupts are disabled so these functions can be
  413. // used from the main program or interrupt context,
  414. // even both in the same program!
  415. intr_state = SREG;
  416. cli();
  417. UENUM = DEBUG_TX_ENDPOINT;
  418. // if we gave up due to timeout before, don't wait again
  419. #if 0
  420. // this seems to be causig a lockup... why????
  421. if (previous_timeout) {
  422. if (!(UEINTX & (1<<RWAL))) {
  423. SREG = intr_state;
  424. return;
  425. }
  426. previous_timeout = 0;
  427. }
  428. #endif
  429. // wait for the FIFO to be ready to accept data
  430. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  431. while (1) {
  432. // are we ready to transmit?
  433. if (UEINTX & (1<<RWAL)) break;
  434. SREG = intr_state;
  435. // have we waited too long? This happens if the user
  436. // is not running an application that is listening
  437. if (UDFNUML == timeout) {
  438. //previous_timeout = 1;
  439. goto error;
  440. }
  441. // has the USB gone offline?
  442. if (!usb_configuration) goto error;
  443. // get ready to try checking again
  444. intr_state = SREG;
  445. cli();
  446. UENUM = DEBUG_TX_ENDPOINT;
  447. }
  448. // actually write the byte into the FIFO
  449. UEDATX = c;
  450. // if this completed a packet, transmit it now!
  451. if (!(UEINTX & (1<<RWAL))) {
  452. UEINTX = 0x3A;
  453. debug_flush_timer = 0;
  454. } else {
  455. debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  456. }
  457. SREG = intr_state;
  458. #if ARDUINO >= 100
  459. return 1;
  460. #endif
  461. error:
  462. #if ARDUINO >= 100
  463. setWriteError();
  464. return 0;
  465. #else
  466. return;
  467. #endif
  468. }
  469. // These are Teensy-specific extensions to the Serial object
  470. // immediately transmit any buffered output.
  471. // This doesn't actually transmit the data - that is impossible!
  472. // USB devices only transmit when the host allows, so the best
  473. // we can do is release the FIFO buffer for when the host wants it
  474. void usb_serial_class::send_now(void)
  475. {
  476. uint8_t intr_state;
  477. intr_state = SREG;
  478. cli();
  479. if (debug_flush_timer) {
  480. UENUM = DEBUG_TX_ENDPOINT;
  481. while ((UEINTX & (1<<RWAL))) {
  482. UEDATX = 0;
  483. }
  484. UEINTX = 0x3A;
  485. debug_flush_timer = 0;
  486. }
  487. SREG = intr_state;
  488. }
  489. uint32_t usb_serial_class::baud(void)
  490. {
  491. return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
  492. }
  493. uint8_t usb_serial_class::stopbits(void)
  494. {
  495. return 1;
  496. }
  497. uint8_t usb_serial_class::paritytype(void)
  498. {
  499. return 0;
  500. }
  501. uint8_t usb_serial_class::numbits(void)
  502. {
  503. return 8;
  504. }
  505. uint8_t usb_serial_class::dtr(void)
  506. {
  507. return 1;
  508. }
  509. uint8_t usb_serial_class::rts(void)
  510. {
  511. return 1;
  512. }
  513. usb_serial_class::operator bool()
  514. {
  515. if (usb_configuration) return true;
  516. return false;
  517. }
  518. // Preinstantiate Objects //////////////////////////////////////////////////////
  519. usb_serial_class Serial = usb_serial_class();
  520. usb_midi_class usbMIDI = usb_midi_class();