summaryrefslogtreecommitdiff
path: root/fw/midi-dials/Src/usbd_midi_if.c
blob: bbcbf974527ed8612a844411b177099b08a122bf (plain)
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
/**
  ******************************************************************************
  * @file           : usbd_midi_if.c
  * @brief          :
  ******************************************************************************

    (CC at)2016 by D.F.Mac. @TripArts Music

  ******************************************************************************

    Modified by keshikan (www.keshikan.net) 2018
    The license is (CC BY 4.0), and takes over from original usbd_midi_if.h/c.

    See also original source code page.
    https://github.com/mimuz/mimuz-tuch/blob/master/STM32/

  ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "usbd_midi_if.h"
#include "stm32f0xx_hal.h"


// basic midi rx/tx functions
static uint16_t MIDI_DataRx(uint8_t *msg, uint16_t length);
static uint16_t MIDI_DataTx(uint8_t *msg, uint16_t length);

// for Cure Series
#define MIDI_BUFFER_SIZ (512)//FIFO buffer byte size for midi message buffer

RingBufferU8 rbuf_usb_rx[MIDI_OUT_JACK_NUM]; //for input from USB
RingBufferU8 rbuf_jack_rx[MIDI_IN_JACK_NUM];  //for input from MIDI-IN jack

//for receiving midi data from jack
MidiAnalysisStatus analyzed_status[MIDI_IN_JACK_NUM];
MIDIEvent midi_event[MIDI_IN_JACK_NUM];	//received midi data

uint8_t rx_midi_msg[MIDI_IN_JACK_NUM];


FUNC_STATUS midiInit()
{
	uint32_t i,j;

	for(i=0; i<MIDI_OUT_JACK_NUM; i++){
		if(BUFFER_SUCCESS != cureRingBufferU8Init(&rbuf_usb_rx[i], MIDI_BUFFER_SIZ))
		{
			return FUNC_ERROR;
		}
	}

	for(i=0; i<MIDI_IN_JACK_NUM; i++){
		if(BUFFER_SUCCESS != cureRingBufferU8Init(&rbuf_jack_rx[i], MIDI_BUFFER_SIZ))
		{
			return FUNC_ERROR;
		}
	}

	//Init RX
	for(i=0; i<MIDI_IN_JACK_NUM; i++){

		rx_midi_msg[i] = 0x00;
		analyzed_status[i].data_idx = 0;
		midi_event[i].length = 0;

		for(j=0; j<MIDI_SENDDATA_MAX; j++){
			midi_event[i].midi_byte[j] = 0x00;
		}
	}

	return FUNC_SUCCESS;
}

FUNC_STATUS midiGetFromUsbRx(uint8_t cable_num, uint8_t* dat)
{
	if(BUFFER_SUCCESS != cureRingBufferU8Dequeue(&rbuf_usb_rx[cable_num], dat))
	{
		return FUNC_ERROR;
	}

	return FUNC_SUCCESS;
}

FUNC_STATUS midiGetFromJackRx(uint8_t cable_num)
{
	if(BUFFER_SUCCESS != cureRingBufferU8Dequeue(&rbuf_jack_rx[cable_num], &rx_midi_msg[cable_num]))
	{
		return FUNC_ERROR;
	}

	return FUNC_SUCCESS;
}

FUNC_STATUS midiSetFromJackRx(uint8_t cable_num, uint8_t* dat)
{
	if(BUFFER_SUCCESS != cureRingBufferU8Enqueue(&rbuf_jack_rx[cable_num], dat))
	{
		return FUNC_ERROR;
	}

	return FUNC_SUCCESS;
}


bool isUsbRxBufEmpty(uint8_t cable_num)
{
	if( 0 != _cureRingBufferU8GetUsedSize(&rbuf_usb_rx[cable_num]) ){
		return false;
	}

	return true;
}

bool isJackRxBufEmpty(uint8_t cable_num)
{
	if( 0 != _cureRingBufferU8GetUsedSize(&rbuf_jack_rx[cable_num]) ){
		return false;
	}

	return true;
}

bool isRxBufEmpty()
{
	uint32_t i;

	for(i=0; i<MIDI_OUT_JACK_NUM; i++){
		if(0 != _cureRingBufferU8GetUsedSize(&rbuf_usb_rx[i]))
		{
			return false;
		}
	}

	for(i=0; i<MIDI_IN_JACK_NUM; i++){
		if(0 != _cureRingBufferU8GetUsedSize(&rbuf_jack_rx[i]))
		{
			return false;
		}
	}
	return true;
}


USBD_MIDI_ItfTypeDef USBD_Interface_fops_FS =
{
  MIDI_DataRx,
  MIDI_DataTx
};


static uint16_t MIDI_DataRx(uint8_t *msg, uint16_t length){
  uint16_t cnt;
  uint16_t msgs = length / 4;
  uint16_t chk = length % 4;
  uint8_t u8b;
  uint8_t midi_size;

  if(0 != chk)
  {
	  return 0;
  }

  for(uint32_t cnt_msgs = 0; cnt_msgs < msgs; cnt_msgs++){

	  uint8_t cable_num = (msg[0 + 4*cnt_msgs] & 0xF0) >> 4;
	  uint8_t code_idx_num = msg[0 + 4*cnt_msgs] & 0x0F;

	  switch (code_idx_num) {

	  	  //not defined
		  case 0x0:
		  case 0x1:
			  midi_size = 0;
			  break;

		  //1byte message
		  case 0x5:
		  case 0xF:
			  midi_size = 1;
			  break;

		  //2byte message
		  case 0x2:
		  case 0x6:
		  case 0xC:
		  case 0xD:
			  midi_size = 2;
			  break;

		  //3byte message
		  case 0x3:
		  case 0x4:
		  case 0x7:
		  case 0x8:
		  case 0x9:
		  case 0xA:
		  case 0xB:
		  case 0xE:
			  midi_size = 3;
			  break;

		  default:
			  midi_size = 0;
			  break;
	  }

	  for(cnt = 0;cnt < midi_size;cnt ++){
		  u8b = *(msg + 4*cnt_msgs + cnt + 1);
		  cureRingBufferU8Enqueue(&rbuf_usb_rx[cable_num], &u8b);
	  }
  }

  return 0;
}

void sendMidiMessage(uint8_t *msg, uint16_t size){
  if(size == 4){
//	APP_Rx_Buffer[0] = msg[0];
//	APP_Rx_Buffer[1] = msg[1];
//	APP_Rx_Buffer[2] = msg[2];
//	APP_Rx_Buffer[3] = msg[3];
//    USBD_MIDI_SendData(&hUsbDeviceFS, APP_Rx_Buffer, size);
    MIDI_DataTx(msg, size);
  }
}

static uint16_t MIDI_DataTx(uint8_t *msg, uint16_t length){
  uint32_t i = 0;
  while (i < length) {
    APP_Rx_Buffer[APP_Rx_ptr_in] = *(msg + i);
    APP_Rx_ptr_in++;
    i++;
    if (APP_Rx_ptr_in == APP_RX_DATA_SIZE) {
      APP_Rx_ptr_in = 0;
    }
  }
  return USBD_OK;
}

bool midiEventIsGenerated(uint8_t cable_num)
{
	uint8_t upper_half_byte= (rx_midi_msg[cable_num]) & 0xF0;

	if( upper_half_byte & 0x80 ){//0x80-0xFF:status byte

		switch(upper_half_byte){

			case 0xF0://0xF0-0xFF:system message
				switch(rx_midi_msg[cable_num]){

					case 0xF0://SysEx Start
						analyzed_status[cable_num].data_idx = 0;
						midi_event[cable_num].midi_byte[ analyzed_status[cable_num].data_idx++ ] = rx_midi_msg[cable_num];
						analyzed_status[cable_num].type = MSG_SYSEX;
						analyzed_status[cable_num].stat = WAIT_SYSTEM_DATA;
						break;

					case 0xF7://SysEx End
						midi_event[cable_num].midi_byte[ analyzed_status[cable_num].data_idx++ ] = rx_midi_msg[cable_num];
						midi_event[cable_num].length = analyzed_status[cable_num].data_idx;
						analyzed_status[cable_num].stat = END_ANALYSIS;
						break;

					case 0xF2://Song Position
						midi_event[cable_num].midi_byte[0] = rx_midi_msg[cable_num];
						analyzed_status[cable_num].type = MSG_THREE_BYTE;
						analyzed_status[cable_num].stat = WAIT_DATA1;
						break;

					case 0xF1://Time Code
					case 0xF3://Song Select
						midi_event[cable_num].midi_byte[0] = rx_midi_msg[cable_num];
						analyzed_status[cable_num].type = MSG_TWO_BYTE;
						analyzed_status[cable_num].stat = WAIT_DATA1;
						break;

					case 0xF4://Undefined
					case 0xF5://Undefined
					case 0xF6://Tune request
					case 0xF8://Timing clock
					case 0xF9://Undefined
					case 0xFA://Start
					case 0xFB://Continue
					case 0xFC://Stop
					case 0xFD://Undefined
					case 0xFE://Active Sensing
					case 0xFF://Reset
						midi_event[cable_num].midi_byte[0] = rx_midi_msg[cable_num];
						midi_event[cable_num].length = 1;
						analyzed_status[cable_num].type = MSG_ONE_BYTE;
						analyzed_status[cable_num].stat = END_ANALYSIS;
						break;
				}
				analyzed_status[cable_num].is_system_common = true;
				break;

			case 0x80://Note Off
			case 0x90://Note On
			case 0xA0://Polyphonic key-pressure
			case 0xB0://ControlChange
			case 0xE0://PitchBend
				midi_event[cable_num].midi_byte[0] = rx_midi_msg[cable_num];
				analyzed_status[cable_num].type = MSG_THREE_BYTE;
				analyzed_status[cable_num].stat = WAIT_DATA1;
				analyzed_status[cable_num].is_system_common = false;
				break;

			case 0xC0://Program Change
			case 0xD0://Channel pressure
				midi_event[cable_num].midi_byte[0] = rx_midi_msg[cable_num];
				analyzed_status[cable_num].type = MSG_TWO_BYTE;
				analyzed_status[cable_num].stat = WAIT_DATA1;
				analyzed_status[cable_num].is_system_common = false;
				break;

			default:
				analyzed_status[cable_num].type = MSG_NOTHING;
				analyzed_status[cable_num].stat = START_ANALYSIS;
				analyzed_status[cable_num].is_system_common = false;
				break;
		}

	}else{//0x00-0x7F:data byte

		switch(analyzed_status[cable_num].stat){

			case WAIT_DATA1:
				midi_event[cable_num].midi_byte[1] = rx_midi_msg[cable_num];

				if(MSG_THREE_BYTE == analyzed_status[cable_num].type ){
					analyzed_status[cable_num].stat = WAIT_DATA2;
				}else if( MSG_TWO_BYTE == analyzed_status[cable_num].type ){
					midi_event[cable_num].length = 2;
					analyzed_status[cable_num].stat = END_ANALYSIS;
				}else{
					analyzed_status[cable_num].stat = START_ANALYSIS;
				}
				break;

			case WAIT_DATA2:
				if(MSG_THREE_BYTE == analyzed_status[cable_num].type ){
					midi_event[cable_num].midi_byte[2] = rx_midi_msg[cable_num];
					midi_event[cable_num].length = 3;
					analyzed_status[cable_num].stat = END_ANALYSIS;
				}else{
					analyzed_status[cable_num].stat = START_ANALYSIS;
				}
				break;

			case WAIT_SYSTEM_DATA:
				midi_event[cable_num].midi_byte[ analyzed_status[cable_num].data_idx++ ] = rx_midi_msg[cable_num];

				if(analyzed_status[cable_num].data_idx > (MIDI_SENDDATA_MAX - 1) ){
					analyzed_status[cable_num].stat = END_ANALYSIS;
				}
				break;

			case END_ANALYSIS://running status:When status byte is omitted.
				midi_event[cable_num].midi_byte[1] = rx_midi_msg[cable_num];
				if(MSG_THREE_BYTE == analyzed_status[cable_num].type){
					analyzed_status[cable_num].stat = WAIT_DATA2;
				}else if(MSG_TWO_BYTE == analyzed_status[cable_num].type){
					midi_event[cable_num].length = 2;
					analyzed_status[cable_num].stat = END_ANALYSIS;
				}
				break;

			case START_ANALYSIS:
				break;

			default:
				break;
		}
	}

	if(END_ANALYSIS == analyzed_status[cable_num].stat){
		return true;
	}else{
		return false;
	}

}


void midiGenerateUsbPacket(uint8_t cable_num)
{
	uint8_t msg_buf[4] = {0x00,0x00,0x00,0x00};
	uint32_t cnt_remain=0, cnt_length;

	switch(analyzed_status[cable_num].type){

		case MSG_ONE_BYTE:
			//byte 0: cable number + code index number
			msg_buf[0] = (cable_num << 4) + ((midi_event[cable_num].midi_byte[0] & 0xF0) >> 4);

			sendMidiMessage(msg_buf,4);
			break;

		case MSG_TWO_BYTE:
		case MSG_THREE_BYTE:
			//byte 0: cable number + code index number
			if(analyzed_status[cable_num].is_system_common){
				msg_buf[0] = (cable_num << 4) + midi_event[cable_num].length;
			}else{
				msg_buf[0] = (cable_num << 4) + ((midi_event[cable_num].midi_byte[0] & 0xF0) >> 4);
			}

			//byte 1-3
			for(uint32_t i=0; i<midi_event[cable_num].length; i++){
				msg_buf[i+1] = midi_event[cable_num].midi_byte[i];
			}

			sendMidiMessage(msg_buf,4);
			break;

		case MSG_SYSEX:
			for(cnt_length = 0; cnt_length < midi_event[cable_num].length; cnt_length++){
				if( 0xF7 !=midi_event[cable_num].midi_byte[cnt_length] ){
					if(2 == cnt_remain){
						msg_buf[3] = midi_event[cable_num].midi_byte[cnt_length];
						msg_buf[0] = (cable_num << 4) + 0x4;	//SysEx starts or continues
						sendMidiMessage(msg_buf,4);
					}else{
						msg_buf[1 + cnt_remain] = midi_event[cable_num].midi_byte[cnt_length];
					}


				}else{

					switch (cnt_remain) {
						case 0:
							msg_buf[0] = (cable_num << 4) + 0x5;	//SysEx ends with following single byte
							msg_buf[1] = 0xF7;
							msg_buf[2] = msg_buf[3] = 0x00;
							sendMidiMessage(msg_buf,4);
							break;

						case 1:
							msg_buf[0] = (cable_num << 4) + 0x6;	//SysEx ends with following two bytes.
							msg_buf[2] = 0xF7;
							msg_buf[3] = 0x00;
							sendMidiMessage(msg_buf,4);
							break;

						case 2:
							msg_buf[0] = (cable_num << 4) + 0x7;	//SysEx ends with following three bytes.
							msg_buf[3] = 0xF7;
							sendMidiMessage(msg_buf,4);
							break;
						default:
							break;
					}
				}

				cnt_remain++;
				if(cnt_remain >=3){
					cnt_remain = 0;
				}
			}
			break;

		default:
			break;
	}
	USBD_MIDI_SendPacket();
}

void midiProcess(){

	for(uint32_t cable_num=0; cable_num<MIDI_IN_JACK_NUM; cable_num++){
		while( FUNC_SUCCESS == midiGetFromJackRx(cable_num) ){
			if( midiEventIsGenerated(cable_num) ){// Generate MIDI event from UART buffer.
				//Analyze MIDI Message.
				midiGenerateUsbPacket(cable_num);
			}
		}
	}
}