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>
41 #include "common.h"
43 #ifndef _TMS320C6X
44 #include "c67x_cintrins.h"
45 #endif
47 #include "inpbuf.h"
48 #include "dbgDib.h"
50 // sinusoid generator parameters
51 /* Performs floating-point to 24-bit fixed point conversion.
52 Resulting fixed-point value is left-justified in 32-bit word. */
53 #define F2INT_SCALE (float)0x7FFFFF
54 #define F2INT_ROUND(x) _spint(x)
55 #define F2INT(x) (((Int32)F2INT_ROUND(F2INT_SCALE * x) << 0x8) & 0xFFFFFF00)
57 #define TWO_PI (6.283185307179586476925286766559L)
58 #define FS_48KHZ (48000.0)
59 #define TWOPIOVERSRATE ( TWO_PI / FS_48KHZ )
61 #define SINP_MAX_CHS ( 8 ) // sin probe maximum number of channels
62 Int8 gSinPNumChs = SINP_MAX_CHS; // sin probe number of channels
63 Int8 gSinPChIdx = 0; // sin probe channel index
64 // sinusoid data generated on these DIB channels
65 Int8 gSinPCh[SINP_MAX_CHS] = {0,1,2,3,4,5,6,7};
67 #define SINP_MAX_GEN ( 2 ) // sin probe maximum number of generators
68 // Configurable from CCS
69 Int8 gSinPNumGen = SINP_MAX_GEN;
70 float gSineProbeAmp[SINP_MAX_GEN] = {0.0625, 0.125}; // sinusoid amplitudes
71 float gSineProbeFreq[SINP_MAX_GEN] = {440.0, 1004.0}; // sinusoid frequencies (Hz)
73 static double gSineProbeArg[SINP_MAX_GEN] = {0.0, 0.0}; // sinusoid function arguments
75 #ifdef CAP_IB_PCM
76 // IB capture (PCM) buffer
77 #ifdef _TMS320C6X
78 #pragma DATA_SECTION(gCapIbPcmBuf, ".gCapIbPcmBuf");
79 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP];
80 #else
81 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP] __attribute__ ((section(".gCapIbPcmBuf")));
82 #endif
83 Int32 gCapIbPcmBufIdx=0;
84 Int32 gCapIbPcmBufWrapCnt=0;
85 static UInt32 capIbPcmStopCnt=5000;
86 #endif // CAP_IB_PCM
88 #ifdef CAP_IP
89 // IB capture buffer
90 #ifdef _TMS320C6X
91 #pragma DATA_SECTION(gCapIbBuf, ".gCapIbBuf");
92 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ];
93 #else
94 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ] __attribute__ ((section(".gCapIbBuf")));
95 //Int32 gCapIbBuf[CAP_IB_BUF_SZ] __attribute__ ((section(".noinit")));
96 #endif
97 Int32 gCapIbBufIdx[2]={0,0};
98 Int32 gCapIbBufWrapCnt[2]={0,0};
99 Int8 gCapIbBufPingPongSel=1;
100 Int32 gNumDiffFrame[2]={0,0};
102 #endif // CAP_IP
104 // Generate sinusoids in IB buffer
105 Void genSinIb(
106 PAF_InpBufConfig *pInpBufConfig
107 )
108 {
109 Int8 numCh;
110 Int16 numSamp;
111 Int8 genIdx;
112 double phaseInc, arg, amp;
113 Int32 *pCh;
114 Int16 i;
116 numCh = pInpBufConfig->stride; // get number of channels
117 numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to generate
119 for (genIdx=0; genIdx<gSinPNumGen; genIdx++)
120 {
121 // compute generator phase increment
122 phaseInc = (double)gSineProbeFreq[genIdx] * TWOPIOVERSRATE;
124 arg = gSineProbeArg[genIdx]; // get generator arg
125 amp = gSineProbeAmp[genIdx]; // get generator amplitude
127 // generate sinusoid on selected channel
128 pCh = &pInpBufConfig->pntr.pLgInt[gSinPCh[gSinPChIdx]];
129 for (i=0; i<numSamp; i++)
130 {
131 *pCh = F2INT(amp * sin(arg));
132 arg += phaseInc;
133 pCh += numCh; // skipped interleaved channels
134 }
136 gSineProbeArg[genIdx] = arg; // save generator arg
138 // update sin probe channel index
139 gSinPChIdx++;
140 if (gSinPChIdx >= gSinPNumChs)
141 {
142 gSinPChIdx = 0;
143 }
144 }
145 }
147 #ifdef CAP_IB_PCM
148 // Capture data in IB buffer to memory
149 Void capIbPcm(
150 PAF_InpBufConfig *pInpBufConfig
151 )
152 {
153 Int8 numCh;
154 Int16 numSamp;
155 Int8 sampSz;
156 Int32 samp;
157 Int8 *pCh;
158 Int16 i, j, k;
159 Int32 *pCapBuf;
161 if (--capIbPcmStopCnt == 0)
162 {
163 SW_BREAKPOINT;
164 }
166 numCh = pInpBufConfig->stride; // get number of channels
167 numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to capture
168 sampSz = pInpBufConfig->sizeofElement; // get sample size (bytes)
170 if ((CAP_IB_PCM_MAX_NUM_SAMP - gCapIbPcmBufIdx) < numSamp)
171 {
172 //return;
173 gCapIbPcmBufIdx = 0;
174 gCapIbPcmBufWrapCnt++;
175 }
177 for (i=0; i<numCh; i++)
178 {
179 pCapBuf = &gCapIbPcmBuf[i][gCapIbPcmBufIdx];
180 pCh = &pInpBufConfig->pntr.pSmInt[i*sampSz];
181 for (j=0; j<numSamp; j++)
182 {
183 samp = (Int32)(*(pCh+sampSz-1));
184 for (k=sampSz-2; k>=0; k--)
185 {
186 samp <<= 8;
187 samp |= (UInt8)(*(pCh+k));
188 }
189 samp <<= 32-8*sampSz;
191 *pCapBuf = samp;
192 pCapBuf++;
193 pCh += numCh * sampSz;
194 }
195 }
196 gCapIbPcmBufIdx += numSamp;
197 }
198 #endif // CAP_IB_PCM
200 #ifdef CAP_IP
201 // Reset IB capture buffer
202 Int capIbReset(Void)
203 {
204 gCapIbBufPingPongSel ^= 0x1;
205 gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
206 gCapIbBufWrapCnt[gCapIbBufPingPongSel] = 0;
207 gNumDiffFrame[gCapIbBufPingPongSel] = 0;
209 return 0;
210 }
212 // Capture data in IB buffer to memory
213 Void capIb(
214 PAF_InpBufConfig *pInpBufConfig
215 )
216 {
217 UInt32 nBytes;
219 nBytes = pInpBufConfig->frameLength * pInpBufConfig->sizeofElement;
221 #if 0
222 // FL: DDP debug
223 if (nBytes != 24576)
224 {
225 Log_info1("capIb(): nBytes=%d", nBytes);
226 gNumDiffFrame[gCapIbBufPingPongSel]++;
227 }
228 #endif
230 if ((CAP_IB_BUF_SZ - gCapIbBufIdx[gCapIbBufPingPongSel]) < nBytes)
231 {
232 //return; // fixed buffer
233 gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
234 gCapIbBufWrapCnt[gCapIbBufPingPongSel]++;
235 }
237 memcpy(&gCapIbBuf[gCapIbBufPingPongSel][gCapIbBufIdx[gCapIbBufPingPongSel]], pInpBufConfig->pntr.pSmInt, nBytes);
238 gCapIbBufIdx[gCapIbBufPingPongSel] += nBytes;
239 }
241 #endif // CAP_IP