firmware: consider reserved ram in loader linkage
[osmocom-bb.git] / src / target / firmware / layer1 / agc.c
1 /* AFC (Automatic Gain Control) Implementation */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25
26 #include <osmocore/gsm_utils.h>
27 #include <debug.h>
28 #include <rffe.h>
29
30 #include <layer1/agc.h>
31 #include <calypso/dsp.h>
32
33 /* compute the input level present at the antenna based on a baseband
34  * power measurement of the DSP at baseband */
35 int16_t agc_inp_dbm8_by_pm(int16_t pm)
36 {
37         /* pm is in 1/8 dBm at baseband */
38         int16_t total_gain_dbm8;
39
40         /* compute total current gain */
41         total_gain_dbm8 = (system_inherent_gain + rffe_get_gain()) * 8;
42
43         /* subtract gain from power measurement at baseband level */
44         return pm - total_gain_dbm8;
45 }
46
47 uint8_t agc_il_by_dbm8(int16_t dbm8)
48 {
49         uint16_t il;
50
51         /* convert from 1/8 dBm to l1c format: [220..0] in -1/2dBm unit */
52         if (dbm8 >= 0)
53                 il = 0;
54         else
55                 il = -dbm8;
56
57         /* saturate */
58         if (il > 4 * 255)
59                 il = 4 * 255;
60
61         return (uint8_t)(il >> 2);
62 }