2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 #include <string.h> // for memcpy
37 #include <xdc/std.h>
38 #include <xdc/runtime/Log.h>
40 #include <ti/sysbios/hal/Cache.h>
42 #include "common.h"
44 #ifndef _TMS320C6X
45 #include "c67x_cintrins.h"
46 #endif
48 #include "inpbuf.h"
49 #include "dbgDib.h"
51 // sinusoid generator parameters
52 /* Performs floating-point to 24-bit fixed point conversion.
53 Resulting fixed-point value is left-justified in 32-bit word. */
54 #define F2INT_SCALE (float)0x7FFFFF
55 #define F2INT_ROUND(x) _spint(x)
56 #define F2INT(x) (((Int32)F2INT_ROUND(F2INT_SCALE * x) << 0x8) & 0xFFFFFF00)
58 #define TWO_PI (6.283185307179586476925286766559L)
59 #define FS_48KHZ (48000.0)
60 #define TWOPIOVERSRATE ( TWO_PI / FS_48KHZ )
62 #define SINP_MAX_CHS ( 8 ) // sin probe maximum number of channels
63 Int8 gSinPNumChs = SINP_MAX_CHS; // sin probe number of channels
64 Int8 gSinPChIdx = 0; // sin probe channel index
65 // sinusoid data generated on these DIB channels
66 Int8 gSinPCh[SINP_MAX_CHS] = {0,1,2,3,4,5,6,7};
68 #define SINP_MAX_GEN ( 2 ) // sin probe maximum number of generators
69 // Configurable from CCS
70 Int8 gSinPNumGen = SINP_MAX_GEN;
71 float gSineProbeAmp[SINP_MAX_GEN] = {0.0625, 0.125}; // sinusoid amplitudes
72 float gSineProbeFreq[SINP_MAX_GEN] = {440.0, 1004.0}; // sinusoid frequencies (Hz)
74 static double gSineProbeArg[SINP_MAX_GEN] = {0.0, 0.0}; // sinusoid function arguments
76 #ifdef CAP_IB_PCM
77 // IB capture (PCM) buffer
78 #ifdef _TMS320C6X
79 #pragma DATA_SECTION(gCapIbPcmBuf, ".gCapIbPcmBuf");
80 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP];
81 #else
82 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP] __attribute__ ((section(".gCapIbPcmBuf")));
83 #endif
84 Int32 gCapIbPcmBufIdx=0;
85 Int32 gCapIbPcmBufWrapCnt=0;
86 static UInt32 capIbPcmStopCnt=5000;
87 #endif // CAP_IB_PCM
89 #ifdef CAP_IP
90 // IB capture buffer
91 #ifdef _TMS320C6X
92 #pragma DATA_SECTION(gCapIbBuf, ".gCapIbBuf");
93 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ];
94 #else
95 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ] __attribute__ ((section(".gCapIbBuf")));
96 //Int32 gCapIbBuf[CAP_IB_BUF_SZ] __attribute__ ((section(".noinit")));
97 #endif
98 Int32 gCapIbBufIdx[2]={0,0};
99 Int32 gCapIbBufWrapCnt[2]={0,0};
100 Int8 gCapIbBufPingPongSel=1;
101 Int32 gCapIbAccBytes=0;
102 Int32 gNumDiffFrame[2]={0,0};
104 #endif // CAP_IP
106 // Generate sinusoids in IB buffer
107 Void genSinIb(
108 PAF_InpBufConfig *pInpBufConfig
109 )
110 {
111 Int8 numCh;
112 Int16 numSamp;
113 Int8 genIdx;
114 double phaseInc, arg, amp;
115 Int32 *pCh;
116 Int16 i;
118 numCh = pInpBufConfig->stride; // get number of channels
119 numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to generate
121 for (genIdx=0; genIdx<gSinPNumGen; genIdx++)
122 {
123 // compute generator phase increment
124 phaseInc = (double)gSineProbeFreq[genIdx] * TWOPIOVERSRATE;
126 arg = gSineProbeArg[genIdx]; // get generator arg
127 amp = gSineProbeAmp[genIdx]; // get generator amplitude
129 // generate sinusoid on selected channel
130 pCh = &pInpBufConfig->pntr.pLgInt[gSinPCh[gSinPChIdx]];
131 for (i=0; i<numSamp; i++)
132 {
133 *pCh = F2INT(amp * sin(arg));
134 arg += phaseInc;
135 pCh += numCh; // skipped interleaved channels
136 }
138 gSineProbeArg[genIdx] = arg; // save generator arg
140 // update sin probe channel index
141 gSinPChIdx++;
142 if (gSinPChIdx >= gSinPNumChs)
143 {
144 gSinPChIdx = 0;
145 }
146 }
147 }
149 #ifdef CAP_IB_PCM
150 // Capture data in IB buffer to memory
151 Void capIbPcm(
152 PAF_InpBufConfig *pInpBufConfig
153 )
154 {
155 Int8 numCh;
156 Int16 numSamp;
157 Int8 sampSz;
158 Int32 samp;
159 Int8 *pCh;
160 Int16 i, j, k;
161 Int32 *pCapBuf;
163 if (--capIbPcmStopCnt == 0)
164 {
165 SW_BREAKPOINT;
166 }
168 numCh = pInpBufConfig->stride; // get number of channels
169 numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to capture
170 sampSz = pInpBufConfig->sizeofElement; // get sample size (bytes)
172 if ((CAP_IB_PCM_MAX_NUM_SAMP - gCapIbPcmBufIdx) < numSamp)
173 {
174 //return;
175 gCapIbPcmBufIdx = 0;
176 gCapIbPcmBufWrapCnt++;
177 }
179 for (i=0; i<numCh; i++)
180 {
181 pCapBuf = &gCapIbPcmBuf[i][gCapIbPcmBufIdx];
182 pCh = &pInpBufConfig->pntr.pSmInt[i*sampSz];
183 for (j=0; j<numSamp; j++)
184 {
185 samp = (Int32)(*(pCh+sampSz-1));
186 for (k=sampSz-2; k>=0; k--)
187 {
188 samp <<= 8;
189 samp |= (UInt8)(*(pCh+k));
190 }
191 samp <<= 32-8*sampSz;
193 *pCapBuf = samp;
194 pCapBuf++;
195 pCh += numCh * sampSz;
196 }
197 }
198 gCapIbPcmBufIdx += numSamp;
199 }
200 #endif // CAP_IB_PCM
202 #ifdef CAP_IP
203 // Reset IB capture buffer
204 Int capIbReset(Void)
205 {
206 gCapIbBufPingPongSel ^= 0x1;
207 gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
208 gCapIbBufWrapCnt[gCapIbBufPingPongSel] = 0;
209 gNumDiffFrame[gCapIbBufPingPongSel] = 0;
210 gCapIbAccBytes=0;
212 return 0;
213 }
215 // Capture data in IB buffer to memory
216 Void capIb(
217 PAF_InpBufConfig *pInpBufConfig
218 )
219 {
220 UInt32 nBytes, bufEnd, currentBufSize, chunkSize, wrapSize;
222 nBytes = pInpBufConfig->frameLength * pInpBufConfig->sizeofElement;
224 #if 0
225 // FL: DDP debug
226 if (nBytes != 24576)
227 {
228 Log_info1("capIb(): nBytes=%d", nBytes);
229 gNumDiffFrame[gCapIbBufPingPongSel]++;
230 }
231 #endif
233 bufEnd = (Int) pInpBufConfig->base.pVoid + pInpBufConfig->sizeofBuffer;
234 currentBufSize = (bufEnd - (Int)pInpBufConfig->pntr.pSmInt);
236 if (currentBufSize >= nBytes)
237 chunkSize = nBytes;
238 else
239 chunkSize = currentBufSize;
241 wrapSize = nBytes - chunkSize;
243 if ((CAP_IB_BUF_SZ - gCapIbBufIdx[gCapIbBufPingPongSel]) < nBytes)
244 {
245 //return; // fixed buffer
246 gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
247 gCapIbBufWrapCnt[gCapIbBufPingPongSel]++;
248 }
250 // invalidate input data
251 Cache_inv((Ptr)pInpBufConfig->pntr.pSmInt, chunkSize, Cache_Type_ALLD, 0);
252 Cache_wait();
254 memcpy(&gCapIbBuf[gCapIbBufPingPongSel][gCapIbBufIdx[gCapIbBufPingPongSel]], pInpBufConfig->pntr.pSmInt, chunkSize);
256 gCapIbBufIdx[gCapIbBufPingPongSel] += chunkSize;
257 gCapIbAccBytes += chunkSize;
259 if(wrapSize > 0)
260 {
261 // invalidate input data
262 Cache_inv((Ptr)pInpBufConfig->base.pSmInt, wrapSize, Cache_Type_ALLD, 0);
263 Cache_wait();
264 memcpy(&gCapIbBuf[gCapIbBufPingPongSel][gCapIbBufIdx[gCapIbBufPingPongSel]], pInpBufConfig->base.pSmInt, wrapSize);
265 gCapIbBufIdx[gCapIbBufPingPongSel] += wrapSize;
266 gCapIbAccBytes += wrapSize;
267 }
268 }
270 #endif // CAP_IP