Piotr Krysik | d2f162f | 2017-11-30 12:50:00 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | ################################################## |
| 4 | # GNU Radio Python Flow Graph |
| 5 | # Title: Trx radio interface |
| 6 | # Author: (C) Piotr Krysik 2017 |
| 7 | # Description: Alpha version of trx radio interface |
| 8 | # Generated: Thu Nov 30 12:45:52 2017 |
| 9 | ################################################## |
| 10 | |
| 11 | from gnuradio import blocks |
| 12 | from gnuradio import digital |
| 13 | from gnuradio import eng_notation |
| 14 | from gnuradio import filter |
| 15 | from gnuradio import gr |
| 16 | from gnuradio import uhd |
| 17 | from gnuradio.eng_option import eng_option |
| 18 | from gnuradio.filter import firdes |
| 19 | from grgsm import gsm_gmsk_mod |
| 20 | from optparse import OptionParser |
| 21 | import grgsm |
| 22 | import math |
| 23 | import time |
| 24 | |
| 25 | |
| 26 | class radio_if_grc(gr.top_block): |
| 27 | |
| 28 | def __init__(self, delay_correction=285.616e-6, osr=4, ppm=-1, rx_freq=938600000, rx_gain=40, samp_rate=13e6/12.0, timing_advance=0, trx_base_port="5700", trx_remote_addr="127.0.0.1", tx_freq=938900000, tx_gain=30, uplink_shift=-(6.0/1625000*(156.25)*3)): |
| 29 | gr.top_block.__init__(self, "Trx radio interface") |
| 30 | |
| 31 | ################################################## |
| 32 | # Parameters |
| 33 | ################################################## |
| 34 | self.delay_correction = delay_correction |
| 35 | self.osr = osr |
| 36 | self.ppm = ppm |
| 37 | self.rx_freq = rx_freq |
| 38 | self.rx_gain = rx_gain |
| 39 | self.samp_rate = samp_rate |
| 40 | self.timing_advance = timing_advance |
| 41 | self.trx_base_port = trx_base_port |
| 42 | self.trx_remote_addr = trx_remote_addr |
| 43 | self.tx_freq = tx_freq |
| 44 | self.tx_gain = tx_gain |
| 45 | self.uplink_shift = uplink_shift |
| 46 | |
| 47 | ################################################## |
| 48 | # Blocks |
| 49 | ################################################## |
| 50 | self.uhd_usrp_source_0 = uhd.usrp_source( |
| 51 | ",".join(("", "")), |
| 52 | uhd.stream_args( |
| 53 | cpu_format="fc32", |
| 54 | channels=range(1), |
| 55 | ), |
| 56 | ) |
| 57 | self.uhd_usrp_source_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) |
| 58 | self.uhd_usrp_source_0.set_samp_rate(samp_rate) |
| 59 | self.uhd_usrp_source_0.set_center_freq(rx_freq, 0) |
| 60 | self.uhd_usrp_source_0.set_gain(rx_gain, 0) |
| 61 | self.uhd_usrp_source_0.set_antenna("RX2", 0) |
| 62 | self.uhd_usrp_sink_0 = uhd.usrp_sink( |
| 63 | ",".join(("", "")), |
| 64 | uhd.stream_args( |
| 65 | cpu_format="fc32", |
| 66 | channels=range(1), |
| 67 | ), |
| 68 | "packet_len", |
| 69 | ) |
| 70 | self.uhd_usrp_sink_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) |
| 71 | self.uhd_usrp_sink_0.set_subdev_spec("A:B", 0) |
| 72 | self.uhd_usrp_sink_0.set_samp_rate(samp_rate) |
| 73 | self.uhd_usrp_sink_0.set_center_freq(tx_freq, 0) |
| 74 | self.uhd_usrp_sink_0.set_gain(tx_gain, 0) |
| 75 | self.uhd_usrp_sink_0.set_antenna("TX/RX", 0) |
| 76 | self.low_pass_filter_0_0 = filter.fir_filter_ccf(1, firdes.low_pass( |
| 77 | 1, samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) |
| 78 | self.gsm_txtime_setter_0 = grgsm.txtime_setter(None if (None is not None) else 0xffffffff, 0, 0, 0, 0, timing_advance, delay_correction+uplink_shift) |
| 79 | self.gsm_trx_burst_if_0 = grgsm.trx_burst_if(trx_remote_addr, trx_base_port) |
| 80 | self.gsm_receiver_0 = grgsm.receiver(4, ([0]), ([4]), False) |
| 81 | self.gsm_preprocess_tx_burst_0 = grgsm.preprocess_tx_burst() |
| 82 | self.gsm_msg_to_tag_0_0 = grgsm.msg_to_tag() |
| 83 | self.gsm_msg_to_tag_0 = grgsm.msg_to_tag() |
| 84 | self.gsm_gmsk_mod_0 = gsm_gmsk_mod( |
| 85 | BT=0.3, |
| 86 | pulse_duration=4, |
| 87 | sps=osr, |
| 88 | ) |
| 89 | self.gsm_controlled_rotator_cc_0_0 = grgsm.controlled_rotator_cc(-ppm/1.0e6*2*math.pi*tx_freq/samp_rate) |
| 90 | self.gsm_controlled_rotator_cc_0 = grgsm.controlled_rotator_cc(ppm/1.0e6*2*math.pi*rx_freq/samp_rate) |
| 91 | self.gsm_burst_type_filter_0 = grgsm.burst_type_filter(([3])) |
| 92 | self.gsm_burst_to_fn_time_0 = grgsm.burst_to_fn_time() |
| 93 | self.digital_burst_shaper_xx_0 = digital.burst_shaper_cc((firdes.window(firdes.WIN_HANN, 16, 0)), 0, 20, False, "packet_len") |
| 94 | self.blocks_pdu_to_tagged_stream_0_0 = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") |
| 95 | |
| 96 | ################################################## |
| 97 | # Connections |
| 98 | ################################################## |
| 99 | self.msg_connect((self.gsm_burst_to_fn_time_0, 'fn_time_out'), (self.gsm_txtime_setter_0, 'fn_time')) |
| 100 | self.msg_connect((self.gsm_burst_type_filter_0, 'bursts_out'), (self.gsm_burst_to_fn_time_0, 'bursts_in')) |
| 101 | self.msg_connect((self.gsm_preprocess_tx_burst_0, 'bursts_out'), (self.blocks_pdu_to_tagged_stream_0_0, 'pdus')) |
| 102 | self.msg_connect((self.gsm_receiver_0, 'C0'), (self.gsm_burst_type_filter_0, 'bursts_in')) |
| 103 | self.msg_connect((self.gsm_receiver_0, 'C0'), (self.gsm_trx_burst_if_0, 'bursts')) |
| 104 | self.msg_connect((self.gsm_trx_burst_if_0, 'bursts'), (self.gsm_txtime_setter_0, 'bursts_in')) |
| 105 | self.msg_connect((self.gsm_txtime_setter_0, 'bursts_out'), (self.gsm_preprocess_tx_burst_0, 'bursts_in')) |
| 106 | self.connect((self.blocks_pdu_to_tagged_stream_0_0, 0), (self.gsm_gmsk_mod_0, 0)) |
| 107 | self.connect((self.digital_burst_shaper_xx_0, 0), (self.gsm_msg_to_tag_0_0, 0)) |
| 108 | self.connect((self.gsm_controlled_rotator_cc_0, 0), (self.low_pass_filter_0_0, 0)) |
| 109 | self.connect((self.gsm_controlled_rotator_cc_0_0, 0), (self.uhd_usrp_sink_0, 0)) |
| 110 | self.connect((self.gsm_gmsk_mod_0, 0), (self.digital_burst_shaper_xx_0, 0)) |
| 111 | self.connect((self.gsm_msg_to_tag_0, 0), (self.gsm_controlled_rotator_cc_0, 0)) |
| 112 | self.connect((self.gsm_msg_to_tag_0_0, 0), (self.gsm_controlled_rotator_cc_0_0, 0)) |
| 113 | self.connect((self.low_pass_filter_0_0, 0), (self.gsm_receiver_0, 0)) |
| 114 | self.connect((self.uhd_usrp_source_0, 0), (self.gsm_msg_to_tag_0, 0)) |
| 115 | |
| 116 | def get_delay_correction(self): |
| 117 | return self.delay_correction |
| 118 | |
| 119 | def set_delay_correction(self, delay_correction): |
| 120 | self.delay_correction = delay_correction |
| 121 | self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) |
| 122 | |
| 123 | def get_osr(self): |
| 124 | return self.osr |
| 125 | |
| 126 | def set_osr(self, osr): |
| 127 | self.osr = osr |
| 128 | self.gsm_gmsk_mod_0.set_sps(self.osr) |
| 129 | |
| 130 | def get_ppm(self): |
| 131 | return self.ppm |
| 132 | |
| 133 | def set_ppm(self, ppm): |
| 134 | self.ppm = ppm |
| 135 | self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) |
| 136 | self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) |
| 137 | |
| 138 | def get_rx_freq(self): |
| 139 | return self.rx_freq |
| 140 | |
| 141 | def set_rx_freq(self, rx_freq): |
| 142 | self.rx_freq = rx_freq |
| 143 | self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) |
| 144 | self.uhd_usrp_source_0.set_center_freq(self.rx_freq, 0) |
| 145 | |
| 146 | def get_rx_gain(self): |
| 147 | return self.rx_gain |
| 148 | |
| 149 | def set_rx_gain(self, rx_gain): |
| 150 | self.rx_gain = rx_gain |
| 151 | self.uhd_usrp_source_0.set_gain(self.rx_gain, 0) |
| 152 | |
| 153 | |
| 154 | def get_samp_rate(self): |
| 155 | return self.samp_rate |
| 156 | |
| 157 | def set_samp_rate(self, samp_rate): |
| 158 | self.samp_rate = samp_rate |
| 159 | self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) |
| 160 | self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) |
| 161 | self.low_pass_filter_0_0.set_taps(firdes.low_pass(1, self.samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) |
| 162 | self.uhd_usrp_sink_0.set_samp_rate(self.samp_rate) |
| 163 | self.uhd_usrp_source_0.set_samp_rate(self.samp_rate) |
| 164 | |
| 165 | def get_timing_advance(self): |
| 166 | return self.timing_advance |
| 167 | |
| 168 | def set_timing_advance(self, timing_advance): |
| 169 | self.timing_advance = timing_advance |
| 170 | self.gsm_txtime_setter_0.set_timing_advance(self.timing_advance) |
| 171 | |
| 172 | def get_trx_base_port(self): |
| 173 | return self.trx_base_port |
| 174 | |
| 175 | def set_trx_base_port(self, trx_base_port): |
| 176 | self.trx_base_port = trx_base_port |
| 177 | |
| 178 | def get_trx_remote_addr(self): |
| 179 | return self.trx_remote_addr |
| 180 | |
| 181 | def set_trx_remote_addr(self, trx_remote_addr): |
| 182 | self.trx_remote_addr = trx_remote_addr |
| 183 | |
| 184 | def get_tx_freq(self): |
| 185 | return self.tx_freq |
| 186 | |
| 187 | def set_tx_freq(self, tx_freq): |
| 188 | self.tx_freq = tx_freq |
| 189 | self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) |
| 190 | self.uhd_usrp_sink_0.set_center_freq(self.tx_freq, 0) |
| 191 | |
| 192 | def get_tx_gain(self): |
| 193 | return self.tx_gain |
| 194 | |
| 195 | def set_tx_gain(self, tx_gain): |
| 196 | self.tx_gain = tx_gain |
| 197 | self.uhd_usrp_sink_0.set_gain(self.tx_gain, 0) |
| 198 | |
| 199 | |
| 200 | def get_uplink_shift(self): |
| 201 | return self.uplink_shift |
| 202 | |
| 203 | def set_uplink_shift(self, uplink_shift): |
| 204 | self.uplink_shift = uplink_shift |
| 205 | self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) |
| 206 | |
| 207 | |
| 208 | def argument_parser(): |
| 209 | parser = OptionParser(option_class=eng_option, usage="%prog: [options]") |
| 210 | parser.add_option( |
| 211 | "", "--delay-correction", dest="delay_correction", type="eng_float", default=eng_notation.num_to_str(285.616e-6), |
| 212 | help="Set delay_correction [default=%default]") |
| 213 | parser.add_option( |
| 214 | "", "--osr", dest="osr", type="intx", default=4, |
| 215 | help="Set OSR [default=%default]") |
| 216 | parser.add_option( |
| 217 | "", "--ppm", dest="ppm", type="eng_float", default=eng_notation.num_to_str(-1), |
| 218 | help="Set Clock offset correction [default=%default]") |
| 219 | parser.add_option( |
| 220 | "-d", "--rx-freq", dest="rx_freq", type="eng_float", default=eng_notation.num_to_str(938600000), |
| 221 | help="Set rx_freq [default=%default]") |
| 222 | parser.add_option( |
| 223 | "-g", "--rx-gain", dest="rx_gain", type="eng_float", default=eng_notation.num_to_str(40), |
| 224 | help="Set rx_gain [default=%default]") |
| 225 | parser.add_option( |
| 226 | "", "--samp-rate", dest="samp_rate", type="eng_float", default=eng_notation.num_to_str(13e6/12.0), |
| 227 | help="Set samp_rate [default=%default]") |
| 228 | parser.add_option( |
| 229 | "", "--timing-advance", dest="timing_advance", type="eng_float", default=eng_notation.num_to_str(0), |
| 230 | help="Set timing_advance [default=%default]") |
| 231 | parser.add_option( |
| 232 | "", "--trx-base-port", dest="trx_base_port", type="string", default="5700", |
| 233 | help="Set 5700 [default=%default]") |
| 234 | parser.add_option( |
| 235 | "", "--trx-remote-addr", dest="trx_remote_addr", type="string", default="127.0.0.1", |
| 236 | help="Set 127.0.0.1 [default=%default]") |
| 237 | parser.add_option( |
| 238 | "-u", "--tx-freq", dest="tx_freq", type="eng_float", default=eng_notation.num_to_str(938900000), |
| 239 | help="Set tx_freq [default=%default]") |
| 240 | parser.add_option( |
| 241 | "-r", "--tx-gain", dest="tx_gain", type="eng_float", default=eng_notation.num_to_str(30), |
| 242 | help="Set tx_gain [default=%default]") |
| 243 | parser.add_option( |
| 244 | "", "--uplink-shift", dest="uplink_shift", type="eng_float", default=eng_notation.num_to_str(-(6.0/1625000*(156.25)*3)), |
| 245 | help="Set uplink_shift [default=%default]") |
| 246 | return parser |
| 247 | |
| 248 | |
| 249 | def main(top_block_cls=radio_if_grc, options=None): |
| 250 | if options is None: |
| 251 | options, _ = argument_parser().parse_args() |
| 252 | |
| 253 | tb = top_block_cls(delay_correction=options.delay_correction, osr=options.osr, ppm=options.ppm, rx_freq=options.rx_freq, rx_gain=options.rx_gain, samp_rate=options.samp_rate, timing_advance=options.timing_advance, trx_base_port=options.trx_base_port, trx_remote_addr=options.trx_remote_addr, tx_freq=options.tx_freq, tx_gain=options.tx_gain, uplink_shift=options.uplink_shift) |
| 254 | tb.start() |
| 255 | tb.wait() |
| 256 | |
| 257 | |
| 258 | if __name__ == '__main__': |
| 259 | main() |