blob: d8c044445a6ebac06fc88448dbfc413ca1959948 [file] [log] [blame]
Piotr Krysik902f4eb2017-09-19 08:04:33 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4# GR-GSM based transceiver
5# Follow graph implementation
6#
7# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
8#
9# All Rights Reserved
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25import pmt
26import time
27import grgsm
28import osmosdr
29
30from math import pi
31
32from gnuradio import blocks
33from gnuradio import gr
34
Vadim Yanitskiy873e44e2017-10-17 06:52:01 +070035class radio_if(gr.top_block):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020036 # PHY specific variables
Piotr Krysik902f4eb2017-09-19 08:04:33 +020037 shiftoff = 400e3
Piotr Krysik902f4eb2017-09-19 08:04:33 +020038 fc = 941.6e6 # TODO: set ARFCN to 0?
Piotr Krysik902f4eb2017-09-19 08:04:33 +020039
40 # Application state flags
41 trx_started = False
42 fc_set = False
43
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070044 def __init__(self, phy_args, phy_sample_rate,
45 phy_rx_gain, phy_tx_gain, phy_ppm,
46 phy_rx_antenna, phy_tx_antenna,
Vadim Yanitskiy5d68aa52017-10-17 11:14:48 +070047 trx_remote_addr, trx_base_port):
Piotr Krysik902f4eb2017-09-19 08:04:33 +020048
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070049 print("[i] Init Radio interface")
Piotr Krysik902f4eb2017-09-19 08:04:33 +020050
51 # PHY specific variables
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070052 self.rx_gain = phy_rx_gain
53 self.tx_gain = phy_tx_gain
Piotr Krysik902f4eb2017-09-19 08:04:33 +020054
55 gr.top_block.__init__(self, "GR-GSM TRX")
56 shift_fc = self.fc - self.shiftoff
57
58 ##################################################
59 # PHY Definition
60 ##################################################
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070061 self.phy = osmosdr.source(args = "numchan=%d %s" % (1, phy_args))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020062
63 self.phy.set_bandwidth(250e3 + abs(self.shiftoff), 0)
64 self.phy.set_center_freq(shift_fc, 0)
Vadim Yanitskiyda5e21f2017-10-18 18:24:48 +070065 self.phy.set_sample_rate(phy_sample_rate)
66 self.phy.set_freq_corr(phy_ppm, 0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020067 self.phy.set_iq_balance_mode(2, 0)
68 self.phy.set_dc_offset_mode(2, 0)
69 self.phy.set_gain_mode(False, 0)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070070 self.phy.set_gain(self.rx_gain, 0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020071 self.phy.set_if_gain(20, 0)
72 self.phy.set_bb_gain(20, 0)
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070073 self.phy.set_antenna(phy_rx_antenna, 0)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020074
75 ##################################################
76 # GR-GSM Magic
77 ##################################################
78 self.blocks_rotator = blocks.rotator_cc(
Vadim Yanitskiyda5e21f2017-10-18 18:24:48 +070079 -2 * pi * self.shiftoff / phy_sample_rate)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020080
81 self.gsm_input = grgsm.gsm_input(
Vadim Yanitskiyda5e21f2017-10-18 18:24:48 +070082 ppm = phy_ppm, osr = 4, fc = self.fc,
83 samp_rate_in = phy_sample_rate)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020084
85 self.gsm_receiver = grgsm.receiver(4, ([0]), ([]))
86
87 self.gsm_clck_ctrl = grgsm.clock_offset_control(
Vadim Yanitskiyda5e21f2017-10-18 18:24:48 +070088 shift_fc, phy_sample_rate, osr = 4)
Piotr Krysik902f4eb2017-09-19 08:04:33 +020089
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +070090 self.gsm_ts_filter = grgsm.burst_timeslot_filter(0)
91 self.gsm_ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL)
92
Vadim Yanitskiy1a88dec2017-10-17 09:00:47 +070093 self.gsm_trx_burst_if = grgsm.trx_burst_if(
Vadim Yanitskiy790b6f02017-10-17 11:47:36 +070094 trx_remote_addr, str(trx_base_port))
Piotr Krysik902f4eb2017-09-19 08:04:33 +020095
96 ##################################################
97 # Connections
98 ##################################################
99 self.connect((self.phy, 0), (self.blocks_rotator, 0))
100 self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
101 self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
102
103 self.msg_connect((self.gsm_receiver, 'measurements'),
104 (self.gsm_clck_ctrl, 'measurements'))
105
106 self.msg_connect((self.gsm_clck_ctrl, 'ctrl'),
107 (self.gsm_input, 'ctrl_in'))
108
109 self.msg_connect((self.gsm_receiver, 'C0'),
Vadim Yanitskiy962e2d82017-10-17 09:24:55 +0700110 (self.gsm_ts_filter, 'in'))
111
112 self.msg_connect((self.gsm_ts_filter, 'out'),
Vadim Yanitskiy1a88dec2017-10-17 09:00:47 +0700113 (self.gsm_trx_burst_if, 'bursts'))
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200114
115 def check_available(self):
116 return self.fc_set
117
118 def shutdown(self):
119 print("[i] Shutdown Radio interface")
120 self.stop()
121 self.wait()
122
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200123 def set_fc(self, fc):
124 self.phy.set_center_freq(fc - self.shiftoff, 0)
125 self.gsm_input.set_fc(fc)
126 self.fc_set = True
127 self.fc = fc
128
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700129 def set_rx_gain(self, gain):
Piotr Krysik902f4eb2017-09-19 08:04:33 +0200130 self.phy.set_gain(gain, 0)
Vadim Yanitskiy01c6afd2017-10-19 01:14:24 +0700131 self.rx_gain = gain
132
133 def set_tx_gain(self, gain):
134 # TODO: TX chain not implemented yet
135 self.tx_gain = gain