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