summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/ipc/xt-shmem/hikey/dsp_debug.c
blob: a74308e423c2a637bf036e8dec66fdc7a59b9e7c (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

#include <stdarg.h>
#include <stdlib.h>
#include "dsp_debug.h"
#include "dsp_memory_config.h"
#include "dsp_driver_mailbox.h"
#include "dsp_comm.h"
#include "dsp_pcm_gain.h"
extern int g_pcm_gain;
extern int MsgFlag;
extern int InpBuf[HOLD_BUF_SIZE];					// assumes 32 bit samples.
extern int OutBuf[HOLD_BUF_SIZE];				// assumes 32 bit samples.

static int hex2asc(int n)
{
	n &= 15;
	if(n > 9){
		return ('a' - 10) + n;
	} else {
		return '0' + n;
	}
}

static void xputs(const char *s, void (*xputc)(unsigned n, void *cookie), void *cookie)
{
	while (*s) {
		xputc(*s++, cookie);
	}
}

void __xprintf(const char *fmt, va_list ap,void (*xputc)(unsigned n, void *cookie), void *cookie)
{
	char scratch[32];
	for(;;){
		switch(*fmt){
		case 0:
			va_end(ap);
			return;
		case '%':
			switch(fmt[1]) {
			case 'c': {
				unsigned n = va_arg(ap, unsigned);
				xputc(n, cookie);
			fmt += 2;
				continue;
		}
			case 'h': {
				unsigned n = va_arg(ap, unsigned);
			xputc(hex2asc(n >> 12), cookie);
				xputc(hex2asc(n >> 8), cookie);
				xputc(hex2asc(n >> 4), cookie);
				xputc(hex2asc(n >> 0), cookie);
				fmt += 2;
				continue;
			}
			case 'b': {
				unsigned n = va_arg(ap, unsigned);
			xputc(hex2asc(n >> 4), cookie);
				xputc(hex2asc(n >> 0), cookie);
				fmt += 2;
				continue;
			}
			case 'p':
			case 'X':
			case 'x': {
				unsigned n = va_arg(ap, unsigned);
				char *p = scratch + 15;
				*p = 0;
				do {
					*--p = hex2asc(n);
				n = n >> 4;
				} while(n != 0);
				while(p > (scratch + 7)) *--p = '0';
				xputs(p, xputc, cookie);
				fmt += 2;
				continue;
			}
  	case 'd': {
				int n = va_arg(ap, int);
				char *p = scratch + 15;
				*p = 0;
				if(n < 0) {
					xputc('-', cookie);
				n = -n;
				}
				do {
					*--p = (n % 10) + '0';
					n /= 10;
				} while(n != 0);
				xputs(p, xputc, cookie);
				fmt += 2;
				continue;
			}

        case 'f': {
                double fnum = va_arg(ap, double);
                unsigned long long ipart, fpart;
                int i=4;
                char *p = scratch + 31;
                *p = '\0';
            
            	if(fnum < 0.0) 
                {
        		    xputc('-', cookie);
        		    fnum = -fnum;
                }
                ipart = (unsigned long long)fnum;
                fpart = ((fnum-ipart)*10000); //10^i = 10000

        		while(i>0)
                {
		            *--p = (fpart % 10) + '0';
	        	    fpart /= 10;
                    i--;
		        }
                *--p = '.';
		        while(ipart > 0)
                {
    		        *--p = (ipart % 10) + '0';
	    	        ipart /= 10;
		        }
                
                xputs(p, xputc, cookie); 
                fmt += 2;    
                continue;
            }

			case 'u': {
				unsigned n = va_arg(ap, unsigned);
				char *p = scratch + 15;
			*p = 0;
				do {
					*--p = (n % 10) + '0';
					n /= 10;
				} while(n != 0);
				xputs(p, xputc, cookie);
				fmt += 2;
				continue;
		}
			case 's': {
				char *s = (char *)va_arg(ap, char*);
				if(s == 0) s = "(null)";
				xputs(s, xputc, cookie);
				fmt += 2;
				continue;
			}
			case 'l': {
				if (fmt[2] == 'x') {
					unsigned long long n = va_arg(ap, unsigned long long);
					char *p = scratch + 23;
					*p = 0;
					do {
						*--p = hex2asc((int)n);
						n = n >> 4;
					} while(n != 0);
				while(p > (scratch + 7)) *--p = '0';
					xputs(p, xputc, cookie);
					fmt += 3;
				continue;
				}
			}
			}
			xputc(*fmt++, cookie);
			break;
		case '\n':
			xputc('\r', cookie);
		default:
			xputc(*fmt++, cookie);
		}
	}
}

static char* log_to_mem_head = 0;
void dsp_debug_init()
{
	log_to_mem_head = (char *)(*((unsigned int *)DRV_DSP_UART_TO_MEM_CUR_ADDR) + DRV_DSP_UART_TO_MEM);

}

static void log_write_to_mem(const char c_data)
{
	*log_to_mem_head = c_data;

	log_to_mem_head++;
	if ((unsigned int)log_to_mem_head >= (DRV_DSP_UART_TO_MEM + DRV_DSP_UART_TO_MEM_SIZE - 1))
		log_to_mem_head = (char *)DRV_DSP_UART_TO_MEM + DRV_DSP_UART_TO_MEM_RESERVE_SIZE;

	*((unsigned int *)DRV_DSP_UART_TO_MEM_CUR_ADDR) = (unsigned int)log_to_mem_head - DRV_DSP_UART_TO_MEM;
}


static void print_char(const char c_data)
{
	if (c_data == '\n')
		log_write_to_mem('\r');
	log_write_to_mem(c_data);
}

typedef void (*xputc_type)(unsigned n, void *cookie);

void print_log(const char *fmt, ...)
{
	va_list args;
va_start(args, fmt);
	__xprintf(fmt, args, (xputc_type)print_char, 0);
	va_end(args);
}
#ifdef HIKEY_XAF_IPC_COMMENT_OUT
#define ISSPACE(c) (c == ' ' || c == 0x09 || c == 0x0A || c == 0x0D || c == 0)

char* dsp_om_trim_zero(char* str)
{
	char *str_begin = 0;
	char *str_end = 0;

	if (!str)
		return 0;

	str_begin = str;
	str_end = str + strlen(str);

	while (str_begin < str_end) {
		if (ISSPACE(*str_begin)) {
			*str_begin = 0;
			str_begin++;
		} else {
			break;
		}
	}
	while (str_begin < str_end) {
		if (ISSPACE(*str_end)) {
			*str_end = 0;
			str_end--;
		} else {
			break;
		}
	}

	return str_begin;
}

char * dsp_om_split_str(char* str, char** split_str)
{
	char *str_begin = 0;
	char *str_end = 0;

	if ((!str) || (!split_str)) {
		DSP_LOGE("input param is null\n");
		return str;
	}

	str_end = str + strlen(str);
	str_begin = dsp_om_trim_zero(str);

	if (str_begin == str_end) {
		DSP_LOGE("input str all space\n");
		return 0;
	}

	*split_str = dsp_om_trim_zero(strchr(str_begin, ' '));

	return str_begin;
}

#ifdef GJB_CHANGE
void send_msg_data_to_ap()
{

    struct hikey_msg_with_content hikey_msg;
    DSP_LOGE("%s\n", __func__);
    hikey_msg.msg_info.msg_id=HIKEY_AUDIO_DSP_AP_OM_CMD;
    hikey_msg.msg_info.msg_len=HIKEY_AP_DSP_MSG_MAX_LEN;
    strncpy(hikey_msg.msg_info.msg_content,"pcm_gain",HIKEY_AP_DSP_MSG_MAX_LEN);
    dsp_mailbox_write(&hikey_msg);
    DSP_LOGE("Exit %s\n", __func__);
}

void send_pcm_data_to_ap()
{
    struct hikey_ap_dsp_msg_body msg_info;
    DSP_LOGE("Enter %s\n", __func__);
    msg_info.msg_id = ID_XAF_DSP_TO_AP;
    msg_info.msg_len = sizeof(msg_info);
    msg_info.xf_dsp_msg.id= 0;
    msg_info.xf_dsp_msg.opcode = 0xc;
    msg_info.xf_dsp_msg.length = 0x400;
    msg_info.xf_dsp_msg.address = 0x8B432000;
    dsp_mailbox_write(&msg_info);
    DSP_LOGE("Exit %s\n", __func__);
}
#else
void send_msg_data_to_ap()
{
    xf_proxy_message_t hikey_msg;
    DSP_LOGE("%s\n", __func__);
    hikey_msg.id=HIKEY_AUDIO_DSP_AP_OM_CMD;
    hikey_msg.length=HIKEY_AP_DSP_MSG_MAX_LEN;
    strncpy(hikey_msg.address,"pcm_gain",HIKEY_AP_DSP_MSG_MAX_LEN);
    dsp_mailbox_write(&hikey_msg);
    DSP_LOGE("Exit %s\n", __func__);
}

void send_pcm_data_to_ap()
{
    xf_proxy_message_t msg_info;
    DSP_LOGE("Enter %s\n", __func__);
    msg_info.id = ID_XAF_DSP_TO_AP;
    msg_info.opcode = 0xc;
    msg_info.length = 0x400;
    msg_info.address = 0x8B432000;
    dsp_mailbox_write(&msg_info);
    DSP_LOGE("Exit %s\n", __func__);
}
#endif
static void dsp_om_read_mem(char *str)
{
	unsigned int addr  = 0;
	unsigned int val = 0;
	if (!str) {
		DSP_LOGE("str is null\n");
		return;
	}

	addr = strtoul(str, 0, 16);
	DSP_LOGD("str:%s addr:0x%x\n", str, addr);

	val = *(unsigned int*)addr;
    send_pcm_data_to_ap();
//send_msg_data_to_ap();
    dsp_ipc_send_irq_to_ap();
	DSP_LOGI("read addr:0x%x value:0x%x\n", addr, val);
	return;
}

static void dsp_om_write_mem(char *str)
{
	char* str_addr  = 0;
	char* str_val = 0;
	unsigned int addr  = 0;
	unsigned int val = 0;
	if (!str) {
	DSP_LOGE("str is null\n");
		return;
	}

	str_addr = dsp_om_split_str(str, &str_val);

	if(!str_addr || !str_val) {
		DSP_LOGE("str:%s str_addr:%s strValue:%s\n", str, str_addr ? str_addr : "null", str_val ? str_val : "null");
		return;
	}
	addr = strtoul(str_addr, 0, 16);
	val= strtoul(str_val, 0, 16);
	DSP_LOGI("str_addr:%s addr:%x str_val:%s val:%x\n", str_addr ? str_addr : "null", addr, str_val ? str_val : "null", val);

	*(unsigned int *)addr = val;

	return;

}

static void dsp_om_pcm_gain(char *str)
{
	char* str_addr  = 0;
	char* str_val = 0;
	unsigned int addr  = 0;
	unsigned int val = 0;
	if (!str) {
	DSP_LOGE("str is null\n");
		return;
	}
	str_addr = dsp_om_split_str(str, &str_val);
	if(!str_addr || !str_val) {
		DSP_LOGE("str:%s str_addr:%s strValue:%s\n", str, str_addr ? str_addr : "null", str_val ? str_val : "null");
		return;
	}
	addr = strtoul(str_addr, 0, 16);
	val= strtoul(str_val, 0, 16);
	DSP_LOGI("str_addr:%s addr:%x str_val:%s val:%x\n", str_addr ? str_addr : "null", addr, str_val ? str_val : "null", val);
    if(ReadData((char*)InpBuf, val) ){
        processAudio(OutBuf, InpBuf, (val/4));  
        if(WriteData((char*)OutBuf, val)) {
            MsgFlag = MSG_PROC; // indicate that the msg is processed.
            DSP_LOGI("PCM gain processed\n");
            send_msg_data_to_ap();
            dsp_ipc_send_irq_to_ap(); // Indicate data is ready  to pickup...  Maybe you need to send a msg to AP.
        }
        else {                
            DSP_LOGI("PCM gain Write error\n");
            MsgFlag = MSG_INCOMP;
            dsp_ipc_send_irq_to_ap();// Report error..
        }
    }
    else {
        DSP_LOGI("PCM gain Read error\n");
        MsgFlag = MSG_INCOMP;
        dsp_ipc_send_irq_to_ap();
    }
	return;
}
typedef void (*om_proc_func)(char *str);

struct om_proc_info {
	char *om_proc_name;
	om_proc_func func;
};

struct om_proc_info om_proc_table[] = {
	{"read_mem", dsp_om_read_mem},
	{"write_mem", dsp_om_write_mem},
	{"pcm_gain", dsp_om_pcm_gain},
};

om_proc_func dsp_om_get_func_by_name(char *name)
{
	unsigned int i = 0;
	unsigned int func_num = sizeof(om_proc_table) / sizeof(om_proc_table[0]);

	if (!name) {
		DSP_LOGE("name is null\n");
		return 0;
	}

	for (i = 0; i < func_num; i++)
		if (!strncmp((char *)om_proc_table[i].om_proc_name, name, strlen((char *)om_proc_table[i].om_proc_name)))
			return om_proc_table[i].func;

	return 0;
}

void dsp_om_func_proc(char *om_str, unsigned int str_len)
{
	char * cmd_name = 0;
	char * str_param = 0;
	om_proc_func proc_func = 0;

	cmd_name = dsp_om_split_str((char *)om_str, &str_param);

	DSP_LOGI("cmd_name:%s\n", cmd_name);
	proc_func = dsp_om_get_func_by_name(cmd_name);
	if (proc_func)
		proc_func(str_param);
	else
		DSP_LOGE("do not find func\n");
}
#endif