1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbd_core.h"
7 #include "usbd_audio.h"
8 
9 #define USBD_VID           0xffff
10 #define USBD_PID           0xffff
11 #define USBD_MAX_POWER     100
12 #define USBD_LANGID_STRING 1033
13 
14 #ifdef CONFIG_USB_HS
15 #define EP_INTERVAL 0x04
16 #else
17 #define EP_INTERVAL 0x01
18 #endif
19 
20 #define AUDIO_IN_EP 0x81
21 
22 #define AUDIO_IN_FU_ID 0x02
23 
24 /* AUDIO Class Config */
25 #define AUDIO_FREQ 16000U
26 
27 #define IN_CHANNEL_NUM 1
28 
29 #if IN_CHANNEL_NUM == 1
30 #define INPUT_CTRL      0x03, 0x03
31 #define INPUT_CH_ENABLE 0x0000
32 #elif IN_CHANNEL_NUM == 2
33 #define INPUT_CTRL      0x03, 0x03, 0x03
34 #define INPUT_CH_ENABLE 0x0003
35 #elif IN_CHANNEL_NUM == 3
36 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03
37 #define INPUT_CH_ENABLE 0x0007
38 #elif IN_CHANNEL_NUM == 4
39 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03, 0x03
40 #define INPUT_CH_ENABLE 0x000f
41 #elif IN_CHANNEL_NUM == 5
42 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03, 0x03, 0x03
43 #define INPUT_CH_ENABLE 0x001f
44 #elif IN_CHANNEL_NUM == 6
45 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
46 #define INPUT_CH_ENABLE 0x003F
47 #elif IN_CHANNEL_NUM == 7
48 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
49 #define INPUT_CH_ENABLE 0x007f
50 #elif IN_CHANNEL_NUM == 8
51 #define INPUT_CTRL      0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
52 #define INPUT_CH_ENABLE 0x00ff
53 #endif
54 
55 /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 1) */
56 /* 16bit(2 Bytes) 单声道(Mono:1) */
57 #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_FREQ * 2 * IN_CHANNEL_NUM) / 1000))
58 
59 #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 +                                                    \
60                                                   AUDIO_AC_DESCRIPTOR_INIT_LEN(1) +                      \
61                                                   AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +                  \
62                                                   AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
63                                                   AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +                 \
64                                                   AUDIO_AS_DESCRIPTOR_INIT_LEN(1))
65 
66 #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) +                       \
67                       AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +                  \
68                       AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
69                       AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
70 
71 #ifdef CONFIG_USBDEV_ADVANCE_DESC
72 static const uint8_t device_descriptor[] = {
73     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
74 };
75 
76 static const uint8_t config_descriptor[] = {
77     USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
78     AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
79     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
80     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
81     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
82     AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, 2, 16, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ))
83 };
84 
85 static const uint8_t device_quality_descriptor[] = {
86     ///////////////////////////////////////
87     /// device qualifier descriptor
88     ///////////////////////////////////////
89     0x0a,
90     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
91     0x00,
92     0x02,
93     0x00,
94     0x00,
95     0x00,
96     0x40,
97     0x00,
98     0x00,
99 };
100 
101 static const char *string_descriptors[] = {
102     (const char[]){ 0x09, 0x04 }, /* Langid */
103     "CherryUSB",                  /* Manufacturer */
104     "CherryUSB UAC DEMO",         /* Product */
105     "2022123456",                 /* Serial Number */
106 };
107 
device_descriptor_callback(uint8_t speed)108 static const uint8_t *device_descriptor_callback(uint8_t speed)
109 {
110     return device_descriptor;
111 }
112 
config_descriptor_callback(uint8_t speed)113 static const uint8_t *config_descriptor_callback(uint8_t speed)
114 {
115     return config_descriptor;
116 }
117 
device_quality_descriptor_callback(uint8_t speed)118 static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
119 {
120     return device_quality_descriptor;
121 }
122 
string_descriptor_callback(uint8_t speed,uint8_t index)123 static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
124 {
125     if (index > 3) {
126         return NULL;
127     }
128     return string_descriptors[index];
129 }
130 
131 const struct usb_descriptor audio_v1_descriptor = {
132     .device_descriptor_callback = device_descriptor_callback,
133     .config_descriptor_callback = config_descriptor_callback,
134     .device_quality_descriptor_callback = device_quality_descriptor_callback,
135     .string_descriptor_callback = string_descriptor_callback
136 };
137 #else
138 const uint8_t audio_v1_descriptor[] = {
139     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
140     USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
141     AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
142     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
143     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
144     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
145     AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, 2, 16, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ)),
146     ///////////////////////////////////////
147     /// string0 descriptor
148     ///////////////////////////////////////
149     USB_LANGID_INIT(USBD_LANGID_STRING),
150     ///////////////////////////////////////
151     /// string1 descriptor
152     ///////////////////////////////////////
153     0x14,                       /* bLength */
154     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
155     'C', 0x00,                  /* wcChar0 */
156     'h', 0x00,                  /* wcChar1 */
157     'e', 0x00,                  /* wcChar2 */
158     'r', 0x00,                  /* wcChar3 */
159     'r', 0x00,                  /* wcChar4 */
160     'y', 0x00,                  /* wcChar5 */
161     'U', 0x00,                  /* wcChar6 */
162     'S', 0x00,                  /* wcChar7 */
163     'B', 0x00,                  /* wcChar8 */
164     ///////////////////////////////////////
165     /// string2 descriptor
166     ///////////////////////////////////////
167     0x26,                       /* bLength */
168     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
169     'C', 0x00,                  /* wcChar0 */
170     'h', 0x00,                  /* wcChar1 */
171     'e', 0x00,                  /* wcChar2 */
172     'r', 0x00,                  /* wcChar3 */
173     'r', 0x00,                  /* wcChar4 */
174     'y', 0x00,                  /* wcChar5 */
175     'U', 0x00,                  /* wcChar6 */
176     'S', 0x00,                  /* wcChar7 */
177     'B', 0x00,                  /* wcChar8 */
178     ' ', 0x00,                  /* wcChar9 */
179     'U', 0x00,                  /* wcChar10 */
180     'A', 0x00,                  /* wcChar11 */
181     'C', 0x00,                  /* wcChar12 */
182     ' ', 0x00,                  /* wcChar13 */
183     'D', 0x00,                  /* wcChar14 */
184     'E', 0x00,                  /* wcChar15 */
185     'M', 0x00,                  /* wcChar16 */
186     'O', 0x00,                  /* wcChar17 */
187     ///////////////////////////////////////
188     /// string3 descriptor
189     ///////////////////////////////////////
190     0x16,                       /* bLength */
191     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
192     '2', 0x00,                  /* wcChar0 */
193     '0', 0x00,                  /* wcChar1 */
194     '2', 0x00,                  /* wcChar2 */
195     '2', 0x00,                  /* wcChar3 */
196     '1', 0x00,                  /* wcChar4 */
197     '2', 0x00,                  /* wcChar5 */
198     '3', 0x00,                  /* wcChar6 */
199     '4', 0x00,                  /* wcChar7 */
200     '5', 0x00,                  /* wcChar8 */
201     '0' + IN_CHANNEL_NUM, 0x00, /* wcChar9 */
202 #ifdef CONFIG_USB_HS
203     ///////////////////////////////////////
204     /// device qualifier descriptor
205     ///////////////////////////////////////
206     0x0a,
207     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
208     0x00,
209     0x02,
210     0x00,
211     0x00,
212     0x00,
213     0x40,
214     0x00,
215     0x00,
216 #endif
217     0x00
218 };
219 #endif
220 
221 volatile bool tx_flag = 0;
222 volatile bool ep_tx_busy_flag = false;
223 volatile uint32_t s_mic_sample_rate;
224 
usbd_event_handler(uint8_t busid,uint8_t event)225 static void usbd_event_handler(uint8_t busid, uint8_t event)
226 {
227     switch (event) {
228         case USBD_EVENT_RESET:
229             break;
230         case USBD_EVENT_CONNECTED:
231             break;
232         case USBD_EVENT_DISCONNECTED:
233             break;
234         case USBD_EVENT_RESUME:
235             break;
236         case USBD_EVENT_SUSPEND:
237             break;
238         case USBD_EVENT_CONFIGURED:
239             break;
240         case USBD_EVENT_SET_REMOTE_WAKEUP:
241             break;
242         case USBD_EVENT_CLR_REMOTE_WAKEUP:
243             break;
244 
245         default:
246             break;
247     }
248 }
249 
usbd_audio_open(uint8_t busid,uint8_t intf)250 void usbd_audio_open(uint8_t busid, uint8_t intf)
251 {
252     tx_flag = 1;
253     ep_tx_busy_flag = false;
254     USB_LOG_RAW("OPEN\r\n");
255 }
256 
usbd_audio_close(uint8_t busid,uint8_t intf)257 void usbd_audio_close(uint8_t busid, uint8_t intf)
258 {
259     USB_LOG_RAW("CLOSE\r\n");
260     ep_tx_busy_flag = false;
261     tx_flag = 0;
262 }
263 
usbd_audio_set_sampling_freq(uint8_t busid,uint8_t ep,uint32_t sampling_freq)264 void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq)
265 {
266     if (ep == AUDIO_IN_EP) {
267         s_mic_sample_rate = sampling_freq;
268     }
269 }
270 
usbd_audio_get_sampling_freq(uint8_t busid,uint8_t ep)271 uint32_t usbd_audio_get_sampling_freq(uint8_t busid, uint8_t ep)
272 {
273     (void)busid;
274 
275     uint32_t freq = 0;
276 
277     if (ep == AUDIO_IN_EP) {
278         freq = s_mic_sample_rate;
279     }
280 
281     return freq;
282 }
283 
usbd_audio_iso_callback(uint8_t busid,uint8_t ep,uint32_t nbytes)284 void usbd_audio_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
285 {
286     USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
287     ep_tx_busy_flag = false;
288 }
289 
290 static struct usbd_endpoint audio_in_ep = {
291     .ep_cb = usbd_audio_iso_callback,
292     .ep_addr = AUDIO_IN_EP
293 };
294 
295 struct usbd_interface intf0;
296 struct usbd_interface intf1;
297 
298 struct audio_entity_info audio_entity_table[] = {
299     { .bEntityId = AUDIO_IN_FU_ID,
300       .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
301       .ep = AUDIO_IN_EP },
302 };
303 
audio_v1_init(uint8_t busid,uintptr_t reg_base)304 void audio_v1_init(uint8_t busid, uintptr_t reg_base)
305 {
306 #ifdef CONFIG_USBDEV_ADVANCE_DESC
307     usbd_desc_register(busid, &audio_v1_descriptor);
308 #else
309     usbd_desc_register(busid, audio_v1_descriptor);
310 #endif
311     usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 1));
312     usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 1));
313     usbd_add_endpoint(busid, &audio_in_ep);
314 
315     usbd_initialize(busid, reg_base, usbd_event_handler);
316 }
317 
318 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
319 
audio_test(uint8_t busid)320 void audio_test(uint8_t busid)
321 {
322     while (1) {
323         if (tx_flag) {
324             memset(write_buffer, 'a', AUDIO_IN_PACKET);
325             ep_tx_busy_flag = true;
326             usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
327             while (ep_tx_busy_flag) {
328                 if (tx_flag == false) {
329                     break;
330                 }
331             }
332         }
333     }
334 }
335