src: use new libosmogsm and include/osmocom/[gsm|core] path to headers
[osmocom-bb.git] / src / target / firmware / layer1 / prim_tx_nb.c
1 /* Layer 1 - Transmit Normal Burst */
2
3 /* (C) 2010 by Dieter Spaar <spaar@mirider.augusta.de>
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <defines.h>
30 #include <debug.h>
31 #include <memory.h>
32 #include <byteorder.h>
33 #include <osmocom/gsm/gsm_utils.h>
34 #include <osmocom/core/msgb.h>
35 #include <calypso/dsp_api.h>
36 #include <calypso/irq.h>
37 #include <calypso/tpu.h>
38 #include <calypso/tsp.h>
39 #include <calypso/dsp.h>
40 #include <calypso/timer.h>
41 #include <comm/sercomm.h>
42
43 #include <layer1/sync.h>
44 #include <layer1/agc.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
55 static uint32_t last_txnb_fn;
56
57 /* p1: type of operation (0: one NB, 1: one RACH burst, 2: four NB) */
58 static int l1s_tx_resp(__unused uint8_t p1, __unused uint8_t burst_id,
59                        __unused uint16_t p3)
60 {
61         putchart('t');
62
63         dsp_api.r_page_used = 1;
64
65         if (burst_id == 3) {
66                 last_txnb_fn = l1s.current_time.fn - 4;
67                 l1s_compl_sched(L1_COMPL_TX_NB);
68         }
69
70         return 0;
71 }
72
73 /* p1: type of operation (0: one NB, 1: one RACH burst, 2: four NB) */
74 static int l1s_tx_cmd(uint8_t p1, uint8_t burst_id, uint16_t p3)
75 {
76         uint16_t arfcn;
77         uint8_t tsc, tn;
78         uint8_t mf_task_id = p3 & 0xff;
79         uint8_t mf_task_flags = p3 >> 8;
80
81         putchart('T');
82
83         /* before sending first of the four bursts, copy data to API ram */
84         if (burst_id == 0) {
85                 uint16_t *info_ptr = dsp_api.ndb->a_cu;
86                 struct msgb *msg;
87                 const uint8_t *data;
88
89                 /* distinguish between DCCH and ACCH */
90                 if (mf_task_flags & MF_F_SACCH) {
91                         msg = msgb_dequeue(&l1s.tx_queue[L1S_CHAN_SACCH]);
92                         data = msg ? msg->l3h : pu_get_meas_frame();
93                 } else {
94                         msg = msgb_dequeue(&l1s.tx_queue[L1S_CHAN_MAIN]);
95                         data = msg ? msg->l3h : pu_get_idle_frame();
96                 }
97
98                 /* Fill data block Header */
99                 info_ptr[0] = (1 << B_BLUD);     // 1st word: Set B_BLU bit.
100                 info_ptr[1] = 0;                 // 2nd word: cleared.
101                 info_ptr[2] = 0;                 // 3rd word: cleared.
102
103                 /* Copy the actual data after the header */
104                 dsp_memcpy_to_api(&info_ptr[3], data, 23, 0);
105
106                 if (msg)
107                         msgb_free(msg);
108         }
109
110         rfch_get_params(&l1s.next_time, &arfcn, &tsc, &tn);
111
112         l1s_tx_apc_helper(arfcn);
113
114         if (p1 == 0)
115                 /* DUL_DSP_TASK, one normal burst */
116                 dsp_load_tch_param(&l1s.next_time,
117                                    SIG_ONLY_MODE, INVALID_CHANNEL, 0, 0, 0, tn);
118
119         else if (p1 == 2)
120                 /* DUL_DSP_TASK, four normal bursts */
121                 dsp_load_tch_param(&l1s.next_time,
122                                    SIG_ONLY_MODE, SDCCH_4, 0, 0, 0, tn);
123
124         dsp_load_tx_task(DUL_DSP_TASK, burst_id, tsc);
125
126         l1s_tx_win_ctrl(arfcn, L1_TXWIN_NB, 0, 3);
127
128         return 0;
129 }
130
131 /* Asynchronous completion handler for NB transmit */
132 static void l1a_tx_nb_compl(__unused enum l1_compl c)
133 {
134         struct msgb *msg;
135
136         msg = l1_create_l2_msg(L1CTL_DATA_CONF, last_txnb_fn, 0, 0);
137         l1_queue_for_l2(msg);
138 }
139
140 void l1s_tx_test(uint8_t base_fn, uint8_t type)
141 {
142         printf("Starting TX %d\n", type);
143
144         if (type == 0) {// one normal burst
145                 tdma_schedule(base_fn, &l1s_tx_cmd, 0, 0, 0, 3);
146                 tdma_schedule(base_fn + 2, &l1s_tx_resp, 0, 0, 0, 3);
147         } else if (type == 2) { // four normal bursts
148                 tdma_schedule(base_fn, &l1s_tx_cmd, 2, 0, 0, 3);
149                 tdma_schedule(base_fn + 1, &l1s_tx_cmd, 2, 1, 0, 3);
150                 tdma_schedule(base_fn + 2, &l1s_tx_resp, 2, 0, 0, 3);
151                 tdma_schedule(base_fn + 2, &l1s_tx_cmd, 2, 2, 0, 3);
152                 tdma_schedule(base_fn + 3, &l1s_tx_resp, 2, 1, 0, 3);
153                 tdma_schedule(base_fn + 3, &l1s_tx_cmd, 2, 3, 0, 3);
154                 tdma_schedule(base_fn + 4, &l1s_tx_resp, 2, 2, 0, 3);
155                 tdma_schedule(base_fn + 5, &l1s_tx_resp, 2, 3, 0, 3);
156         }
157 }
158
159 /* sched sets for uplink */
160 const struct tdma_sched_item nb_sched_set_ul[] = {
161         SCHED_ITEM_DT(l1s_tx_cmd, 3, 2, 0),                                             SCHED_END_FRAME(),
162         SCHED_ITEM_DT(l1s_tx_cmd, 3, 2, 1),                                             SCHED_END_FRAME(),
163         SCHED_ITEM(l1s_tx_resp, -4, 2, 0),      SCHED_ITEM_DT(l1s_tx_cmd, 3, 2, 2),     SCHED_END_FRAME(),
164         SCHED_ITEM(l1s_tx_resp, -4, 2, 1),      SCHED_ITEM_DT(l1s_tx_cmd, 3, 2, 3),     SCHED_END_FRAME(),
165                                                 SCHED_ITEM(l1s_tx_resp, -4, 2, 2),      SCHED_END_FRAME(),
166                                                 SCHED_ITEM(l1s_tx_resp, -4, 2, 3),      SCHED_END_FRAME(),
167         SCHED_END_SET()
168 };
169
170 static __attribute__ ((constructor)) void prim_tx_nb_init(void)
171 {
172         l1s.completion[L1_COMPL_TX_NB] = &l1a_tx_nb_compl;
173 }