[processor-sdk/performance-audio-sr.git] / psdk_cust / libarch_k2g_1_0_1_0 / examples / common / test_dsp_kernel.c
1 /******************************************************************************
2 * Copyright (c) 2015-2017, Texas Instruments Incorporated - http://www.ti.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
28 #include <stdio.h>
29 #include <ti/libarch/libarch.h>
32 /*
33 * This file shows an example of a DSP library that uses the library framework API.
34 */
36 /* Size requirement of each of the 4 memory types */
37 #ifdef SOC_K2H
38 #define TEST_MEM_SIZE_VFAST (28*1024UL)
39 #define TEST_MEM_SIZE_FAST (512*1024UL)
40 #define TEST_MEM_SIZE_MED (4608*1024UL) //4.5MB
41 #define TEST_MEM_SIZE_SLOW (10240*1024UL) //10MB
42 #endif
43 #ifdef SOC_AM572x
44 #define TEST_MEM_SIZE_VFAST (28*1024UL)
45 #define TEST_MEM_SIZE_FAST (20*1024UL)
46 #define TEST_MEM_SIZE_MED (256*1024UL) //256KB
47 #define TEST_MEM_SIZE_SLOW (1024*1024UL) //1MB
48 #endif
49 #ifdef SOC_C6678
50 #define TEST_MEM_SIZE_VFAST (28*1024UL)
51 #define TEST_MEM_SIZE_FAST (20*1024UL)
52 #define TEST_MEM_SIZE_MED (2048*1024UL) //2MB
53 #define TEST_MEM_SIZE_SLOW (10240*1024UL) //10MB
54 #endif
55 #ifdef SOC_K2G
56 #define TEST_MEM_SIZE_VFAST (28*1024UL)
57 #define TEST_MEM_SIZE_FAST (20*1024UL)
58 #define TEST_MEM_SIZE_MED (128*1024UL) //128KB
59 #define TEST_MEM_SIZE_SLOW (1024*1024UL) //1MB
60 #endif
61 #define LIBFW_TEST_INIT_NOERR 0
62 #define LIBFW_TEST_INIT_ERROR 1
64 #define LIBFW_TEST_DATA_SIZE 256
66 /* Define memory descriptors for memory management */
67 lib_memdscr_t test_vfast_buf;
68 lib_memdscr_t test_fast_buf;
69 lib_memdscr_t test_med_buf;
70 lib_memdscr_t test_slow_buf;
72 /* Define a memory descriptor array */
73 lib_memdscr_t * test_memdscr_tab[LIB_MEMTYPE_N] = {
74 &test_vfast_buf,
75 &test_fast_buf,
76 &test_med_buf,
77 &test_slow_buf
78 };
80 #ifdef SOC_K2G
81 /* defined in src/ti/libarch/src/k2g/k2g_cfg.c */
82 extern signed char* getGlobalAddr(signed char* addr);
83 #else
84 void * getGlobalAddr(signed char* addr)
85 {
86 return ((void *)addr);
87 }
88 #endif
90 /*==============================================================================
91 * This function returns the address of the memory descriptor array
92 *============================================================================*/
93 void * testGetMemHandle()
94 {
95 return((void *)&test_memdscr_tab[0]);
96 } /* testGetMemHandle */
98 /*==============================================================================
99 * Example of library memory requirement query function
100 * It returns the size requirement of each of the 4 memory types defined in
101 * the library framework.
102 *============================================================================*/
103 void test_GetSizes(size_t *smem_size_vfast, size_t *smem_size_fast,
104 size_t *smem_size_med, size_t *smem_size_slow)
105 {
106 *smem_size_vfast = TEST_MEM_SIZE_VFAST; // very fast scratch memory
107 *smem_size_fast = TEST_MEM_SIZE_FAST; // fast scratch memory
108 *smem_size_med = TEST_MEM_SIZE_MED; // medium speed scratch memory
109 *smem_size_slow = TEST_MEM_SIZE_SLOW; // slow scratch memory
110 } /* test_GetSizes */
112 /*==============================================================================
113 * Example of library initialization function
114 * It performs necessary initialization through library framework API in order
115 * to do memory allocations.
116 *============================================================================*/
117 int test_Init(void * svfast_buf_base, size_t svfast_buf_size,
118 void * sfast_buf_base, size_t sfast_buf_size,
119 void * smed_buf_base, size_t smed_buf_size,
120 void * sslow_buf_base, size_t sslow_buf_size)
121 {
122 lib_memdscr_t **test_mem_handle = testGetMemHandle();
124 /* Verify supplied memories meet requirements */
125 if( (svfast_buf_base!=NULL) && (svfast_buf_size>=TEST_MEM_SIZE_VFAST)
126 &&(sfast_buf_base !=NULL) && (sfast_buf_size >=TEST_MEM_SIZE_FAST)
127 &&(smed_buf_base !=NULL) && (smed_buf_size >=TEST_MEM_SIZE_MED)
128 &&(sslow_buf_base !=NULL) && (sslow_buf_size >=TEST_MEM_SIZE_MED)
129 ) {
130 lib_smem_vinit(test_mem_handle, svfast_buf_base, svfast_buf_size);
131 lib_smem_finit(test_mem_handle, sfast_buf_base, sfast_buf_size);
132 lib_smem_minit(test_mem_handle, smed_buf_base, smed_buf_size);
133 lib_smem_sinit(test_mem_handle, sslow_buf_base, sslow_buf_size);
135 return(LIBFW_TEST_INIT_NOERR);
136 }
137 else {
138 return(LIBFW_TEST_INIT_ERROR);
139 }
140 } /* test_Init */
143 /*==============================================================================
144 * Example of library processing function
145 * It uses library framework API to do:
146 * - scratch memory allocations
147 * - memory transfers
148 *============================================================================*/
149 void lib_dsp_func(const double *in1, const double *in2, double *out,
150 int num_elements)
151 {
152 double *vfast_buf, *fast_buf1, *fast_buf2, *med_buf, *slow_buf;
153 void *test_mem_handle;
154 lib_emt_Handle test_emt_handle;
155 int i;
157 printf("DSP library kernel for LibArch testing:\n");
159 /* Use memory allocation API to allocate memories. This can also be done in
160 test_Init, but that would require usage of global variables. */
161 test_mem_handle = testGetMemHandle();
162 vfast_buf = (double *)lib_smem_valloc(test_mem_handle, num_elements*sizeof(double), 3);
163 fast_buf1 = (double *)lib_smem_falloc(test_mem_handle, num_elements*sizeof(double), 3);
164 med_buf = (double *)lib_smem_malloc(test_mem_handle, num_elements*sizeof(double), 3);
165 slow_buf = (double *)lib_smem_salloc(test_mem_handle, num_elements*sizeof(double), 3);
167 if(vfast_buf==NULL || fast_buf1==NULL || med_buf==NULL || slow_buf==NULL) {
168 printf("Scratch (shared) heap allocation error!\n");
169 return;
170 }
172 /* Use external memory transfer API */
173 if((test_emt_handle=lib_emt_alloc(1))==NULL) {
174 printf("External memory transfer handle allocation error!\n");
175 return;
176 }
178 printf("DSP library kernel: Transferring input 1 to very fast memory buffer (L1D).\n");
179 if(lib_emt_copy1D1D(test_emt_handle,
180 (void *)getGlobalAddr((signed char *)in1),
181 (void *)getGlobalAddr((signed char *)vfast_buf),
182 num_elements*sizeof(double))) {
183 printf("External memory transfer error!\n");
184 return;
185 }
187 lib_emt_wait(test_emt_handle);
189 printf("DSP library kernel: Transferring input 2 to fast memory buffer (L2).\n");
190 if(lib_emt_copy1D1D(test_emt_handle,
191 (void *)getGlobalAddr((signed char *)in2),
192 (void *)getGlobalAddr((signed char *)fast_buf1),
193 num_elements*sizeof(double))){
194 printf("External memory transfer error!\n");
195 return;
196 }
198 lib_emt_wait(test_emt_handle);
200 /*
201 for(i=0;i<10;i++)
202 {
203 printf("Input 1: %e, Input 2: %e\n", vfast_buf[i],fast_buf1[i]);
204 }
205 */
207 /* Process input data */
208 printf("DSP library kernel: Processing data in very fast memory.\n");
209 for(i=0;i<num_elements;i++){
210 vfast_buf[i] += fast_buf1[i];
211 }
213 /* Allocate another block of memory */
214 fast_buf2 = lib_smem_falloc(test_mem_handle, num_elements*sizeof(double), 3);
216 /* Use internal memory transfer API */
217 printf("DSP library kernel: Transferring data from very fast memory to fast memory.\n");
218 lib_imt_copy((void *)vfast_buf, (void *)fast_buf2, num_elements*sizeof(double));
219 lib_imt_wait();
220 /*
221 for(i=0;i<10;i++)
222 {
223 printf("Output: %e\n", fast_buf2[i]);
224 }
225 */
227 printf("DSP library kernel: Transferring data from fast memory to output buffer.\n");
228 if(lib_emt_copy1D1D(test_emt_handle,
229 (void *)getGlobalAddr((signed char *)fast_buf2),
230 (void *)getGlobalAddr((signed char *)out),
231 num_elements*sizeof(double))) {
232 printf("Memory transfer to output error!\n");
233 return;
234 }
236 lib_emt_wait(test_emt_handle);
238 lib_emt_free(test_emt_handle);
240 printf("DSP library kernel finished.\n");
242 } /* lib_dsp_func */
244 /* Nothing after this line */