include: Rename l1a_l23_interface.h to l1ctl_proto.h
[osmocom-bb.git] / src / target / firmware / layer1 / prim_pm.c
1 /* Layer 1 Power Measurement */
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 #include <string.h>
26 #include <stdlib.h>
27
28 #include <defines.h>
29 #include <debug.h>
30 #include <memory.h>
31 #include <byteorder.h>
32 #include <osmocore/gsm_utils.h>
33 #include <osmocore/msgb.h>
34 #include <calypso/dsp_api.h>
35 #include <calypso/irq.h>
36 #include <calypso/tpu.h>
37 #include <calypso/tsp.h>
38 #include <calypso/dsp.h>
39 #include <calypso/timer.h>
40 #include <comm/sercomm.h>
41
42 #include <layer1/sync.h>
43 #include <layer1/agc.h>
44 #include <layer1/tdma_sched.h>
45 #include <layer1/tpu_window.h>
46 #include <layer1/l23_api.h>
47
48 #include <l1ctl_proto.h>
49
50 static void l1ddsp_meas_read(uint8_t nbmeas, uint16_t *pm)
51 {
52         uint8_t i;
53
54         for (i = 0; i < nbmeas; i++)
55                 pm[i] = (uint16_t) ((dsp_api.db_r->a_pm[i] & 0xffff) >> 3);
56         dsp_api.r_page_used = 1;
57 }
58
59 /* scheduler callback to issue a power measurement task to the DSP */
60 static int l1s_pm_cmd(uint8_t num_meas,
61                       __unused uint8_t p2, uint16_t arfcn)
62 {
63         putchart('P');
64
65         dsp_api.db_w->d_task_md = num_meas; /* number of measurements */
66         dsp_api.ndb->d_fb_mode = 0; /* wideband search */
67         dsp_end_scenario();
68
69         /* Tell the RF frontend to set the gain appropriately */
70         rffe_set_gain(-85, CAL_DSP_TGT_BB_LVL);
71
72         /* Program TPU */
73         /* FIXME: RXWIN_PW needs to set up multiple times in case
74          * num_meas > 1 */
75         l1s_rx_win_ctrl(arfcn, L1_RXWIN_PW, 0);
76         //l1s_rx_win_ctrl(arfcn, L1_RXWIN_NB);
77         tpu_end_scenario();
78
79         return 0;
80 }
81
82 /* scheduler callback to read power measurement resposnse from the DSP */
83 static int l1s_pm_resp(uint8_t num_meas, __unused uint8_t p2,
84                        uint16_t arfcn)
85 {
86         struct l1ctl_pm_conf *pmr;
87         uint16_t pm_level[2];
88
89         putchart('p');
90
91         l1ddsp_meas_read(num_meas, pm_level);
92
93         printf("PM MEAS: ARFCN=%u, %-4d dBm at baseband, %-4d dBm at RF\n",
94                 arfcn, pm_level[0]/8, agc_inp_dbm8_by_pm(pm_level[0])/8);
95
96         printd("PM MEAS: %-4d dBm, %-4d dBm ARFCN=%u\n",
97                 agc_inp_dbm8_by_pm(pm_level[0])/8,
98                 agc_inp_dbm8_by_pm(pm_level[1])/8, arfcn);
99
100         if (!l1s.pm.msg)
101                 l1s.pm.msg = l1ctl_msgb_alloc(L1CTL_PM_CONF);
102
103         if (msgb_tailroom(l1s.pm.msg) < sizeof(*pmr)) {
104                 /* flush current msgb */
105                 l1_queue_for_l2(l1s.pm.msg);
106                 /* allocate a new msgb and initialize header */
107                 l1s.pm.msg = l1ctl_msgb_alloc(L1CTL_PM_CONF);
108         }
109
110         pmr = msgb_put(l1s.pm.msg, sizeof(*pmr));
111         pmr->band_arfcn = htons(arfcn);
112         /* FIXME: do this as RxLev rather than DBM8 ? */
113         pmr->pm[0] = dbm2rxlev(agc_inp_dbm8_by_pm(pm_level[0])/8);
114         if (num_meas > 1)
115                 pmr->pm[1] = dbm2rxlev(agc_inp_dbm8_by_pm(pm_level[1])/8);
116         else
117                 pmr->pm[1] = 0;
118
119         if (l1s.pm.mode == 1) {
120                 if (l1s.pm.range.arfcn_next <= l1s.pm.range.arfcn_end) {
121                         /* schedule PM for next ARFCN in range */
122                         l1s_pm_test(1, l1s.pm.range.arfcn_next);
123                         l1s.pm.range.arfcn_next++;
124                 } else {
125                         /* we have finished, flush the msgb to L2 */
126                         struct l1ctl_hdr *l1h = l1s.pm.msg->l1h;
127                         l1h->flags |= L1CTL_F_DONE;
128                         l1_queue_for_l2(l1s.pm.msg);
129                         l1s.pm.msg = NULL;
130                 }
131         }
132
133         return 0;
134 }
135
136 static const struct tdma_sched_item pm_sched_set[] = {
137         SCHED_ITEM(l1s_pm_cmd, 1, 0),           SCHED_END_FRAME(),
138                                                 SCHED_END_FRAME(),
139         SCHED_ITEM(l1s_pm_resp, 1, 0),          SCHED_END_FRAME(),
140         SCHED_END_SET()
141 };
142
143 /* Schedule a power measurement test */
144 void l1s_pm_test(uint8_t base_fn, uint16_t arfcn)
145 {
146         printd("l1s_pm_test(%u, %u)\n", base_fn, arfcn);
147         tdma_schedule_set(base_fn, pm_sched_set, arfcn);
148 }