1 //
2 // Copyright 2010-2012 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #include "codec_ctrl.hpp"
9 #include "soft_time_ctrl.hpp"
10 #include "usrp1_iface.hpp"
11 #include <uhd/device.hpp>
12 #include <uhd/property_tree.hpp>
13 #include <uhd/transport/usb_zero_copy.hpp>
14 #include <uhd/types/dict.hpp>
15 #include <uhd/types/stream_cmd.hpp>
16 #include <uhd/usrp/dboard_eeprom.hpp>
17 #include <uhd/usrp/dboard_id.hpp>
18 #include <uhd/usrp/dboard_manager.hpp>
19 #include <uhd/usrp/mboard_eeprom.hpp>
20 #include <uhd/usrp/subdev_spec.hpp>
21 #include <uhd/utils/pimpl.hpp>
22 #include <atomic>
23 #include <complex>
24 #include <memory>
25 
26 #ifndef INCLUDED_USRP1_IMPL_HPP
27 #    define INCLUDED_USRP1_IMPL_HPP
28 
29 static const std::string USRP1_EEPROM_MAP_KEY = "B000";
30 static const size_t USRP1_MAX_RATE_USB2       = 32000000; // bytes/s
31 
32 #    define FR_RB_CAPS 3
33 #    define FR_MODE 13
34 #    define FR_DEBUG_EN 14
35 #    define FR_DC_OFFSET_CL_EN 15
36 #    define FR_ADC_OFFSET_0 16
37 #    define FR_ADC_OFFSET_1 17
38 #    define FR_ADC_OFFSET_2 18
39 #    define FR_ADC_OFFSET_3 19
40 
41 #    define I2C_DEV_EEPROM 0x50
42 #    define I2C_ADDR_BOOT (I2C_DEV_EEPROM | 0x0)
43 #    define I2C_ADDR_TX_A (I2C_DEV_EEPROM | 0x4)
44 #    define I2C_ADDR_RX_A (I2C_DEV_EEPROM | 0x5)
45 #    define I2C_ADDR_TX_B (I2C_DEV_EEPROM | 0x6)
46 #    define I2C_ADDR_RX_B (I2C_DEV_EEPROM | 0x7)
47 
48 #    define SPI_ENABLE_CODEC_A 0x02
49 #    define SPI_ENABLE_CODEC_B 0x04
50 
51 /*!
52  * USRP1 implementation guts:
53  * The implementation details are encapsulated here.
54  * Handles properties on the mboard, dboard, dsps...
55  */
56 class usrp1_impl : public uhd::device
57 {
58 public:
59     //! used everywhere to differentiate slots/sides...
60     enum dboard_slot_t { DBOARD_SLOT_A = 'A', DBOARD_SLOT_B = 'B' };
61     // and a way to enumerate through a list of the above...
62     static const std::vector<dboard_slot_t> _dboard_slots;
63 
64     // structors
65     usrp1_impl(const uhd::device_addr_t&);
66     ~usrp1_impl(void);
67 
68     // the io interface
69     uhd::rx_streamer::sptr get_rx_stream(const uhd::stream_args_t& args);
70     uhd::tx_streamer::sptr get_tx_stream(const uhd::stream_args_t& args);
71     bool recv_async_msg(uhd::async_metadata_t&, double);
72 
73     static uhd::usrp::mboard_eeprom_t get_mb_eeprom(uhd::i2c_iface::sptr);
74 
75 private:
76     // controllers
77     uhd::usrp::fx2_ctrl::sptr _fx2_ctrl;
78     usrp1_iface::sptr _iface;
79     uhd::usrp::soft_time_ctrl::sptr _soft_time_ctrl;
80     uhd::transport::usb_zero_copy::sptr _data_transport;
81     struct db_container_type
82     {
83         usrp1_codec_ctrl::sptr codec;
84         uhd::usrp::dboard_manager::sptr dboard_manager;
85     };
86     uhd::dict<std::string, db_container_type> _dbc;
87 
88     double _master_clock_rate; // clock rate shadow
89 
90     // weak pointers to streamers for update purposes
91     std::weak_ptr<uhd::rx_streamer> _rx_streamer;
92     std::weak_ptr<uhd::tx_streamer> _tx_streamer;
93 
94     void set_mb_eeprom(const uhd::usrp::mboard_eeprom_t&);
95     void set_db_eeprom(
96         const std::string&, const std::string&, const uhd::usrp::dboard_eeprom_t&);
97     double update_rx_codec_gain(const std::string&, const double); // sets A and B at once
98     void update_rx_subdev_spec(const uhd::usrp::subdev_spec_t&);
99     void update_tx_subdev_spec(const uhd::usrp::subdev_spec_t&);
100     double update_rx_samp_rate(size_t dspno, const double);
101     double update_tx_samp_rate(size_t dspno, const double);
102     void update_rates(void);
103     double update_rx_dsp_freq(const size_t, const double);
104     double update_tx_dsp_freq(const size_t, const double);
105     void update_tick_rate(const double rate);
106     uhd::meta_range_t get_rx_dsp_freq_range(void);
107     uhd::meta_range_t get_tx_dsp_freq_range(void);
108     uhd::meta_range_t get_rx_dsp_host_rates(void);
109     uhd::meta_range_t get_tx_dsp_host_rates(void);
110     size_t _rx_dc_offset_shadow;
111     void set_enb_rx_dc_offset(const std::string& db, const bool);
112     std::complex<double> set_rx_dc_offset(
113         const std::string& db, const std::complex<double>&);
114 
115     static uhd::usrp::dboard_iface::sptr make_dboard_iface(usrp1_iface::sptr,
116         usrp1_codec_ctrl::sptr,
117         dboard_slot_t,
118         const double&,
119         const uhd::usrp::dboard_id_t&);
120 
121     // handle io stuff
122     UHD_PIMPL_DECL(io_impl) _io_impl;
123     void io_init(void);
124     void rx_stream_on_off(bool);
125     void tx_stream_on_off(bool);
126     void handle_overrun(size_t);
127 
128     // channel mapping shadows
129     uhd::usrp::subdev_spec_t _rx_subdev_spec, _tx_subdev_spec;
130 
131     // capabilities
132     size_t get_num_ducs(void);
133     size_t get_num_ddcs(void);
134     bool has_rx_halfband(void);
135     bool has_tx_halfband(void);
136 
137     void vandal_conquest_loop(std::atomic<bool>&);
138 
139     void set_reg(const std::pair<uint8_t, uint32_t>& reg);
140 
141     // handle the enables
142     bool _rx_enabled, _tx_enabled;
enable_rx(bool enb)143     void enable_rx(bool enb)
144     {
145         _rx_enabled = enb;
146         _fx2_ctrl->usrp_rx_enable(enb);
147     }
enable_tx(bool enb)148     void enable_tx(bool enb)
149     {
150         _tx_enabled = enb;
151         _fx2_ctrl->usrp_tx_enable(enb);
152         for (const std::string& key : _dbc.keys()) {
153             _dbc[key].codec->enable_tx_digital(enb);
154         }
155     }
156 
157     // conditionally disable and enable rx
disable_rx(void)158     bool disable_rx(void)
159     {
160         if (_rx_enabled) {
161             enable_rx(false);
162             return true;
163         }
164         return false;
165     }
restore_rx(bool last)166     void restore_rx(bool last)
167     {
168         if (last != _rx_enabled) {
169             enable_rx(last);
170         }
171     }
172 
173     // conditionally disable and enable tx
disable_tx(void)174     bool disable_tx(void)
175     {
176         if (_tx_enabled) {
177             enable_tx(false);
178             return true;
179         }
180         return false;
181     }
restore_tx(bool last)182     void restore_tx(bool last)
183     {
184         if (last != _tx_enabled) {
185             enable_tx(last);
186         }
187     }
188 };
189 
190 #endif /* INCLUDED_USRP1_IMPL_HPP */
191