summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/layer1/tpu_window.c
blob: 0b30e80be3d1ace2616a552e4edc3eba904dbe70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* TPU window control routines for Layer 1 */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdint.h>
#include <debug.h>
#include <stdio.h>

#include <rffe.h>
#include <calypso/tpu.h>
#include <calypso/tsp.h>
#include <abb/twl3025.h>
#include <rf/trf6151.h>

#include <layer1/tpu_window.h>

/* all units in GSM quarter-bits (923.1ns) */
#define L1_TDMA_LENGTH_Q	5000
#define L1_BURST_LENGTH_Q	625	/* L1_TDMA_LENGTH_Q/8 */

#define L1_NB_MARGIN_Q		(3 * 4)
#define L1_SB_MARGIN_Q		(23 * 4)
#define L1_TAIL_DURATION_Q	(3 * 4)

/* Sample length as required by the Calypso DSP */
#define L1_NB_DURATION_Q	(L1_BURST_LENGTH_Q + 2 * L1_NB_MARGIN_Q - L1_TAIL_DURATION_Q)
#define L1_SB_DURATION_Q	(L1_BURST_LENGTH_Q + 2 * L1_SB_MARGIN_Q - L1_TAIL_DURATION_Q)
#define L1_FB_DURATION_Q	(11 * L1_TDMA_LENGTH_Q + 2057)	/* more than 11 full slots */
#define L1_FB26_DURATION_Q	(L1_TDMA_LENGTH_Q + 798)
#define	L1_PW_DURATION_Q	289

#define DSP_SETUP_TIME		66

static const uint16_t rx_burst_duration[_NUM_L1_RXWIN] = {
	[L1_RXWIN_PW]	= L1_PW_DURATION_Q,
	[L1_RXWIN_FB]	= L1_FB_DURATION_Q,
	[L1_RXWIN_SB]	= L1_SB_DURATION_Q,
	[L1_RXWIN_NB]	= L1_NB_DURATION_Q,
};

#define L1_TX_NB_DURATION_Q	626
#define L1_TX_AB_DURATION_Q	386

static const uint16_t tx_burst_duration[_NUM_L1_TXWIN] = {
	[L1_TXWIN_NB]	= L1_TX_NB_DURATION_Q,
	[L1_TXWIN_AB]	= L1_TX_AB_DURATION_Q,
};

void l1s_rx_win_ctrl(uint16_t arfcn, enum l1_rxwin_type wtype)
{
	int16_t start = DSP_SETUP_TIME;
	int16_t stop = start + rx_burst_duration[wtype] - 1;

	/* FIXME: AGC */
	/* FIXME: RF PLL */

	/* window open for TRF6151 */
	/* FIXME: why do we need the magic value 100 ? */
	rffe_mode(gsm_arfcn2band(arfcn), 0);
	trf6151_rx_window(start - 100, arfcn, 40, 0);

	/* Window open for ABB */
	twl3025_downlink(1, start);

	/* Delay 11 full TDMA frames */
	if (wtype == L1_RXWIN_FB) {
		uint8_t i;
		for (i = 0; i < 11; i++)
			tpu_enq_at(0);

		stop -= 11 * L1_TDMA_LENGTH_Q;
	}

	/* Window close for ABB */
	twl3025_downlink(0, stop);

	/* window close for TRF6151 */
	trf6151_set_mode(TRF6151_IDLE);
}

void l1s_tx_win_ctrl(uint16_t arfcn, enum l1_txwin_type wtype, uint8_t pwr)
{
	/* uplink is three TS after downlink ( "+ 32" gives a TA of 1) */
	uint16_t offset = (L1_BURST_LENGTH_Q * 3) + 28;

	/* this is needed to cause a delay of one more TDMA frame,
	 * otherwise we have an off-by-one error and send the bursts
	 * at the wrong point in time, resulting in only 3 out of 4
	 * bursts arriving at the BTS */
	tpu_enq_at(5000 - 10); /* TPU_CLOCK_RANGE - EPSILON_SYNC */

#ifdef CONFIG_TX_ENABLE
	/* window open for TRF6151 and RFFE */
	rffe_mode(gsm_arfcn2band(arfcn), 1);
	trf6151_tx_window(offset, arfcn);
#endif

	/* Window open for ABB */
	twl3025_uplink(1, offset);

	/* Window close for ABB */
	twl3025_uplink(0, tx_burst_duration[wtype] + offset + 2); // TODO: "+ 2"

	/* window close for TRF6151 and RFFE */
	trf6151_set_mode(TRF6151_IDLE);
}

void tpu_end_scenario(void)
{
	tpu_enq_sleep();
	tpu_enable(1);
}