123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- /*
- * Copyright (c) 2024, sakumisu
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include "usbd_core.h"
- #include "usbd_audio.h"
- #define USING_FEEDBACK 0
- #define USBD_VID 0xffff
- #define USBD_PID 0xffff
- #define USBD_MAX_POWER 100
- #define USBD_LANGID_STRING 1033
- #ifdef CONFIG_USB_HS
- #define EP_INTERVAL 0x04
- #define FEEDBACK_ENDP_PACKET_SIZE 0x04
- #else
- #define EP_INTERVAL 0x01
- #define FEEDBACK_ENDP_PACKET_SIZE 0x03
- #endif
- #define AUDIO_OUT_EP 0x01
- #define AUDIO_OUT_FEEDBACK_EP 0x82
- #define AUDIO_OUT_CLOCK_ID 0x01
- #define AUDIO_OUT_FU_ID 0x03
- #define AUDIO_FREQ 48000
- #define HALF_WORD_BYTES 2 //2 half word (one channel)
- #define SAMPLE_BITS 16 //16 bit per channel
- #define BMCONTROL (AUDIO_V2_FU_CONTROL_MUTE | AUDIO_V2_FU_CONTROL_VOLUME)
- #define OUT_CHANNEL_NUM 2
- #if OUT_CHANNEL_NUM == 1
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x00000000
- #elif OUT_CHANNEL_NUM == 2
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x00000003
- #elif OUT_CHANNEL_NUM == 3
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x00000007
- #elif OUT_CHANNEL_NUM == 4
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x0000000f
- #elif OUT_CHANNEL_NUM == 5
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x0000001f
- #elif OUT_CHANNEL_NUM == 6
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x0000003F
- #elif OUT_CHANNEL_NUM == 7
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x0000007f
- #elif OUT_CHANNEL_NUM == 8
- #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
- #define OUTPUT_CH_ENABLE 0x000000ff
- #endif
- #define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_FREQ * HALF_WORD_BYTES * OUT_CHANNEL_NUM) / 1000))
- #if USING_FEEDBACK == 0
- #define USB_AUDIO_CONFIG_DESC_SIZ (9 + \
- AUDIO_V2_AC_DESCRIPTOR_INIT_LEN + \
- AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
- AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
- AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
- AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
- AUDIO_V2_AS_DESCRIPTOR_INIT_LEN)
- #else
- #define USB_AUDIO_CONFIG_DESC_SIZ (9 + \
- AUDIO_V2_AC_DESCRIPTOR_INIT_LEN + \
- AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
- AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
- AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
- AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
- AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT_LEN)
- #endif
- #define AUDIO_AC_SIZ (AUDIO_V2_SIZEOF_AC_HEADER_DESC + \
- AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
- AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
- AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
- AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
- #ifdef CONFIG_USBDEV_ADVANCE_DESC
- static const uint8_t device_descriptor[] = {
- USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
- };
- static const uint8_t config_descriptor[] = {
- USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
- AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_SPEAKER, 0x00, 0x00),
- AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(AUDIO_OUT_CLOCK_ID, 0x03, 0x03),
- AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_TERMINAL_STREAMING, 0x01, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, 0x0000),
- AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_OUT_FU_ID, 0x02, OUTPUT_CTRL),
- AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_OUTTERM_SPEAKER, 0x03, 0x01, 0x0000),
- #if USING_FEEDBACK == 0
- AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET, EP_INTERVAL),
- #else
- AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, AUDIO_OUT_PACKET, EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP),
- #endif
- };
- static const uint8_t device_quality_descriptor[] = {
- ///////////////////////////////////////
- /// device qualifier descriptor
- ///////////////////////////////////////
- 0x0a,
- USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
- 0x00,
- 0x02,
- 0x00,
- 0x00,
- 0x00,
- 0x40,
- 0x00,
- 0x00,
- };
- static const char *string_descriptors[] = {
- (const char[]){ 0x09, 0x04 }, /* Langid */
- "CherryUSB", /* Manufacturer */
- "CherryUSB UAC DEMO", /* Product */
- "2022123456", /* Serial Number */
- };
- static const uint8_t *device_descriptor_callback(uint8_t speed)
- {
- return device_descriptor;
- }
- static const uint8_t *config_descriptor_callback(uint8_t speed)
- {
- return config_descriptor;
- }
- static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
- {
- return device_quality_descriptor;
- }
- static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
- {
- if (index > 3) {
- return NULL;
- }
- return string_descriptors[index];
- }
- const struct usb_descriptor audio_v2_descriptor = {
- .device_descriptor_callback = device_descriptor_callback,
- .config_descriptor_callback = config_descriptor_callback,
- .device_quality_descriptor_callback = device_quality_descriptor_callback,
- .string_descriptor_callback = string_descriptor_callback
- };
- #else
- const uint8_t audio_v2_descriptor[] = {
- USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
- USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
- AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_SPEAKER, 0x00, 0x00),
- AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(AUDIO_OUT_CLOCK_ID, 0x03, 0x03),
- AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_TERMINAL_STREAMING, 0x01, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, 0x0000),
- AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_OUT_FU_ID, 0x02, OUTPUT_CTRL),
- AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_OUTTERM_SPEAKER, 0x03, 0x01, 0x0000),
- #if USING_FEEDBACK == 0
- AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET, EP_INTERVAL),
- #else
- AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, AUDIO_OUT_PACKET, EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP),
- #endif
- ///////////////////////////////////////
- /// string0 descriptor
- ///////////////////////////////////////
- USB_LANGID_INIT(USBD_LANGID_STRING),
- ///////////////////////////////////////
- /// string1 descriptor
- ///////////////////////////////////////
- 0x14, /* bLength */
- USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
- 'C', 0x00, /* wcChar0 */
- 'h', 0x00, /* wcChar1 */
- 'e', 0x00, /* wcChar2 */
- 'r', 0x00, /* wcChar3 */
- 'r', 0x00, /* wcChar4 */
- 'y', 0x00, /* wcChar5 */
- 'U', 0x00, /* wcChar6 */
- 'S', 0x00, /* wcChar7 */
- 'B', 0x00, /* wcChar8 */
- ///////////////////////////////////////
- /// string2 descriptor
- ///////////////////////////////////////
- 0x26, /* bLength */
- USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
- 'C', 0x00, /* wcChar0 */
- 'h', 0x00, /* wcChar1 */
- 'e', 0x00, /* wcChar2 */
- 'r', 0x00, /* wcChar3 */
- 'r', 0x00, /* wcChar4 */
- 'y', 0x00, /* wcChar5 */
- 'U', 0x00, /* wcChar6 */
- 'S', 0x00, /* wcChar7 */
- 'B', 0x00, /* wcChar8 */
- ' ', 0x00, /* wcChar9 */
- 'U', 0x00, /* wcChar10 */
- 'A', 0x00, /* wcChar11 */
- 'C', 0x00, /* wcChar12 */
- ' ', 0x00, /* wcChar13 */
- 'D', 0x00, /* wcChar14 */
- 'E', 0x00, /* wcChar15 */
- 'M', 0x00, /* wcChar16 */
- 'O', 0x00, /* wcChar17 */
- ///////////////////////////////////////
- /// string3 descriptor
- ///////////////////////////////////////
- 0x16, /* bLength */
- USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
- '2', 0x00, /* wcChar0 */
- '0', 0x00, /* wcChar1 */
- '2', 0x00, /* wcChar2 */
- '1', 0x00, /* wcChar3 */
- '0', 0x00, /* wcChar4 */
- '3', 0x00, /* wcChar5 */
- '1', 0x00, /* wcChar6 */
- '0', 0x00, /* wcChar7 */
- '0', 0x00, /* wcChar8 */
- #if USING_FEEDBACK == 0
- '3', 0x00, /* wcChar9 */
- #else
- '4', 0x00, /* wcChar9 */
- #endif
- #ifdef CONFIG_USB_HS
- ///////////////////////////////////////
- /// device qualifier descriptor
- ///////////////////////////////////////
- 0x0a,
- USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
- 0x00,
- 0x02,
- 0x00,
- 0x00,
- 0x00,
- 0x40,
- 0x00,
- 0x00,
- #endif
- 0x00
- };
- #endif
- static const uint8_t default_sampling_freq_table[] = {
- AUDIO_SAMPLE_FREQ_NUM(5),
- AUDIO_SAMPLE_FREQ_4B(8000),
- AUDIO_SAMPLE_FREQ_4B(8000),
- AUDIO_SAMPLE_FREQ_4B(0x00),
- AUDIO_SAMPLE_FREQ_4B(16000),
- AUDIO_SAMPLE_FREQ_4B(16000),
- AUDIO_SAMPLE_FREQ_4B(0x00),
- AUDIO_SAMPLE_FREQ_4B(32000),
- AUDIO_SAMPLE_FREQ_4B(32000),
- AUDIO_SAMPLE_FREQ_4B(0x00),
- AUDIO_SAMPLE_FREQ_4B(48000),
- AUDIO_SAMPLE_FREQ_4B(48000),
- AUDIO_SAMPLE_FREQ_4B(0x00),
- AUDIO_SAMPLE_FREQ_4B(96000),
- AUDIO_SAMPLE_FREQ_4B(96000),
- AUDIO_SAMPLE_FREQ_4B(0x00),
- };
- USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[AUDIO_OUT_PACKET];
- USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t s_speaker_feedback_buffer[4];
- volatile bool rx_flag = 0;
- static void usbd_event_handler(uint8_t busid, uint8_t event)
- {
- switch (event) {
- case USBD_EVENT_RESET:
- break;
- case USBD_EVENT_CONNECTED:
- break;
- case USBD_EVENT_DISCONNECTED:
- break;
- case USBD_EVENT_RESUME:
- break;
- case USBD_EVENT_SUSPEND:
- break;
- case USBD_EVENT_CONFIGURED:
- break;
- case USBD_EVENT_SET_REMOTE_WAKEUP:
- break;
- case USBD_EVENT_CLR_REMOTE_WAKEUP:
- break;
- default:
- break;
- }
- }
- void usbd_audio_open(uint8_t busid, uint8_t intf)
- {
- rx_flag = 1;
- /* setup first out ep read transfer */
- usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
- #ifdef CONFIG_USB_HS
- uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_HS(AUDIO_FREQ);
- AUDIO_FEEDBACK_TO_BUF_HS(s_speaker_feedback_buffer, feedback_value);
- #else
- uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_FREQ);
- AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
- #endif
- usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
- USB_LOG_RAW("OPEN\r\n");
- }
- void usbd_audio_close(uint8_t busid, uint8_t intf)
- {
- USB_LOG_RAW("CLOSE\r\n");
- rx_flag = 0;
- }
- void usbd_audio_get_sampling_freq_table(uint8_t busid, uint8_t ep, uint8_t **sampling_freq_table)
- {
- if (ep == AUDIO_OUT_EP) {
- *sampling_freq_table = (uint8_t *)default_sampling_freq_table;
- }
- }
- void usbd_audio_iso_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
- {
- USB_LOG_RAW("actual out len:%d\r\n", nbytes);
- usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
- }
- #if USING_FEEDBACK == 1
- void usbd_audio_iso_out_feedback_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
- {
- USB_LOG_RAW("actual feedback len:%d\r\n", nbytes);
- #ifdef CONFIG_USB_HS
- uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_HS(AUDIO_FREQ);
- AUDIO_FEEDBACK_TO_BUF_HS(s_speaker_feedback_buffer, feedback_value);
- #else
- uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_FREQ);
- AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
- #endif
- usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
- }
- #endif
- static struct usbd_endpoint audio_out_ep = {
- .ep_cb = usbd_audio_iso_out_callback,
- .ep_addr = AUDIO_OUT_EP
- };
- #if USING_FEEDBACK == 1
- static struct usbd_endpoint audio_out_feedback_ep = {
- .ep_cb = usbd_audio_iso_out_feedback_callback,
- .ep_addr = AUDIO_OUT_FEEDBACK_EP
- };
- #endif
- struct usbd_interface intf0;
- struct usbd_interface intf1;
- struct audio_entity_info audio_entity_table[] = {
- { .bEntityId = AUDIO_OUT_CLOCK_ID,
- .bDescriptorSubtype = AUDIO_CONTROL_CLOCK_SOURCE,
- .ep = AUDIO_OUT_EP },
- { .bEntityId = AUDIO_OUT_FU_ID,
- .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
- .ep = AUDIO_OUT_EP },
- };
- void audio_v2_init(uint8_t busid, uintptr_t reg_base)
- {
- #ifdef CONFIG_USBDEV_ADVANCE_DESC
- usbd_desc_register(busid, &audio_v2_descriptor);
- #else
- usbd_desc_register(busid, audio_v2_descriptor);
- #endif
- usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0200, audio_entity_table, 2));
- usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0200, audio_entity_table, 2));
- usbd_add_endpoint(busid, &audio_out_ep);
- #if USING_FEEDBACK == 1
- usbd_add_endpoint(busid, &audio_out_feedback_ep);
- #endif
- usbd_initialize(busid, reg_base, usbd_event_handler);
- }
- void audio_v2_test(uint8_t busid)
- {
- if (rx_flag) {
- }
- }
|