treewide: remove FSF address
[osmocom-bb.git] / src / target / firmware / layer1 / toa.c
blob4c9fff8da4a1a08680769f5fca08b14e5410a870
1 /* AFC (Automatic Frequency Correction) Implementation */
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
6 * All Rights Reserved
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <stdint.h>
21 #include <stdio.h>
23 #include <debug.h>
24 #include <osmocom/gsm/gsm_utils.h>
26 #include <layer1/toa.h>
27 #include <layer1/avg.h>
28 #include <layer1/sync.h>
30 /* Over how many TDMA frames do we want to average? */
31 #define TOA_PERIOD 250
32 /* How many of our measurements have to be valid? */
33 #define TOA_MIN_MUN_VALID 125
35 // FIXME:
36 #define TOA_SNR_THRESHOLD 2560 /* 2.5 dB in fx6.10 */
38 struct toa_state {
39 struct running_avg ravg; /* running average */
43 static void toa_ravg_output(struct running_avg *ravg, int32_t avg);
45 static struct toa_state toa_state = {
46 .ravg = {
47 .outfn = &toa_ravg_output,
48 .period = TOA_PERIOD,
49 .min_valid = TOA_MIN_MUN_VALID,
53 void toa_input(int32_t offset, uint32_t snr)
55 int valid = 1;
57 if (snr < TOA_SNR_THRESHOLD || offset < 0 || offset >31)
58 valid = 0;
59 runavg_input(&toa_state.ravg, offset, valid);
60 runavg_check_output(&toa_state.ravg);
63 void toa_reset(void)
65 toa_state.ravg.num_samples = toa_state.ravg.num_samples_valid = 0;
66 toa_state.ravg.acc_val = 0;
69 /* callback function for runavg */
70 static void toa_ravg_output(struct running_avg *ravg, int32_t avg)
72 if (avg != 16) {
73 printf("TOA AVG is not 16 qbits, correcting (got %ld)\n", avg);
74 l1s.tpu_offset_correction = avg - 16;