2024-04-16 22:11:30 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, sakumisu
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
2022-03-26 16:07:46 +08:00
|
|
|
#include "usbd_core.h"
|
|
|
|
|
#include "usbd_audio.h"
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
#else
|
|
|
|
|
#define EP_INTERVAL 0x01
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define AUDIO_IN_EP 0x81
|
|
|
|
|
|
2023-06-21 20:43:35 +08:00
|
|
|
#define AUDIO_IN_FU_ID 0x02
|
|
|
|
|
|
2025-12-18 21:59:48 +08:00
|
|
|
#define AUDIO_MIC_FREQ 16000U
|
|
|
|
|
#define AUDIO_MIC_FRAME_SIZE_BYTE 2u
|
|
|
|
|
#define AUDIO_MIC_RESOLUTION_BIT 16u
|
2022-03-26 16:07:46 +08:00
|
|
|
|
2025-12-05 18:32:10 +08:00
|
|
|
#define IN_CHANNEL_NUM 2
|
2022-06-07 22:08:25 +08:00
|
|
|
|
|
|
|
|
#if IN_CHANNEL_NUM == 1
|
|
|
|
|
#define INPUT_CTRL 0x03, 0x03
|
2025-12-05 18:32:10 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x0001
|
2023-01-02 10:53:30 +08:00
|
|
|
#elif IN_CHANNEL_NUM == 2
|
|
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x0003
|
|
|
|
|
#elif IN_CHANNEL_NUM == 3
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x0007
|
|
|
|
|
#elif IN_CHANNEL_NUM == 4
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x000f
|
|
|
|
|
#elif IN_CHANNEL_NUM == 5
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x001f
|
|
|
|
|
#elif IN_CHANNEL_NUM == 6
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x003F
|
|
|
|
|
#elif IN_CHANNEL_NUM == 7
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x007f
|
|
|
|
|
#elif IN_CHANNEL_NUM == 8
|
2023-01-02 10:53:30 +08:00
|
|
|
#define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
|
2022-06-07 22:08:25 +08:00
|
|
|
#define INPUT_CH_ENABLE 0x00ff
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-03-26 16:07:46 +08:00
|
|
|
/* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 1) */
|
|
|
|
|
/* 16bit(2 Bytes) 单声道(Mono:1) */
|
2025-12-18 21:59:48 +08:00
|
|
|
#define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * 2 * IN_CHANNEL_NUM) / 1000))
|
2022-03-26 16:07:46 +08:00
|
|
|
|
2025-12-05 18:32:10 +08:00
|
|
|
#define USB_CONFIG_SIZE (unsigned long)(9 + \
|
|
|
|
|
AUDIO_AC_DESCRIPTOR_LEN(1) + \
|
|
|
|
|
AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
|
|
|
|
|
AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
|
|
|
|
|
AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
|
|
|
|
|
AUDIO_AS_DESCRIPTOR_LEN(1))
|
2022-03-26 16:07:46 +08:00
|
|
|
|
2022-06-07 22:08:25 +08:00
|
|
|
#define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) + \
|
|
|
|
|
AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
|
2023-01-02 10:53:30 +08:00
|
|
|
AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
|
2022-04-08 14:24:41 +08:00
|
|
|
AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
|
|
|
|
|
|
2025-02-01 19:13:44 +08:00
|
|
|
static const uint8_t device_descriptor[] = {
|
|
|
|
|
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const uint8_t config_descriptor[] = {
|
2025-12-05 18:32:10 +08:00
|
|
|
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
|
2025-02-01 19:13:44 +08:00
|
|
|
AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
|
|
|
|
|
AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
|
|
|
|
|
AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
|
|
|
|
|
AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
|
2025-12-18 21:59:48 +08:00
|
|
|
AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ))
|
2025-02-01 19:13:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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_v1_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
|
|
|
|
|
};
|
2022-03-26 16:07:46 +08:00
|
|
|
|
|
|
|
|
volatile bool tx_flag = 0;
|
2022-08-21 13:03:26 +08:00
|
|
|
volatile bool ep_tx_busy_flag = false;
|
2025-05-30 16:25:53 +08:00
|
|
|
volatile uint32_t s_mic_sample_rate;
|
2022-03-26 16:07:46 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
static void usbd_event_handler(uint8_t busid, uint8_t event)
|
2023-06-21 21:26:25 +08:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 19:51:50 +08:00
|
|
|
void usbd_audio_open(uint8_t busid, uint8_t intf)
|
2022-03-26 16:07:46 +08:00
|
|
|
{
|
|
|
|
|
tx_flag = 1;
|
2024-03-23 17:50:49 +08:00
|
|
|
ep_tx_busy_flag = false;
|
2022-06-04 21:08:19 +08:00
|
|
|
USB_LOG_RAW("OPEN\r\n");
|
2022-03-26 16:07:46 +08:00
|
|
|
}
|
2023-06-21 21:26:25 +08:00
|
|
|
|
2024-02-06 19:51:50 +08:00
|
|
|
void usbd_audio_close(uint8_t busid, uint8_t intf)
|
2022-03-26 16:07:46 +08:00
|
|
|
{
|
2022-06-04 21:08:19 +08:00
|
|
|
USB_LOG_RAW("CLOSE\r\n");
|
2024-03-23 17:50:49 +08:00
|
|
|
ep_tx_busy_flag = false;
|
2022-03-26 16:07:46 +08:00
|
|
|
tx_flag = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-30 16:25:53 +08:00
|
|
|
void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq)
|
|
|
|
|
{
|
|
|
|
|
if (ep == AUDIO_IN_EP) {
|
|
|
|
|
s_mic_sample_rate = sampling_freq;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t usbd_audio_get_sampling_freq(uint8_t busid, uint8_t ep)
|
|
|
|
|
{
|
|
|
|
|
(void)busid;
|
|
|
|
|
|
|
|
|
|
uint32_t freq = 0;
|
|
|
|
|
|
|
|
|
|
if (ep == AUDIO_IN_EP) {
|
|
|
|
|
freq = s_mic_sample_rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return freq;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 19:51:50 +08:00
|
|
|
void usbd_audio_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
|
2022-03-26 16:07:46 +08:00
|
|
|
{
|
2025-03-28 22:39:30 +08:00
|
|
|
USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
|
2022-08-21 13:03:26 +08:00
|
|
|
ep_tx_busy_flag = false;
|
2022-03-26 16:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-20 16:20:23 +08:00
|
|
|
static struct usbd_endpoint audio_in_ep = {
|
2022-03-26 16:07:46 +08:00
|
|
|
.ep_cb = usbd_audio_iso_callback,
|
|
|
|
|
.ep_addr = AUDIO_IN_EP
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-20 21:22:08 +08:00
|
|
|
struct usbd_interface intf0;
|
|
|
|
|
struct usbd_interface intf1;
|
|
|
|
|
|
2023-06-21 20:43:35 +08:00
|
|
|
struct audio_entity_info audio_entity_table[] = {
|
|
|
|
|
{ .bEntityId = AUDIO_IN_FU_ID,
|
|
|
|
|
.bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
|
|
|
|
|
.ep = AUDIO_IN_EP },
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-01 12:28:27 +08:00
|
|
|
// In windows, audio driver cannot remove auto, so when you modify any descriptor information, please modify string descriptors too.
|
|
|
|
|
|
2024-08-09 22:49:46 +08:00
|
|
|
void audio_v1_init(uint8_t busid, uintptr_t reg_base)
|
2022-03-26 16:07:46 +08:00
|
|
|
{
|
2025-02-01 19:13:44 +08:00
|
|
|
usbd_desc_register(busid, &audio_v1_descriptor);
|
2026-01-24 17:39:42 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 1));
|
|
|
|
|
usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 1));
|
|
|
|
|
usbd_add_endpoint(busid, &audio_in_ep);
|
2022-08-20 16:20:23 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
usbd_initialize(busid, reg_base, usbd_event_handler);
|
2022-03-26 16:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-22 14:06:12 +08:00
|
|
|
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
|
2022-08-21 13:03:26 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
void audio_test(uint8_t busid)
|
2022-03-26 16:07:46 +08:00
|
|
|
{
|
|
|
|
|
while (1) {
|
|
|
|
|
if (tx_flag) {
|
2023-04-22 14:06:12 +08:00
|
|
|
memset(write_buffer, 'a', AUDIO_IN_PACKET);
|
2022-08-21 13:03:26 +08:00
|
|
|
ep_tx_busy_flag = true;
|
2024-02-18 19:36:20 +08:00
|
|
|
usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
|
2022-08-21 13:03:26 +08:00
|
|
|
while (ep_tx_busy_flag) {
|
2023-04-22 14:06:12 +08:00
|
|
|
if (tx_flag == false) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-08-21 13:03:26 +08:00
|
|
|
}
|
2022-03-26 16:07:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-08 14:24:41 +08:00
|
|
|
}
|