blob: 5e7659fea583ffaad73db1dcb3c97dd9faba979f [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001/*****************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2012
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35
36/*******************************************************************************
37 *
38 * Filename:
39 * ---------
40 * pcm4way.c
41 *
42 * Project:
43 * --------
44 * MAUI
45 *
46 * Description:
47 * ------------
48 * EXTCODEC interface
49 *
50 * Author:
51 * -------
52 * -------
53 *
54 *==============================================================================
55 * HISTORY
56 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
57 *------------------------------------------------------------------------------
58 * removed!
59 * removed!
60 * removed!
61 *
62 * removed!
63 * removed!
64 * removed!
65 * removed!
66 * removed!
67 * removed!
68 *
69 * removed!
70 * removed!
71 * removed!
72 * removed!
73 *
74 * removed!
75 * removed!
76 * removed!
77 *
78 * removed!
79 * removed!
80 * removed!
81 *
82 * removed!
83 * removed!
84 * removed!
85 *
86 * removed!
87 * removed!
88 * removed!
89 *
90 * removed!
91 * removed!
92 * removed!
93 *
94 * removed!
95 * removed!
96 * removed!
97 * removed!
98 *
99 * removed!
100 * removed!
101 * removed!
102 *
103 * removed!
104 * removed!
105 *
106 * removed!
107 * removed!
108 * removed!
109 *
110 *------------------------------------------------------------------------------
111 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
112 *==============================================================================
113 *******************************************************************************/
114
115#include "kal_public_api.h"
116#include "kal_trace.h"
117#include "sync_data.h"
118#include "l1sp_trc.h"
119// #include "reg_base.h"
120#include "l1audio.h"
121#include "am.h"
122
123#include "media.h"
124#include "extcodec.h"
125#include "sal_def.h"
126#include "sal_exp.h"
127
128#include "sp_drv.h"
129#include "sp_enhance.h"
130
131
132#define ACLOOPBACK_FRAME_BUF_NO (64) // REMIND: should be pow of 2
133#define ACLOOPBACK_NB_FRAME_SIZE (160)
134//static uint16 gSpc_acLoopback_PCM_UL_BUF[ACLOOPBACK_NB_FRAME_SIZE];
135#if defined (__SMART_PHONE_MODEM__)
136static uint16 gSpc_acLoopback_PCM_DL_BUF[ACLOOPBACK_FRAME_BUF_NO][ACLOOPBACK_NB_FRAME_SIZE];
137#endif // defined (__SMART_PHONE_MODEM__)
138
139typedef struct _EXTCODEC_T_
140{
141 kal_uint8 pnwBufInfo; //ref to SPC_PNW_MSG_BUFFER_x
142 kal_uint8 pnwBand; //0: for narrow band, 1: for wide band
143 kal_uint32 micBufSize; // unit is byte (8bit)
144 kal_uint32 spkBufSize; // unit is byte (8bit)
145 kal_uint32 DL_tmp_w;
146 kal_uint32 DL_tmp_r;
147 kal_uint32 delay;
148 kal_uint32 running;
149 kal_spinlockid extcodecspinLockID;
150}_EXTCODEC_T_;
151
152_EXTCODEC_T_ extcodec;
153
154
155uint16 Extcodec_writePatternToSpk(uint16 pattern)
156{
157 uint32 I;
158 uint16 bufLen = SAL_PcmEx_GetBufLen(SAL_PCMEX_EXTCOD_BUF_DL);
159 volatile uint16* ptr = SAL_PcmEx_GetBuf(SAL_PCMEX_EXTCOD_BUF_DL);
160 for( I = bufLen; I > 0; I-- ){
161 *ptr++ = pattern;
162 }
163 return bufLen;
164}
165
166void Extcodec_Set_Delay(kal_uint32 delay)
167{
168 extcodec.delay = delay;
169}
170
171void Extcodec_Call_Open()
172{
173#if defined (__SMART_PHONE_MODEM__)
174 int I;
175 kal_take_spinlock(extcodec.extcodecspinLockID, KAL_INFINITE_WAIT);
176 extcodec.running = 1;
177 kal_give_spinlock(extcodec.extcodecspinLockID);
178 extcodec.DL_tmp_w = 0;
179 extcodec.DL_tmp_r = 0;
180 //extcodec.pnwBufInfo = (param & 0xf); // i.e. param & (SPC_PNW_MSG_BUFFER_SE|SPC_PNW_MSG_BUFFER_SPK|SPC_PNW_MSG_BUFFER_MIC|SPC_PNW_MSG_BUFFER_SD)
181 //ASSERT(extcodec.pnwBufInfo != 0);
182
183 extcodec.micBufSize = 0;
184 extcodec.spkBufSize = 0;
185 memset(gSpc_acLoopback_PCM_DL_BUF, 0, sizeof(uint16)*ACLOOPBACK_FRAME_BUF_NO*ACLOOPBACK_NB_FRAME_SIZE);
186
187 ASSERT(SAL_PcmEx_CheckStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF) && SAL_PcmEx_CheckStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF));
188 SAL_PcmEx_SetStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_ON);
189 SAL_PcmEx_SetStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_ON);
190 for(I = 0; ;I ++){
191 kal_bool is_ready = true;
192 int test_return_DL, test_return_UL;
193 test_return_DL = SAL_PcmEx_CheckStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_RDY);
194 test_return_UL = SAL_PcmEx_CheckStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_RDY);
195 kal_prompt_trace(MOD_L1SP,"extcodec check status");
196 if( !test_return_DL){
197 is_ready &= false;
198 }
199
200 if( !test_return_UL){
201 is_ready &= false;
202 }
203
204 if(is_ready){
205 break;
206 }
207 ASSERT(I < 40); // wait 400ms
208 kal_sleep_task( AUD_1TICK(2) );
209 }
210#endif // defined (__SMART_PHONE_MODEM__)
211}
212
213void Extcodec_Call_Close()
214{
215#if defined (__SMART_PHONE_MODEM__)
216 Sal_PCMEx_Config_t cfg;
217 kal_take_spinlock(extcodec.extcodecspinLockID, KAL_INFINITE_WAIT);
218 extcodec.running = 0;
219 kal_give_spinlock(extcodec.extcodecspinLockID);
220 memset(&cfg, 0, sizeof(Sal_PCMEx_Config_t));
221 /* the begining of configure the SAL */
222 ASSERT(SAL_PcmEx_CheckStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF) || SAL_PcmEx_CheckStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_RDY));
223 ASSERT(SAL_PcmEx_CheckStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF) || SAL_PcmEx_CheckStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_RDY));
224 SAL_PcmEx_SetStateUL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF);
225 SAL_PcmEx_SetStateDL(SAL_PCMEX_TYPE_EXT_COD, SAL_PCMEX_OFF);
226 cfg.swi = SAL_PCMEX_SWITCH_OFF;
227 cfg.type = SAL_PCMEX_TYPE_EXT_COD;
228 cfg.band = SAL_PCM_NARROWBAND;
229 SAL_PcmEx_Config(&cfg);
230 Extcodec_writePatternToSpk(0);
231
232 // clear buffer
233 extcodec.pnwBufInfo = 0;
234 extcodec.micBufSize = 0;
235 extcodec.spkBufSize = 0;
236#endif // defined (__SMART_PHONE_MODEM__)
237}
238
239void Extcodec_Init()
240{
241 extcodec.extcodecspinLockID = kal_create_spinlock("EXTCODEC_LOCK");
242}
243uint16 Extcodec_GetFromMic(uint16 *toBuf)
244{
245 uint32 I;
246 uint16 bufLen = SAL_PcmEx_GetBufLen(SAL_PCMEX_EXTCOD_BUF_UL);
247 volatile uint16* ptr = SAL_PcmEx_GetBuf(SAL_PCMEX_EXTCOD_BUF_UL);
248 if( SP_IsMicMute() ){
249 for( I = bufLen; I > 0; I-- ){
250 *toBuf++ = 0;
251 }
252 } else {
253 for( I = bufLen; I > 0; I-- ){
254 *toBuf++ = *ptr++;
255 }
256 }
257 return bufLen;
258}
259
260uint16 Extcodec_PutToSpk(const uint16 *fromBuf)
261{
262 uint32 I;
263 uint16 bufLen = SAL_PcmEx_GetBufLen(SAL_PCMEX_EXTCOD_BUF_DL);
264 volatile uint16* ptr = SAL_PcmEx_GetBuf(SAL_PCMEX_EXTCOD_BUF_DL);
265 for( I = bufLen; I > 0; I-- ){
266 *ptr++ = *fromBuf++;
267 }
268 return bufLen;
269}
270
271void Extcodec_hisr_dl_loopback(void *data)
272{
273#if defined (__SMART_PHONE_MODEM__)
274 MD_TRC_SPC_AC_LOOPBACK_HISR_DL();
275 kal_take_spinlock(extcodec.extcodecspinLockID, KAL_INFINITE_WAIT);
276 if(extcodec.running == 0)
277 {
278 kal_give_spinlock(extcodec.extcodecspinLockID);
279 MD_TRC_SPC_AC_LOOPBACK_HISR_SKIP();
280 return;
281 }
282
283 if(extcodec.DL_tmp_w > extcodec.DL_tmp_r &&(extcodec.DL_tmp_w - extcodec.DL_tmp_r) >= extcodec.delay)
284 {
285 extcodec.spkBufSize = Extcodec_PutToSpk(gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_r & (ACLOOPBACK_FRAME_BUF_NO - 1)]);
286 (extcodec.DL_tmp_r)++;
287 MD_TRC_SPC_AC_LOOPBACK_TO_SPK(
288 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_r & (ACLOOPBACK_FRAME_BUF_NO - 1)][0],
289 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_r & (ACLOOPBACK_FRAME_BUF_NO - 1)][1],
290 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_r & (ACLOOPBACK_FRAME_BUF_NO - 1)][2],
291 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_r & (ACLOOPBACK_FRAME_BUF_NO - 1)][3]);
292 } else {
293 Extcodec_writePatternToSpk(0);
294 }
295 kal_give_spinlock(extcodec.extcodecspinLockID);
296#endif // defined (__SMART_PHONE_MODEM__)
297}
298
299void Extcodec_hisr_ul_loopback(void *data)
300{
301#if defined (__SMART_PHONE_MODEM__)
302 MD_TRC_SPC_AC_LOOPBACK_HISR_UL();
303 kal_take_spinlock(extcodec.extcodecspinLockID, KAL_INFINITE_WAIT);
304 if(extcodec.running == 0)
305 {
306 kal_give_spinlock(extcodec.extcodecspinLockID);
307 MD_TRC_SPC_AC_LOOPBACK_HISR_SKIP();
308 return;
309 }
310
311 if((extcodec.DL_tmp_w - extcodec.DL_tmp_r) <= extcodec.delay)
312 {
313 extcodec.micBufSize = Extcodec_GetFromMic((uint16*)gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_w & (ACLOOPBACK_FRAME_BUF_NO - 1)]);
314 (extcodec.DL_tmp_w)++;
315 MD_TRC_SPC_AC_LOOPBACK_FROM_SD(
316 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_w & (ACLOOPBACK_FRAME_BUF_NO - 1)][0],
317 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_w & (ACLOOPBACK_FRAME_BUF_NO - 1)][1],
318 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_w & (ACLOOPBACK_FRAME_BUF_NO - 1)][2],
319 gSpc_acLoopback_PCM_DL_BUF[extcodec.DL_tmp_w & (ACLOOPBACK_FRAME_BUF_NO - 1)][3]);
320 }
321 kal_give_spinlock(extcodec.extcodecspinLockID);
322#endif // defined (__SMART_PHONE_MODEM__)
323}
324
325