update FTC to v1.0.0.2, add ftc-stop
[tas2557sw-android/tas2557-ftc.git] / tas2557_ftc.c
1 /*\r
2 ** =============================================================================\r
3 ** Copyright (c) 2016  Texas Instruments Inc.\r
4 **\r
5 ** This program is free software; you can redistribute it and/or modify it under\r
6 ** the terms of the GNU General Public License as published by the Free Software \r
7 ** Foundation; version 2.\r
8 **\r
9 ** This program is distributed in the hope that it will be useful, but WITHOUT\r
10 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
11 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\r
12 **\r
13 ** You should have received a copy of the GNU General Public License along with\r
14 ** this program; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
15 ** Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
16 **\r
17 ** File:\r
18 **     tas2557_ftc.c\r
19 **\r
20 ** Description:\r
21 **     factory test program for TAS2557 Android devices\r
22 **\r
23 ** =============================================================================\r
24 */\r
25 \r
26 #include <stdio.h>\r
27 #include <stdint.h>\r
28 #include <math.h>\r
29 #include <sys/types.h>\r
30 \r
31 #include "system.h"\r
32 #include "tas2557.h"        // TAS2557 Driver\r
33 #include "tas2557_ftc_lib.h"    \r
34 #include "tas2557_ftc.h"    // TAS2557 Factory Test and Calibration Tool\r
35 \r
36 #define PI                   3.14159\r
37 \r
38 static struct TFTCConfiguration *gpFTCC;\r
39 \r
40 // -----------------------------------------------------------------------------\r
41 // tas2557_ftc\r
42 // -----------------------------------------------------------------------------\r
43 // Description:\r
44 //      Obtains Re, f0, Q and T_cal from the speaker. This only needs to be\r
45 //      executed once during production line test.\r
46 // -----------------------------------------------------------------------------\r
47 uint32_t tas2557_ftc(double t_cal, struct TFTCConfiguration *pFTCC)\r
48 {\r
49         int nResult = 0;\r
50         double dev_a_re = pFTCC->nTSpkCharDevA.nPPC3_Re0;   // Default Re\r
51         uint32_t dev_a_prm_pow = 0;           // Total RMS power coefficient\r
52         uint32_t dev_a_prm_tlimit = 0;        // Delta temperature limit coefficient\r
53         uint8_t nPGID;\r
54         uint32_t libVersion;\r
55         uint32_t result = 0;\r
56         pid_t nPlaybackProcess;\r
57 \r
58         gpFTCC = pFTCC;\r
59 \r
60         libVersion = get_lib_ver();\r
61         printf("libVersion=0x%x\r\n", libVersion);\r
62 \r
63         /* get device PGID */\r
64         nPGID = tas2557_get_PGID();\r
65         printf("PGID=0x%x\r\n", nPGID);\r
66 \r
67         /* set device PGID to FTC process */\r
68         tas2557_ftc_set_PGID(nPGID);\r
69 \r
70         /* STEP 1: Play calibration signal */\r
71         tas2557_mixer_command("PRI_MI2S_RX Audio Mixer MultiMedia1", 1); //platform dependent\r
72         nPlaybackProcess = sys_play_wav("silense.wav", "loop");\r
73 \r
74         /* STEP 2: start calibration process */\r
75         tas2557_ftc_start();\r
76 \r
77         /* STEP 3: Wait for algorithm to converge */\r
78         sys_delay(gpFTCC->nCalibrationTime); \r
79 \r
80         /* STEP 4: Get actual Re from TAS2557 */\r
81         dev_a_re = get_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0);\r
82 \r
83         /* STEP 5: check speaker bounds */\r
84         nResult = check_spk_bounds(gpFTCC, dev_a_re);\r
85 \r
86         /* STEP 6: Set temperature limit to target TMAX */\r
87         if((nResult& RE1_CHK_MSK) == RESULT_PASS){\r
88                 dev_a_prm_pow = calc_prm_pow (dev_a_re, \r
89                         gpFTCC->nTSpkCharDevA.nSpkTMax - t_cal, \r
90                         gpFTCC->nTSpkCharDevA.nPPC3_RTV, \r
91                         gpFTCC->nTSpkCharDevA.nPPC3_RTM, \r
92                         gpFTCC->nTSpkCharDevA.nPPC3_RTVA, \r
93                         gpFTCC->nTSpkCharDevA.nPPC3_SysGain);\r
94                 dev_a_prm_tlimit = calc_prm_tlimit(gpFTCC->nTSpkCharDevA.nSpkTMax - t_cal, \r
95                         gpFTCC->nTSpkCharDevA.nSpkReAlpha, \r
96                         gpFTCC->nTSpkCharDevA.nPPC3_DevNonlinPer, \r
97                         gpFTCC->nTSpkCharDevA.nPPC3_RTV, \r
98                         gpFTCC->nTSpkCharDevA.nPPC3_RTM, \r
99                         gpFTCC->nTSpkCharDevA.nPPC3_RTVA);\r
100                 set_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0, dev_a_re, gpFTCC->nTSpkCharDevA.nSpkReAlpha);\r
101                 set_temp_cal(dev_a_prm_pow, dev_a_prm_tlimit);\r
102         }\r
103 \r
104         tas2557_ftc_stop();\r
105 \r
106         sys_stop_wav(nPlaybackProcess);\r
107 \r
108         /* STEP 7: Save Re, and Cal Temp into a file */\r
109         tas2557_save_cal(gpFTCC, dev_a_re, dev_a_prm_pow, dev_a_prm_tlimit, t_cal, nResult, "tas2557_cal.txt");\r
110 \r
111         /* STEP 8: Save .bin file for TAS2555 driver */\r
112         if ((nResult & RE1_CHK_MSK) == RESULT_PASS) {\r
113                 tas2557_open_bin("tas2557_cal.bin");\r
114                 set_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0, dev_a_re, gpFTCC->nTSpkCharDevA.nSpkReAlpha);\r
115                 set_temp_cal(dev_a_prm_pow, dev_a_prm_tlimit);\r
116 \r
117                 tas2557_close_bin();\r
118         }\r
119 \r
120         if (gpFTCC->bLoadCalibration)\r
121                 tas2557_load_calibration(0xFF);\r
122 \r
123         tas2557_ftc_release();\r
124 \r
125         return result;\r
126 }\r