src: use new libosmogsm and include/osmocom/[gsm|core] path to headers
[osmocom-bb.git] / src / target / firmware / layer1 / prim_rx_nb.c
1 /* Layer 1 - Receiving Normal Bursts */
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 <osmocom/gsm/gsm_utils.h>
33 #include <osmocom/core/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/afc.h>
44 #include <layer1/toa.h>
45 #include <layer1/tdma_sched.h>
46 #include <layer1/mframe_sched.h>
47 #include <layer1/tpu_window.h>
48 #include <layer1/l23_api.h>
49 #include <layer1/rfch.h>
50 #include <layer1/prim.h>
51
52 #include <l1ctl_proto.h>
53
54 struct l1s_rxnb_state {
55         struct l1s_meas_hdr meas[4];
56
57         struct msgb *msg;
58         struct l1ctl_info_dl *dl;
59         struct l1ctl_data_ind *di;
60 };
61
62 static struct l1s_rxnb_state rxnb;
63
64 static int l1s_nb_resp(__unused uint8_t p1, uint8_t burst_id, uint16_t p3)
65 {
66         struct gsm_time rx_time;
67         uint8_t mf_task_id = p3 & 0xff;
68         uint8_t mf_task_flags = p3 >> 8;
69         uint16_t rf_arfcn;
70         uint8_t tsc, tn;
71
72         putchart('n');
73
74         /* just for debugging, d_task_d should not be 0 */
75         if (dsp_api.db_r->d_task_d == 0) {
76                 puts("EMPTY\n");
77                 return 0;
78         }
79
80         /* DSP burst ID needs to correspond with what we expect */
81         if (dsp_api.db_r->d_burst_d != burst_id) {
82                 printf("BURST ID %u!=%u\n", dsp_api.db_r->d_burst_d, burst_id);
83                 return 0;
84         }
85
86         /* get radio parameters for _this_ burst */
87         gsm_fn2gsmtime(&rx_time, l1s.current_time.fn - 1);
88         rfch_get_params(&rx_time, &rf_arfcn, &tsc, &tn);
89
90         /* collect measurements */
91         rxnb.meas[burst_id].toa_qbit = dsp_api.db_r->a_serv_demod[D_TOA];
92         rxnb.meas[burst_id].pm_dbm8 =
93                 agc_inp_dbm8_by_pm(dsp_api.db_r->a_serv_demod[D_PM] >> 3);
94         rxnb.meas[burst_id].freq_err =
95                         ANGLE_TO_FREQ(dsp_api.db_r->a_serv_demod[D_ANGLE]);
96         rxnb.meas[burst_id].snr = dsp_api.db_r->a_serv_demod[D_SNR];
97
98         /* feed computed frequency error into AFC loop */
99         if (rxnb.meas[burst_id].snr > AFC_SNR_THRESHOLD)
100                 afc_input(rxnb.meas[burst_id].freq_err, rf_arfcn, 1);
101         else
102                 afc_input(rxnb.meas[burst_id].freq_err, rf_arfcn, 0);
103
104         /* feed computed TOA into TA loop */
105         toa_input(rxnb.meas[burst_id].toa_qbit << 2, rxnb.meas[burst_id].snr);
106
107         /* Tell the RF frontend to set the gain appropriately */
108         rffe_set_gain(rxnb.meas[burst_id].pm_dbm8/8, CAL_DSP_TGT_BB_LVL);
109
110         /* 4th burst, get frame data */
111         if (dsp_api.db_r->d_burst_d == 3) {
112                 uint8_t i;
113                 uint16_t num_biterr;
114                 uint32_t avg_snr = 0;
115                 int32_t avg_dbm8 = 0;
116
117                 /* Get radio parameters for the first burst */
118                 gsm_fn2gsmtime(&rx_time, l1s.current_time.fn - 4);
119                 rfch_get_params(&rx_time, &rf_arfcn, &tsc, &tn);
120
121                 /* Set Channel Number depending on MFrame Task ID */
122                 rxnb.dl->chan_nr = mframe_task2chan_nr(mf_task_id, tn);
123
124                 /* Set SACCH indication in Link IDentifier */
125                 if (mf_task_flags & MF_F_SACCH)
126                         rxnb.dl->link_id = 0x40;
127                 else
128                         rxnb.dl->link_id = 0x00;
129
130                 rxnb.dl->band_arfcn = htons(rf_arfcn);
131
132                 rxnb.dl->frame_nr = htonl(rx_time.fn);
133
134                 /* compute average snr and rx level */
135                 for (i = 0; i < 4; ++i) {
136                         avg_snr += rxnb.meas[i].snr;
137                         avg_dbm8 += rxnb.meas[i].pm_dbm8;
138                 }
139                 rxnb.dl->snr = avg_snr / 4;
140                 rxnb.dl->rx_level = dbm2rxlev(avg_dbm8 / (8*4));
141
142                 num_biterr = dsp_api.ndb->a_cd[2] & 0xffff;
143                 if (num_biterr > 0xff)
144                         rxnb.dl->num_biterr = 0xff;
145                 else
146                         rxnb.dl->num_biterr = num_biterr;
147
148                 rxnb.dl->fire_crc = ((dsp_api.ndb->a_cd[0] & 0xffff) & ((1 << B_FIRE1) | (1 << B_FIRE0))) >> B_FIRE0;
149
150                 /* update rx level for pm report */
151                 pu_update_rx_level(rxnb.dl->rx_level);
152
153                 /* copy actual data, skipping the information block [0,1,2] */
154                 dsp_memcpy_from_api(rxnb.di->data, &dsp_api.ndb->a_cd[3], 23, 0);
155
156                 l1_queue_for_l2(rxnb.msg);
157                 rxnb.msg = NULL; rxnb.dl = NULL; rxnb.di = NULL;
158
159                 /* clear downlink task */
160                 dsp_api.db_w->d_task_d = 0;
161         }
162
163         /* mark READ page as being used */
164         dsp_api.r_page_used = 1;
165
166         return 0;
167 }
168
169 static int l1s_nb_cmd(__unused uint8_t p1, uint8_t burst_id,
170                       __unused uint16_t p3)
171 {
172         uint16_t arfcn;
173         uint8_t tsc, tn;
174
175         putchart('N');
176
177         if (burst_id == 1) {
178                 /* allocate message only at 2nd burst in case of
179                  * consecutive/overlapping normal burst RX tasks */
180                 /* FIXME: we actually want all allocation out of L1S! */
181                 if (rxnb.msg) {
182                         /* Can happen when resetting ... */
183                         printf("nb_cmd(0) and rxnb.msg != NULL\n");
184                         msgb_free(rxnb.msg);
185                 }
186                 /* allocate msgb as needed. FIXME: from L1A ?? */
187                 rxnb.msg = l1ctl_msgb_alloc(L1CTL_DATA_IND);
188                 if (!rxnb.msg)
189                         printf("nb_cmd(0): unable to allocate msgb\n");
190                 rxnb.dl = (struct l1ctl_info_dl *) msgb_put(rxnb.msg, sizeof(*rxnb.dl));
191                 rxnb.di = (struct l1ctl_data_ind *) msgb_put(rxnb.msg, sizeof(*rxnb.di));
192         }
193
194         rfch_get_params(&l1s.next_time, &arfcn, &tsc, &tn);
195
196         /* DDL_DSP_TASK, four normal bursts */
197         dsp_load_tch_param(&l1s.next_time,
198                            SIG_ONLY_MODE, SDCCH_4, 0, 0, 0, tn);
199
200         dsp_load_rx_task(ALLC_DSP_TASK, burst_id, tsc);
201
202         l1s_rx_win_ctrl(arfcn, L1_RXWIN_NB, 0);
203
204         return 0;
205 }
206
207 const struct tdma_sched_item nb_sched_set[] = {
208         SCHED_ITEM_DT(l1s_nb_cmd, 0, 0, 0),                                             SCHED_END_FRAME(),
209         SCHED_ITEM_DT(l1s_nb_cmd, 0, 0, 1),                                             SCHED_END_FRAME(),
210         SCHED_ITEM(l1s_nb_resp, -4, 0, 0),      SCHED_ITEM_DT(l1s_nb_cmd, 0, 0, 2),     SCHED_END_FRAME(),
211         SCHED_ITEM(l1s_nb_resp, -4, 0, 1),      SCHED_ITEM_DT(l1s_nb_cmd, 0, 0, 3),     SCHED_END_FRAME(),
212                                                 SCHED_ITEM(l1s_nb_resp, -4, 0, 2),      SCHED_END_FRAME(),
213                                                 SCHED_ITEM(l1s_nb_resp, -4, 0, 3),      SCHED_END_FRAME(),
214         SCHED_END_SET()
215 };