test utility for stereo solution (dual TAS2555)
[tas2555sw-android/tas2555-util-stereo.git] / main.c
1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016  Texas Instruments Inc.
4 **
5 ** This program is free software; you can redistribute it and/or modify it under
6 ** the terms of the GNU General Public License as published by the Free Software 
7 ** Foundation; version 2.
8 **
9 ** This program is distributed in the hope that it will be useful, but WITHOUT
10 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 **
13 ** You should have received a copy of the GNU General Public License along with
14 ** this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15 ** Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 **
17 ** File:
18 **     main.c
19 **
20 ** Description:
21 **     test program for TAS2555 Android Linux drivers
22 **
23 ** =============================================================================
24 */
26 #include <sys/cdefs.h>
27 #include <sys/stat.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #include <math.h>
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <fcntl.h>
38 #include "ti_audio.h"
40 static void usage(){
42         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
43                 "usage: ti_audio -r channel book page register [count]               (read, hexdecimal)",
44                 "       ti_audio -w channel book page register value1 [value2 ...]  (write, hexdecimal)",
45                 "       ti_audio -d on                                    (turn on the debug msg, bool)",
46                 "       ti_audio -p [n]                                (get/[set] DSP program, decimal)",
47                 "       ti_audio -c [n]                          (get/[set] DSP configuration, decimal)",
48                 "       ti_audio -s [n]                                (get/[set] sample rate, decimal)",
49                 "       ti_audio -b channel [n]                           (get/[set] bit rate, decimal)",
50                 "       ti_audio -v channel [n]                             (get/[set] volume, decimal)",
51                 "       ti_audio -f                                           (trigger firmware reload)",               
52                 "       ti_audio -o on                                      (turn on/off TAS2555, bool)",
53                 "       ti_audio -t                                            (get firmware timestamp)",
54                 "                channel: 1 - left channel, 2 - right channel, 3 - both channels",
55                 TIAUDIO_VERSION);       
56 }
58 static int str2hexchar(char *argv, unsigned char *pUInt8_Val){
59         int str_len = strlen(argv), i, result = -1, j;
60         unsigned char val[2] = {0};
62         if(str_len > 2){
63                 fprintf(stderr, "invalid parameters\n");
64                 goto err;
65         }
67         for(i = (str_len-1), j=0; i >= 0; i--, j++){
68                 if((argv[i] <= '9')&&(argv[i] >= '0')){
69                         val[j] = argv[i] - 0x30;
70                 }else if((argv[i] <='f')&&(argv[i]>= 'a')){
71                         val[j] = argv[i] - 0x57;
72                 }else if((argv[i] <='F')&&(argv[i]>= 'A')){
73                         val[j] = argv[i] - 0x37;
74                 }else{
75                         fprintf(stderr, "reg/data out of range\n");
76                         goto err;
77                 }
78         }
79         
80         *pUInt8_Val = (unsigned char)(val[0]|(val[1]<<4));
81         result = 0;
82         
83 err:
84         return result;
85 }
87 /*
88 static int str2hexshort(char *argv, unsigned short *pUInt16_Val){
89         int str_len = strlen(argv), i, j, result = -1;
90         unsigned char val[4] = {0};
92         if(str_len > 4){
93                 fprintf(stderr, "invalid parameters\n");
94                 goto err;
95         }
97         for(i = (str_len-1), j=0; i >= 0; i--, j++){
98                 if((argv[i] <= '9')&&(argv[i] >= '0')){
99                         val[j] = argv[i] - 0x30;
100                 }else if((argv[i] <='f')&&(argv[i]>= 'a')){
101                         val[j] = argv[i] - 0x57;
102                 }else if((argv[i] <='F')&&(argv[i]>= 'A')){
103                         val[j] = argv[i] - 0x37;
104                 }else{
105                         fprintf(stderr, "reg/data out of range\n");
106                         goto err;
107                 }
108         }
109         
110         *pUInt16_Val = (unsigned short)(val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12));
111         result = 0;
113 err:
114         return result;
116 */
118 static int str2decimal(char *argv, unsigned int *pUInt32_Val){
119         int max_len = strlen(MAX_INT_STR), i, result = -1, j;
120         int str_len = strlen(argv);
121         unsigned int nValue = 0;
122         unsigned char temp;
124         if(str_len > max_len){
125                 fprintf(stderr, "invalid parameters\n");
126                 goto err;
127         }
128         
129         for(i = (str_len-1), j=0; i >= 0; i--, j++){
130                 if((argv[i] <= '9')&&(argv[i] >= '0')){
131                         temp = argv[i] - 0x30;
132                         nValue += (temp * pow(10, j));
133                 }else{
134                         fprintf(stderr, "reg/data out of range\n");
135                         goto err;
136                 }
137         }
138         
139         *pUInt32_Val = nValue;
140         result = 0;
141         
142 err:
143         return result;
146 static int TiAudio_Reg_Write(int fileHandle, int argc, char **argv){
147         int err = -1;
148         unsigned char *pBuff = NULL, book, page, reg,value;
149         unsigned char channel;
150         unsigned int whole_reg = 0;
151         unsigned int temp_reg = 0;
152         int i=0, reg_count = 0;
153         unsigned char *pChannel;
154         
155         if(argc < 7){
156                 fprintf(stderr, "invalid para numbers\n");
157                 goto err;
158         }
159         
160         reg_count = argc - 6;
162         pBuff = (unsigned char *)malloc(reg_count + 5);
163         if(pBuff == NULL){
164                 fprintf(stderr, "not enough mem\n");
165                 goto err;               
166         }
167         
168         pBuff[0] = TIAUDIO_CMD_REG_WITE;
169         
170         err = str2hexchar(argv[2], &channel);
171         if(err < 0){
172                 goto err;
173         }
174         
175         err = str2hexchar(argv[3], &book);
176         if(err < 0){
177                 goto err;
178         }
179         
180         err = str2hexchar(argv[4], &page);
181         if(err < 0){
182                 goto err;
183         }
184         
185         err = str2hexchar(argv[5], &reg);
186         if(err < 0){
187                 goto err;
188         }
189         
190         pBuff[1] = channel;
191         if(channel == 1)
192                 pChannel = LEFT_CHANNEL_STR;
193         else if(channel == 2)
194                 pChannel = RIGHT_CHANNEL_STR;
195         else if(channel == 3){
196                 pChannel = BOTH_CHANNELS_STR;
197         }
198         else{
199                 fprintf(stderr, "chanel err=%d\n", channel);    
200                 goto err;
201         }
202         
203         whole_reg = TAS2555_REG(book, page, reg);
204         pBuff[2] = (whole_reg & 0xff000000) >> 24;
205         pBuff[3] = (whole_reg & 0x00ff0000) >> 16;
206         pBuff[4] = (whole_reg & 0x0000ff00) >> 8;
207         pBuff[5] = (whole_reg & 0x000000ff) ;
208         
209         for(i=0; i< reg_count; i++){
210                 err = str2hexchar(argv[i+6], &value);
211                 if(err < 0){
212                         goto err;
213                 }
214                 pBuff[i + 6] = value;
215         }
216         
217         err = write(fileHandle, pBuff, reg_count+6);
218         if(err != (reg_count+6)){
219                 fprintf(stderr, "write err=%d\n", err);
220         }else{
221                 for(i=0; i< reg_count; i++){
222                         temp_reg = whole_reg + i;
223                         fprintf(stderr, "W CHL[%s] B[%d]P[%d]R[%d]=0x%x\n", pChannel,
224                                 TAS2555_BOOK_ID(temp_reg), TAS2555_PAGE_ID(temp_reg), TAS2555_PAGE_REG(temp_reg), pBuff[i + 6] );
225                 }
226         }       
228 err:
229         if(pBuff != NULL)
230                 free(pBuff);    
231         
232         return err;
235 static int TiAudio_Reg_Read(int fileHandle, int argc, char **argv){
236         int err = -1;
237         unsigned char cmd[6];
238         unsigned char *pBuff = NULL, book, page, reg, len;
239         unsigned int whole_reg = 0;
240         unsigned char channel;
241         unsigned int temp_reg = 0;
242         int i=0, reg_count = 0;
243         unsigned char *pChannel;
244         
245         if((argc != 6) &&(argc != 7))  {
246                 fprintf(stderr, "invalid para numbers\n");
247                 goto err;
248         }
249         
250         if(argc == 6)
251                 reg_count = 1;
252         else{
253                 err = str2hexchar(argv[6], &len);
254                 if(err < 0){
255                         goto err;
256                 }
257                 reg_count = len;
258         }
259         
260         pBuff = (unsigned char *)malloc(reg_count);
261         if(pBuff == NULL){
262                 fprintf(stderr, "not enough mem\n");
263                 goto err;               
264         }
265         
266         cmd[0] = TIAUDIO_CMD_REG_READ;
267         
268         err = str2hexchar(argv[2], &channel);
269         if(err < 0){
270                 goto err;
271         }
272         
273         err = str2hexchar(argv[3], &book);
274         if(err < 0){
275                 goto err;
276         }
277         
278         err = str2hexchar(argv[4], &page);
279         if(err < 0){
280                 goto err;
281         }
282         
283         err = str2hexchar(argv[5], &reg);
284         if(err < 0){
285                 goto err;
286         }
287         
288         if((channel >= 1)&&(channel <=3))
289         {
290                 
291         }else{
292                 fprintf(stderr, "chanel err=%d\n", channel);    
293                 goto err;
294         }
295         
296         whole_reg = TAS2555_REG(book, page, reg);
297         cmd[2] = (whole_reg & 0xff000000) >> 24;
298         cmd[3] = (whole_reg & 0x00ff0000) >> 16;
299         cmd[4] = (whole_reg & 0x0000ff00) >> 8;
300         cmd[5] = (whole_reg & 0x000000ff) ;
301         
302         
303         if(channel&0x01){
304                 cmd[1] = 1;
305                 pChannel = LEFT_CHANNEL_STR;
306                 err = write(fileHandle, cmd, 6);
307                 if(err != 6){
308                         fprintf(stderr, "read err=%d\n", err);
309                         goto err;
310                 }
311                 
312                 err = read(fileHandle, pBuff, reg_count);
313                 if(err != reg_count){
314                         fprintf(stderr, "read err=%d\n", err);
315                         goto err;
316                 }else{
317                         for(i=0; i< reg_count; i++){
318                                 temp_reg = whole_reg + i;
319                                 fprintf(stderr, "R CHL[%s] B[%d]P[%d]R[%d]=0x%x\n", pChannel,
320                                         TAS2555_BOOK_ID(temp_reg), TAS2555_PAGE_ID(temp_reg), TAS2555_PAGE_REG(temp_reg), pBuff[i]);
321                         }
322                 }
323         }
324         
325         if(channel&0x02){
326                 cmd[1] = 2;
327                 pChannel = RIGHT_CHANNEL_STR;
328                 err = write(fileHandle, cmd, 6);
329                 if(err != 6){
330                         fprintf(stderr, "read err=%d\n", err);
331                         goto err;
332                 }
333                 
334                 err = read(fileHandle, pBuff, reg_count);
335                 if(err != reg_count){
336                         fprintf(stderr, "read err=%d\n", err);
337                         goto err;
338                 }else{
339                         for(i=0; i< reg_count; i++){
340                                 temp_reg = whole_reg + i;
341                                 fprintf(stderr, "R CHL[%s] B[%d]P[%d]R[%d]=0x%x\n", pChannel,
342                                         TAS2555_BOOK_ID(temp_reg), TAS2555_PAGE_ID(temp_reg), TAS2555_PAGE_REG(temp_reg), pBuff[i]);
343                         }
344                 }
345         }
346         
348 err:
349         if(pBuff != NULL)
350                 free(pBuff);    
351         
352         return err;
355 static int TiAudio_Debug_On(int fileHandle, int argc, char **argv){
356         int err = -1;
357         unsigned char pBuff[2], on;
358         
359         if(argc != 3) {
360                 fprintf(stderr, "invalid para numbers\n");
361                 goto err;
362         }
363         
364         pBuff[0] = TIAUDIO_CMD_DEBUG_ON;
365         
366         err = str2hexchar(argv[2], &on);
367         if(err < 0){
368                 goto err;
369         }
370         
371         pBuff[1] = on; 
372         
373         err = write(fileHandle, pBuff, 2);
374         if(err != 2){
375                 fprintf(stderr, "set err=%d\n", err);
376                 goto err;
377         }
378         
379         if(on == 0){
380                 fprintf(stderr, "DBG msg Off\n");
381         }else{
382                 fprintf(stderr, "DBG msg On\n");
383         }
384         
385 err:
387         return err;
390 static int TiAudio_Program(int fileHandle, int argc, char **argv){
391         int err = -1;
392         unsigned char pBuff[PROGRAM_BUF_SIZE], bSet = 0;
393         unsigned int nProgram;
394         
395         if(argc == 2){
396                 bSet = 0;
397         }else if(argc == 3){
398                 bSet = 1;
399         }else{
400                 fprintf(stderr, "invalid para numbers\n");
401                 goto err;
402         }
403         
404         pBuff[0] = TIAUDIO_CMD_PROGRAM;
405         
406         if(bSet == 1){
407                 err = str2decimal(argv[2], &nProgram);
408                 if(err < 0){
409                         fprintf(stderr, "invalid para numbers\n");              
410                         goto err;
411                 }
412                 
413                 pBuff[1] = nProgram;
414         }
415         
416         err = write(fileHandle, pBuff, (1+bSet));
417         if(err != (bSet+1)){
418                 fprintf(stderr, "write err=%d\n", err);
419                 goto err;
420         }
421         
422         if(bSet == 1){
423                 fprintf(stderr, "Program Set to %d\n", nProgram);
424         }else{
425                 err = read(fileHandle, pBuff, PROGRAM_BUF_SIZE);
426                 if(err != PROGRAM_BUF_SIZE){
427                         fprintf(stderr, "read err=%d\n", err);
428                         goto err;
429                 }else{
430                         unsigned char nPrograms = pBuff[0];
431                         unsigned char nCurProgram = pBuff[1];
432                         unsigned char *pName = &pBuff[2];
433                         unsigned char *pDescription = &pBuff[2 + FW_NAME_SIZE];
434                         fprintf(stderr, "Total Programs   : %d\n", nPrograms);
435                         fprintf(stderr, "Current Programs : %d\n", nCurProgram);
436                         fprintf(stderr, "\t Name: %s\n", pName);
437                         fprintf(stderr, "\t Description : %s\n", pDescription);
438                 }
439         }
441 err:
443         return err;
446 static int TiAudio_Configuration(int fileHandle, int argc, char **argv){
447         int err = -1;
448         unsigned char pBuff[CONFIGURATION_BUF_SIZE], bSet = 0;
449         unsigned int nConfiguration;
450         
451         if(argc == 2){
452                 bSet = 0;
453         }else if(argc == 3){
454                 bSet = 1;
455         }else{
456                 fprintf(stderr, "invalid para numbers\n");
457                 goto err;
458         }
459         
460         pBuff[0] = TIAUDIO_CMD_CONFIGURATION;
461         
462         if(bSet == 1){
463                 err = str2decimal(argv[2], &nConfiguration);
464                 if(err < 0){
465                         goto err;
466                 }
467                 
468                 pBuff[1] = nConfiguration;
469         }
470         
471         err = write(fileHandle, pBuff, (1+bSet));
472         if(err != (bSet+1)){
473                 fprintf(stderr, "write err=%d\n", err);
474                 goto err;
475         }
476         
477         if(bSet == 1){
478                 fprintf(stderr, "Configuration Set to %d\n", nConfiguration);
479         }else{
480                 err = read(fileHandle, pBuff, CONFIGURATION_BUF_SIZE);
481                 if(err != CONFIGURATION_BUF_SIZE){
482                         fprintf(stderr, "read err=%d\n", err);
483                         goto err;
484                 }else{
485                         unsigned char nConfigurations = pBuff[0];
486                         unsigned char nCurConfiguration = pBuff[1];
487                         unsigned char *pName = &pBuff[2];
488                         unsigned char nProgram = pBuff[2 + FW_NAME_SIZE];
489                         unsigned char nPLL = pBuff[3 + FW_NAME_SIZE];
490                         unsigned int nSampleRate = pBuff[4 + FW_NAME_SIZE] + 
491                                 ((unsigned int)pBuff[5 + FW_NAME_SIZE] << 8) +
492                                 ((unsigned int)pBuff[6 + FW_NAME_SIZE] << 16) +
493                                 ((unsigned int)pBuff[7 + FW_NAME_SIZE] << 24);                  
494                         unsigned char *pDescription = &pBuff[8 + FW_NAME_SIZE];
495                         fprintf(stderr, "Total Configurations : %d\n", nConfigurations);
496                         fprintf(stderr, "Current Configuration: %d\n", nCurConfiguration);
497                         fprintf(stderr, "\t Name: %s\n", pName);
498                         fprintf(stderr, "\t Description : %s\n", pDescription);
499                         fprintf(stderr, "\t nProgram: %d\n", nProgram);
500                         fprintf(stderr, "\t nPLL: %d\n", nPLL);
501                         fprintf(stderr, "\t nSampleRate: %d\n", nSampleRate);
502                 }
503         }
505 err:
507         return err;
510 static int TiAudio_SampleRate(int fileHandle, int argc, char **argv){
511         int err = -1;
512         unsigned char pBuff[5];
513         unsigned char bSet = 0;
514         unsigned int nSampleRate;
515         int nLen;
516         
517         if(argc == 2){
518                 bSet = 0;
519         }else if(argc == 3){
520                 bSet = 1;
521         }else{
522                 fprintf(stderr, "invalid para numbers\n");
523                 goto err;
524         }
525         
526         pBuff[0] = TIAUDIO_CMD_SAMPLERATE;
527         
528         if(bSet == 1){
529                 if(strcmp(argv[2], SR_STR_48K) == 0)
530                         nSampleRate = SR_NUM_48K;
531                 else if(strcmp(argv[2], SR_STR_44K) == 0)
532                         nSampleRate = SR_NUM_44K;
533                 else if(strcmp(argv[2], SR_STR_16K) == 0)
534                         nSampleRate = SR_NUM_16K;
535                 else{
536                         fprintf(stderr, "invalid para numbers\n");
537                         goto err;
538                 }
539                 
540                 pBuff[1] = (nSampleRate&0xff000000)>>24;
541                 pBuff[2] = (nSampleRate&0x00ff0000)>>16;
542                 pBuff[3] = (nSampleRate&0x0000ff00)>>8;
543                 pBuff[4] = (nSampleRate&0x000000ff);
544         }
545         
546         if(bSet) nLen = 5; 
547         else nLen = 1;
548         
549         err = write(fileHandle, pBuff, nLen);
550         if(err != nLen){
551                 fprintf(stderr, "write err=%d\n", err);
552                 goto err;
553         }
554         
555         if(bSet == 1){
556                 fprintf(stderr, "Sample Rate Set to %d\n", nSampleRate);
557         }else{
558                 err = read(fileHandle, pBuff, 4);
559                 if(err != 4){
560                         fprintf(stderr, "read err=%d\n", err);
561                         goto err;
562                 }else{
563                         nSampleRate = pBuff[0] + 
564                                 ((unsigned int)pBuff[1] << 8) +
565                                 ((unsigned int)pBuff[2] << 16) +
566                                 ((unsigned int)pBuff[3] << 24);                 
567                         fprintf(stderr, "\t nSampleRate: %d\n", nSampleRate);
568                 }
569         }
571 err:
573         return err;
576 static int TiAudio_BitRate(int fileHandle, int argc, char **argv){
577         int err = -1;
578         unsigned char cmd[3];
579         unsigned char pBuff[5];
580         unsigned char bSet = 0;
581         unsigned char channel;
582         unsigned int nBitRate;
583         unsigned char *pChannel;
584         
585         if(argc == 3){
586                 bSet = 0;
587         }else if(argc == 4){
588                 bSet = 1;
589         }else{
590                 fprintf(stderr, "invalid para numbers\n");
591                 goto err;
592         }
593         
594         cmd[0] = TIAUDIO_CMD_BITRATE;
595         err = str2hexchar(argv[2], &channel);
596         if(err < 0){
597                 goto err;
598         }
599         
600         if((channel >= 1)&&(channel <=3))
601         {
602         
603         }else{
604                 fprintf(stderr, "chanel err=%d\n", channel);    
605                 goto err;
606         }
607                 
608         if(bSet == 1){          
609                 if(strcmp(argv[3], "32") == 0)
610                         nBitRate = 32;
611                 else if(strcmp(argv[3], "24") == 0)
612                         nBitRate = 24;
613                 else if(strcmp(argv[3], "20") == 0)
614                         nBitRate = 20;
615                 else if(strcmp(argv[3], "16") == 0)
616                         nBitRate = 16;                  
617                 else{
618                         fprintf(stderr, "invalid para numbers\n");
619                         goto err;
620                 }
621                 
622                 cmd[2] = nBitRate;
623         }
624         
625         if(channel&0x01){
626                 cmd[1] = 1;
627                 pChannel = LEFT_CHANNEL_STR;
628                 err = write(fileHandle, cmd, (2+bSet));
629                 if(err != (2+bSet)){
630                         fprintf(stderr, "write err=%d\n", err);
631                         goto err;
632                 }
633         
634                 if(bSet == 1){
635                         fprintf(stderr, "CHL[%s] BitRate Set to %d\n", pChannel, nBitRate);
636                 }else{
637                         err = read(fileHandle, pBuff, 1);
638                         if(err != 1){
639                                 fprintf(stderr, "read err=%d\n", err);
640                                 goto err;
641                         }else{
642                                 nBitRate = pBuff[0];
643                                 fprintf(stderr, "\t CHL[%s] BitRate: %d\n", pChannel, nBitRate);
644                         }
645                 }       
646         }
647         
648         if(channel&0x02){
649                 cmd[1] = 2;
650                 pChannel = RIGHT_CHANNEL_STR;
651                 err = write(fileHandle, cmd, (2+bSet));
652                 if(err != (2+bSet)){
653                         fprintf(stderr, "write err=%d\n", err);
654                         goto err;
655                 }
656         
657                 if(bSet == 1){
658                         fprintf(stderr, "CHL[%s] BitRate Set to %d\n", pChannel, nBitRate);
659                 }else{
660                         err = read(fileHandle, pBuff, 1);
661                         if(err != 1){
662                                 fprintf(stderr, "read err=%d\n", err);
663                                 goto err;
664                         }else{
665                                 nBitRate = pBuff[0];
666                                 fprintf(stderr, "\t CHL[%s] BitRate: %d\n", pChannel, nBitRate);
667                         }
668                 }       
669         }       
671 err:
673         return err;
676 static int TiAudio_DACVolume(int fileHandle, int argc, char **argv){
677         int err = -1;
678         unsigned char cmd[3];
679         unsigned char pBuff[5];
680         unsigned char bSet = 0;
681         unsigned int nVol;
682         unsigned char channel;
683         unsigned char *pChannel;
684                 
685         if(argc == 3){
686                 bSet = 0;
687         }else if(argc == 4){
688                 bSet = 1;
689         }else{
690                 fprintf(stderr, "invalid para numbers\n");
691                 goto err;
692         }
693         
694         cmd[0] = TIAUDIO_CMD_DACVOLUME;
695         
696         err = str2hexchar(argv[2], &channel);
697         if(err < 0){
698                 goto err;
699         }
701         if((channel >= 1)&&(channel <=3))
702         {
703         
704         }else{
705                 fprintf(stderr, "chanel err=%d\n", channel);    
706                 goto err;
707         }
708         
709         if(bSet == 1){
710                 err = str2decimal(argv[3], &nVol);
711                 if(err < 0){
712                         goto err;
713                 }
714                 
715                 cmd[2] = nVol;
716         }
717         
718         if(channel&0x01){
719                 cmd[1] = 1;
720                 pChannel = LEFT_CHANNEL_STR;
721                 err = write(fileHandle, cmd, (2+bSet));
722                 if(err != (2+bSet)){
723                         fprintf(stderr, "write err=%d\n", err);
724                         goto err;
725                 }
726         
727                 if(bSet == 1){
728                         fprintf(stderr, "CHL[%s] DAC Volume Set to %d\n", pChannel, nVol);
729                 }else{
730                         err = read(fileHandle, pBuff, 1);
731                         if(err != 1){
732                                 fprintf(stderr, "read err=%d\n", err);
733                                 goto err;
734                         }else{
735                                 nVol = pBuff[0];                
736                                 fprintf(stderr, "\tCHL[%s] DAC Volume: %d\n", pChannel, nVol);
737                         }
738                 }
739         }
740         
741         if(channel&0x02){
742                 cmd[1] = 2;
743                 pChannel = RIGHT_CHANNEL_STR;
744                 err = write(fileHandle, cmd, (2+bSet));
745                 if(err != (2+bSet)){
746                         fprintf(stderr, "write err=%d\n", err);
747                         goto err;
748                 }
749         
750                 if(bSet == 1){
751                         fprintf(stderr, "CHL[%s] DAC Volume Set to %d\n", pChannel, nVol);
752                 }else{
753                         err = read(fileHandle, pBuff, 1);
754                         if(err != 1){
755                                 fprintf(stderr, "read err=%d\n", err);
756                                 goto err;
757                         }else{
758                                 nVol = pBuff[0];                
759                                 fprintf(stderr, "\tCHL[%s] DAC Volume: %d\n", pChannel, nVol);
760                         }
761                 }
762         }       
763         
764 err:
766         return err;
769 static int TiAudio_SpeakerOn(int fileHandle, int argc, char **argv){
770         int err = -1;
771         unsigned char pBuff[2], on;
772         
773         if(argc != 3) {
774                 fprintf(stderr, "invalid para numbers\n");
775                 goto err;
776         }
777         
778         pBuff[0] = TIAUDIO_CMD_SPEAKER;
779         
780         err = str2hexchar(argv[2], &on);
781         if(err < 0){
782                 goto err;
783         }
784         
785         pBuff[1] = on; 
786         
787         err = write(fileHandle, pBuff, 2);
788         if(err != 2){
789                 fprintf(stderr, "set err=%d\n", err);
790                 goto err;
791         }
792         
793         if(on == 0){
794                 fprintf(stderr, "TAS2555 Power Off\n");
795         }else{
796                 fprintf(stderr, "TAS2555 Power On\n");
797         }
798         
799 err:
801         return err;
804 static int TiAudio_Timestamp(int fileHandle, int argc, char **argv){
805         int err = -1;
806         unsigned char pBuff[4];
807         
808         pBuff[0] = TIAUDIO_CMD_FW_TIMESTAMP;
809         
810         err = write(fileHandle, pBuff, 1);
811         if(err != 1){
812                 fprintf(stderr, "write err=%d\n", err);
813                 goto err;
814         }
815         
816         err = read(fileHandle, pBuff, 4);
817         if(err != 4){
818                 fprintf(stderr, "read err=%d\n", err);
819                 goto err;
820         }else{
821                 unsigned int nTimestamp = pBuff[0] + 
822                         ((unsigned int)pBuff[1] << 8) +
823                         ((unsigned int)pBuff[2] << 16) +
824                         ((unsigned int)pBuff[3] << 24);
825         
826                 time_t t = (time_t)nTimestamp;  
827                 struct tm *p;  
828                 p=localtime(&t);  
829                 char s[100];  
830                 strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);  
831                 fprintf(stderr, "FW Timestamp : %d: %s\n", (int)t, s); 
832         }
834 err:
836         return err;
839 static int TiAudio_TriggerFWReload(int fileHandle, int argc, char **argv){
840         int err = -1;
841         unsigned char pBuff[4]; 
842         pBuff[0] = TIAUDIO_CMD_FW_RELOAD;
843         
844         err = write(fileHandle, pBuff, 1);
845         if(err != 1){
846                 fprintf(stderr, "write err=%d\n", err);
847                 goto err;
848         }
849         
850         fprintf(stderr, "Firmware Reload Triggered\n"); 
851         
852 err:
854         return err;
857 static int getDevHandle(){
858         int fileHandle = -1;
859         fileHandle = open(TI_AUDIO_NAME, O_RDWR);
860         if(fileHandle < 0 ){
861                 fprintf(stderr, "[ERROR]file(%s) open_RDWR error\n", TI_AUDIO_NAME);
862         }               
864         return fileHandle;
867 int main(int argc, char **argv)
869         int ret = 0;
870         int ch;
871         int fileHandle = -1;
873         fileHandle = getDevHandle();
874         if(fileHandle < 0 ){
875                 fprintf(stderr, " file handle err=%d\n", fileHandle);
876                 return ret;
877         }
878         
879         if(argc == 1){
880                 usage();
881                 return 0;
882         }
884         while ((ch = getopt(argc, argv, "wrdpcsbvotf")) != -1) {
885                 switch (ch) {
886                 case 'w': 
887                         ret = TiAudio_Reg_Write(fileHandle, argc, argv);                        
888                         break;
889                 case 'r':
890                         ret = TiAudio_Reg_Read(fileHandle, argc, argv);
891                         break;
892                 case 'd':
893                         ret = TiAudio_Debug_On(fileHandle, argc, argv);
894                         break;          
895                 case 'p':
896                         ret = TiAudio_Program(fileHandle, argc, argv);
897                         break;                                  
898                 case 'c':
899                         ret = TiAudio_Configuration(fileHandle, argc, argv);
900                         break;          
901                 case 's':
902                         ret = TiAudio_SampleRate(fileHandle, argc, argv);
903                         break;          
904                 case 'b':
905                         ret = TiAudio_BitRate(fileHandle, argc, argv);
906                         break;
907                 case 'v':
908                         ret = TiAudio_DACVolume(fileHandle, argc, argv);
909                         break;          
910                 case 'o':
911                         ret = TiAudio_SpeakerOn(fileHandle, argc, argv);
912                         break;                          
913                 case 't':
914                         ret = TiAudio_Timestamp(fileHandle, argc, argv);
915                         break;          
916                 case 'f':
917                         ret = TiAudio_TriggerFWReload(fileHandle, argc, argv);
918                         break;
919                 default:
920                         usage();
921                         break;
922                 }
923         }
925         if(fileHandle > 0 )
926                 close(fileHandle);
927         
928         return ret;