-
Notifications
You must be signed in to change notification settings - Fork 28
/
usbh_midi.cc
422 lines (356 loc) · 12.6 KB
/
usbh_midi.cc
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
/**
******************************************************************************
* @file usbh_midi.cc
* @author Dan Green. Based on CDC Class in STM32 USB Host Library v3.5.0
* @brief USB Host Audio Class MIDI Streaming Subclass driver
*
* See "Universal Serial Bus Device Class Definition for MIDI Devices"
* Revision 1.0 November 1, 1999
*
******************************************************************************
* @attention
* Copyright (c) 2022 Dan Green.
* Licensed by the MIT License, see LICENSE file
*
* Portions of this file may be also:
* <h2><center>© Copyright (c) 2015 STMicroelectronics.
* All rights reserved.</center></h2>
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
#include "usbh_midi.hh"
static void MIDI_ProcessTransmission(USBH_HandleTypeDef *phost);
static void MIDI_ProcessReception(USBH_HandleTypeDef *phost);
/**
* @brief USBH_MIDI_InterfaceInit
* The function init the MIDI class.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_InterfaceInit(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status;
uint8_t interface;
// In most STM Host Lib Classes, we USBH_malloc() a class handle here.
// But in this class, we already specified it by passing it in the pData field
// of the USBH_ClassTypeDef struct passed to USBH_RegisterClass
// This allows the app to own the class handle, managing its memory as it likes
// without requiring either dynamic memory or static/globals/singletons
if (phost->pActiveClass->pData == nullptr) {
USBH_DbgLog("Cannot allocate memory for CDC Handle");
return USBH_FAIL;
}
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
// Look for an optional Audio Control interface
interface = USBH_FindInterface(phost, AudioClassCode, AudioControlSubclassCode, AnyProtocol);
if ((interface == NoValidInterfaceFound) || (interface >= USBH_MAX_NUM_INTERFACES)) {
USBH_DbgLog("Did not find an audio control interface, continuing\n");
} else {
USBH_DbgLog("Found Audio Control subclass\n");
host.link_endpoint_pipe(MSHandle->ControlItf.ControlEP, interface, 0);
host.open_pipe(MSHandle->ControlItf.ControlEP, EndPointType::Intr); // TODO: Is it an Intr EP type?
host.set_toggle(MSHandle->ControlItf.ControlEP, 0);
}
// Look for MidiStreamingSubClass
interface = USBH_FindInterface(phost, AudioClassCode, MidiStreamingSubClass, AnyProtocol);
if ((interface == NoValidInterfaceFound) || (interface >= USBH_MAX_NUM_INTERFACES)) {
USBH_DbgLog("Cannot find the interface for MIDI subclass: %s.", phost->pActiveClass->Name);
return USBH_FAIL;
}
status = USBH_SelectInterface(phost, interface);
if (status != USBH_OK)
return USBH_FAIL;
if (host.is_in_ep(interface, 0))
host.link_endpoint_pipe(MSHandle->DataItf.InEP, interface, 0);
else
host.link_endpoint_pipe(MSHandle->DataItf.OutEP, interface, 0);
if (host.is_in_ep(interface, 1))
host.link_endpoint_pipe(MSHandle->DataItf.InEP, interface, 1);
else
host.link_endpoint_pipe(MSHandle->DataItf.OutEP, interface, 1);
host.open_pipe(MSHandle->DataItf.OutEP, EndPointType::Bulk);
host.open_pipe(MSHandle->DataItf.InEP, EndPointType::Bulk);
MSHandle->state = MidiStreamingState::Idle;
host.set_toggle(MSHandle->DataItf.OutEP, 0);
host.set_toggle(MSHandle->DataItf.InEP, 0);
return USBH_OK;
}
/**
* @brief USBH_MIDI_InterfaceDeInit
* The function DeInit the Pipes used for the MIDI class.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_InterfaceDeInit(USBH_HandleTypeDef *phost)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
host.close_and_free_pipe(MSHandle->ControlItf.ControlEP);
host.close_and_free_pipe(MSHandle->DataItf.InEP);
host.close_and_free_pipe(MSHandle->DataItf.OutEP);
// USBH_free(phost->pActiveClass->pData);
// phost->pActiveClass->pData = nullptr;
return USBH_OK;
}
/**
* @brief USBH_MIDI_ClassRequest
* The function is responsible for handling Standard requests
* for MIDI class.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_ClassRequest(USBH_HandleTypeDef *phost)
{
if (phost->pUser)
phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
return USBH_OK;
}
/**
* @brief USBH_MIDI_Process
* The function is for managing state machine for MIDI data transfers
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_Process(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status = USBH_BUSY;
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
switch (MSHandle->state) {
case MidiStreamingState::Idle:
status = USBH_OK;
break;
case MidiStreamingState::TransferData:
MIDI_ProcessTransmission(phost);
MIDI_ProcessReception(phost);
break;
case MidiStreamingState::Error: {
USBH_StatusTypeDef req_status = USBH_ClrFeature(phost, 0x00U);
if (req_status == USBH_OK) {
MSHandle->state = MidiStreamingState::Idle;
}
} break;
}
return status;
}
/**
* @brief USBH_MIDI_SOFProcess
* The function is for managing SOF callback
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_SOFProcess(USBH_HandleTypeDef *phost)
{
UNUSED(phost);
return USBH_OK;
}
/**
* @brief USBH_MIDI_Stop
* Stop current MIDI Transmission
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_MIDI_Stop(USBH_HandleTypeDef *phost)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
if (phost->gState == HOST_CLASS) {
MSHandle->state = MidiStreamingState::Idle;
USBH_ClosePipe(phost, MSHandle->ControlItf.ControlEP.pipe);
USBH_ClosePipe(phost, MSHandle->DataItf.InEP.pipe);
USBH_ClosePipe(phost, MSHandle->DataItf.OutEP.pipe);
}
return USBH_OK;
}
/**
* @brief This function return last received data size
* @param None
* @retval None
*/
uint16_t USBH_MIDI_GetLastReceivedDataSize(USBH_HandleTypeDef *phost)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
uint32_t dataSize;
if (phost->gState == HOST_CLASS) {
dataSize = USBH_LL_GetLastXferSize(phost, MSHandle->DataItf.InEP.pipe);
} else {
dataSize = 0U;
}
return (uint16_t)dataSize;
}
/**
* @brief This function prepares the state before issuing the class specific commands
* @param None
* @retval None
*/
USBH_StatusTypeDef USBH_MIDI_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
USBH_StatusTypeDef status = USBH_BUSY;
if ((MSHandle->state == MidiStreamingState::Idle) || (MSHandle->state == MidiStreamingState::TransferData)) {
MSHandle->pTxData = pbuff;
MSHandle->TxDataLength = length;
MSHandle->state = MidiStreamingState::TransferData;
MSHandle->data_tx_state = MidiStreamingDataState::SendData;
status = USBH_OK;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
}
return status;
}
/**
* @brief This function prepares the state before issuing the class specific commands
* @param None
* @retval None
*/
USBH_StatusTypeDef USBH_MIDI_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return USBH_FAIL;
USBH_StatusTypeDef status = USBH_BUSY;
if ((MSHandle->state == MidiStreamingState::Idle) || (MSHandle->state == MidiStreamingState::TransferData)) {
MSHandle->pRxData = pbuff;
MSHandle->RxDataLength = length;
MSHandle->state = MidiStreamingState::TransferData;
MSHandle->data_rx_state = MidiStreamingDataState::ReceiveData;
status = USBH_OK;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
}
return status;
}
/**
* @brief The function is responsible for sending data to the device
* @param pdev: Selected device
* @retval None
*/
static void MIDI_ProcessTransmission(USBH_HandleTypeDef *phost)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return;
USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE;
switch (MSHandle->data_tx_state) {
case MidiStreamingDataState::SendData:
if (MSHandle->TxDataLength > MSHandle->DataItf.OutEP.size) {
USBH_BulkSendData(
phost, MSHandle->pTxData, MSHandle->DataItf.OutEP.size, MSHandle->DataItf.OutEP.pipe, 1U);
} else {
USBH_BulkSendData(
phost, MSHandle->pTxData, (uint16_t)MSHandle->TxDataLength, MSHandle->DataItf.OutEP.pipe, 1U);
}
MSHandle->data_tx_state = MidiStreamingDataState::SendDataWait;
break;
case MidiStreamingDataState::SendDataWait:
URB_Status = USBH_LL_GetURBState(phost, MSHandle->DataItf.OutEP.pipe);
/* Check the status done for transmission */
if (URB_Status == USBH_URB_DONE) {
if (MSHandle->TxDataLength > MSHandle->DataItf.OutEP.size) {
MSHandle->TxDataLength -= MSHandle->DataItf.OutEP.size;
MSHandle->pTxData += MSHandle->DataItf.OutEP.size;
} else {
MSHandle->TxDataLength = 0U;
}
if (MSHandle->TxDataLength > 0U) {
MSHandle->data_tx_state = MidiStreamingDataState::SendData;
} else {
MSHandle->data_tx_state = MidiStreamingDataState::Idle;
MSHandle->tx_callback();
}
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
} else {
if (URB_Status == USBH_URB_NOTREADY) {
MSHandle->data_tx_state = MidiStreamingDataState::SendData;
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
}
}
break;
default:
break;
}
}
/**
* @brief This function responsible for reception of data from the device
* @param pdev: Selected device
* @retval None
*/
static void MIDI_ProcessReception(USBH_HandleTypeDef *phost)
{
USBHostHelper host{phost};
auto MSHandle = host.get_class_handle<MidiStreamingHandle>();
if (!MSHandle)
return;
USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE;
uint32_t length;
switch (MSHandle->data_rx_state) {
case MidiStreamingDataState::ReceiveData:
USBH_BulkReceiveData(phost, MSHandle->pRxData, MSHandle->DataItf.InEP.size, MSHandle->DataItf.InEP.pipe);
MSHandle->data_rx_state = MidiStreamingDataState::ReceiveDataWait;
break;
case MidiStreamingDataState::ReceiveDataWait:
URB_Status = USBH_LL_GetURBState(phost, MSHandle->DataItf.InEP.pipe);
/*Check the status done for reception*/
if (URB_Status == USBH_URB_DONE) {
length = USBH_LL_GetLastXferSize(phost, MSHandle->DataItf.InEP.pipe);
if (((MSHandle->RxDataLength - length) > 0U) && (length > MSHandle->DataItf.InEP.size)) {
MSHandle->RxDataLength -= length;
MSHandle->pRxData += length;
MSHandle->data_rx_state = MidiStreamingDataState::ReceiveData;
} else {
MSHandle->data_rx_state = MidiStreamingDataState::Idle;
MSHandle->rx_callback(std::span<uint8_t>(MSHandle->pRxData, length));
}
#if (USBH_USE_OS == 1U)
phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
(void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
}
break;
default:
break;
}
}
// Note: To use this class like other STM32 Host Classes,
// Do something like this (in your application code):
// static MidiStreamingHandle MSHandle;
//
// USBH_ClassTypeDef MIDI_Class_Ops = {
// "MIDI",
// AudioClassCode,
// USBH_MIDI_InterfaceInit,
// USBH_MIDI_InterfaceDeInit,
// USBH_MIDI_ClassRequest,
// USBH_MIDI_Process,
// USBH_MIDI_SOFProcess,
// &MSHandle,
// };