usbhostaudio.c
6.73 KB
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
#include "c_def.h"
#include "debug.h"
#include "oem.h"
#ifdef USB_HOST_AUDIO_ENABLE
#include "audio_pll.h"
#include "mem_reloc.h"
#include "IRP.h"
#include "hstack.h"
#include "usbmsc.h"
//jj+
#include "hcd.h"
#include "usbd.h"
#include "fifo.h"
#include "app_cmd.h"
#include "app_main.h"
#include "app_timer.h"
#include "app_dac.h"
#include "app_sdram.h"
#include <string.h>
#include "usbhostaudio.h"
#include "usbhost_audio_driver.h"
#include "usb.h"
#include "otg.h"
#ifdef OPTEK_DSRC_ENABLE //jj+
#include "optek_dsp_ip.h"
#include "optek_d2as_table.h"
#endif
#if 1//ndef USB_HOST_AUDIO_LIB
extern U32 omitcnt;
extern U8 usbhost_audio_connected;
U8 src_ok_flag; //if succeed for USB to find a sample frequency.
FIFO UHA_MicInFifo;
void usbhost_audio_device_get_except_src_config(U32 *sample_freq, U8 *sample_bits, U8 *num_freq);
int usbhost_audio_device_src_config_get(void);
U8 SwapIntf( USB_DEVICE * usbDev, U32 BITS_RES, U32 SamFreqV );
int usbhost_audio_device_src_setting(void)
{
U8 ret;
U32 stream_in_sr;
U8 stream_bit;
USB_DEVICE * usbDev = &singleUsbDevice;
DBG_Printf("u_a_d_src_setting ......\n\r");
#if 1
if (!usbd_get_usbhost_audio_device_status())
{
DBG_Printf("No usbhostAudio\n\r");
return FALSE;
}
#endif
if ( (app_main_data.media == MEDIA_OPTICAL) || (app_main_data.media == MEDIA_OPTICAL1) )
{
goto SWAP_IF_INIT;
}
stream_in_sr = stream_in_sample_rate; //init to stream sample rate
#ifdef OPTEK_DSRC_ENABLE
extern U32 UHA_SAMPLE_FREQ;
int retsrc;
#if 1
//usbhost_audio_device_get_SampleFreqList(usbDev);
retsrc = usbhost_audio_device_src_config_get();//jj???
if (retsrc == 0)
{
return FALSE;
}
#else //Force to 44 or 48, for test!
//retsrc = USBHOST_AUDIO_SRC_44P1K_BIT;
//retsrc = USBHOST_AUDIO_SRC_48K_BIT;
retsrc = USBHOST_AUDIO_SRC_48K_BIT | USBHOST_AUDIO_SRC_44P1K_BIT;
#endif
if ( (retsrc & USBHOST_AUDIO_SRC_44P1K_BIT) && (retsrc & USBHOST_AUDIO_SRC_48K_BIT) )
{
//don't need to use sw src
d2as_need_flag = FALSE;
UHA_SAMPLE_FREQ = stream_in_sr;
}
else if (retsrc & USBHOST_AUDIO_SRC_44P1K_BIT && stream_in_sr != 44100 )
{
//must use sw src
d2as_need_flag = TRUE;
UHA_SAMPLE_FREQ = 44100;
app_main_data.playing_stream_sample_rate = 44100;
//need to SRC to UHA_SAMPLE_FREQ from SwapIntf
//audio_pll_set(UHA_SAMPLE_FREQ);
}
else if (retsrc & USBHOST_AUDIO_SRC_48K_BIT && stream_in_sr != 48000 )
{
//must use sw src
d2as_need_flag = TRUE;
UHA_SAMPLE_FREQ = 48000;
app_main_data.playing_stream_sample_rate = 48000;
}
else
{
d2as_need_flag = FALSE;
//app_timer_usbhost_audio_time_set(2000);
//DBG_Assert(FALSE);
DBG_Printf ("\nSRC unncsry:%d for %d\n\r", retsrc, stream_in_sr);
}
//d2as_need_flag = FALSE; //TEST
DBG_Printf ("\nd2as_need_flag=%d\n\r", d2as_need_flag);
if (d2as_need_flag)
{
extern void *optek_d2as1; //for 32kHz,64kHz,128kHz
if (stream_in_sr == 48000)
{
//jj Init SRC as input source sample rate 48000Hz (output to 44100Hz/P160_M12_coeffs) for SRC operation ! P160_M12_coeffs
//be ready to change 44.1kHz songs to 48kHz songs
//? optek_dsrc_alloc( 12, 512, OUT_SAMPLE_MASK, 2,PROCESSING_DATA_16b );
optek_d2as1 = optek_dsrc_open(optek_d2as1, SPDIF_D2AS_M, SPDIF_D2AS_N, P160_M12_coeffs, 0, SPDIF_48K_T0_44P1K_BASE_STEP,
SPDIF_48K_T0_44P1K_BASE_INTER_D, SPDIF_D2AS_HP_MASK, SPDIF_D2AS_CHAN, PROCESSING_DATA_16b);
DBG_Printf ("\n<dsrc> open for 48 to 44.1 kHz)\n\r" );
}
else if (stream_in_sr == 44100)
{
//jj Init SRC as input source sample rate 44100Hz (output to 48000Hz/P192_M12_coeffs) for SRC operation ! P192_M12_coeffs
//be ready to change 48kHz songs to 44.1kHz songs
//? optek_dsrc_alloc( 12, 512, OUT_SAMPLE_MASK, 2,PROCESSING_DATA_16b );
optek_d2as1 = optek_dsrc_open(optek_d2as1, SPDIF_D2AS_M, SPDIF_D2AS_N, P192_M12_coeffs, 0, SPDIF_44P1K_TO_48K_BASE_STEP,
SPDIF_44P1K_TO_48K_BASE_INTER_D, SPDIF_D2AS_HP_MASK, SPDIF_D2AS_CHAN, PROCESSING_DATA_16b);
DBG_Printf ("\n<dsrc> open for 44.1 to 48 kHz)\n\r" );
}
}
else
{
DBG_Printf ("\n<dsrc> is NOT open!\n\r" );
}
#endif //OPTEK_DSRC_ENABLE
#ifdef WAV_DATA_TEST
//only for wecall 16 bits/16KHz
stream_out_sr = 44100;
stream_bit = 24;
#else
U32 stream_out_sr;
#ifdef OPTEK_DSRC_ENABLE
stream_out_sr = UHA_SAMPLE_FREQ;
stream_bit = app_main_data.playing_stream_sample_bits;
#else
stream_out_sr = app_main_data.playing_stream_sample_rate;
stream_bit = app_main_data.playing_stream_sample_bits;
#endif
DBG_Printf(" Played stream:%x/%x\n\r", stream_bit, stream_out_sr);
#endif
SWAP_IF_INIT:
if ( (app_main_data.media == MEDIA_OPTICAL) || (app_main_data.media == MEDIA_OPTICAL1) )
{
stream_out_sr = app_main_data.playing_stream_sample_rate;
stream_bit = app_main_data.playing_stream_sample_bits;
}
//DBG_Printf (" fifo_init=%d\n\r", MIC_FIFO_BUF_SIZE);
#if 0
U8 *pBuf = (U8 *)USB_HOST_AUDIO_FIFO_START;
int size = USB_HOST_AUDIO_FIFO_END - USB_HOST_AUDIO_FIFO_START;
fifo_init (&UHA_MicInFifo, pBuf, (size), TRUE);
#else
int size = MIC_FIFO_BUF_SIZE;
fifo_init (&UHA_MicInFifo, (U8 *)micFifo_buf, (size), TRUE);//JJ+0
#endif
ret = SwapIntf(usbDev, stream_bit, stream_out_sr);
omitcnt = 0; //remove head noise data, set to 0
if (ret == enURBRST_SUCESS)
{
DBG_Printf("\n src swap ok.\n\r");
//DBG_Printf("src swap ok, bit:%d, sr:%d pll:%d\n\r", stream_bit, stream_out_sr, UHA_SAMPLE_FREQ);
src_ok_flag = TRUE;
return ret;
}
else //Some device doesn't response to set sample freq (Stall), so assume it works with the only sample freq, otherwise exception.
{
//See details in usbhost_audio_device_get_except_src_config().
U32 sample_freq;
U8 sample_bits, num_freq;
DBG_Printf("\n src swap error!!!\n\r");
//DBG_Printf("src swap error,bit:%d, sr:%d\n\r", stream_bit, stream_out_sr);
usbhost_audio_device_get_except_src_config(&sample_freq, &sample_bits, &num_freq);
if ( (num_freq == 1) &&
(stream_out_sr == sample_freq) &&
(stream_bit == sample_bits) )
{
src_ok_flag = TRUE;
ret = enURBRST_SUCESS;
}
else
{
src_ok_flag = FALSE;
ret = enURBRST_FAIL;
}
return ret; //wanted config not found
}
}
U16 USBDevSampleRateList(void);
//Get a int value with bit value indicate support sample freq list in usbhostaudio.h.
int usbhost_audio_device_src_config_get(void)
{
int ret;
if (!usbd_get_usbhost_audio_device_status())
{
DBG_Printf("No usbhostAudio!\n\r");
return FALSE;
}
ret = USBDevSampleRateList();
DBG_Printf("usbHost audio src config:0x%x\n\r", ret);
return ret;
}
#endif //USB_HOST_LIB
#else //USB_HOST_AUDIO_ENABLE
#include "usbd.h"
void HostAudioOutLaunch(USB_DEVICE * usbDev ) //jj called from deviceConnectAction()
{
}
int haudio_removeIsoOut( USB_DEVICE * usbDev )
{
}
int haudio_removeIsoIn( USB_DEVICE * usbDev )
{
}
#endif //USB_HOST_ENABLE