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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jerzy Kasenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
#include <string.h>
#include "bsp/board_api.h"
#include "tusb.h"
#include "usb_descriptors.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTOTYPES
//--------------------------------------------------------------------+
// List of supported sample rates
const uint32_t sample_rates[] = {44100, 48000};
uint32_t current_sample_rate = 44100;
#define N_SAMPLE_RATES TU_ARRAY_SIZE(sample_rates)
/* Blink pattern
* - 25 ms : streaming data
* - 250 ms : device not mounted
* - 1000 ms : device mounted
* - 2500 ms : device is suspended
*/
enum {
BLINK_STREAMING = 25,
BLINK_NOT_MOUNTED = 250,
BLINK_MOUNTED = 1000,
BLINK_SUSPENDED = 2500,
};
enum {
VOLUME_CTRL_0_DB = 0,
VOLUME_CTRL_10_DB = 2560,
VOLUME_CTRL_20_DB = 5120,
VOLUME_CTRL_30_DB = 7680,
VOLUME_CTRL_40_DB = 10240,
VOLUME_CTRL_50_DB = 12800,
VOLUME_CTRL_60_DB = 15360,
VOLUME_CTRL_70_DB = 17920,
VOLUME_CTRL_80_DB = 20480,
VOLUME_CTRL_90_DB = 23040,
VOLUME_CTRL_100_DB = 25600,
VOLUME_CTRL_SILENCE = 0x8000,
};
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
// Audio controls
// Current states
uint8_t mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1]; // +1 for master channel 0
int16_t volume[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1];// +1 for master channel 0
// Buffer for microphone data
int32_t mic_buf[CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ / 4];
// Buffer for speaker data
int32_t spk_buf[CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_SZ / 4];
// Speaker data size received in the last frame
int spk_data_size;
// Resolution per format
const uint8_t resolutions_per_format[CFG_TUD_AUDIO_FUNC_1_N_FORMATS] = {CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX,
CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX};
// Current resolution, update on format change
uint8_t current_resolution;
void led_blinking_task(void);
void audio_task(void);
void audio_control_task(void);
/*------------- MAIN -------------*/
int main(void) {
board_init();
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {
.role = TUSB_ROLE_DEVICE,
.speed = TUSB_SPEED_AUTO};
tusb_init(BOARD_TUD_RHPORT, &dev_init);
board_init_after_tusb();
TU_LOG1("Headset running\r\n");
while (1) {
tud_task();// TinyUSB device task
audio_task();
audio_control_task();
led_blinking_task();
}
}
//--------------------------------------------------------------------+
// Device callbacks
//--------------------------------------------------------------------+
// Invoked when device is mounted
void tud_mount_cb(void) {
blink_interval_ms = BLINK_MOUNTED;
}
// Invoked when device is unmounted
void tud_umount_cb(void) {
blink_interval_ms = BLINK_NOT_MOUNTED;
}
// Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en) {
(void) remote_wakeup_en;
blink_interval_ms = BLINK_SUSPENDED;
}
// Invoked when usb bus is resumed
void tud_resume_cb(void) {
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
}
//--------------------------------------------------------------------+
// Audio Callback Functions
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// UAC1 Helper Functions
//--------------------------------------------------------------------+
static bool audio10_set_req_ep(tusb_control_request_t const *p_request, uint8_t *pBuff) {
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
switch (ctrlSel) {
case AUDIO10_EP_CTRL_SAMPLING_FREQ:
if (p_request->bRequest == AUDIO10_CS_REQ_SET_CUR) {
// Request uses 3 bytes
TU_VERIFY(p_request->wLength == 3);
current_sample_rate = tu_unaligned_read32(pBuff) & 0x00FFFFFF;
TU_LOG2("EP set current freq: %" PRIu32 "\r\n", current_sample_rate);
return true;
}
break;
// Unknown/Unsupported control
default:
TU_BREAKPOINT();
return false;
}
return false;
}
static bool audio10_get_req_ep(uint8_t rhport, tusb_control_request_t const *p_request) {
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
switch (ctrlSel) {
case AUDIO10_EP_CTRL_SAMPLING_FREQ:
if (p_request->bRequest == AUDIO10_CS_REQ_GET_CUR) {
TU_LOG2("EP get current freq\r\n");
uint8_t freq[3];
freq[0] = (uint8_t) (current_sample_rate & 0xFF);
freq[1] = (uint8_t) ((current_sample_rate >> 8) & 0xFF);
freq[2] = (uint8_t) ((current_sample_rate >> 16) & 0xFF);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, freq, sizeof(freq));
}
break;
// Unknown/Unsupported control
default:
TU_BREAKPOINT();
return false;
}
return false;
}
static bool audio10_set_req_entity(tusb_control_request_t const *p_request, uint8_t *pBuff) {
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
// If request is for our speaker feature unit
if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) {
switch (ctrlSel) {
case AUDIO10_FU_CTRL_MUTE:
switch (p_request->bRequest) {
case AUDIO10_CS_REQ_SET_CUR:
// Only 1st form is supported
TU_VERIFY(p_request->wLength == 1);
mute[channelNum] = pBuff[0];
TU_LOG2(" Set Mute: %d of channel: %u\r\n", mute[channelNum], channelNum);
return true;
default:
return false; // not supported
}
case AUDIO10_FU_CTRL_VOLUME:
switch (p_request->bRequest) {
case AUDIO10_CS_REQ_SET_CUR:
// Only 1st form is supported
TU_VERIFY(p_request->wLength == 2);
volume[channelNum] = (int16_t)tu_unaligned_read16(pBuff) / 256;
TU_LOG2(" Set Volume: %d dB of channel: %u\r\n", volume[channelNum], channelNum);
return true;
default:
return false; // not supported
}
// Unknown/Unsupported control
default:
TU_BREAKPOINT();
return false;
}
}
return false;
}
static bool audio10_get_req_entity(uint8_t rhport, tusb_control_request_t const *p_request) {
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
// If request is for our speaker feature unit
if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) {
switch (ctrlSel) {
case AUDIO10_FU_CTRL_MUTE:
// Audio control mute cur parameter block consists of only one byte - we thus can send it right away
// There does not exist a range parameter block for mute
TU_LOG2(" Get Mute of channel: %u\r\n", channelNum);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &mute[channelNum], 1);
case AUDIO10_FU_CTRL_VOLUME:
switch (p_request->bRequest) {
case AUDIO10_CS_REQ_GET_CUR:
TU_LOG2(" Get Volume of channel: %u\r\n", channelNum);
{
int16_t vol = (int16_t) volume[channelNum];
vol = vol * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &vol, sizeof(vol));
}
case AUDIO10_CS_REQ_GET_MIN:
TU_LOG2(" Get Volume min of channel: %u\r\n", channelNum);
{
int16_t min = -90; // -90 dB
min = min * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &min, sizeof(min));
}
case AUDIO10_CS_REQ_GET_MAX:
TU_LOG2(" Get Volume max of channel: %u\r\n", channelNum);
{
int16_t max = 30; // +30 dB
max = max * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &max, sizeof(max));
}
case AUDIO10_CS_REQ_GET_RES:
TU_LOG2(" Get Volume res of channel: %u\r\n", channelNum);
{
int16_t res = 1; // 1 dB
res = res * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &res, sizeof(res));
}
// Unknown/Unsupported control
default:
TU_BREAKPOINT();
return false;
}
break;
// Unknown/Unsupported control
default:
TU_BREAKPOINT();
return false;
}
}
return false;
}
//--------------------------------------------------------------------+
// UAC2 Helper Functions
//--------------------------------------------------------------------+
#if TUD_OPT_HIGH_SPEED
// Helper for clock get requests
static bool audio20_clock_get_request(uint8_t rhport, tusb_control_request_t const *p_request) {
uint8_t const ctrl_sel = TU_U16_HIGH(p_request->wValue);
if (ctrl_sel == AUDIO20_CS_CTRL_SAM_FREQ) {
if (p_request->bRequest == AUDIO20_CS_REQ_CUR) {
TU_LOG1("Clock get current freq %" PRIu32 "\r\n", current_sample_rate);
audio20_control_cur_4_t curf = {(int32_t) tu_htole32(current_sample_rate)};
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &curf, sizeof(curf));
} else if (p_request->bRequest == AUDIO20_CS_REQ_RANGE) {
audio20_control_range_4_n_t(N_SAMPLE_RATES) rangef =
{
.wNumSubRanges = tu_htole16(N_SAMPLE_RATES)};
TU_LOG1("Clock get %d freq ranges\r\n", N_SAMPLE_RATES);
for (uint8_t i = 0; i < N_SAMPLE_RATES; i++) {
rangef.subrange[i].bMin = (int32_t) sample_rates[i];
rangef.subrange[i].bMax = (int32_t) sample_rates[i];
rangef.subrange[i].bRes = 0;
TU_LOG1("Range %d (%d, %d, %d)\r\n", i, (int) rangef.subrange[i].bMin, (int) rangef.subrange[i].bMax, (int) rangef.subrange[i].bRes);
}
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &rangef, sizeof(rangef));
}
} else if (ctrl_sel == AUDIO20_CS_CTRL_CLK_VALID &&
p_request->bRequest == AUDIO20_CS_REQ_CUR) {
audio20_control_cur_1_t cur_valid = {.bCur = 1};
TU_LOG1("Clock get is valid %u\r\n", cur_valid.bCur);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &cur_valid, sizeof(cur_valid));
}
TU_LOG1("Clock get request not supported, selector = %u, request = %u\r\n",
ctrl_sel, p_request->bRequest);
return false;
}
// Helper for clock set requests
static bool audio20_clock_set_request(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t const *buf) {
(void) rhport;
uint8_t const ctrl_sel = TU_U16_HIGH(p_request->wValue);
TU_VERIFY(p_request->bRequest == AUDIO20_CS_REQ_CUR);
if (ctrl_sel == AUDIO20_CS_CTRL_SAM_FREQ) {
TU_VERIFY(p_request->wLength == sizeof(audio20_control_cur_4_t));
current_sample_rate = (uint32_t) ((audio20_control_cur_4_t const *) buf)->bCur;
TU_LOG1("Clock set current freq: %" PRIu32 "\r\n", current_sample_rate);
return true;
} else {
TU_LOG1("Clock set request not supported, selector = %u, request = %u\r\n",
ctrl_sel, p_request->bRequest);
return false;
}
}
// Helper for feature unit get requests
static bool audio20_feature_unit_get_request(uint8_t rhport, tusb_control_request_t const *p_request) {
uint8_t const ctrl_sel = TU_U16_HIGH(p_request->wValue);
uint8_t const channel_num = TU_U16_LOW(p_request->wValue);
if (ctrl_sel == AUDIO20_FU_CTRL_MUTE && p_request->bRequest == AUDIO20_CS_REQ_CUR) {
audio20_control_cur_1_t mute1 = {.bCur = mute[channel_num]};
TU_LOG1("Get channel %u mute %d\r\n", channel_num, mute1.bCur);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &mute1, sizeof(mute1));
} else if (ctrl_sel == AUDIO20_FU_CTRL_VOLUME) {
if (p_request->bRequest == AUDIO20_CS_REQ_RANGE) {
audio20_control_range_2_n_t(1) range_vol = {
.wNumSubRanges = tu_htole16(1),
.subrange[0] = {.bMin = tu_htole16(-VOLUME_CTRL_50_DB), tu_htole16(VOLUME_CTRL_0_DB), tu_htole16(256)}};
TU_LOG1("Get channel %u volume range (%d, %d, %u) dB\r\n", channel_num,
range_vol.subrange[0].bMin / 256, range_vol.subrange[0].bMax / 256, range_vol.subrange[0].bRes / 256);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &range_vol, sizeof(range_vol));
} else if (p_request->bRequest == AUDIO20_CS_REQ_CUR) {
audio20_control_cur_2_t cur_vol = {.bCur = tu_htole16(volume[channel_num])};
TU_LOG1("Get channel %u volume %d dB\r\n", channel_num, cur_vol.bCur / 256);
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &cur_vol, sizeof(cur_vol));
}
}
TU_LOG1("Feature unit get request not supported, selector = %u, request = %u\r\n",
ctrl_sel, p_request->bRequest);
return false;
}
// Helper for feature unit set requests
static bool audio20_feature_unit_set_request(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t const *buf) {
(void) rhport;
uint8_t const ctrl_sel = TU_U16_HIGH(p_request->wValue);
uint8_t const channel_num = TU_U16_LOW(p_request->wValue);
TU_VERIFY(p_request->bRequest == AUDIO20_CS_REQ_CUR);
if (ctrl_sel == AUDIO20_FU_CTRL_MUTE) {
TU_VERIFY(p_request->wLength == sizeof(audio20_control_cur_1_t));
mute[channel_num] = ((audio20_control_cur_1_t const *) buf)->bCur;
TU_LOG1("Set channel %d Mute: %d\r\n", channel_num, mute[channel_num]);
return true;
} else if (ctrl_sel == AUDIO20_FU_CTRL_VOLUME) {
TU_VERIFY(p_request->wLength == sizeof(audio20_control_cur_2_t));
volume[channel_num] = ((audio20_control_cur_2_t const *) buf)->bCur;
TU_LOG1("Set channel %d volume: %d dB\r\n", channel_num, volume[channel_num] / 256);
return true;
} else {
TU_LOG1("Feature unit set request not supported, selector = %u, request = %u\r\n",
ctrl_sel, p_request->bRequest);
return false;
}
}
static bool audio20_get_req_entity(uint8_t rhport, tusb_control_request_t const *p_request) {
uint8_t const entity_id = TU_U16_HIGH(p_request->wIndex);
if (entity_id == UAC2_ENTITY_CLOCK)
return audio20_clock_get_request(rhport, p_request);
if (entity_id == UAC2_ENTITY_SPK_FEATURE_UNIT)
return audio20_feature_unit_get_request(rhport, p_request);
else {
TU_LOG1("Get request not handled, entity = %d, selector = %d, request = %d\r\n",
entity_id, TU_U16_HIGH(p_request->wValue), p_request->bRequest);
}
return false;
}
static bool audio20_set_req_entity(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t *buf) {
uint8_t const entity_id = TU_U16_HIGH(p_request->wIndex);
if (entity_id == UAC2_ENTITY_SPK_FEATURE_UNIT)
return audio20_feature_unit_set_request(rhport, p_request, buf);
if (entity_id == UAC2_ENTITY_CLOCK)
return audio20_clock_set_request(rhport, p_request, buf);
TU_LOG1("Set request not handled, entity = %d, selector = %d, request = %d\r\n",
entity_id, TU_U16_HIGH(p_request->wValue), p_request->bRequest);
return false;
}
#endif // TUD_OPT_HIGH_SPEED
// Invoked when audio class specific set request received for an EP
bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t *pBuff) {
(void) rhport;
(void) pBuff;
if (tud_audio_version() == 1) {
return audio10_set_req_ep(p_request, pBuff);
} else if (tud_audio_version() == 2) {
// We do not support any requests here
}
return false;// Yet not implemented
}
// Invoked when audio class specific get request received for an EP
bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
(void) rhport;
if (tud_audio_version() == 1) {
return audio10_get_req_ep(rhport, p_request);
} else if (tud_audio_version() == 2) {
// We do not support any requests here
}
return false;// Yet not implemented
}
// Invoked when audio class specific get request received for an entity
bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
(void) rhport;
if (tud_audio_version() == 1) {
return audio10_get_req_entity(rhport, p_request);
#if TUD_OPT_HIGH_SPEED
} else if (tud_audio_version() == 2) {
return audio20_get_req_entity(rhport, p_request);
#endif
}
return false;
}
// Invoked when audio class specific set request received for an entity
bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p_request, uint8_t *buf) {
(void) rhport;
if (tud_audio_version() == 1) {
return audio10_set_req_entity(p_request, buf);
#if TUD_OPT_HIGH_SPEED
} else if (tud_audio_version() == 2) {
return audio20_set_req_entity(rhport, p_request, buf);
#endif
}
return false;
}
bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
(void) rhport;
uint8_t const itf = tu_u16_low(p_request->wIndex);
uint8_t const alt = tu_u16_low(p_request->wValue);
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt == 0) {
blink_interval_ms = BLINK_MOUNTED;
}
return true;
}
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
(void) rhport;
uint8_t const itf = tu_u16_low(p_request->wIndex);
uint8_t const alt = tu_u16_low(p_request->wValue);
TU_LOG2("Set interface %d alt %d\r\n", itf, alt);
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt != 0) {
blink_interval_ms = BLINK_STREAMING;
}
// Clear buffer when streaming format is changed
spk_data_size = 0;
if (alt != 0) {
current_resolution = resolutions_per_format[alt - 1];
}
return true;
}
//--------------------------------------------------------------------+
// AUDIO Task
//--------------------------------------------------------------------+
// This task simulates an audio transfer callback, one frame is sent/received every 1ms.
// In a real application, this would be replaced with actual I2S send/receive callback.
void audio_task(void) {
static uint32_t start_ms = 0;
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms == curr_ms) return;// not enough time
start_ms = curr_ms;
// When new data arrived, copy data from speaker buffer, to microphone buffer
// and send it over
// Only support speaker & headphone both have the same resolution
// If one is 16bit another is 24bit be care of LOUD noise !
spk_data_size = tud_audio_read(spk_buf, sizeof(spk_buf));
if (spk_data_size) {
if (current_resolution == 16) {
int16_t *src = (int16_t *) spk_buf;
int16_t *limit = (int16_t *) spk_buf + spk_data_size / 2;
int16_t *dst = (int16_t *) mic_buf;
while (src < limit) {
// Combine two channels into one
int32_t left = *src++;
int32_t right = *src++;
*dst++ = (int16_t) ((left >> 1) + (right >> 1));
}
tud_audio_write((uint8_t *) mic_buf, (uint16_t) (spk_data_size / 2));
spk_data_size = 0;
} else if (current_resolution == 24) {
int32_t *src = spk_buf;
int32_t *limit = spk_buf + spk_data_size / 4;
int32_t *dst = mic_buf;
while (src < limit) {
// Combine two channels into one
int32_t left = *src++;
int32_t right = *src++;
*dst++ = (int32_t) ((uint32_t) ((left >> 1) + (right >> 1)) & 0xffffff00ul);
}
tud_audio_write((uint8_t *) mic_buf, (uint16_t) (spk_data_size / 2));
spk_data_size = 0;
}
}
}
void audio_control_task(void) {
// Press on-board button to control volume
// Open host volume control, volume should switch between 10% and 100%
// Poll every 50ms
const uint32_t interval_ms = 50;
static uint32_t start_ms = 0;
static uint32_t btn_prev = 0;
if (tusb_time_millis_api() - start_ms < interval_ms) return;// not enough time
start_ms += interval_ms;
uint32_t btn = board_button_read();
// Even UAC1 spec have status interrupt support like UAC2, most host do not support it
// So you have to either use UAC2 or use old day HID volume control
TU_VERIFY((tud_audio_version() == 1),);
if (!btn_prev && btn) {
// Adjust volume between 0dB (100%) and -30dB (10%)
for (int i = 0; i < CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1; i++) {
volume[i] = volume[i] == 0 ? -VOLUME_CTRL_30_DB : 0;
}
// 6.1 Interrupt Data Message
const audio_interrupt_data_t data = {.v2 = {
.bInfo = 0, // Class-specific interrupt, originated from an interface
.bAttribute = AUDIO20_CS_REQ_CUR, // Caused by current settings
.wValue_cn_or_mcn = 0, // CH0: master volume
.wValue_cs = AUDIO20_FU_CTRL_VOLUME, // Volume change
.wIndex_ep_or_int = 0, // From the interface itself
.wIndex_entity_id = UAC2_ENTITY_SPK_FEATURE_UNIT,// From feature unit
}};
tud_audio_int_write(&data);
}
btn_prev = btn;
}
//--------------------------------------------------------------------+
// BLINKING TASK
//--------------------------------------------------------------------+
void led_blinking_task(void) {
static uint32_t start_ms = 0;
static bool led_state = false;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) return;
start_ms += blink_interval_ms;
board_led_write(led_state);
led_state = 1 - led_state;
}
|