support TAS2559 stereo
[tas2557sw-android/tas2557-stereo-util.git] / ti_audio_s / 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_s -r channel book page register [count]               (read, hexdecimal)",
44                 "       ti_audio_s -w channel book page register value1 [value2 ...]  (write, hexdecimal)",
45                 "       ti_audio_s -d on                                    (turn on the debug msg, bool)",
46                 "       ti_audio_s -p [n]                                (get/[set] DSP program, decimal)",
47                 "       ti_audio_s -c [n]                          (get/[set] DSP configuration, decimal)",
48                 "       ti_audio_s -s [n]                                (get/[set] sample rate, decimal)",
49                 "       ti_audio_s -b channel [n]                           (get/[set] bit rate, decimal)",
50                 "       ti_audio_s -v channel [n]                             (get/[set] volume, decimal)",
51                 "       ti_audio_s -f                                           (trigger firmware reload)",             
52                 "       ti_audio_s -o on                                      (turn on/off TAS2557, bool)",
53                 "       ti_audio_s -t                                            (get firmware timestamp)",
54                 "                channel: 1 - left/DevA, 2 - right/DevB, 3 - both",
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         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         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 nAppMode = pBuff[2]; 
433                         unsigned short nBoost = ((unsigned short)pBuff[3]<<8) + pBuff[4];                       
434                         unsigned char *pName = &pBuff[5];
435                         unsigned char *pDescription = &pBuff[5 + FW_NAME_SIZE];
437                         if(nAppMode == 0) 
438                                 fprintf(stderr, "\t AppMode : %s\n", "ROM1"); 
439                         else if(nAppMode == 1) 
440                                 fprintf(stderr, "\t AppMode : %s\n", "ROM2"); 
441                         else if(nAppMode == 2) 
442                                 fprintf(stderr, "\t AppMode : %s\n", "Tuning Mode"); 
443                         else
444                                 fprintf(stderr, "\t AppMode : %d (unknown)\n", nAppMode);
446             if (nBoost == 0)
447                                 fprintf(stderr, "\t BoostMode : %s\n", "BOOST_OFF"); 
448                         else if (nBoost == 1)
449                                 fprintf(stderr, "\t BoostMode : %s\n", "ROOST_ON_DEV_A"); 
450             else if (nBoost == 2)
451                                 fprintf(stderr, "\t BoostMode : %s\n", "ROOST_ON_DEV_B"); 
452             else if (nBoost == 3)
453                                 fprintf(stderr, "\t BoostMode : %s\n", "ROOST_ON_BOTH"); 
454                         else
455                                 fprintf(stderr, "\t BoostMode : %d(unknown)\n", nBoost); 
456                 
457                         fprintf(stderr, "Total Programs   : %d\n", nPrograms);
458                         fprintf(stderr, "Current Programs : %d\n", nCurProgram);
459                         fprintf(stderr, "\t Name: %s\n", pName);
460                         fprintf(stderr, "\t Description : %s\n", pDescription);
461                 }
462         }
464 err:
466         return err;
469 static int TiAudio_Configuration(int fileHandle, int argc, char **argv){
470         int err = -1;
471         unsigned char pBuff[CONFIGURATION_BUF_SIZE], bSet = 0;
472         unsigned int nConfiguration;
473         
474         if(argc == 2){
475                 bSet = 0;
476         }else if(argc == 3){
477                 bSet = 1;
478         }else{
479                 fprintf(stderr, "invalid para numbers\n");
480                 goto err;
481         }
482         
483         pBuff[0] = TIAUDIO_CMD_CONFIGURATION;
484         
485         if(bSet == 1){
486                 err = str2decimal(argv[2], &nConfiguration);
487                 if(err < 0){
488                         goto err;
489                 }
490                 
491                 pBuff[1] = nConfiguration;
492         }
493         
494         err = write(fileHandle, pBuff, (1+bSet));
495         if(err != (bSet+1)){
496                 fprintf(stderr, "write err=%d\n", err);
497                 goto err;
498         }
499         
500         if(bSet == 1){
501                 fprintf(stderr, "Configuration Set to %d\n", nConfiguration);
502         }else{
503                 err = read(fileHandle, pBuff, CONFIGURATION_BUF_SIZE);
504                 if(err != CONFIGURATION_BUF_SIZE){
505                         fprintf(stderr, "read err=%d\n", err);
506                         goto err;
507                 }else{
508                         unsigned char nConfigurations = pBuff[0];
509                         unsigned char nCurConfiguration = pBuff[1];
510                         unsigned char *pName = &pBuff[2];
511                         unsigned char nProgram = pBuff[2 + FW_NAME_SIZE];
512                         unsigned char nPLL = pBuff[3 + FW_NAME_SIZE];
513                         unsigned int nSampleRate = pBuff[4 + FW_NAME_SIZE] + 
514                                 ((unsigned int)pBuff[5 + FW_NAME_SIZE] << 8) +
515                                 ((unsigned int)pBuff[6 + FW_NAME_SIZE] << 16) +
516                                 ((unsigned int)pBuff[7 + FW_NAME_SIZE] << 24);                  
517                         unsigned char *pDescription = &pBuff[8 + FW_NAME_SIZE];
518                         fprintf(stderr, "Total Configurations : %d\n", nConfigurations);
519                         fprintf(stderr, "Current Configuration: %d\n", nCurConfiguration);
520                         fprintf(stderr, "\t Name: %s\n", pName);
521                         fprintf(stderr, "\t Description : %s\n", pDescription);
522                         fprintf(stderr, "\t nProgram: %d\n", nProgram);
523                         fprintf(stderr, "\t nPLL: %d\n", nPLL);
524                         fprintf(stderr, "\t nSampleRate: %d\n", nSampleRate);
525                 }
526         }
528 err:
530         return err;
533 static int TiAudio_SampleRate(int fileHandle, int argc, char **argv){
534         int err = -1;
535         unsigned char pBuff[5];
536         unsigned char bSet = 0;
537         unsigned int nSampleRate;
538         int nLen;
539         
540         if(argc == 2){
541                 bSet = 0;
542         }else if(argc == 3){
543                 bSet = 1;
544         }else{
545                 fprintf(stderr, "invalid para numbers\n");
546                 goto err;
547         }
548         
549         pBuff[0] = TIAUDIO_CMD_SAMPLERATE;
550         
551         if(bSet == 1){
552                 if(strcmp(argv[2], SR_STR_48K) == 0)
553                         nSampleRate = SR_NUM_48K;
554                 else if(strcmp(argv[2], SR_STR_44K) == 0)
555                         nSampleRate = SR_NUM_44K;
556                 else if(strcmp(argv[2], SR_STR_16K) == 0)
557                         nSampleRate = SR_NUM_16K;
558                 else{
559                         fprintf(stderr, "invalid para numbers\n");
560                         goto err;
561                 }
562                 
563                 pBuff[1] = (nSampleRate&0xff000000)>>24;
564                 pBuff[2] = (nSampleRate&0x00ff0000)>>16;
565                 pBuff[3] = (nSampleRate&0x0000ff00)>>8;
566                 pBuff[4] = (nSampleRate&0x000000ff);
567         }
568         
569         if(bSet) nLen = 5; 
570         else nLen = 1;
571         
572         err = write(fileHandle, pBuff, nLen);
573         if(err != nLen){
574                 fprintf(stderr, "write err=%d\n", err);
575                 goto err;
576         }
577         
578         if(bSet == 1){
579                 fprintf(stderr, "Sample Rate Set to %d\n", nSampleRate);
580         }else{
581                 err = read(fileHandle, pBuff, 4);
582                 if(err != 4){
583                         fprintf(stderr, "read err=%d\n", err);
584                         goto err;
585                 }else{
586                         nSampleRate = pBuff[0] + 
587                                 ((unsigned int)pBuff[1] << 8) +
588                                 ((unsigned int)pBuff[2] << 16) +
589                                 ((unsigned int)pBuff[3] << 24);                 
590                         fprintf(stderr, "\t nSampleRate: %d\n", nSampleRate);
591                 }
592         }
594 err:
596         return err;
599 static int TiAudio_BitRate(int fileHandle, int argc, char **argv){
600         int err = -1;
601         unsigned char cmd[3];
602         unsigned char pBuff[5];
603         unsigned char bSet = 0;
604         unsigned char channel;
605         int nBitRate = -1;
606         char *pChannel;
607         
608         if(argc == 3){
609                 bSet = 0;
610         }else if(argc == 4){
611                 bSet = 1;
612         }else{
613                 fprintf(stderr, "invalid para numbers\n");
614                 goto err;
615         }
616         
617         cmd[0] = TIAUDIO_CMD_BITRATE;
618         err = str2hexchar(argv[2], &channel);
619         if(err < 0){
620                 goto err;
621         }
622         
623         if((channel >= 1)&&(channel <=3))
624         {
625         
626         }else{
627                 fprintf(stderr, "chanel err=%d\n", channel);    
628                 goto err;
629         }
630                 
631         if(bSet == 1){          
632                 if(strcmp(argv[3], "32") == 0)
633                         nBitRate = 32;
634                 else if(strcmp(argv[3], "24") == 0)
635                         nBitRate = 24;
636                 else if(strcmp(argv[3], "20") == 0)
637                         nBitRate = 20;
638                 else if(strcmp(argv[3], "16") == 0)
639                         nBitRate = 16;                  
640                 else{
641                         fprintf(stderr, "invalid para numbers\n");
642                         goto err;
643                 }
644                 
645                 cmd[2] = nBitRate;
646         }
647         
648         if(channel&0x01){
649                 cmd[1] = 1;
650                 pChannel = LEFT_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         }
670         
671         if(channel&0x02){
672                 cmd[1] = 2;
673                 pChannel = RIGHT_CHANNEL_STR;
674                 err = write(fileHandle, cmd, (2+bSet));
675                 if(err != (2+bSet)){
676                         fprintf(stderr, "write err=%d\n", err);
677                         goto err;
678                 }
679         
680                 if(bSet == 1){
681                         fprintf(stderr, "CHL[%s] BitRate Set to %d\n", pChannel, nBitRate);
682                 }else{
683                         err = read(fileHandle, pBuff, 1);
684                         if(err != 1){
685                                 fprintf(stderr, "read err=%d\n", err);
686                                 goto err;
687                         }else{
688                                 nBitRate = pBuff[0];
689                                 fprintf(stderr, "\t CHL[%s] BitRate: %d\n", pChannel, nBitRate);
690                         }
691                 }       
692         }       
694 err:
696         return err;
699 static int TiAudio_DACVolume(int fileHandle, int argc, char **argv){
700         int err = -1;
701         unsigned char cmd[3];
702         unsigned char pBuff[5];
703         unsigned char bSet = 0;
704         unsigned int nVol;
705         unsigned char channel;
706         char *pChannel;
707                 
708         if(argc == 3){
709                 bSet = 0;
710         }else if(argc == 4){
711                 bSet = 1;
712         }else{
713                 fprintf(stderr, "invalid para numbers\n");
714                 goto err;
715         }
716         
717         cmd[0] = TIAUDIO_CMD_DACVOLUME;
718         
719         err = str2hexchar(argv[2], &channel);
720         if(err < 0){
721                 goto err;
722         }
724         if((channel >= 1)&&(channel <=3))
725         {
726         
727         }else{
728                 fprintf(stderr, "chanel err=%d\n", channel);    
729                 goto err;
730         }
731         
732         if(bSet == 1){
733                 err = str2decimal(argv[3], &nVol);
734                 if(err < 0){
735                         goto err;
736                 }
737                 
738                 cmd[2] = nVol;
739         }
740         
741         if(channel&0x01){
742                 cmd[1] = 1;
743                 pChannel = LEFT_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         if(channel&0x02){
765                 cmd[1] = 2;
766                 pChannel = RIGHT_CHANNEL_STR;
767                 err = write(fileHandle, cmd, (2+bSet));
768                 if(err != (2+bSet)){
769                         fprintf(stderr, "write err=%d\n", err);
770                         goto err;
771                 }
772         
773                 if(bSet == 1){
774                         fprintf(stderr, "CHL[%s] DAC Volume Set to %d\n", pChannel, nVol);
775                 }else{
776                         err = read(fileHandle, pBuff, 1);
777                         if(err != 1){
778                                 fprintf(stderr, "read err=%d\n", err);
779                                 goto err;
780                         }else{
781                                 nVol = pBuff[0];                
782                                 fprintf(stderr, "\tCHL[%s] DAC Volume: %d\n", pChannel, nVol);
783                         }
784                 }
785         }       
786         
787 err:
789         return err;
792 static int TiAudio_SpeakerOn(int fileHandle, int argc, char **argv){
793         int err = -1;
794         unsigned char pBuff[2], on;
795         
796         if(argc != 3) {
797                 fprintf(stderr, "invalid para numbers\n");
798                 goto err;
799         }
800         
801         pBuff[0] = TIAUDIO_CMD_SPEAKER;
802         
803         err = str2hexchar(argv[2], &on);
804         if(err < 0){
805                 goto err;
806         }
807         
808         pBuff[1] = on; 
809         
810         err = write(fileHandle, pBuff, 2);
811         if(err != 2){
812                 fprintf(stderr, "set err=%d\n", err);
813                 goto err;
814         }
815         
816         if(on == 0){
817                 fprintf(stderr, "TAS2555 Power Off\n");
818         }else{
819                 fprintf(stderr, "TAS2555 Power On\n");
820         }
821         
822 err:
824         return err;
827 static int TiAudio_Timestamp(int fileHandle){
828         int err = -1;
829         unsigned char pBuff[4];
830         
831         pBuff[0] = TIAUDIO_CMD_FW_TIMESTAMP;
832         
833         err = write(fileHandle, pBuff, 1);
834         if(err != 1){
835                 fprintf(stderr, "write err=%d\n", err);
836                 goto err;
837         }
838         
839         err = read(fileHandle, pBuff, 4);
840         if(err != 4){
841                 fprintf(stderr, "read err=%d\n", err);
842                 goto err;
843         }else{
844                 unsigned int nTimestamp = pBuff[0] + 
845                         ((unsigned int)pBuff[1] << 8) +
846                         ((unsigned int)pBuff[2] << 16) +
847                         ((unsigned int)pBuff[3] << 24);
848         
849                 time_t t = (time_t)nTimestamp;  
850                 struct tm *p;  
851                 p=localtime(&t);  
852                 char s[100];  
853                 strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);  
854                 fprintf(stderr, "FW Timestamp : %d: %s\n", (int)t, s); 
855         }
857 err:
859         return err;
862 static int TiAudio_TriggerFWReload(int fileHandle){
863         int err = -1;
864         unsigned char pBuff[4]; 
865         pBuff[0] = TIAUDIO_CMD_FW_RELOAD;
866         
867         err = write(fileHandle, pBuff, 1);
868         if(err != 1){
869                 fprintf(stderr, "write err=%d\n", err);
870                 goto err;
871         }
872         
873         fprintf(stderr, "Firmware Reload Triggered\n"); 
874         
875 err:
877         return err;
880 static int getDevHandle(){
881         int fileHandle = -1;
882         fileHandle = open(TI_AUDIO_NAME, O_RDWR);
883         if(fileHandle < 0 ){
884                 fprintf(stderr, "[ERROR]file(%s) open_RDWR error\n", TI_AUDIO_NAME);
885         }               
887         return fileHandle;
890 int main(int argc, char **argv)
892         int ret = 0;
893         int ch;
894         int fileHandle = -1;
896         fileHandle = getDevHandle();
897         if(fileHandle < 0 ){
898                 fprintf(stderr, " file handle err=%d\n", fileHandle);
899                 return ret;
900         }
901         
902         if(argc == 1){
903                 usage();
904                 return 0;
905         }
907         while ((ch = getopt(argc, argv, "wrdpcsbvotf")) != -1) {
908                 switch (ch) {
909                 case 'w': 
910                         ret = TiAudio_Reg_Write(fileHandle, argc, argv);                        
911                         break;
912                 case 'r':
913                         ret = TiAudio_Reg_Read(fileHandle, argc, argv);
914                         break;
915                 case 'd':
916                         ret = TiAudio_Debug_On(fileHandle, argc, argv);
917                         break;          
918                 case 'p':
919                         ret = TiAudio_Program(fileHandle, argc, argv);
920                         break;                                  
921                 case 'c':
922                         ret = TiAudio_Configuration(fileHandle, argc, argv);
923                         break;          
924                 case 's':
925                         ret = TiAudio_SampleRate(fileHandle, argc, argv);
926                         break;          
927                 case 'b':
928                         ret = TiAudio_BitRate(fileHandle, argc, argv);
929                         break;
930                 case 'v':
931                         ret = TiAudio_DACVolume(fileHandle, argc, argv);
932                         break;          
933                 case 'o':
934                         ret = TiAudio_SpeakerOn(fileHandle, argc, argv);
935                         break;                          
936                 case 't':
937                         ret = TiAudio_Timestamp(fileHandle);
938                         break;          
939                 case 'f':
940                         ret = TiAudio_TriggerFWReload(fileHandle);
941                         break;
942                 default:
943                         usage();
944                         break;
945                 }
946         }
948         if(fileHandle > 0 )
949                 close(fileHandle);
950         
951         return ret;