Initial import of OsmocomBB into git repository
[osmocom-bb.git] / src / target / firmware / layer1 / tpu_window.c
1 /* TPU window control routines for Layer 1 */
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 <debug.h>
25 #include <stdio.h>
26
27 #include <rffe.h>
28 #include <calypso/tpu.h>
29 #include <calypso/tsp.h>
30 #include <abb/twl3025.h>
31 #include <rf/trf6151.h>
32
33 #include <layer1/tpu_window.h>
34
35 /* all units in GSM quarter-bits (923.1ns) */
36 #define L1_TDMA_LENGTH_Q        5000
37 #define L1_BURST_LENGTH_Q       625     /* L1_TDMA_LENGTH_Q/8 */
38
39 #define L1_NB_MARGIN_Q          (3 * 4)
40 #define L1_SB_MARGIN_Q          (23 * 4)
41 #define L1_TAIL_DURATION_Q      (3 * 4)
42
43 /* Sample length as required by the Calypso DSP */
44 #define L1_NB_DURATION_Q        (L1_BURST_LENGTH_Q + 2 * L1_NB_MARGIN_Q - L1_TAIL_DURATION_Q)
45 #define L1_SB_DURATION_Q        (L1_BURST_LENGTH_Q + 2 * L1_SB_MARGIN_Q - L1_TAIL_DURATION_Q)
46 #define L1_FB_DURATION_Q        (11 * L1_TDMA_LENGTH_Q + 2057)  /* more than 11 full slots */
47 #define L1_FB26_DURATION_Q      (L1_TDMA_LENGTH_Q + 798)
48 #define L1_PW_DURATION_Q        289
49
50 #define DSP_SETUP_TIME          66
51
52 static const uint16_t rx_burst_duration[_NUM_L1_RXWIN] = {
53         [L1_RXWIN_PW]   = L1_PW_DURATION_Q,
54         [L1_RXWIN_FB]   = L1_FB_DURATION_Q,
55         [L1_RXWIN_SB]   = L1_SB_DURATION_Q,
56         [L1_RXWIN_NB]   = L1_NB_DURATION_Q,
57 };
58
59 void l1s_rx_win_ctrl(uint16_t arfcn, enum l1_rxwin_type wtype)
60 {
61         int16_t start = DSP_SETUP_TIME;
62         int16_t stop = start + rx_burst_duration[wtype] - 1;
63
64         /* FIXME: AGC */
65         /* FIXME: RF PLL */
66
67         /* window open for TRF6151 */
68         /* FIXME: why do we need the magic value 100 ? */
69         rffe_mode(gsm_arfcn2band(arfcn), 0);
70         trf6151_rx_window(start - 100, arfcn, 40, 0);
71
72         /* Window open for ABB */
73         twl3025_downlink(1, start);
74
75         /* Delay 11 full TDMA frames */
76         if (wtype == L1_RXWIN_FB) {
77                 uint8_t i;
78                 for (i = 0; i < 11; i++)
79                         tpu_enq_at(0);
80
81                 stop -= 11 * L1_TDMA_LENGTH_Q;
82         }
83
84         /* Window close for ABB */
85         twl3025_downlink(0, stop);
86
87         /* FIXME: window close for TRF6151 */
88 }
89
90 void tpu_end_scenario(void)
91 {
92         tpu_enq_sleep();
93         tpu_enable(1);
94 }