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 | # |
| 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 | |
| 25 | import pmt |
| 26 | import time |
| 27 | import grgsm |
| 28 | import osmosdr |
| 29 | |
| 30 | from math import pi |
| 31 | |
| 32 | from gnuradio import blocks |
| 33 | from gnuradio import gr |
| 34 | |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 35 | class radio_if(gr.top_block): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 36 | # PHY specific variables |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 37 | shiftoff = 400e3 |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 38 | fc = 941.6e6 # TODO: set ARFCN to 0? |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 39 | |
| 40 | # Application state flags |
| 41 | trx_started = False |
| 42 | fc_set = False |
| 43 | |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 44 | 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 Yanitskiy | 5d68aa5 | 2017-10-17 11:14:48 +0700 | [diff] [blame] | 47 | trx_remote_addr, trx_base_port): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 48 | |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 49 | print("[i] Init Radio interface") |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 50 | |
| 51 | # PHY specific variables |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 52 | self.rx_gain = phy_rx_gain |
| 53 | self.tx_gain = phy_tx_gain |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 54 | |
| 55 | gr.top_block.__init__(self, "GR-GSM TRX") |
| 56 | shift_fc = self.fc - self.shiftoff |
| 57 | |
| 58 | ################################################## |
| 59 | # PHY Definition |
| 60 | ################################################## |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 61 | self.phy = osmosdr.source(args = "numchan=%d %s" % (1, phy_args)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 62 | |
| 63 | self.phy.set_bandwidth(250e3 + abs(self.shiftoff), 0) |
| 64 | self.phy.set_center_freq(shift_fc, 0) |
Vadim Yanitskiy | da5e21f | 2017-10-18 18:24:48 +0700 | [diff] [blame] | 65 | self.phy.set_sample_rate(phy_sample_rate) |
| 66 | self.phy.set_freq_corr(phy_ppm, 0) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 67 | 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 Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 70 | self.phy.set_gain(self.rx_gain, 0) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 71 | self.phy.set_if_gain(20, 0) |
| 72 | self.phy.set_bb_gain(20, 0) |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 73 | self.phy.set_antenna(phy_rx_antenna, 0) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 74 | |
| 75 | ################################################## |
| 76 | # GR-GSM Magic |
| 77 | ################################################## |
| 78 | self.blocks_rotator = blocks.rotator_cc( |
Vadim Yanitskiy | da5e21f | 2017-10-18 18:24:48 +0700 | [diff] [blame] | 79 | -2 * pi * self.shiftoff / phy_sample_rate) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 80 | |
| 81 | self.gsm_input = grgsm.gsm_input( |
Vadim Yanitskiy | da5e21f | 2017-10-18 18:24:48 +0700 | [diff] [blame] | 82 | ppm = phy_ppm, osr = 4, fc = self.fc, |
| 83 | samp_rate_in = phy_sample_rate) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 84 | |
| 85 | self.gsm_receiver = grgsm.receiver(4, ([0]), ([])) |
| 86 | |
| 87 | self.gsm_clck_ctrl = grgsm.clock_offset_control( |
Vadim Yanitskiy | da5e21f | 2017-10-18 18:24:48 +0700 | [diff] [blame] | 88 | shift_fc, phy_sample_rate, osr = 4) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 89 | |
Vadim Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 90 | self.gsm_ts_filter = grgsm.burst_timeslot_filter(0) |
| 91 | self.gsm_ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL) |
| 92 | |
Vadim Yanitskiy | 1a88dec | 2017-10-17 09:00:47 +0700 | [diff] [blame] | 93 | self.gsm_trx_burst_if = grgsm.trx_burst_if( |
Vadim Yanitskiy | 790b6f0 | 2017-10-17 11:47:36 +0700 | [diff] [blame] | 94 | trx_remote_addr, str(trx_base_port)) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 95 | |
| 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 Yanitskiy | 962e2d8 | 2017-10-17 09:24:55 +0700 | [diff] [blame] | 110 | (self.gsm_ts_filter, 'in')) |
| 111 | |
| 112 | self.msg_connect((self.gsm_ts_filter, 'out'), |
Vadim Yanitskiy | 1a88dec | 2017-10-17 09:00:47 +0700 | [diff] [blame] | 113 | (self.gsm_trx_burst_if, 'bursts')) |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 114 | |
| 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 Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 123 | 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 Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 129 | def set_rx_gain(self, gain): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 130 | self.phy.set_gain(gain, 0) |
Vadim Yanitskiy | 01c6afd | 2017-10-19 01:14:24 +0700 | [diff] [blame] | 131 | self.rx_gain = gain |
| 132 | |
| 133 | def set_tx_gain(self, gain): |
| 134 | # TODO: TX chain not implemented yet |
| 135 | self.tx_gain = gain |