update to version v1.0.0.2 : add "restore original state after FTC"
[tas2557sw-android/tas2557dm-ftc.git] / tas2557dm_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         double dev_b_re = pFTCC->nTSpkCharDevB.nPPC3_Re0;   // Default Re\r
54         uint32_t dev_b_prm_pow = 0;           // Total RMS power coefficient\r
55         uint32_t dev_b_prm_tlimit = 0;        // Delta temperature limit coefficient\r
56         uint8_t nPGID;\r
57         uint32_t libVersion;\r
58         uint32_t result = 0;\r
59         pid_t nPlaybackProcess;\r
60 \r
61         gpFTCC = pFTCC;\r
62 \r
63         libVersion = get_lib_ver();\r
64         printf("libVersion=0x%x\r\n", libVersion);\r
65 \r
66         /* get device PGID */\r
67         tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
68         nPGID = tas2557_get_PGID();\r
69         printf("PGID=0x%x\r\n", nPGID);\r
70 \r
71         /* set device PGID to FTC process */\r
72         tas2557_ftc_set_PGID(nPGID);\r
73 \r
74         // STEP 1: Play calibration signal\r
75         tas2557_mixer_command("PRI_MI2S_RX Audio Mixer MultiMedia1", 1); //platform dependent\r
76         nPlaybackProcess = sys_play_wav("silense.wav", "loop");\r
77 \r
78         // STEP 2: start calibration process\r
79         tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
80     tas2557_ftc_start();\r
81         tas2557_switch_device(gpFTCC->nTSpkCharDevB.nDevAddr);\r
82         tas2557_ftc_start();\r
83 \r
84         // STEP 3: Wait for algorithm to converge\r
85         sys_delay(gpFTCC->nCalibrationTime); // Delay \r
86 \r
87         // STEP 4: Get actual Re from TAS2557\r
88         tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
89         dev_a_re = get_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0);\r
90         tas2557_switch_device(gpFTCC->nTSpkCharDevB.nDevAddr);\r
91         dev_b_re = get_re(gpFTCC->nTSpkCharDevB.nPPC3_Re0);\r
92 \r
93         // STEP 5: check speaker bounds\r
94         nResult = check_spk_bounds(gpFTCC, dev_a_re, dev_b_re);\r
95 \r
96         // STEP 6: Set temperature limit to target TMAX\r
97         if ((nResult& RE1_CHK_MSK) == RESULT_PASS) {\r
98                 tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
99                 dev_a_prm_pow = calc_prm_pow (dev_a_re, \r
100                         gpFTCC->nTSpkCharDevA.nSpkTMax - t_cal, \r
101                         gpFTCC->nTSpkCharDevA.nPPC3_RTV, \r
102                         gpFTCC->nTSpkCharDevA.nPPC3_RTM, \r
103                         gpFTCC->nTSpkCharDevA.nPPC3_RTVA, \r
104                         gpFTCC->nTSpkCharDevA.nPPC3_SysGain);\r
105                 dev_a_prm_tlimit = calc_prm_tlimit(gpFTCC->nTSpkCharDevA.nSpkTMax - t_cal, \r
106                         gpFTCC->nTSpkCharDevA.nSpkReAlpha, \r
107                         gpFTCC->nTSpkCharDevA.nPPC3_DevNonlinPer, \r
108                         gpFTCC->nTSpkCharDevA.nPPC3_RTV, \r
109                         gpFTCC->nTSpkCharDevA.nPPC3_RTM, \r
110                         gpFTCC->nTSpkCharDevA.nPPC3_RTVA);\r
111                 set_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0, dev_a_re, gpFTCC->nTSpkCharDevA.nSpkReAlpha);\r
112                 set_temp_cal(dev_a_prm_pow, dev_a_prm_tlimit);\r
113         }\r
114 \r
115         if ((nResult& RE2_CHK_MSK) == RESULT_PASS) {\r
116                 tas2557_switch_device(gpFTCC->nTSpkCharDevB.nDevAddr);\r
117                 dev_b_prm_pow = calc_prm_pow (dev_b_re, \r
118                         gpFTCC->nTSpkCharDevB.nSpkTMax - t_cal, \r
119                         gpFTCC->nTSpkCharDevB.nPPC3_RTV, \r
120                         gpFTCC->nTSpkCharDevB.nPPC3_RTM, \r
121                         gpFTCC->nTSpkCharDevB.nPPC3_RTVA, \r
122                         gpFTCC->nTSpkCharDevB.nPPC3_SysGain);\r
123                 dev_b_prm_tlimit = calc_prm_tlimit(gpFTCC->nTSpkCharDevB.nSpkTMax - t_cal, \r
124                         gpFTCC->nTSpkCharDevB.nSpkReAlpha, \r
125                         gpFTCC->nTSpkCharDevB.nPPC3_DevNonlinPer, \r
126                         gpFTCC->nTSpkCharDevB.nPPC3_RTV, \r
127                         gpFTCC->nTSpkCharDevB.nPPC3_RTM, \r
128                         gpFTCC->nTSpkCharDevB.nPPC3_RTVA);\r
129                 set_re(gpFTCC->nTSpkCharDevB.nPPC3_Re0, dev_b_re, gpFTCC->nTSpkCharDevB.nSpkReAlpha);\r
130                 set_temp_cal(dev_b_prm_pow, dev_b_prm_tlimit);\r
131         }\r
132 \r
133         // STEP 7: start calibration process\r
134         tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
135         tas2557_ftc_stop();\r
136         tas2557_switch_device(gpFTCC->nTSpkCharDevB.nDevAddr);\r
137         tas2557_ftc_stop();\r
138 \r
139         sys_stop_wav(nPlaybackProcess);\r
140 \r
141         // STEP 8: Save Re, and Cal Temp into a file\r
142         tas2557_save_cal(gpFTCC, dev_a_re, dev_a_prm_pow, dev_a_prm_tlimit, \r
143                 dev_b_re, dev_b_prm_pow, dev_b_prm_tlimit, \r
144                 t_cal, nResult, "tas2557_cal.txt");\r
145 \r
146     // STEP 9: Save .bin file for TAS2555 driver\r
147         if ((nResult & (RE1_CHK_MSK | RE2_CHK_MSK)) == RESULT_PASS) {\r
148                 tas2557_open_bin("tas2557_cal.bin", gpFTCC->nTSpkCharDevA.nDevAddr, gpFTCC->nTSpkCharDevB.nDevAddr);\r
149                 if ((nResult & RE1_CHK_MSK) == RESULT_PASS) {\r
150                         tas2557_switch_device(gpFTCC->nTSpkCharDevA.nDevAddr);\r
151                         set_re(gpFTCC->nTSpkCharDevA.nPPC3_Re0, dev_a_re, gpFTCC->nTSpkCharDevA.nSpkReAlpha);\r
152                         set_temp_cal(dev_a_prm_pow, dev_a_prm_tlimit);\r
153                 }\r
154 \r
155                 if ((nResult & RE2_CHK_MSK) == RESULT_PASS) {\r
156                         tas2557_switch_device(gpFTCC->nTSpkCharDevB.nDevAddr);\r
157                         set_re(gpFTCC->nTSpkCharDevB.nPPC3_Re0, dev_b_re, gpFTCC->nTSpkCharDevB.nSpkReAlpha);\r
158                         set_temp_cal(dev_b_prm_pow, dev_b_prm_tlimit);\r
159                 }\r
160 \r
161                 tas2557_close_bin();\r
162         }\r
163 \r
164         if (gpFTCC->bLoadCalibration)\r
165                 tas2557_load_calibration(0xFF);\r
166 \r
167         tas2557_ftc_release();\r
168 \r
169     return result;\r
170 }\r