Debian Codegraph @

src/osmo-bts-lc15/misc/lc15bts_mgr_calib.c

raw symbols SCIP
1 /* OCXO calibration control for Litecell 1.5 BTS management daemon */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_mgr_calib.c
7 * (C) 2014,2015 by Holger Hans Peter Freyther
8 * (C) 2014 by Harald Welte for the IPA code from the oml router
9 *
10 * All Rights Reserved
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27 #include "misc/lc15bts_mgr.h"
28 #include "misc/lc15bts_misc.h"
29 #include "misc/lc15bts_clock.h"
30 #include "misc/lc15bts_swd.h"
31 #include "misc/lc15bts_par.h"
32 #include "misc/lc15bts_led.h"
33 #include "osmo-bts/msg_utils.h"
34
35 #include <osmocom/core/logging.h>
36 #include <osmocom/core/select.h>
37
38 #include <osmocom/ctrl/control_cmd.h>
39
40 #include <osmocom/gsm/ipa.h>
41 #include <osmocom/gsm/protocol/ipaccess.h>
42
43 #include <osmocom/abis/abis.h>
44 #include <osmocom/abis/e1_input.h>
45 #include <osmocom/abis/ipa.h>
46
47 #include <time.h>
48
49 static void calib_adjust(struct lc15bts_mgr_instance *mgr);
50 static void calib_state_reset(struct lc15bts_mgr_instance *mgr, int reason);
51 static void calib_loop_run(void *_data);
52
53 static int ocxodac_saved_value = -1;
54
55 enum calib_state {
56 CALIB_INITIAL,
57 CALIB_IN_PROGRESS,
58 };
59
60 enum calib_result {
61 CALIB_FAIL_START,
62 CALIB_FAIL_GPSFIX,
63 CALIB_FAIL_CLKERR,
64 CALIB_FAIL_OCXODAC,
65 CALIB_SUCCESS,
66 };
67
68 static void calib_start(struct lc15bts_mgr_instance *mgr)
69 {
70 int rc;
71
72 rc = lc15bts_clock_err_open();
73 if (rc != 0) {
74 LOGP(DCALIB, LOGL_ERROR, "Failed to open clock error module %d\n", rc);
75 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
76 return;
77 }
78
79 rc = lc15bts_clock_dac_open();
80 if (rc != 0) {
81 LOGP(DCALIB, LOGL_ERROR, "Failed to open OCXO dac module %d\n", rc);
82 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
83 return;
84 }
85
86 calib_adjust(mgr);
87 }
88
89 static void calib_adjust(struct lc15bts_mgr_instance *mgr)
90 {
91 int rc;
92 int fault;
93 int error_ppt;
94 int accuracy_ppq;
95 int interval_sec;
96 int dac_value;
97 int new_dac_value;
98 int dac_correction;
99 time_t now;
100 time_t last_gps_fix;
101
102 rc = lc15bts_clock_err_get(&fault, &error_ppt,
103 &accuracy_ppq, &interval_sec);
104 if (rc < 0) {
105 LOGP(DCALIB, LOGL_ERROR,
106 "Failed to get clock error measurement %d\n", rc);
107 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
108 return;
109 }
110
111 /* get current time */
112 now = time(NULL);
113
114 /* first time after start of manager program */
115 if (mgr->gps.last_update == 0)
116 mgr->gps.last_update = now;
117
118 /* read last GPS 3D fix from storage */
119 rc = lc15bts_par_get_gps_fix(&last_gps_fix);
120 if (rc < 0) {
121 LOGP(DCALIB, LOGL_NOTICE, "Last GPS 3D fix can not read (%d). Last GPS 3D fix sets to zero\n", rc);
122 last_gps_fix = 0;
123 }
124
125 if (fault) {
126 LOGP(DCALIB, LOGL_NOTICE, "GPS has no fix\n");
127 calib_state_reset(mgr, CALIB_FAIL_GPSFIX);
128 return;
129 }
130
131 /* We got GPS 3D fix */
132 LOGP(DCALIB, LOGL_DEBUG, "Got GPS 3D fix warn_flags=0x%08x, last=%lld, now=%lld\n",
133 mgr->lc15bts_ctrl.warn_flags,
134 (long long)last_gps_fix,
135 (long long)now);
136
137 rc = lc15bts_clock_dac_get(&dac_value);
138 if (rc < 0) {
139 LOGP(DCALIB, LOGL_ERROR,
140 "Failed to get OCXO dac value %d\n", rc);
141 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
142 return;
143 }
144
145 /* Set OCXO initial dac value */
146 if (ocxodac_saved_value < 0)
147 ocxodac_saved_value = dac_value;
148
149 LOGP(DCALIB, LOGL_NOTICE,
150 "Calibration ERR(%f PPB) ACC(%f PPB) INT(%d) DAC(%d)\n",
151 error_ppt / 1000., accuracy_ppq / 1000000., interval_sec, dac_value);
152
153 /* Need integration time to correct */
154 if (interval_sec) {
155 /* 1 unit of correction equal about 0.5 - 1 PPB correction */
156 dac_correction = (int)(-error_ppt * 0.0015);
157 new_dac_value = dac_value + dac_correction;
158
159 if (new_dac_value > 4095)
160 new_dac_value = 4095;
161 else if (new_dac_value < 0)
162 new_dac_value = 0;
163
164 /* We have a fix, make sure the measured error is
165 meaningful (10 times the accuracy) */
166 if ((new_dac_value != dac_value) && ((100l * abs(error_ppt)) > accuracy_ppq)) {
167
168 LOGP(DCALIB, LOGL_NOTICE,
169 "Going to apply %d as new clock setting.\n",
170 new_dac_value);
171
172 rc = lc15bts_clock_dac_set(new_dac_value);
173 if (rc < 0) {
174 LOGP(DCALIB, LOGL_ERROR,
175 "Failed to set OCXO dac value %d\n", rc);
176 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
177 return;
178 }
179 rc = lc15bts_clock_err_reset();
180 if (rc < 0) {
181 LOGP(DCALIB, LOGL_ERROR,
182 "Failed to reset clock error module %d\n", rc);
183 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
184 return;
185 }
186 }
187 /* New conditions to store DAC value:
188 * - Resolution accuracy less or equal than 0.01PPB (or 10000 PPQ)
189 * - Error less or equal than 2PPB (or 2000PPT)
190 * - Solution different than the last one */
191 else if (accuracy_ppq <= 10000) {
192 if((dac_value != ocxodac_saved_value) && (abs(error_ppt) < 2000)) {
193 LOGP(DCALIB, LOGL_NOTICE, "Saving OCXO DAC value to memory... val = %d\n", dac_value);
194 rc = lc15bts_clock_dac_save();
195 if (rc < 0) {
196 LOGP(DCALIB, LOGL_ERROR,
197 "Failed to save OCXO dac value %d\n", rc);
198 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
199 } else {
200 ocxodac_saved_value = dac_value;
201 }
202 }
203
204 rc = lc15bts_clock_err_reset();
205 if (rc < 0) {
206 LOGP(DCALIB, LOGL_ERROR,
207 "Failed to reset clock error module %d\n", rc);
208 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
209 }
210 }
211 } else {
212 LOGP(DCALIB, LOGL_NOTICE, "Skipping this iteration, no integration time\n");
213 }
214
215 calib_state_reset(mgr, CALIB_SUCCESS);
216 return;
217 }
218
219 static void calib_close(struct lc15bts_mgr_instance *mgr)
220 {
221 lc15bts_clock_err_close();
222 lc15bts_clock_dac_close();
223 }
224
225 static void calib_state_reset(struct lc15bts_mgr_instance *mgr, int outcome)
226 {
227 if (mgr->calib.calib_from_loop) {
228 /*
229 * In case of success calibrate in two hours again
230 * and in case of a failure in some minutes.
231 *
232 * TODO NTQ: Select timeout based on last error and accuracy
233 */
234 int timeout = 60;
235 //int timeout = 2 * 60 * 60;
236 //if (outcome != CALIB_SUCESS) }
237 // timeout = 5 * 60;
238 //}
239
240 mgr->calib.calib_timeout.data = mgr;
241 mgr->calib.calib_timeout.cb = calib_loop_run;
242 osmo_timer_schedule(&mgr->calib.calib_timeout, timeout, 0);
243 /* TODO: do we want to notify if we got a calibration error, like no gps fix? */
244 lc15bts_swd_event(mgr, SWD_CHECK_CALIB);
245 }
246
247 mgr->calib.state = CALIB_INITIAL;
248 calib_close(mgr);
249 }
250
251 static int calib_run(struct lc15bts_mgr_instance *mgr, int from_loop)
252 {
253 if (mgr->calib.state != CALIB_INITIAL) {
254 LOGP(DCALIB, LOGL_ERROR, "Calib is already in progress.\n");
255 return -1;
256 }
257
258 mgr->calib.calib_from_loop = from_loop;
259
260 /* From now on everything will be handled from the failure */
261 mgr->calib.state = CALIB_IN_PROGRESS;
262 calib_start(mgr);
263 return 0;
264 }
265
266 static void calib_loop_run(void *_data)
267 {
268 int rc;
269 struct lc15bts_mgr_instance *mgr = _data;
270
271 LOGP(DCALIB, LOGL_NOTICE, "Going to calibrate the system.\n");
272 rc = calib_run(mgr, 1);
273 if (rc != 0) {
274 calib_state_reset(mgr, CALIB_FAIL_START);
275 }
276 }
277
278 int lc15bts_mgr_calib_run(struct lc15bts_mgr_instance *mgr)
279 {
280 return calib_run(mgr, 0);
281 }
282
283 int lc15bts_mgr_calib_init(struct lc15bts_mgr_instance *mgr)
284 {
285 mgr->calib.state = CALIB_INITIAL;
286 mgr->calib.calib_timeout.data = mgr;
287 mgr->calib.calib_timeout.cb = calib_loop_run;
288 osmo_timer_schedule(&mgr->calib.calib_timeout, 0, 0);
289
290 return 0;
291 }
292