| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 112 |
now = time(NULL); |
| 113 |
|
| 114 |
|
| 115 |
if (mgr->gps.last_update == 0) |
| 116 |
mgr->gps.last_update = now; |
| 117 |
|
| 118 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 154 |
if (interval_sec) { |
| 155 |
|
| 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 |
|
| 165 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
int timeout = 60; |
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 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 |
|
| 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 |
|