-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
540 lines (471 loc) · 15.5 KB
/
main.c
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
/* Name: main.c
* MIDI CONVERTER
*
* MOCO: Midi Output Converter
* MICO: Midi Input Converter
*
* (C) 2010 by morecat_lab
*/
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include "usbdrv.h"
#include "oddebug.h"
extern void parseUSBMidiMessage(uchar *, uchar);
extern uchar parseSerialMidiMessage(uchar);
#define setPortBit0(port, bit) *(port) &= ~_BV(bit);
#define setPortBit1(port, bit) *(port) |= _BV(bit);
#define flipPortBit(port, bit) (*(port) ^= _BV(bit))
#define setPortBit(port, bit, val) { \
if ((val) == 0) { setPortBit0((port), (bit)); } else \
{ setPortBit1((port), (bit)); } \
}
/* debugging LED */
#ifdef ARDUINO
#define LED_OFF { PORTB &= ~0x20; }
#define LED_ON { PORTB |= 0x20; }
#define FLIP_LED { PORTB ^= 0x20; }
#else
#define LED_OFF { PORTB &= ~0x01; }
#define LED_ON { PORTB |= 0x01; }
#define FLIP_LED { PORTB ^= 0x01; }
#endif
// #define RECEIVER
// #define TRANSMITTER
/*
PORT ASSIGN
PD0: RxD (Serial MIDI)
PD1: TxD (Serial MIDI)
PD2: USB D+
PD3: USB D-
PD5: LED Monitor
*/
#define HW_CDC_BULK_OUT_SIZE 8
#define HW_CDC_BULK_IN_SIZE 8
#define TRUE 1
#define FALSE 0
enum {
SEND_ENCAPSULATED_COMMAND = 0,
GET_ENCAPSULATED_RESPONSE,
SET_COMM_FEATURE,
GET_COMM_FEATURE,
CLEAR_COMM_FEATURE,
SET_LINE_CODING = 0x20,
GET_LINE_CODING,
SET_CONTROL_LINE_STATE,
SEND_BREAK
};
// This deviceDescrMIDI[] is based on
// http://www.usb.org/developers/devclass_docs/midi10.pdf
// Appendix B. Example: Simple MIDI Adapter (Informative)
// B.1 Device Descriptor
static PROGMEM char deviceDescrMIDI[] = { /* USB device descriptor */
18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
USBDESCR_DEVICE, /* descriptor type */
0x10, 0x01, /* USB version supported */
0, /* device class: defined at interface level */
0, /* subclass */
0, /* protocol */
8, /* max packet size */
USB_CFG_VENDOR_ID, /* 2 bytes */
USB_CFG_DEVICE_ID, /* 2 bytes */
USB_CFG_DEVICE_VERSION, /* 2 bytes */
1, /* manufacturer string index */
2, /* product string index */
0, /* serial number string index */
1, /* number of configurations */
};
// B.2 Configuration Descriptor
static PROGMEM char configDescrMIDI[] = { /* USB configuration descriptor */
9, /* sizeof(usbDescrConfig): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
101, 0, /* total length of data returned (including inlined descriptors) */
2, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
#if USB_CFG_IS_SELF_POWERED
(1 << 7) | USBATTR_SELFPOWER, /* attributes */
#else
(1 << 7), /* attributes */
#endif
USB_CFG_MAX_BUS_POWER / 2, /* max USB current in 2mA units */
// B.3 AudioControl Interface Descriptors
// The AudioControl interface describes the device structure (audio function topology)
// and is used to manipulate the Audio Controls. This device has no audio function
// incorporated. However, the AudioControl interface is mandatory and therefore both
// the standard AC interface descriptor and the classspecific AC interface descriptor
// must be present. The class-specific AC interface descriptor only contains the header
// descriptor.
// B.3.1 Standard AC Interface Descriptor
// The AudioControl interface has no dedicated endpoints associated with it. It uses the
// default pipe (endpoint 0) for all communication purposes. Class-specific AudioControl
// Requests are sent using the default pipe. There is no Status Interrupt endpoint provided.
/* descriptor follows inline: */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
0, /* index of this interface */
0, /* alternate setting for this interface */
0, /* endpoints excl 0: number of endpoint descriptors to follow */
1, /* */
1, /* */
0, /* */
0, /* string index for interface */
// B.3.2 Class-specific AC Interface Descriptor
// The Class-specific AC interface descriptor is always headed by a Header descriptor
// that contains general information about the AudioControl interface. It contains all
// the pointers needed to describe the Audio Interface Collection, associated with the
// described audio function. Only the Header descriptor is present in this device
// because it does not contain any audio functionality as such.
/* descriptor follows inline: */
9, /* sizeof(usbDescrCDC_HeaderFn): length of descriptor in bytes */
36, /* descriptor type */
1, /* header functional descriptor */
0x0, 0x01, /* bcdADC */
9, 0, /* wTotalLength */
1, /* */
1, /* */
// B.4 MIDIStreaming Interface Descriptors
// B.4.1 Standard MS Interface Descriptor
/* descriptor follows inline: */
9, /* length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
1, /* index of this interface */
0, /* alternate setting for this interface */
2, /* endpoints excl 0: number of endpoint descriptors to follow */
1, /* AUDIO */
3, /* MS */
0, /* unused */
0, /* string index for interface */
// B.4.2 Class-specific MS Interface Descriptor
/* descriptor follows inline: */
7, /* length of descriptor in bytes */
36, /* descriptor type */
1, /* header functional descriptor */
0x0, 0x01, /* bcdADC */
65, 0, /* wTotalLength */
// B.4.3 MIDI IN Jack Descriptor
/* descriptor follows inline: */
6, /* bLength */
36, /* descriptor type */
2, /* MIDI_IN_JACK desc subtype */
1, /* EMBEDDED bJackType */
1, /* bJackID */
0, /* iJack */
/* descriptor follows inline: */
6, /* bLength */
36, /* descriptor type */
2, /* MIDI_IN_JACK desc subtype */
2, /* EXTERNAL bJackType */
2, /* bJackID */
0, /* iJack */
//B.4.4 MIDI OUT Jack Descriptor
/* descriptor follows inline: */
9, /* length of descriptor in bytes */
36, /* descriptor type */
3, /* MIDI_OUT_JACK descriptor */
1, /* EMBEDDED bJackType */
3, /* bJackID */
1, /* No of input pins */
2, /* BaSourceID */
1, /* BaSourcePin */
0, /* iJack */
/* descriptor follows inline: */
9, /* bLength of descriptor in bytes */
36, /* bDescriptorType */
3, /* MIDI_OUT_JACK bDescriptorSubtype */
2, /* EXTERNAL bJackType */
4, /* bJackID */
1, /* bNrInputPins */
1, /* baSourceID (0) */
1, /* baSourcePin (0) */
0, /* iJack */
// B.5 Bulk OUT Endpoint Descriptors
//B.5.1 Standard Bulk OUT Endpoint Descriptor
/* descriptor follows inline: */
9, /* bLenght */
USBDESCR_ENDPOINT, /* bDescriptorType = endpoint */
0x1, /* bEndpointAddress OUT endpoint number 1 */
3, /* bmAttributes: 2:Bulk, 3:Interrupt endpoint */
8, 0, /* wMaxPacketSize */
10, /* bIntervall in ms */
0, /* bRefresh */
0, /* bSyncAddress */
// B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor
/* descriptor follows inline: */
5, /* bLength of descriptor in bytes */
37, /* bDescriptorType */
1, /* bDescriptorSubtype */
1, /* bNumEmbMIDIJack */
1, /* baAssocJackID (0) */
//B.6 Bulk IN Endpoint Descriptors
//B.6.1 Standard Bulk IN Endpoint Descriptor
/* descriptor follows inline: */
9, /* bLenght */
USBDESCR_ENDPOINT, /* bDescriptorType = endpoint */
0x81, /* bEndpointAddress IN endpoint number 1 */
3, /* bmAttributes: 2: Bulk, 3: Interrupt endpoint */
8, 0, /* wMaxPacketSize */
10, /* bIntervall in ms */
0, /* bRefresh */
0, /* bSyncAddress */
// B.6.2 Class-specific MS Bulk IN Endpoint Descriptor
/* descriptor follows inline: */
5, /* bLength of descriptor in bytes */
37, /* bDescriptorType */
1, /* bDescriptorSubtype */
1, /* bNumEmbMIDIJack (0) */
3, /* baAssocJackID (0) */
};
uchar usbFunctionDescriptor(usbRequest_t * rq) {
if (rq->wValue.bytes[1] == USBDESCR_DEVICE) {
usbMsgPtr = (uchar *) deviceDescrMIDI;
return sizeof(deviceDescrMIDI);
} else { /* must be config descriptor */
usbMsgPtr = (uchar *) configDescrMIDI;
return sizeof(configDescrMIDI);
}
}
/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */
static uchar sendEmptyFrame;
// static uchar modeBuffer[7];
uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (void *)data;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
/* Prepare bulk-in endpoint to respond to early termination */
if ((rq->bmRequestType & USBRQ_DIR_MASK) ==
USBRQ_DIR_HOST_TO_DEVICE)
sendEmptyFrame = 1;
}
return 0xff;
}
/*---------------------------------------------------------------------------*/
/* usbFunctionRead */
/*---------------------------------------------------------------------------*/
uchar usbFunctionRead( uchar *data, uchar len ) {
data[0] = 0;
data[1] = 0;
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;
data[6] = 0;
return 7;
}
/*---------------------------------------------------------------------------*/
/* usbFunctionWrite */
/*---------------------------------------------------------------------------*/
uchar usbFunctionWrite(uchar * data, uchar len) {
// setPortBit1(&PORTD, 5); // DEBUG LED
/* parseMidiMessage(data, len); */
return 1;
}
/*---------------------------------------------------------------------------*/
/* usbFunctionWriteOut */
/* */
/* this Function is called if a MIDI Out message (from PC) arrives. */
/* */
/*---------------------------------------------------------------------------*/
#ifdef RECEIVER
#define RX_SIZE (HW_CDC_BULK_IN_SIZE)
static uchar utxrdy = FALSE; /* USB Packet ready in utx_buf */
static uchar rx_buf[RX_SIZE]; /* tempory buffer */
static uchar utx_buf[RX_SIZE]; /* BULK_IN buffer */
#endif
#ifdef TRANSMITTER
#define TX_SIZE (HW_CDC_BULK_OUT_SIZE<<2)
#define TX_MASK (TX_SIZE-1)
static uchar uwptr = 0, irptr = 0;
static uchar tx_buf[TX_SIZE];
#endif
void usbFunctionWriteOut(uchar *data, uchar len ) {
#ifdef TRANSMITTER
parseUSBMidiMessage(data, len);
#endif
}
#ifdef TRANSMITTER
void parseUSBMidiMessage(uchar *data, uchar len) {
uchar cin = (*data) & 0x0f; /* CABLE NOを無視する */
uchar i;
#ifdef DEBUG
if (cin == 8) {
LED_OFF;
} else if (cin == 9) {
if (((*(data+3)) & 0x7f) == 0) {
LED_OFF;
} else {
LED_ON;
}
}
#else
FLIP_LED;
#endif
if (cin > 1) { /* ignore cin == 0 and cin == 1 */
for (i = 1 ; i < 4 ; i++) {
tx_buf[uwptr++] = *(data + i);
uwptr &= TX_MASK;
if (i == 1) {
if ((cin == 5) || /* single byte system common */
(cin == 15)) /* single byte */
break;
}
if (i == 2) {
if ((cin == 2) || /* two-byte system common */
(cin == 6) || /* system ex end with 2 bytes */
(cin == 12) || /* program change */
(cin == 13)) /* channel pressure */
break;
}
}
}
if (len > 4) {
parseUSBMidiMessage(data+4, len-4);
}
}
#endif
#ifdef RECEIVER
uchar parseSerialMidiMessage(uchar RxByte) {
static uchar PC = 0;
static uchar SysEx = FALSE;
static uchar stateTransTable[] = {
0, /* 0 dummy */
0, /* 1 dummy */
3, /* 2->3 NOTE OFF (3) */
2 | 0x80, /* 3->2 */
5, /* 4->5 NOTE ON (3) */
4 | 0x80, /* 5->4 */
7, /* 6->7 Polyphonic key pressure (3) */
6 | 0x80, /* 7->6 */
9, /* 8->9 Control Change (3) */
8 | 0x80, /* 8->9 */
10 | 0x80, /* 10->10 program change (2) */
0, /* dummy */
12 | 0x80, /* 12->12 Channel Pressure (2) */
0, /* 13 dummy */
15, /* 14->15 Pitch Bend (3) */
14 | 0x80 /* 15->14 */
};
FLIP_LED; // DEBUG LED
if(SysEx){ /* MIDI System Message */
if(RxByte == 0xf7){ /* MIDI_EndSysEx */
SysEx = FALSE;
}
return FALSE;
}
if (RxByte >= 0xF8){ /* Single Byte Message */
utx_buf[0] = 0x0f;
utx_buf[1] = RxByte;
utx_buf[2] = 0;
utx_buf[3] = 0;
return TRUE;
}
if(RxByte > 0x7F){ /* Channel message */
if(RxByte == 0xf0){ /* MIDI_StartSysEx */
SysEx = TRUE;
return FALSE;
}
PC = 0;
}
if (PC == 0) {
PC = (((RxByte >> 4) & 0x07) + 1) * 2;
// conversion
// 0x80 -> 2, 0x90 -> 4, 0xa0 -> 6, 0xb0 -> 8, 0xc0 -> 10, 0xd0 -> 12, 0xe0 -> 14
rx_buf[0] = RxByte >> 4;
rx_buf[1] = RxByte;
rx_buf[3] = 0;
} else {
uchar tt = stateTransTable[PC];
rx_buf[(PC & 1) + 2] = RxByte;
PC = tt & 0x0f;
if ((tt & 0x80) != 0) {
memcpy(utx_buf, rx_buf, 4);
return TRUE;
}
}
return FALSE;
}
#endif
#ifdef ARDUINO
#define PORTD_DDR (0x00)
#else
#define PORTD_DDR (0b00100010)
#endif
static void hardwareInit(void) {
uchar i, j;
/* activate pull-ups except on USB lines */
USB_CFG_IOPORT =
(uchar) ~ ((1 << USB_CFG_DMINUS_BIT) |
(1 << USB_CFG_DPLUS_BIT) | PORTD_DDR) ;
/* all pins input except USB (-> USB reset) */
#ifdef USB_CFG_PULLUP_IOPORT
/* use usbDeviceConnect()/usbDeviceDisconnect() if available */
USBDDR = 0 | PORTD_DDR; /* we do RESET by deactivating pullup */
usbDeviceDisconnect();
#else
USBDDR = (1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT) | PORTD_DDR ;
/* for output (LED, TxD) */
#endif
j = 0;
while (--j) { /* USB Reset by device only required on Watchdog Reset */
i = 0;
while (--i); /* delay >10ms for USB reset */
}
#ifdef USB_CFG_PULLUP_IOPORT
usbDeviceConnect();
#else
USBDDR = 0 | PORTD_DDR; /* remove USB reset condition */
#endif
/* set baud rate */
UBRRL = 31; /* 312500Hz at 16MHz clock */
/* */
UCSRB = (1<<RXEN) | (1<<TXEN);
/* DEBUGGING LED */
#ifdef ARDUINO
DDRB = 0x20;
#else
DDRB = 0x01;
#endif
PORTB = 0;
}
int main(void) {
/* WDTを作動させる */
wdt_enable(WDTO_1S);
odDebugInit();
hardwareInit();
usbInit();
sendEmptyFrame = 0;
sei();
PORTB = 1;
for(;;){
wdt_reset();
usbPoll();
#ifdef TRANSMITTER
/* send to Serial MIDI line */
if( (UCSRA & (1<<UDRE)) && uwptr!=irptr ) {
UDR = tx_buf[irptr++];
irptr &= TX_MASK;
}
#if USB_CFG_HAVE_FLOWCONTROL == 1
if( usbAllRequestsAreDisabled() &&
((uwptr-irptr)&TX_MASK)<(TX_SIZE-HW_CDC_BULK_OUT_SIZE) ) {
usbEnableAllRequests();
}
#endif
#endif
#ifdef RECEIVER
/* receive from Serial MIDI line */
if( UCSRA & (1<<RXC)) {
utxrdy |= parseSerialMidiMessage(UDR);
}
/* send packets to USB MIDI */
if( usbInterruptIsReady() && utxrdy ) {
usbSetInterrupt(utx_buf, 4);
utxrdy = FALSE;
}
#endif
}
}
/* EOF */