Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # GR-GSM based transceiver |
| 5 | # Follow graph implementation |
| 6 | # |
Vadim Yanitskiy | 3f9abbd | 2018-08-10 05:29:23 +0700 | [diff] [blame] | 7 | # (C) 2016-2018 by Vadim Yanitskiy <axilirator@gmail.com> |
Piotr Krysik | d2f162f | 2017-11-30 12:50:00 +0100 | [diff] [blame] | 8 | # (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com> |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 9 | # |
| 10 | # All Rights Reserved |
| 11 | # |
| 12 | # This program is free software; you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License as published by |
| 14 | # the Free Software Foundation; either version 2 of the License, or |
| 15 | # (at your option) any later version. |
| 16 | # |
| 17 | # This program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License along |
| 23 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | |
| 26 | import pmt |
| 27 | import time |
| 28 | import grgsm |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 29 | |
| 30 | from math import pi |
| 31 | |
Vadim Yanitskiy | 3f9abbd | 2018-08-10 05:29:23 +0700 | [diff] [blame] | 32 | from gnuradio import eng_notation |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 33 | from gnuradio import digital |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 34 | from gnuradio import blocks |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 35 | from gnuradio import uhd |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 36 | from gnuradio import gr |
| 37 | |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 38 | from gnuradio import filter |
| 39 | from gnuradio.filter import firdes |
| 40 | |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 41 | |
| 42 | # HACK: should be implemented in C++! |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 43 | class dict_toggle_sign(gr.basic_block): |
| 44 | def __init__(self): # only default arguments here |
| 45 | gr.basic_block.__init__(self, |
| 46 | name='Change sign of elts in dict', |
| 47 | in_sig=[], |
| 48 | out_sig=[] |
| 49 | ) |
| 50 | self.message_port_register_in(pmt.intern("dict_in")) |
| 51 | self.message_port_register_out(pmt.intern("dict_out")) |
| 52 | self.set_msg_handler(pmt.intern("dict_in"), self.change_sign) |
| 53 | |
| 54 | def change_sign(self, msg): |
| 55 | if pmt.is_dict(msg): |
| 56 | d = pmt.to_python(msg) |
| 57 | for key, value in d.items(): |
| 58 | d[key] *= -1 |
| 59 | self.message_port_pub(pmt.intern("dict_out"), pmt.to_pmt(d)) |
| 60 | |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 61 | class radio_if(gr.top_block): |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 62 | # PHY specific variables |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 63 | rx_freq = 935e6 |
| 64 | tx_freq = 890e6 |
| 65 | osr = 4 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 66 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 67 | # Application state flags |
| 68 | trx_started = False |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 69 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 70 | # GSM timings (in microseconds [uS]) |
| 71 | # One timeslot duration is 576.9 μs = 15/26 ms, |
| 72 | # or 156.25 symbol periods (a symbol period is 48/13 μs) |
| 73 | GSM_SYM_PERIOD_uS = 48.0 / 13.0 |
| 74 | GSM_TS_PERIOD_uS = GSM_SYM_PERIOD_uS * 156.25 |
| 75 | GSM_UL_DL_SHIFT_uS = -(GSM_TS_PERIOD_uS * 3) |
Vadim Yanitskiy | 9dded9b | 2017-12-05 01:00:51 +0700 | [diff] [blame] | 76 | |
Piotr Krysik | 58659a4 | 2018-09-12 00:35:05 +0700 | [diff] [blame] | 77 | GSM_SYM_RATE = (1.0 / GSM_SYM_PERIOD_uS) * 1e6 |
| 78 | SAMPLE_RATE = GSM_SYM_RATE * osr |
Vadim Yanitskiy | 5621252 | 2018-09-07 19:09:12 +0700 | [diff] [blame] | 79 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 80 | # FIXME: shall be measured (automatically?) for |
| 81 | # particular device and particular clock rate. |
| 82 | # The current value is measured for USRP B2X0 at 26e6. |
| 83 | delay_correction = (285.616 + 2 * GSM_SYM_PERIOD_uS) * 1e-6 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 84 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 85 | def __init__(self, phy_args, phy_sample_rate, |
| 86 | phy_rx_gain, phy_tx_gain, phy_ppm, |
| 87 | phy_rx_antenna, phy_tx_antenna, |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 88 | phy_freq_offset, trx_bind_addr, |
Piotr Krysik | 11583ad | 2018-09-13 14:41:06 +0200 | [diff] [blame] | 89 | trx_remote_addr, trx_base_port): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 90 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 91 | print("[i] Init Radio interface (L:%s:%u <-> R:%s:%u)" |
| 92 | % (trx_bind_addr, trx_base_port + 2, |
| 93 | trx_remote_addr, trx_base_port + 102)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 94 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 95 | # PHY specific variables |
| 96 | self.sample_rate = phy_sample_rate |
| 97 | self.rx_gain = phy_rx_gain |
| 98 | self.tx_gain = phy_tx_gain |
| 99 | self.ppm = phy_ppm |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 100 | self.freq_offset = phy_freq_offset |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 101 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 102 | gr.top_block.__init__(self, "GR-GSM TRX") |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 103 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 104 | # TRX Burst Interface |
| 105 | self.trx_burst_if = grgsm.trx_burst_if( |
| 106 | trx_bind_addr, trx_remote_addr, |
| 107 | str(trx_base_port)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 108 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 109 | # RX path definition |
| 110 | self.phy_src = uhd.usrp_source(phy_args, |
| 111 | uhd.stream_args(cpu_format="fc32", |
| 112 | channels=range(1))) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 113 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 114 | self.phy_src.set_clock_rate(26e6, uhd.ALL_MBOARDS) |
| 115 | self.phy_src.set_center_freq(self.rx_freq, 0) |
| 116 | self.phy_src.set_antenna(phy_rx_antenna, 0) |
| 117 | self.phy_src.set_samp_rate(phy_sample_rate) |
| 118 | self.phy_src.set_bandwidth(650e3, 0) |
| 119 | self.phy_src.set_gain(phy_rx_gain) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 120 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 121 | self.msg_to_tag_src = grgsm.msg_to_tag() |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 122 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 123 | self.rotator_src = grgsm.controlled_rotator_cc( |
| 124 | self.calc_phase_inc(self.rx_freq)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 125 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 126 | self.lpf = filter.fir_filter_ccf(1, firdes.low_pass( |
| 127 | 1, phy_sample_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 128 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 129 | self.gsm_receiver = grgsm.receiver(self.osr, ([0]), ([])) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 130 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 131 | self.ts_filter = grgsm.burst_timeslot_filter(0) |
| 132 | self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL) |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 133 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 134 | # Connections |
| 135 | self.connect( |
| 136 | (self.phy_src, 0), |
| 137 | (self.msg_to_tag_src, 0)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 138 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 139 | self.connect( |
| 140 | (self.msg_to_tag_src, 0), |
| 141 | (self.rotator_src, 0)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 142 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 143 | self.connect( |
| 144 | (self.rotator_src, 0), |
| 145 | (self.lpf, 0)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 146 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 147 | self.connect( |
| 148 | (self.lpf, 0), |
| 149 | (self.gsm_receiver, 0)) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 150 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 151 | self.msg_connect( |
| 152 | (self.gsm_receiver, 'C0'), |
| 153 | (self.ts_filter, 'in')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 154 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 155 | self.msg_connect( |
| 156 | (self.ts_filter, 'out'), |
| 157 | (self.trx_burst_if, 'bursts')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 158 | |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 159 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 160 | # TX Path Definition |
| 161 | self.phy_sink = uhd.usrp_sink(phy_args, |
| 162 | uhd.stream_args(cpu_format="fc32", |
| 163 | channels=range(1)), "packet_len") |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 164 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 165 | self.phy_sink.set_clock_rate(26e6, uhd.ALL_MBOARDS) |
| 166 | self.phy_sink.set_antenna(phy_tx_antenna, 0) |
| 167 | self.phy_sink.set_samp_rate(phy_sample_rate) |
| 168 | self.phy_sink.set_center_freq(self.tx_freq, 0) |
| 169 | self.phy_sink.set_gain(self.tx_gain) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 170 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 171 | self.tx_time_setter = grgsm.txtime_setter( |
| 172 | 0xffffffff, 0, 0, 0, 0, 0, |
| 173 | self.delay_correction + self.GSM_UL_DL_SHIFT_uS * 1e-6) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 174 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 175 | self.tx_burst_proc = grgsm.preprocess_tx_burst() |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 176 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 177 | self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream( |
| 178 | blocks.byte_t, 'packet_len') |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 179 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 180 | self.gmsk_mod = grgsm.gsm_gmsk_mod( |
| 181 | BT = 0.3, pulse_duration = 4, sps = self.osr) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 182 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 183 | self.burst_shaper = digital.burst_shaper_cc( |
| 184 | (firdes.window(firdes.WIN_HANN, 16, 0)), |
| 185 | 0, 20, False, "packet_len") |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 186 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 187 | self.msg_to_tag_sink = grgsm.msg_to_tag() |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 188 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 189 | self.rotator_sink = grgsm.controlled_rotator_cc( |
| 190 | -self.calc_phase_inc(self.tx_freq)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 191 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 192 | # Connections |
| 193 | self.msg_connect( |
| 194 | (self.trx_burst_if, 'bursts'), |
| 195 | (self.tx_time_setter, 'bursts_in')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 196 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 197 | self.msg_connect( |
| 198 | (self.tx_time_setter, 'bursts_out'), |
| 199 | (self.tx_burst_proc, 'bursts_in')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 200 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 201 | self.msg_connect( |
| 202 | (self.tx_burst_proc, 'bursts_out'), |
| 203 | (self.pdu_to_tagged_stream, 'pdus')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 204 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 205 | self.connect( |
| 206 | (self.pdu_to_tagged_stream, 0), |
| 207 | (self.gmsk_mod, 0)) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 208 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 209 | self.connect( |
| 210 | (self.gmsk_mod, 0), |
| 211 | (self.burst_shaper, 0)) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 212 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 213 | self.connect( |
| 214 | (self.burst_shaper, 0), |
| 215 | (self.msg_to_tag_sink, 0)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 216 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 217 | self.connect( |
| 218 | (self.msg_to_tag_sink, 0), |
| 219 | (self.rotator_sink, 0)) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 220 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 221 | self.connect( |
| 222 | (self.rotator_sink, 0), |
| 223 | (self.phy_sink, 0)) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 224 | |
| 225 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 226 | # RX & TX synchronization |
| 227 | self.bt_filter = grgsm.burst_type_filter([3]) |
| 228 | self.burst_to_fn_time = grgsm.burst_to_fn_time() |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 229 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 230 | # Connections |
| 231 | self.msg_connect( |
| 232 | (self.gsm_receiver, 'C0'), |
| 233 | (self.bt_filter, 'bursts_in')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 234 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 235 | self.msg_connect( |
| 236 | (self.bt_filter, 'bursts_out'), |
| 237 | (self.burst_to_fn_time, 'bursts_in')) |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 238 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 239 | self.msg_connect( |
| 240 | (self.burst_to_fn_time, 'fn_time_out'), |
| 241 | (self.tx_time_setter, 'fn_time')) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 242 | |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 243 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 244 | # AFC (Automatic Frequency Correction) |
| 245 | self.gsm_clck_ctrl = grgsm.clock_offset_control( |
| 246 | self.rx_freq, phy_sample_rate, osr = self.osr) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 247 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 248 | self.dict_toggle_sign = dict_toggle_sign() |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 249 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 250 | # Connections |
| 251 | self.msg_connect( |
| 252 | (self.gsm_receiver, 'measurements'), |
| 253 | (self.gsm_clck_ctrl, 'measurements')) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 254 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 255 | self.msg_connect( |
| 256 | (self.gsm_clck_ctrl, 'ctrl'), |
| 257 | (self.msg_to_tag_src, 'msg')) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 258 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 259 | self.msg_connect( |
| 260 | (self.gsm_clck_ctrl, 'ctrl'), |
| 261 | (self.dict_toggle_sign, 'dict_in')) |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 262 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 263 | self.msg_connect( |
| 264 | (self.dict_toggle_sign, 'dict_out'), |
| 265 | (self.msg_to_tag_sink, 'msg')) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 266 | |
Vadim Yanitskiy | 45457e9 | 2018-03-03 23:37:30 +0700 | [diff] [blame] | 267 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 268 | # Some UHD devices (such as UmTRX) do start the clock |
| 269 | # not from 0, so it's required to reset it manually. |
| 270 | # Resetting UHD source will also affect the sink. |
| 271 | self.phy_src.set_time_now(uhd.time_spec(0.0)) |
Vadim Yanitskiy | 45457e9 | 2018-03-03 23:37:30 +0700 | [diff] [blame] | 272 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 273 | def shutdown(self): |
| 274 | print("[i] Shutdown Radio interface") |
| 275 | self.stop() |
| 276 | self.wait() |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 277 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 278 | def calc_phase_inc(self, fc): |
| 279 | return self.ppm / 1.0e6 * 2 * pi * fc / self.sample_rate |
Vadim Yanitskiy | d222ee5 | 2017-12-03 23:49:09 +0700 | [diff] [blame] | 280 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 281 | def set_rx_freq(self, fc): |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 282 | fc += self.freq_offset |
Piotr Krysik | d6e8bb7 | 2018-09-13 14:53:26 +0200 | [diff] [blame] | 283 | print("[#] Shifting RX freq. to %s (offset is %s)" |
| 284 | % (eng_notation.num_to_str(fc), |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 285 | eng_notation.num_to_str(self.freq_offset))) |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 286 | self.phy_src.set_center_freq(fc, 0) |
| 287 | self.rotator_src.set_phase_inc(self.calc_phase_inc(fc)) |
| 288 | self.rx_freq = fc |
Vadim Yanitskiy | 89aa469 | 2017-11-14 00:15:20 +0700 | [diff] [blame] | 289 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 290 | def set_tx_freq(self, fc): |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 291 | fc += self.freq_offset |
Piotr Krysik | d6e8bb7 | 2018-09-13 14:53:26 +0200 | [diff] [blame] | 292 | print("[#] Shifting TX freq. to %s (offset is %s)" |
| 293 | % (eng_notation.num_to_str(fc), |
Piotr Krysik | db65883 | 2018-09-13 15:42:55 +0200 | [diff] [blame] | 294 | eng_notation.num_to_str(self.freq_offset))) |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 295 | self.phy_sink.set_center_freq(fc, 0) |
| 296 | self.rotator_sink.set_phase_inc(-self.calc_phase_inc(fc)) |
| 297 | self.tx_freq = fc |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 298 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 299 | def set_rx_gain(self, gain): |
| 300 | self.phy_src.set_gain(gain, 0) |
| 301 | self.rx_gain = gain |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 302 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 303 | def set_tx_gain(self, gain): |
| 304 | self.phy_sink.set_gain(gain, 0) |
| 305 | self.tx_gain = gain |
Vadim Yanitskiy | 34266e7 | 2017-12-05 01:01:43 +0700 | [diff] [blame] | 306 | |
Piotr Krysik | ba6fd0e | 2018-09-06 19:57:57 +0200 | [diff] [blame] | 307 | def set_ta(self, ta): |
| 308 | print("[i] Setting TA value %d" % ta) |
| 309 | advance_time_sec = ta * self.GSM_SYM_PERIOD_uS * 1e-6 |
| 310 | self.tx_time_setter.set_timing_advance(advance_time_sec) |