1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 #include "USBAudio_Types.h"
25 USBAudio::USBAudio(uint32_t frequency_in, uint8_t channel_nb_in, uint32_t frequency_out, uint8_t channel_nb_out, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
33 FREQ_IN = frequency_in;
34 FREQ_OUT = frequency_out;
36 this->channel_nb_in = channel_nb_in;
37 this->channel_nb_out = channel_nb_out;
39 // stereo -> *2, mono -> *1
40 PACKET_SIZE_ISO_IN = (FREQ_IN / 500) * channel_nb_in;
41 PACKET_SIZE_ISO_OUT = (FREQ_OUT / 500) * channel_nb_out;
43 // STEREO -> left and right
44 channel_config_in = (channel_nb_in == 1) ? CHANNEL_M : CHANNEL_L + CHANNEL_R;
45 channel_config_out = (channel_nb_out == 1) ? CHANNEL_M : CHANNEL_L + CHANNEL_R;
49 buf_stream_out = NULL;
63 bool USBAudio::read(uint8_t * buf) {
66 while (!available || !SOF_handler);
71 bool USBAudio::readNB(uint8_t * buf) {
83 bool USBAudio::readWrite(uint8_t * buf_read, uint8_t * buf_write) {
84 buf_stream_in = buf_read;
88 USBDevice::writeNB(EP3IN, buf_write, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
90 buf_stream_out = buf_write;
101 bool USBAudio::write(uint8_t * buf) {
105 USBDevice::writeNB(EP3IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
107 buf_stream_out = buf;
109 while (!SOF_handler);
117 float USBAudio::getVolume() {
118 return (mute) ? 0.0 : volume;
122 bool USBAudio::EPISO_OUT_callback() {
125 if (buf_stream_in != NULL) {
126 readEP(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
128 buf_stream_in = NULL;
130 readStart(EP3OUT, PACKET_SIZE_ISO_IN);
135 bool USBAudio::EPISO_IN_callback() {
143 // Called in ISR context on each start of frame
144 void USBAudio::SOF(int frameNumber) {
148 // read the isochronous endpoint
149 if (buf_stream_in != NULL) {
150 if (USBDevice::readEP_NB(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN)) {
153 readStart(EP3OUT, PACKET_SIZE_ISO_IN);
154 buf_stream_in = NULL;
162 if (buf_stream_out != NULL) {
163 USBDevice::writeNB(EP3IN, (uint8_t *)buf_stream_out, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
164 buf_stream_out = NULL;
172 // Called in ISR context
173 // Set configuration. Return false if the configuration is not supported.
174 bool USBAudio::USBCallback_setConfiguration(uint8_t configuration) {
175 if (configuration != DEFAULT_CONFIGURATION) {
179 // Configure isochronous endpoint
180 realiseEndpoint(EP3OUT, PACKET_SIZE_ISO_IN, ISOCHRONOUS);
181 realiseEndpoint(EP3IN, PACKET_SIZE_ISO_OUT, ISOCHRONOUS);
183 // activate readings on this endpoint
184 readStart(EP3OUT, PACKET_SIZE_ISO_IN);
189 // Called in ISR context
190 // Set alternate setting. Return false if the alternate setting is not supported
191 bool USBAudio::USBCallback_setInterface(uint16_t interface, uint8_t alternate) {
192 if (interface == 0 && alternate == 0) {
195 if (interface == 1 && (alternate == 0 || alternate == 1)) {
198 if (interface == 2 && (alternate == 0 || alternate == 1)) {
206 // Called in ISR context
207 // Called by USBDevice on Endpoint0 request
208 // This is used to handle extensions to standard requests and class specific requests.
209 // Return true if class handles this request
210 bool USBAudio::USBCallback_request() {
211 bool success = false;
212 CONTROL_TRANSFER * transfer = getTransferPtr();
214 // Process class-specific requests
215 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
217 // Feature Unit: Interface = 0, ID = 2
218 if (transfer->setup.wIndex == 0x0200) {
221 if ((transfer->setup.wValue & 0xff) == 0) {
223 switch (transfer->setup.wValue >> 8) {
225 switch (transfer->setup.bRequest) {
226 case REQUEST_GET_CUR:
227 transfer->remaining = 1;
228 transfer->ptr = &mute;
229 transfer->direction = DEVICE_TO_HOST;
233 case REQUEST_SET_CUR:
234 transfer->remaining = 1;
235 transfer->notify = true;
236 transfer->direction = HOST_TO_DEVICE;
244 switch (transfer->setup.bRequest) {
245 case REQUEST_GET_CUR:
246 transfer->remaining = 2;
247 transfer->ptr = (uint8_t *)&volCur;
248 transfer->direction = DEVICE_TO_HOST;
251 case REQUEST_GET_MIN:
252 transfer->remaining = 2;
253 transfer->ptr = (uint8_t *)&volMin;
254 transfer->direction = DEVICE_TO_HOST;
257 case REQUEST_GET_MAX:
258 transfer->remaining = 2;
259 transfer->ptr = (uint8_t *)&volMax;
260 transfer->direction = DEVICE_TO_HOST;
263 case REQUEST_GET_RES:
264 transfer->remaining = 2;
265 transfer->ptr = (uint8_t *)&volRes;
266 transfer->direction = DEVICE_TO_HOST;
270 case REQUEST_SET_CUR:
271 transfer->remaining = 2;
272 transfer->notify = true;
273 transfer->direction = HOST_TO_DEVICE;
276 case REQUEST_SET_MIN:
277 transfer->remaining = 2;
278 transfer->notify = true;
279 transfer->direction = HOST_TO_DEVICE;
282 case REQUEST_SET_MAX:
283 transfer->remaining = 2;
284 transfer->notify = true;
285 transfer->direction = HOST_TO_DEVICE;
288 case REQUEST_SET_RES:
289 transfer->remaining = 2;
290 transfer->notify = true;
291 transfer->direction = HOST_TO_DEVICE;
306 // Called in ISR context when a data OUT stage has been performed
307 void USBAudio::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
308 if ((length == 1) || (length == 2)) {
309 uint16_t data = (length == 1) ? *buf : *((uint16_t *)buf);
310 CONTROL_TRANSFER * transfer = getTransferPtr();
311 switch (transfer->setup.wValue >> 8) {
313 switch (transfer->setup.bRequest) {
314 case REQUEST_SET_CUR:
323 switch (transfer->setup.bRequest) {
324 case REQUEST_SET_CUR:
326 volume = (float)volCur/(float)volMax;
341 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
342 + (5 * INTERFACE_DESCRIPTOR_LENGTH) \
343 + (1 * CONTROL_INTERFACE_DESCRIPTOR_LENGTH + 1) \
344 + (2 * INPUT_TERMINAL_DESCRIPTOR_LENGTH) \
345 + (1 * FEATURE_UNIT_DESCRIPTOR_LENGTH) \
346 + (2 * OUTPUT_TERMINAL_DESCRIPTOR_LENGTH) \
347 + (2 * STREAMING_INTERFACE_DESCRIPTOR_LENGTH) \
348 + (2 * FORMAT_TYPE_I_DESCRIPTOR_LENGTH) \
349 + (2 * (ENDPOINT_DESCRIPTOR_LENGTH + 2)) \
350 + (2 * STREAMING_ENDPOINT_DESCRIPTOR_LENGTH) )
352 #define TOTAL_CONTROL_INTF_LENGTH (CONTROL_INTERFACE_DESCRIPTOR_LENGTH + 1 + \
353 2*INPUT_TERMINAL_DESCRIPTOR_LENGTH + \
354 FEATURE_UNIT_DESCRIPTOR_LENGTH + \
355 2*OUTPUT_TERMINAL_DESCRIPTOR_LENGTH)
357 uint8_t * USBAudio::configurationDesc() {
358 static uint8_t configDescriptor[] = {
360 CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
361 CONFIGURATION_DESCRIPTOR, // bDescriptorType
362 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
363 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
364 0x03, // bNumInterfaces
365 DEFAULT_CONFIGURATION, // bConfigurationValue
366 0x00, // iConfiguration
367 0x80, // bmAttributes
370 // Interface 0, Alternate Setting 0, Audio Control
371 INTERFACE_DESCRIPTOR_LENGTH, // bLength
372 INTERFACE_DESCRIPTOR, // bDescriptorType
373 0x00, // bInterfaceNumber
374 0x00, // bAlternateSetting
375 0x00, // bNumEndpoints
376 AUDIO_CLASS, // bInterfaceClass
377 SUBCLASS_AUDIOCONTROL, // bInterfaceSubClass
378 0x00, // bInterfaceProtocol
382 // Audio Control Interface
383 CONTROL_INTERFACE_DESCRIPTOR_LENGTH + 1,// bLength
384 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
385 CONTROL_HEADER, // bDescriptorSubtype
386 LSB(0x0100), // bcdADC (LSB)
387 MSB(0x0100), // bcdADC (MSB)
388 LSB(TOTAL_CONTROL_INTF_LENGTH), // wTotalLength
389 MSB(TOTAL_CONTROL_INTF_LENGTH), // wTotalLength
390 0x02, // bInCollection
391 0x01, // baInterfaceNr
392 0x02, // baInterfaceNr
394 // Audio Input Terminal (Speaker)
395 INPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength
396 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
397 CONTROL_INPUT_TERMINAL, // bDescriptorSubtype
399 LSB(TERMINAL_USB_STREAMING), // wTerminalType
400 MSB(TERMINAL_USB_STREAMING), // wTerminalType
401 0x00, // bAssocTerminal
402 channel_nb_in, // bNrChannels
403 (uint8_t)(LSB(channel_config_in)), // wChannelConfig
404 (uint8_t)(MSB(channel_config_in)), // wChannelConfig
405 0x00, // iChannelNames
408 // Audio Feature Unit (Speaker)
409 FEATURE_UNIT_DESCRIPTOR_LENGTH, // bLength
410 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
411 CONTROL_FEATURE_UNIT, // bDescriptorSubtype
414 0x01, // bControlSize
416 CONTROL_VOLUME, // bmaControls(0)
417 0x00, // bmaControls(1)
420 // Audio Output Terminal (Speaker)
421 OUTPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength
422 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
423 CONTROL_OUTPUT_TERMINAL, // bDescriptorSubtype
425 LSB(TERMINAL_SPEAKER), // wTerminalType
426 MSB(TERMINAL_SPEAKER), // wTerminalType
427 0x00, // bAssocTerminal
432 // Audio Input Terminal (Microphone)
433 INPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength
434 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
435 CONTROL_INPUT_TERMINAL, // bDescriptorSubtype
437 LSB(TERMINAL_MICROPHONE), // wTerminalType
438 MSB(TERMINAL_MICROPHONE), // wTerminalType
439 0x00, // bAssocTerminal
440 channel_nb_out, // bNrChannels
441 (uint8_t)(LSB(channel_config_out)), // wChannelConfig
442 (uint8_t)(MSB(channel_config_out)), // wChannelConfig
443 0x00, // iChannelNames
446 // Audio Output Terminal (Microphone)
447 OUTPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength
448 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
449 CONTROL_OUTPUT_TERMINAL, // bDescriptorSubtype
451 LSB(TERMINAL_USB_STREAMING), // wTerminalType
452 MSB(TERMINAL_USB_STREAMING), // wTerminalType
453 0x00, // bAssocTerminal
462 // Interface 1, Alternate Setting 0, Audio Streaming - Zero Bandwith
463 INTERFACE_DESCRIPTOR_LENGTH, // bLength
464 INTERFACE_DESCRIPTOR, // bDescriptorType
465 0x01, // bInterfaceNumber
466 0x00, // bAlternateSetting
467 0x00, // bNumEndpoints
468 AUDIO_CLASS, // bInterfaceClass
469 SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass
470 0x00, // bInterfaceProtocol
473 // Interface 1, Alternate Setting 1, Audio Streaming - Operational
474 INTERFACE_DESCRIPTOR_LENGTH, // bLength
475 INTERFACE_DESCRIPTOR, // bDescriptorType
476 0x01, // bInterfaceNumber
477 0x01, // bAlternateSetting
478 0x01, // bNumEndpoints
479 AUDIO_CLASS, // bInterfaceClass
480 SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass
481 0x00, // bInterfaceProtocol
484 // Audio Streaming Interface
485 STREAMING_INTERFACE_DESCRIPTOR_LENGTH, // bLength
486 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
487 STREAMING_GENERAL, // bDescriptorSubtype
488 0x01, // bTerminalLink
490 LSB(FORMAT_PCM), // wFormatTag
491 MSB(FORMAT_PCM), // wFormatTag
493 // Audio Type I Format
494 FORMAT_TYPE_I_DESCRIPTOR_LENGTH, // bLength
495 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
496 STREAMING_FORMAT_TYPE, // bDescriptorSubtype
497 FORMAT_TYPE_I, // bFormatType
498 channel_nb_in, // bNrChannels
499 0x02, // bSubFrameSize
500 16, // bBitResolution
501 0x01, // bSamFreqType
502 (uint8_t)(LSB(FREQ_IN)), // tSamFreq
503 (uint8_t)((FREQ_IN >> 8) & 0xff), // tSamFreq
504 (uint8_t)((FREQ_IN >> 16) & 0xff), // tSamFreq
506 // Endpoint - Standard Descriptor
507 ENDPOINT_DESCRIPTOR_LENGTH + 2, // bLength
508 ENDPOINT_DESCRIPTOR, // bDescriptorType
509 PHY_TO_DESC(EPISO_OUT), // bEndpointAddress
510 E_ISOCHRONOUS, // bmAttributes
511 (uint8_t)(LSB(PACKET_SIZE_ISO_IN)), // wMaxPacketSize
512 (uint8_t)(MSB(PACKET_SIZE_ISO_IN)), // wMaxPacketSize
515 0x00, // bSynchAddress
517 // Endpoint - Audio Streaming
518 STREAMING_ENDPOINT_DESCRIPTOR_LENGTH, // bLength
519 ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
520 ENDPOINT_GENERAL, // bDescriptor
521 0x00, // bmAttributes
522 0x00, // bLockDelayUnits
523 LSB(0x0000), // wLockDelay
524 MSB(0x0000), // wLockDelay
532 // Interface 1, Alternate Setting 0, Audio Streaming - Zero Bandwith
533 INTERFACE_DESCRIPTOR_LENGTH, // bLength
534 INTERFACE_DESCRIPTOR, // bDescriptorType
535 0x02, // bInterfaceNumber
536 0x00, // bAlternateSetting
537 0x00, // bNumEndpoints
538 AUDIO_CLASS, // bInterfaceClass
539 SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass
540 0x00, // bInterfaceProtocol
543 // Interface 1, Alternate Setting 1, Audio Streaming - Operational
544 INTERFACE_DESCRIPTOR_LENGTH, // bLength
545 INTERFACE_DESCRIPTOR, // bDescriptorType
546 0x02, // bInterfaceNumber
547 0x01, // bAlternateSetting
548 0x01, // bNumEndpoints
549 AUDIO_CLASS, // bInterfaceClass
550 SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass
551 0x00, // bInterfaceProtocol
554 // Audio Streaming Interface
555 STREAMING_INTERFACE_DESCRIPTOR_LENGTH, // bLength
556 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
557 SUBCLASS_AUDIOCONTROL, // bDescriptorSubtype
558 0x05, // bTerminalLink (output terminal microphone)
563 // Audio Type I Format
564 FORMAT_TYPE_I_DESCRIPTOR_LENGTH, // bLength
565 INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
566 SUBCLASS_AUDIOSTREAMING, // bDescriptorSubtype
567 FORMAT_TYPE_I, // bFormatType
568 channel_nb_out, // bNrChannels
569 0x02, // bSubFrameSize
570 0x10, // bBitResolution
571 0x01, // bSamFreqType
572 (uint8_t)(LSB(FREQ_OUT)), // tSamFreq
573 (uint8_t)((FREQ_OUT >> 8) & 0xff), // tSamFreq
574 (uint8_t)((FREQ_OUT >> 16) & 0xff), // tSamFreq
576 // Endpoint - Standard Descriptor
577 ENDPOINT_DESCRIPTOR_LENGTH + 2, // bLength
578 ENDPOINT_DESCRIPTOR, // bDescriptorType
579 PHY_TO_DESC(EPISO_IN), // bEndpointAddress
580 E_ISOCHRONOUS, // bmAttributes
581 (uint8_t)(LSB(PACKET_SIZE_ISO_OUT)), // wMaxPacketSize
582 (uint8_t)(MSB(PACKET_SIZE_ISO_OUT)), // wMaxPacketSize
585 0x00, // bSynchAddress
587 // Endpoint - Audio Streaming
588 STREAMING_ENDPOINT_DESCRIPTOR_LENGTH, // bLength
589 ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
590 ENDPOINT_GENERAL, // bDescriptor
591 0x00, // bmAttributes
592 0x00, // bLockDelayUnits
593 LSB(0x0000), // wLockDelay
594 MSB(0x0000), // wLockDelay
599 return configDescriptor;
602 uint8_t * USBAudio::stringIinterfaceDesc() {
603 static uint8_t stringIinterfaceDescriptor[] = {
605 STRING_DESCRIPTOR, //bDescriptorType 0x03
606 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
608 return stringIinterfaceDescriptor;
611 uint8_t * USBAudio::stringIproductDesc() {
612 static uint8_t stringIproductDescriptor[] = {
614 STRING_DESCRIPTOR, //bDescriptorType 0x03
615 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
617 return stringIproductDescriptor;