-
Notifications
You must be signed in to change notification settings - Fork 41
/
uac1_device_audio_task.c
executable file
·816 lines (687 loc) · 32.7 KB
/
uac1_device_audio_task.c
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/* -*- mode: c++; tab-width: 4; c-basic-offset: 4 -*- */
/* This source file is part of the ATMEL AVR32-SoftwareFramework-AT32UC3-1.5.0 Release */
/*This file is prepared for Doxygen automatic documentation generation.*/
/*! \file ******************************************************************
*
* \brief Management of the USB device Audio task.
*
* This file manages the USB device Audio task.
*
* - Compiler: IAR EWAVR32 and GNU GCC for AVR32
* - Supported devices: All AVR32 devices with a USB module can be used.
* - AppNote:
*
* \author Atmel Corporation: http://www.atmel.com \n
* Support and FAQ: http://support.atmel.no/
*
***************************************************************************/
/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an Atmel
* AVR product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* Modified by Alex Lee and SDR-Widget team for the sdr-widget project.
* See http://code.google.com/p/sdr-widget/
* Copyright under GNU General Public License v2
*/
//_____ I N C L U D E S ___________________________________________________
#include <stdio.h>
#include "usart.h" // Shall be included before FreeRTOS header files, since 'inline' is defined to ''; leading to
// link errors
#include "conf_usb.h"
#if USB_DEVICE_FEATURE == ENABLED
#include "board.h"
#include "features.h"
#ifdef FREERTOS_USED
#include "FreeRTOS.h"
#include "task.h"
#endif
#include "gpio.h"
#include "pdca.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "uac1_usb_descriptors.h"
#include "usb_standard_request.h"
#include "usb_specific_request.h"
#include "uac1_usb_specific_request.h"
#include "device_audio_task.h"
#include "uac1_device_audio_task.h"
#if LCD_DISPLAY // Multi-line LCD display
#include "taskLCD.h"
#endif
#include "composite_widget.h"
#include "taskAK5394A.h"
// To access input select constants
#include "Mobo_config.h"
//_____ M A C R O S ________________________________________________________
//_____ D E F I N I T I O N S ______________________________________________
// #define FB_RATE_DELTA (1<<12)
#define FB_RATE_DELTA 64 // BSB 20130603 stability??
#define FB_RATE_DELTA_NUM 2
// BSB 20130605 FB_rate calculation with 2 levels, imported from UAC2 code
#define SPK1_GAP_USKIP SPK_BUFFER_SIZE * 7 / 4 // Almost a full buffer up in distance => enable skip/insert
#define SPK1_GAP_U2 SPK_BUFFER_SIZE * 6 / 4 // A half buffer up in distance => Speed up host a lot
#define SPK1_GAP_U1 SPK_BUFFER_SIZE * 5 / 4 // A quarter buffer up in distance => Speed up host a bit
#define SPK1_GAP_NOM SPK_BUFFER_SIZE * 4 / 4 // Ideal distance is half the size of linear buffer
#define SPK1_GAP_L1 SPK_BUFFER_SIZE * 3 / 4 // A quarter buffer down in distance => Slow down host a bit
#define SPK1_GAP_L2 SPK_BUFFER_SIZE * 2 / 4 // A half buffer down in distance => Slow down host a lot
#define SPK1_GAP_LSKIP SPK_BUFFER_SIZE * 1 / 4 // Almost a full buffer down in distance => enable skip/insert
#define SPK1_PACKETS_PER_GAP_CALCULATION 8 // This is UAC1 which counts in ms. Gap calculation every 8ms, EP reporting every 32
#define SPK1_PACKETS_PER_GAP_SKIP 1 // After a skip/insert, recalculate gap immediately, then again after 1ms
#define SPK1_HOST_FB_DEAD_AFTER 200 // How many audio packets may arrive without host polling feedback, before we declare FB dead?
#define SPK1_SKIP_EN_GAP 1 // Enable skip/insert due to low gap
#define SPK1_SKIP_EN_DEAD 2 // Enable skip/insert due to dead host feedback system
#define SPK1_SKIP_LIMIT_14 2<<14 // 10.14 and 12.14 format. |accumulated error| must be > 2 samples.
//_____ D E C L A R A T I O N S ____________________________________________
//? why are these defined as statics?
static U32 index, spk_index;
static U16 old_gap = SPK_BUFFER_SIZE;
static U8 audio_buffer_out, spk_buffer_in; // the ID number of the buffer used for sending out to the USB
static volatile U32 *audio_buffer_ptr;
//static volatile U32 *spk_buffer_ptr;
static U8 ep_audio_in, ep_audio_out, ep_audio_out_fb;
//!
//! @brief This function initializes the hardware/software resources
//! required for device Audio task.
//!
void uac1_device_audio_task_init(U8 ep_in, U8 ep_out, U8 ep_out_fb)
{
index =0;
audio_buffer_out = 0;
audio_buffer_ptr = audio_buffer_0;
spk_index = 0;
spk_buffer_in = 0;
// spk_buffer_ptr = spk_buffer_0;
mute = FALSE;
spk_mute = FALSE;
// With working volume flash
// spk_vol_usb_L = usb_volume_flash(CH_LEFT, 0, VOL_READ); // Fetch stored or default volume setting
// spk_vol_usb_R = usb_volume_flash(CH_RIGHT, 0, VOL_READ);
// Without working volume flash, spk_vol_usb_? = VOL_DEFAULT is set in device_audio_task.c
spk_vol_mult_L = usb_volume_format(spk_vol_usb_L);
spk_vol_mult_R = usb_volume_format(spk_vol_usb_R);
ep_audio_in = ep_in;
ep_audio_out = ep_out;
ep_audio_out_fb = ep_out_fb;
xTaskCreate(uac1_device_audio_task,
configTSK_USB_DAUDIO_NAME,
configTSK_USB_DAUDIO_STACK_SIZE,
NULL,
configTSK_USB_DAUDIO_PRIORITY,
NULL);
}
//!
//! @brief Entry point of the device Audio task management
//!
void uac1_device_audio_task(void *pvParameters)
{
Bool playerStarted = FALSE; // BSB 20150516: changed into global variable
static U32 time=0;
static Bool startup=TRUE;
int i;
// int delta_num = 0;
U16 num_samples, num_remaining, gap;
S16 time_to_calculate_gap = 0; // BSB 20131101 New variables for skip/insert
U16 packets_since_feedback = 0;
U8 skip_enable = 0;
U8 skip_indicate = 0; // Should we show skipping on module LEDs?
U16 samples_to_transfer_OUT = 1; // Default value 1. Skip:0. Insert:2
S32 FB_error_acc = 0; // BSB 20131102 Accumulated error for skip/insert
U8 sample_HSB;
U8 sample_MSB;
U8 sample_SB;
U8 sample_LSB;
U8 toggle_07 = 0; // BSB 20131206 keep track of GPIO_07 / PX31
S32 sample_L = 0;
S32 sample_R = 0; // BSB 20131102 Expanded for skip/insert, 20160322 changed to S32!
const U8 EP_AUDIO_IN = ep_audio_in;
const U8 EP_AUDIO_OUT = ep_audio_out;
const U8 EP_AUDIO_OUT_FB = ep_audio_out_fb;
const U8 IN_LEFT = FEATURE_IN_NORMAL ? 0 : 1;
const U8 IN_RIGHT = FEATURE_IN_NORMAL ? 1 : 0;
const U8 OUT_LEFT = FEATURE_OUT_NORMAL ? 0 : 1;
const U8 OUT_RIGHT = FEATURE_OUT_NORMAL ? 1 : 0;
volatile avr32_pdca_channel_t *pdca_channel = pdca_get_handler(PDCA_CHANNEL_SSC_RX);
volatile avr32_pdca_channel_t *spk_pdca_channel = pdca_get_handler(PDCA_CHANNEL_SSC_TX);
uint32_t silence_USB = SILENCE_USB_LIMIT; // BSB 20150621: detect silence in USB channel, initially assume silence
uint32_t silence_det = 0;
// BSB 20130602: code section moved to uac1_usb_specific_request.c
// if (current_freq.frequency == 48000) FB_rate = 48 << 14;
// else FB_rate = (44 << 14) + (1 << 14)/10;
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
while (TRUE) {
vTaskDelayUntil(&xLastWakeTime, UAC1_configTSK_USB_DAUDIO_PERIOD);
// First, check the device enumeration state
if (!Is_device_enumerated()) { time=0; startup=TRUE; continue; };
if( startup ) {
time+=UAC1_configTSK_USB_DAUDIO_PERIOD;
#define STARTUP_LED_DELAY 10000
if ( time<= 1*STARTUP_LED_DELAY ) {
LED_On( LED0 );
pdca_disable_interrupt_reload_counter_zero(PDCA_CHANNEL_SSC_RX);
pdca_disable(PDCA_CHANNEL_SSC_RX);
// LED_On( LED1 );
} else if( time== 2*STARTUP_LED_DELAY ) LED_On( LED1 );
else if( time== 3*STARTUP_LED_DELAY ) LED_On( LED2 );
else if( time== 4*STARTUP_LED_DELAY ) LED_On( LED3 );
else if( time== 5*STARTUP_LED_DELAY ) LED_Off( LED0 );
else if( time== 6*STARTUP_LED_DELAY ) LED_Off( LED1 );
else if( time== 7*STARTUP_LED_DELAY ) LED_Off( LED2 );
else if( time== 8*STARTUP_LED_DELAY ) LED_Off( LED3 );
else if( time >= 9*STARTUP_LED_DELAY ) {
startup=FALSE;
audio_buffer_in = 0;
audio_buffer_out = 0;
spk_buffer_in = 0;
spk_buffer_out = 0; // Only place outside taskAK53984A.c where spk_buffer_out is written!
index = 0;
if (!FEATURE_ADC_NONE){
// Wait for the next frame synchronization event
// to avoid channel inversion. Start with left channel - FS goes low
while (!gpio_get_pin_value(AK5394_LRCK));
while (gpio_get_pin_value(AK5394_LRCK));
// Enable now the transfer.
pdca_enable(PDCA_CHANNEL_SSC_RX);
pdca_enable_interrupt_reload_counter_zero(PDCA_CHANNEL_SSC_RX);
}
}
}
//else {
num_samples = 48;
if (usb_alternate_setting == 1) {
if (Is_usb_in_ready(EP_AUDIO_IN)) { // Endpoint buffer free ?
Usb_ack_in_ready(EP_AUDIO_IN); // acknowledge in ready
// Sync AK data stream with USB data stream
// AK data is being filled into ~audio_buffer_in, ie if audio_buffer_in is 0
// buffer 0 is set in the reload register of the pdca
// So the actual loading is occuring in buffer 1
// USB data is being taken from audio_buffer_out
// find out the current status of PDCA transfer
// gap is how far the audio_buffer_out is from overlapping audio_buffer_in
num_remaining = pdca_channel->tcr;
if (audio_buffer_in != audio_buffer_out) {
// AK and USB using same buffer
if ( index < (AUDIO_BUFFER_SIZE - num_remaining)) gap = AUDIO_BUFFER_SIZE - num_remaining - index;
else gap = AUDIO_BUFFER_SIZE - index + AUDIO_BUFFER_SIZE - num_remaining + AUDIO_BUFFER_SIZE;
} else {
// usb and pdca working on different buffers
gap = (AUDIO_BUFFER_SIZE - index) + (AUDIO_BUFFER_SIZE - num_remaining);
}
// Sync the USB stream with the AK stream
// throttle back
if (gap < AUDIO_BUFFER_SIZE/2) {
num_samples--;
} else {
// speed up
if (gap > (AUDIO_BUFFER_SIZE + AUDIO_BUFFER_SIZE/2)) {
num_samples++;
}
}
Usb_reset_endpoint_fifo_access(EP_AUDIO_IN);
for( i=0 ; i < num_samples ; i++ ) {
// Fill endpoint with sample raw
if (mute==FALSE) {
if (audio_buffer_out == 0) {
sample_LSB = audio_buffer_0[index+IN_LEFT];
sample_SB = audio_buffer_0[index+IN_LEFT] >> 8;
sample_MSB = audio_buffer_0[index+IN_LEFT] >> 16;
} else {
sample_LSB = audio_buffer_1[index+IN_LEFT];
sample_SB = audio_buffer_1[index+IN_LEFT] >> 8;
sample_MSB = audio_buffer_1[index+IN_LEFT] >> 16;
}
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_LSB);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_SB);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_MSB);
if (audio_buffer_out == 0) {
sample_LSB = audio_buffer_0[index+IN_RIGHT];
sample_SB = audio_buffer_0[index+IN_RIGHT] >> 8;
sample_MSB = audio_buffer_0[index+IN_RIGHT] >> 16;
} else {
sample_LSB = audio_buffer_1[index+IN_RIGHT];
sample_SB = audio_buffer_1[index+IN_RIGHT] >> 8;
sample_MSB = audio_buffer_1[index+IN_RIGHT] >> 16;
}
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_LSB);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_SB);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, sample_MSB);
index += 2;
if (index >= AUDIO_BUFFER_SIZE) {
index=0;
audio_buffer_out = 1 - audio_buffer_out;
}
} else {
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
Usb_write_endpoint_data(EP_AUDIO_IN, 8, 0x00);
}
}
Usb_send_in(EP_AUDIO_IN); // send the current bank
}
} // end alt setting == 1
if (usb_alternate_setting_out == 1) {
// BSB 20131031 actual gap calculation moved to after OUT data processing
if (Is_usb_in_ready(EP_AUDIO_OUT_FB)) { // Endpoint buffer free ?
#ifdef USB_STATE_MACHINE_DEBUG // BSB 20131206 Toggle GPIO_07 / TP72 to indicate FB EP poll
if (toggle_07 == 1) { // = PX31 = TP72
gpio_clr_gpio_pin(AVR32_PIN_PX31);
toggle_07 = 0;
}
else {
gpio_set_gpio_pin(AVR32_PIN_PX31);
toggle_07 = 1;
}
#endif
Usb_ack_in_ready(EP_AUDIO_OUT_FB); // acknowledge in ready
Usb_reset_endpoint_fifo_access(EP_AUDIO_OUT_FB);
/* BSB 20131101
* A "stupid" Host is able to read the feedback but does not consider it. Emulate that by resetting packets_since_feedback
* and sending the Host the initial feedback value. Initial feedback is seeded with a hardcoded offset FB_INITIAL_OFFSET
*
* A "dead" Host is not reading the feedback. Emulate that by not resetting packets_since_feedbakc and sending the Host the
* initial feedback value.
*/
if (FEATURE_HDEAD_OFF)
packets_since_feedback = 0;
if (Is_usb_full_speed_mode()) {
// FB rate is 3 bytes in 10.14 format
if ( (FEATURE_HSTUPID_ON) || (FEATURE_HDEAD_ON) ) { // BSB 20131101
sample_LSB = FB_rate_nominal; // but not consider it. Emulate this by sending the nominal
sample_SB = FB_rate_nominal >> 8; // FB rate and not the one generated by the firmware's feedback
sample_MSB = FB_rate_nominal >> 16;
}
else { // Send firmware's feedback
sample_LSB = FB_rate;
sample_SB = FB_rate >> 8;
sample_MSB = FB_rate >> 16;
}
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_LSB);
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_SB);
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_MSB);
}
else {
// HS mode - Not likely to ever be used in UAC1 - UNTESTED code!
// FB rate is 4 bytes in 12.14 format
#if (defined HW_GEN_DIN10) || (defined HW_GEN_DIN20) // With WM8805 input, USB subsystem will be running off a completely wacko MCLK!
if ( (input_select != MOBO_SRC_UAC1) || (FEATURE_HSTUPID_ON) || (FEATURE_HDEAD_ON) ) { // BSB 20131101
#else
if ( (FEATURE_HSTUPID_ON) || (FEATURE_HDEAD_ON) ) { // BSB 20131101
#endif
sample_LSB = FB_rate_nominal;
sample_SB = FB_rate_nominal >> 8;
sample_MSB = FB_rate_nominal >> 16;
sample_HSB = FB_rate_nominal >> 24;
}
else {
sample_LSB = FB_rate;
sample_SB = FB_rate >> 8;
sample_MSB = FB_rate >> 16;
sample_HSB = FB_rate >> 24;
}
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_LSB);
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_SB);
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_MSB);
Usb_write_endpoint_data(EP_AUDIO_OUT_FB, 8, sample_HSB);
}
Usb_send_in(EP_AUDIO_OUT_FB);
} // end if (Is_usb_in_ready(EP_AUDIO_OUT_FB)) // Endpoint buffer free ?
if (Is_usb_out_received(EP_AUDIO_OUT)) {
spk_usb_heart_beat++; // indicates EP_AUDIO_OUT receiving data from host
Usb_reset_endpoint_fifo_access(EP_AUDIO_OUT);
num_samples = Usb_byte_count(EP_AUDIO_OUT) / 6; // Hardcoded 24-bit mono samples, 6 bytes for stereo
if( (!playerStarted) || (audio_OUT_must_sync) ) { // BSB 20140917 attempting to help uacX_device_audio_task.c synchronize to DMA
time_to_calculate_gap = 0; // BSB 20131031 moved gap calculation for DAC use
packets_since_feedback = 0; // BSB 20131031 assuming feedback system may soon kick in
FB_error_acc = 0; // BSB 20131102 reset feedback error
FB_rate = FB_rate_initial; // BSB 20131113 reset feedback rate
old_gap = SPK_BUFFER_SIZE; // BSB 20131115 moved here
skip_enable = 0; // BSB 20131115 Not skipping yet...
skip_indicate = 0;
usb_buffer_toggle = 0; // BSB 20131201 Attempting improved playerstarted detection
// BSB 20150725: Buffer alignment takes place as soon as 1st nonzero sample is received.
// FIX: Move above code as well?
} // end if (!playerStarted) || (audio_OUT_must_sync)
// BSB 20140917 attempting to help uacX_device_audio_task.c synchronize to DMA
audio_OUT_alive = 1; // Indicate samples arriving on audio OUT endpoint. Do this after syncing
// Received samples in 10.14 or 12.14 format is num_samples * 1<<14
// Error increases when Host (in average) sends too much data compared to FB_rate
// A high error means we must skip.
/* // Try to detect a dead Host feedback system
if (FEATURE_NOSKIP_OFF) { // If skip/insert isn't disabled...
if (packets_since_feedback > SPK1_HOST_FB_DEAD_AFTER)
skip_enable |= SPK1_SKIP_EN_DEAD; // Enable skip/insert due to dead host feedback system
else {
packets_since_feedback ++;
skip_enable &= ~SPK1_SKIP_EN_DEAD; // Disable skip/insert due to dead host feedback system
}
}
*/
// Default:1 Skip:0 Insert:2 Only one skip or insert per USB package
// .. prior to for(num_samples) Hence 1st sample in a package is skipped or inserted
samples_to_transfer_OUT = 1;
if (skip_enable == 0) { // Respond to all skip enablers
FB_error_acc = 0;
}
else {
FB_error_acc = FB_error_acc + ((S32)num_samples * 1<<14) - FB_rate;
if (FB_error_acc > SPK1_SKIP_LIMIT_14) { // Must skip
samples_to_transfer_OUT = 0; // Do some skippin'
FB_error_acc = FB_error_acc - (1<<14);
time_to_calculate_gap = -1; // Immediate gap re-calculation
skip_indicate = 1;
LED_On(LED0); // Indicate skipping on module LED
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('s');
#endif
}
else if (FB_error_acc < -SPK1_SKIP_LIMIT_14) { // Must insert
samples_to_transfer_OUT = 2; // Do some insertin'
FB_error_acc = FB_error_acc + (1<<14);
time_to_calculate_gap = -1; // Immediate gap re-calculation
skip_indicate = 1;
LED_On(LED1); // Indicate skipping on module LED
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('i');
#endif
}
}
// BSB 20131106 some notes on using AB-1.1 analog output for debug
// sample_L = 0x007FFFFF; // posative 24-bit full scale for calibration AB-1.1: 2.744VDC NB!! ES9023 keep squares at least 3dB
// sample_R = 0xFF800001; // negitive 24-bit full scale for calibration AB-1.2: -2.745VDC below full-scale!
// sample_L = (U32)num_samples << 16; // +127 is maximum. Expect 9 times 44 and once 45. Multimeter:945.4mVDC dead on!
// sample_R = (U32)FB_rate_initial << 2; // It was <<'ed by 14, expect 44.1 / Multimeter: 948.6mVDC not quite dead on..
// sample_R = (U32)num_samples << 16; // +127 is maximum. Expect 9 times 44 and once 45. Multimeter: 948.0mVDC
// sample_L = (U32)FB_rate_initial << 2; // It was <<'ed by 14, expect 44.1 / Multimeter: 946.1mVDC dead on!
// sample_L = (U32)FB_error_acc << 2;
// sample_R = (U32)num_samples << 16;
// ON this particular AB-1.2, the Left channel is more accurate at this particular measurement. We'll wait with further
// calibration. FB_rate_initial is verified. Value of 1<<16 = 21mV
silence_det = 0; // We're looking for non-zero audio data..
for (i = 0; i < num_samples; i++) {
sample_LSB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_SB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_MSB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_L = (((U32) sample_MSB) << 16) + (((U32)sample_SB) << 8) + sample_LSB;
silence_det |= sample_L;
#ifdef FEATURE_VOLUME_CTRL
if (spk_vol_mult_L != VOL_MULT_UNITY) { // Only touch gain-controlled samples
// 24-bit data words. First shift up to 32 bit. Do math and shift down
sample_L <<= 8;
sample_L = (S32)( (int64_t)( (int64_t)(sample_L) * (int64_t)spk_vol_mult_L ) >> VOL_MULT_SHIFT) ;
sample_L += rand8(); // dither in bits 7:0, will this be optimized away due to next line?
sample_L >>= 8;
}
#endif
sample_LSB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_SB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_MSB = Usb_read_endpoint_data(EP_AUDIO_OUT, 8);
sample_R = (((U32) sample_MSB) << 16) + (((U32)sample_SB) << 8) + sample_LSB;
silence_det |= sample_R;
#ifdef FEATURE_VOLUME_CTRL
if (spk_vol_mult_R != VOL_MULT_UNITY) { // Only touch gain-controlled samples
// 24-bit data words. First shift up to 32 bit. Do math and shift down
sample_R <<= 8;
sample_R = (S32)( (int64_t)( (int64_t)(sample_R) * (int64_t)spk_vol_mult_R ) >> VOL_MULT_SHIFT) ;
sample_R += rand8(); // dither in bits 7:0, will this be optimized away due to next line?
sample_R >>= 8;
}
#endif
// New site for setting playerStarted and aligning buffers
if ( (silence_det != 0) && (input_select == MOBO_SRC_NONE) ) { // There is actual USB audio.
#if (defined HW_GEN_DIN10) || (defined HW_GEN_DIN20) // With WM8805 subsystem, handle semaphore
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('t'); // Debug semaphore, lowercase letters in USB tasks
if (xSemaphoreTake(input_select_semphr, 0) == pdTRUE) { // Re-take of taken semaphore returns false
print_dbg_char('+');
input_select = MOBO_SRC_UAC1;
} // Hopefully, this code won't be called repeatedly. Would there be time??
else
print_dbg_char('-');
print_dbg_char('\n');
#else // not debug
if (xSemaphoreTake(input_select_semphr, 0) == pdTRUE)
input_select = MOBO_SRC_UAC1;
#endif
#else // not HW_GEN_D10 // No WM8805, take control
input_select = MOBO_SRC_UAC1;
#endif
}
// Do we own output (semaphore)? If so, change I2S setting and resync _once_
if ( (!playerStarted) && (input_select == MOBO_SRC_UAC1) ) {
playerStarted = TRUE; // Arrival of nonzero sample is now indication of playerStarted
// silence_USB = SILENCE_USB_INIT; // Let loop code determine silence. FIX: test with sample rate changes!
mobo_xo_select(current_freq.frequency, input_select); // Give USB the I2S control with proper MCLK
#if (defined HW_GEN_DIN10) || (defined HW_GEN_DIN20) // With WM8805 subsystem set RGB front LED
mobo_led_select(current_freq.frequency, input_select);
#endif
// Align buffers
audio_OUT_must_sync = 0; // BSB 20140917 attempting to help uacX_device_audio_task.c synchronize to DMA
num_remaining = spk_pdca_channel->tcr;
spk_buffer_in = spk_buffer_out; // Keep resyncing until playerStarted becomes true
LED_Off(LED0); // The LEDs on the PCB near the MCU
LED_Off(LED1);
#ifdef USB_STATE_MACHINE_DEBUG
if (spk_buffer_in == 1) // Debug message 'p' removed along with #ifdefs
gpio_set_gpio_pin(AVR32_PIN_PX30); // BSB 20140820 debug on GPIO_06/TP71 (was PX55 / GPIO_03)
else
gpio_clr_gpio_pin(AVR32_PIN_PX30); // BSB 20140820 debug on GPIO_06/TP71 (was PX55 / GPIO_03)
#endif
spk_index = SPK_BUFFER_SIZE - num_remaining;
spk_index = spk_index & ~((U32)1); // Clear LSB in order to start with L sample
}
// Semaphore not taken, or muted, output zeros
if ( (input_select != MOBO_SRC_UAC1) || (spk_mute) ) {
sample_L = 0;
sample_R = 0;
}
while (samples_to_transfer_OUT-- > 0) { // Default:1 Skip:0 Insert:2 Apply to 1st stereo sample in packet
if (spk_buffer_in == 0) {
spk_buffer_0[spk_index+OUT_LEFT] = sample_L;
spk_buffer_0[spk_index+OUT_RIGHT] = sample_R;
}
else {
spk_buffer_1[spk_index+OUT_LEFT] = sample_L;
spk_buffer_1[spk_index+OUT_RIGHT] = sample_R;
}
spk_index += 2;
if (spk_index >= SPK_BUFFER_SIZE) {
spk_index = 0;
spk_buffer_in = 1 - spk_buffer_in;
#ifdef USB_STATE_MACHINE_DEBUG
if (spk_buffer_in == 1)
gpio_set_gpio_pin(AVR32_PIN_PX30); // BSB 20140820 debug on GPIO_06/TP71 (was PX55 / GPIO_03)
else
gpio_clr_gpio_pin(AVR32_PIN_PX30); // BSB 20140820 debug on GPIO_06/TP71 (was PX55 / GPIO_03)
#endif
// BSB 20131201 attempting improved playerstarted detection
usb_buffer_toggle--; // Counter is increased by DMA, decreased by seq. code
}
}
samples_to_transfer_OUT = 1; // Revert to default:1. I.e. only one skip or insert per USB package
} // end for num_samples
// Detect USB silence. We're counting USB packets. UAC2: 250us, UAC1: 1ms
if (silence_det == 0) {
if (!USB_IS_SILENT())
silence_USB += 4;
}
else // stereo sample is non-zero
silence_USB = SILENCE_USB_INIT; // USB interface is not silent!
Usb_ack_out_received_free(EP_AUDIO_OUT);
if ( (USB_IS_SILENT()) && (input_select == MOBO_SRC_UAC1) ) { // Oops, we just went silent, probably from pause
input_select = MOBO_SRC_NONE; // Indicate WM may take over control
playerStarted = FALSE;
// Clear buffers for good measure! That may offload uac1_AK5394A_task() ?? and present a good mute to WM8805
for (i = 0; i < SPK_BUFFER_SIZE; i++) { // Clear USB subsystem's buffer in order to mute I2S
spk_buffer_0[i] = 0;
spk_buffer_1[i] = 0;
}
#if (defined HW_GEN_DIN10) || (defined HW_GEN_DIN20) // With WM8805 present, handle semaphores
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('g'); // Debug semaphore, lowercase letters for USB tasks
if( xSemaphoreGive(input_select_semphr) == pdTRUE ) {
print_dbg_char('+');
}
else
print_dbg_char('-');
print_dbg_char('\n');
#else
xSemaphoreGive(input_select_semphr);
#endif
// mobo_led(FLED_DARK, FLED_YELLOW, FLED_DARK); // Indicate silence detected by USB subsystem
#endif
}
/* BSB 20131031 New location of gap calculation code */
// Calculate gap after N packets, NOT each time feedback endpoint is polled
if (time_to_calculate_gap > 0)
time_to_calculate_gap--;
else {
if (time_to_calculate_gap == -1) // Immediately after a skip/insert and then again shortly
time_to_calculate_gap = SPK1_PACKETS_PER_GAP_SKIP - 1;
else // Initially and a while after any skip/insert
time_to_calculate_gap = SPK1_PACKETS_PER_GAP_CALCULATION - 1;
if (usb_alternate_setting_out == 1) { // Used with explicit feedback and not ADC data
num_remaining = spk_pdca_channel->tcr;
if (spk_buffer_in != spk_buffer_out) { // CS4344 and USB using same buffer
if ( spk_index < (SPK_BUFFER_SIZE - num_remaining))
gap = SPK_BUFFER_SIZE - num_remaining - spk_index;
else
gap = SPK_BUFFER_SIZE - spk_index + SPK_BUFFER_SIZE - num_remaining + SPK_BUFFER_SIZE;
}
else // usb and pdca working on different buffers
gap = (SPK_BUFFER_SIZE - spk_index) + (SPK_BUFFER_SIZE - num_remaining);
if(playerStarted) {
if (FEATURE_NOSKIP_OFF) { // If skip/insert isn't disabled...
if (gap < SPK1_GAP_LSKIP) {
skip_enable |= SPK1_SKIP_EN_GAP; // Enable skip/insert due to excessive buffer gap
}
else if (gap > SPK1_GAP_USKIP) {
skip_enable |= SPK1_SKIP_EN_GAP; // Enable skip/insert due to excessive buffer gap
}
else {
skip_enable &= ~SPK1_SKIP_EN_GAP; // Remove skip enable due to excessive buffer gap
}
}
if (gap < old_gap) {
if (gap < SPK1_GAP_L2) { // gap < outer lower bound => 2*FB_RATE_DELTA
FB_rate -= 2*FB_RATE_DELTA;
old_gap = gap;
skip_indicate = 0; // Feedback system is running again!
LED_On(LED0);
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('/');
#endif
}
else if (gap < SPK1_GAP_L1) { // gap < inner lower bound => 1*FB_RATE_DELTA
FB_rate -= FB_RATE_DELTA;
old_gap = gap;
skip_indicate = 0; // Feedback system is running again!
LED_On(LED0);
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('-');
#endif
}
else if (skip_indicate == 0) { // Go back to indicating feedback system on module LEDs
LED_Off(LED0);
LED_Off(LED1);
}
}
else if (gap > old_gap) {
if (gap > SPK1_GAP_U2) { // gap > outer upper bound => 2*FB_RATE_DELTA
FB_rate += 2*FB_RATE_DELTA;
old_gap = gap;
skip_indicate = 0; // Feedback system is running again!
LED_On(LED1);
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('*');
#endif
}
else if (gap > SPK1_GAP_U1) { // gap > inner upper bound => 1*FB_RATE_DELTA
FB_rate += FB_RATE_DELTA;
old_gap = gap;
skip_indicate = 0; // Feedback system is running again!
LED_On(LED1);
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('+');
#endif
}
else if (skip_indicate == 0) { // Go back to indicating feedback system on module LEDs
LED_Off(LED0);
LED_Off(LED1);
}
}
else if (skip_indicate == 0) { // Go back to indicating feedback system on module LEDs
LED_Off(LED0);
LED_Off(LED1);
}
} // end if(playerStarted)
} // end if (usb_alternate_setting_out == 1)
} // end if time_to_calculate_gap == 0
/* BSB 20131031 End of new location for gap calculation code */
} // end usb_out_received
} // end usb_alternate_setting_out == 1
else { // usb_alternate_setting_out != 1 // USB connection was turned off
playerStarted = FALSE;
silence_USB = SILENCE_USB_LIMIT; // Indicate USB silence
if (input_select == MOBO_SRC_UAC1) { // Set from playing nonzero USB
input_select = MOBO_SRC_NONE;
// Silencing incoming (OUT endpoint) audio buffer for good measure. Resorting to this buffer is in fact muting the WM8805
for (i = 0; i < SPK_BUFFER_SIZE; i++) {
spk_buffer_0[i] = 0;
spk_buffer_1[i] = 0;
}
#if (defined HW_GEN_DIN10) || (defined HW_GEN_DIN20) // With WM8805 present, handle semaphores
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('h'); // Debug semaphore, lowercase letters for USB tasks
if (xSemaphoreGive(input_select_semphr) == pdTRUE) {
print_dbg_char('+');
}
else
print_dbg_char('-');
print_dbg_char('\n');
#else
xSemaphoreGive(input_select_semphr);
#endif
// mobo_led(FLED_DARK, FLED_YELLOW, FLED_DARK); // Indicate silence detected by USB subsystem
#endif
}
}
// BSB 20131201 attempting improved playerstarted detection
if (usb_buffer_toggle == USB_BUFFER_TOGGLE_LIM) { // Counter is increased by DMA, decreased by seq. code
usb_buffer_toggle = USB_BUFFER_TOGGLE_PARK; // When it reaches limit, stop counting and park this mechanism
playerStarted = FALSE;
#ifdef USB_STATE_MACHINE_DEBUG
print_dbg_char('q');
#endif
}
//} // end startup else
} // end while vTask
}
#endif // USB_DEVICE_FEATURE == ENABLED