aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/dboard/magnesium/magnesium_cpld_ctrl.cpp
blob: 71aa431322c4525395f749f08a5c3a1aad150fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// Copyright 2017 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0
//

#include "magnesium_cpld_ctrl.hpp"
#include "magnesium_constants.hpp"
#include <uhd/utils/log.hpp>
#include <boost/format.hpp>
#include <chrono>

namespace {
    //! Address of the CPLD scratch register
    const uint8_t CPLD_REGS_SCRATCH = 0x0040;

    //! Address of the CPLD reset register
    const uint8_t CPLD_REGS_RESET   = 0x0041;
}

magnesium_cpld_ctrl::magnesium_cpld_ctrl(
        write_spi_t write_fn,
        read_spi_t read_fn
)
{
    _write_fn = [write_fn](const uint8_t addr, const uint32_t data){
        UHD_LOG_TRACE("MG_CPLD",
            str(boost::format("Writing to CPLD: 0x%02X -> 0x%04X")
                % uint32_t(addr) % data));
        const uint32_t spi_transaction = 0
            | ((addr & 0x7F) << 16)
            | data
        ;
        write_fn(spi_transaction);
    };
    _read_fn = [read_fn](const uint8_t addr){
        UHD_LOG_TRACE("MG_CPLD",
            str(boost::format("Reading from CPLD address 0x%02X")
                % uint32_t(addr)));
        const uint32_t spi_transaction = (1<<23)
            | ((addr & 0x7F) << 16)
        ;
        return read_fn(spi_transaction);
    };

    reset();
    _loopback_test();
}

/******************************************************************************
 * API
 *****************************************************************************/
void magnesium_cpld_ctrl::reset()
{
    std::lock_guard<std::mutex> l(_set_mutex);
    UHD_LOG_TRACE("MG_CPLD", "Resetting CPLD to default state");
    // Note: We won't use _regs and commit() here, because the reset will screw
    // up the state of the CPLD w.r.t. to the local cache anyway.
    _write_fn(CPLD_REGS_RESET, 1);
    _write_fn(CPLD_REGS_RESET, 0);
    // Now make sure the local state and the CPLD state are the same by pushing
    // our local changes out:
    _regs = magnesium_cpld_regs_t();
    commit(true);
}

uint16_t magnesium_cpld_ctrl::get_reg(const uint8_t addr)
{
    std::lock_guard<std::mutex> l(_set_mutex);
    return _read_fn(addr);
}

void magnesium_cpld_ctrl::set_scratch(const uint16_t val)
{
    std::lock_guard<std::mutex> l(_set_mutex);
    _regs.scratch = val;
    commit();
}

uint16_t magnesium_cpld_ctrl::get_scratch()
{
    return get_reg(CPLD_REGS_SCRATCH);
}

void magnesium_cpld_ctrl::set_tx_switches(
    const chan_sel_t chan,
    const tx_sw1_t tx_sw1,
    const tx_sw2_t tx_sw2,
    const tx_sw3_t tx_sw3,
    const lowband_mixer_path_sel_t select_lowband_mixer_path,
    const bool enb_lowband_mixer,
    const atr_state_t atr_state,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_tx_sw1 = magnesium_cpld_regs_t::ch1_idle_tx_sw1_t(tx_sw1);
            _regs.ch1_idle_tx_sw2 = magnesium_cpld_regs_t::ch1_idle_tx_sw2_t(tx_sw2);
            _regs.ch1_idle_tx_sw3 = magnesium_cpld_regs_t::ch1_idle_tx_sw3_t(tx_sw3);
            _regs.ch1_idle_tx_lowband_mixer_path_select = magnesium_cpld_regs_t::ch1_idle_tx_lowband_mixer_path_select_t(select_lowband_mixer_path);
            _regs.ch1_idle_tx_mixer_en = enb_lowband_mixer;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_tx_sw1 = magnesium_cpld_regs_t::ch1_on_tx_sw1_t(tx_sw1);
            _regs.ch1_on_tx_sw2 = magnesium_cpld_regs_t::ch1_on_tx_sw2_t(tx_sw2);
            _regs.ch1_on_tx_sw3 = magnesium_cpld_regs_t::ch1_on_tx_sw3_t(tx_sw3);
            _regs.ch1_on_tx_lowband_mixer_path_select =
                magnesium_cpld_regs_t::ch1_on_tx_lowband_mixer_path_select_t(select_lowband_mixer_path);
            _regs.ch1_on_tx_mixer_en = enb_lowband_mixer;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_tx_sw1 = magnesium_cpld_regs_t::ch2_idle_tx_sw1_t(tx_sw1);
            _regs.ch2_idle_tx_sw2 = magnesium_cpld_regs_t::ch2_idle_tx_sw2_t(tx_sw1);
            _regs.ch2_idle_tx_sw3 = magnesium_cpld_regs_t::ch2_idle_tx_sw3_t(tx_sw1);
            _regs.ch2_idle_tx_lowband_mixer_path_select =
                magnesium_cpld_regs_t::ch2_idle_tx_lowband_mixer_path_select_t(select_lowband_mixer_path);
            _regs.ch2_idle_tx_mixer_en = enb_lowband_mixer;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_tx_sw1 = magnesium_cpld_regs_t::ch2_on_tx_sw1_t(tx_sw1);
            _regs.ch2_on_tx_sw2 = magnesium_cpld_regs_t::ch2_on_tx_sw2_t(tx_sw2);
            _regs.ch2_on_tx_sw3 = magnesium_cpld_regs_t::ch2_on_tx_sw3_t(tx_sw3);
            _regs.ch2_on_tx_lowband_mixer_path_select =
                magnesium_cpld_regs_t::ch2_on_tx_lowband_mixer_path_select_t(select_lowband_mixer_path);
            _regs.ch2_on_tx_mixer_en = enb_lowband_mixer;
        }
    }

    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_rx_switches(
    const chan_sel_t chan,
    const rx_sw2_t rx_sw2,
    const rx_sw3_t rx_sw3,
    const rx_sw4_t rx_sw4,
    const rx_sw5_t rx_sw5,
    const rx_sw6_t rx_sw6,
    const lowband_mixer_path_sel_t select_lowband_mixer_path,
    const bool enb_lowband_mixer,
    const atr_state_t atr_state,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_rx_sw2 = magnesium_cpld_regs_t::ch1_idle_rx_sw2_t(rx_sw2);
            _regs.ch1_idle_rx_sw3 = magnesium_cpld_regs_t::ch1_idle_rx_sw3_t(rx_sw3);
            _regs.ch1_idle_rx_sw4 = magnesium_cpld_regs_t::ch1_idle_rx_sw4_t(rx_sw4);
            _regs.ch1_idle_rx_sw5 = magnesium_cpld_regs_t::ch1_idle_rx_sw5_t(rx_sw5);
            _regs.ch1_idle_rx_sw6 = magnesium_cpld_regs_t::ch1_idle_rx_sw6_t(rx_sw6);
            _regs.ch1_idle_rx_loband_mixer_path_sel = magnesium_cpld_regs_t::ch1_idle_rx_loband_mixer_path_sel_t(select_lowband_mixer_path);
            _regs.ch1_idle_rx_mixer_en = enb_lowband_mixer;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_rx_sw2 = magnesium_cpld_regs_t::ch1_on_rx_sw2_t(rx_sw2);
            _regs.ch1_on_rx_sw3 = magnesium_cpld_regs_t::ch1_on_rx_sw3_t(rx_sw3);
            _regs.ch1_on_rx_sw4 = magnesium_cpld_regs_t::ch1_on_rx_sw4_t(rx_sw4);
            _regs.ch1_on_rx_sw5 = magnesium_cpld_regs_t::ch1_on_rx_sw5_t(rx_sw5);
            _regs.ch1_on_rx_sw6 = magnesium_cpld_regs_t::ch1_on_rx_sw6_t(rx_sw6);
            _regs.ch1_on_rx_loband_mixer_path_sel = magnesium_cpld_regs_t::ch1_on_rx_loband_mixer_path_sel_t(select_lowband_mixer_path);
            _regs.ch1_on_rx_mixer_en = enb_lowband_mixer;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_rx_sw2 = magnesium_cpld_regs_t::ch2_idle_rx_sw2_t(rx_sw2);
            _regs.ch2_idle_rx_sw3 = magnesium_cpld_regs_t::ch2_idle_rx_sw3_t(rx_sw3);
            _regs.ch2_idle_rx_sw4 = magnesium_cpld_regs_t::ch2_idle_rx_sw4_t(rx_sw4);
            _regs.ch2_idle_rx_sw5 = magnesium_cpld_regs_t::ch2_idle_rx_sw5_t(rx_sw5);
            _regs.ch2_idle_rx_sw6 = magnesium_cpld_regs_t::ch2_idle_rx_sw6_t(rx_sw6);
            _regs.ch2_idle_rx_loband_mixer_path_sel =
                magnesium_cpld_regs_t::ch2_idle_rx_loband_mixer_path_sel_t(select_lowband_mixer_path);
            _regs.ch2_idle_rx_mixer_en = enb_lowband_mixer;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_rx_sw2 = magnesium_cpld_regs_t::ch2_on_rx_sw2_t(rx_sw2);
            _regs.ch2_on_rx_sw3 = magnesium_cpld_regs_t::ch2_on_rx_sw3_t(rx_sw3);
            _regs.ch2_on_rx_sw4 = magnesium_cpld_regs_t::ch2_on_rx_sw4_t(rx_sw4);
            _regs.ch2_on_rx_sw5 = magnesium_cpld_regs_t::ch2_on_rx_sw5_t(rx_sw5);
            _regs.ch2_on_rx_sw6 = magnesium_cpld_regs_t::ch2_on_rx_sw6_t(rx_sw6);
            _regs.ch2_on_rx_loband_mixer_path_sel = magnesium_cpld_regs_t::ch2_on_rx_loband_mixer_path_sel_t(select_lowband_mixer_path);
            _regs.ch2_on_rx_mixer_en = enb_lowband_mixer;
        }
    }
    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_tx_atr_bits(
    const chan_sel_t chan,
    const atr_state_t atr_state,
    const bool tx_led,
    const bool tx_pa_enb,
    const bool tx_amp_enb,
    const bool tx_myk_en,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_tx_led = tx_led;
            _regs.ch1_idle_tx_pa_en = tx_pa_enb;
            _regs.ch1_idle_tx_amp_en = tx_amp_enb;
            _regs.ch1_idle_tx_myk_en = tx_myk_en;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_tx_led = tx_led;
            _regs.ch1_on_tx_pa_en = tx_pa_enb;
            _regs.ch1_on_tx_amp_en = tx_amp_enb;
            _regs.ch1_on_tx_myk_en = tx_myk_en;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_tx_led = tx_led;
            _regs.ch2_idle_tx_pa_en = tx_pa_enb;
            _regs.ch2_idle_tx_amp_en = tx_amp_enb;
            _regs.ch2_idle_tx_myk_en = tx_myk_en;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_tx_led = tx_led;
            _regs.ch2_on_tx_pa_en = tx_pa_enb;
            _regs.ch2_on_tx_amp_en = tx_amp_enb;
            _regs.ch2_on_tx_myk_en = tx_myk_en;
        }
    }
    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_trx_sw_atr_bits(
    const chan_sel_t chan,
    const atr_state_t atr_state,
    const sw_trx_t trx_sw,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_sw_trx =
                magnesium_cpld_regs_t::ch1_idle_sw_trx_t(trx_sw);
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_sw_trx =
                magnesium_cpld_regs_t::ch1_on_sw_trx_t(trx_sw);
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_sw_trx =
                magnesium_cpld_regs_t::ch2_idle_sw_trx_t(trx_sw);
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_sw_trx =
                magnesium_cpld_regs_t::ch2_on_sw_trx_t(trx_sw);
        }
    }
    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_rx_input_atr_bits(
    const chan_sel_t chan,
    const atr_state_t atr_state,
    const rx_sw1_t rx_sw1,
    const bool rx_led,
    const bool rx2_led,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_rx_sw1 =
                magnesium_cpld_regs_t::ch1_idle_rx_sw1_t(rx_sw1);
            _regs.ch1_idle_rx_led = rx_led;
            _regs.ch1_idle_rx2_led = rx2_led;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_rx_sw1 =
                magnesium_cpld_regs_t::ch1_on_rx_sw1_t(rx_sw1);
            _regs.ch1_on_rx_led = rx_led;
            _regs.ch1_on_rx2_led = rx2_led;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_rx_sw1 =
                magnesium_cpld_regs_t::ch2_idle_rx_sw1_t(rx_sw1);
            _regs.ch2_idle_rx_led = rx_led;
            _regs.ch2_idle_rx2_led = rx2_led;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_rx_sw1 =
                magnesium_cpld_regs_t::ch2_on_rx_sw1_t(rx_sw1);
            _regs.ch2_on_rx_led = rx_led;
            _regs.ch2_on_rx2_led = rx2_led;
        }
    }

    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_rx_atr_bits(
    const chan_sel_t chan,
    const atr_state_t atr_state,
    const bool rx_amp_enb,
    const bool rx_myk_en,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_rx_amp_en = rx_amp_enb;
            _regs.ch1_idle_rx_myk_en = rx_myk_en;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_rx_amp_en = rx_amp_enb;
            _regs.ch1_on_rx_myk_en = rx_myk_en;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_rx_amp_en = rx_amp_enb;
            _regs.ch2_idle_rx_myk_en = rx_myk_en;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_rx_amp_en = rx_amp_enb;
            _regs.ch2_on_rx_myk_en = rx_myk_en;
        }
    }

    if (not defer_commit) {
        commit();
    }
}

void magnesium_cpld_ctrl::set_rx_lna_atr_bits(
    const chan_sel_t chan,
    const atr_state_t atr_state,
    const bool rx_lna1_enb,
    const bool rx_lna2_enb,
    const bool defer_commit
) {
    std::lock_guard<std::mutex> l(_set_mutex);
    if (chan == CHAN1 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch1_idle_rx_lna1_en = rx_lna1_enb;
            _regs.ch1_idle_rx_lna2_en = rx_lna2_enb;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch1_on_rx_lna1_en = rx_lna1_enb;
            _regs.ch1_on_rx_lna2_en = rx_lna2_enb;
        }
    }
    if (chan == CHAN2 or chan == BOTH) {
        if (atr_state == IDLE or atr_state == ANY) {
            _regs.ch2_idle_rx_lna1_en = rx_lna1_enb;
            _regs.ch2_idle_rx_lna2_en = rx_lna2_enb;
        }
        if (atr_state == ON or atr_state == ANY) {
            _regs.ch2_on_rx_lna1_en = rx_lna1_enb;
            _regs.ch2_on_rx_lna2_en = rx_lna2_enb;
        }
    }

    if (not defer_commit) {
        commit();
    }
}


/******************************************************************************
 * Private methods
 *****************************************************************************/
void magnesium_cpld_ctrl::_loopback_test()
{
    UHD_LOG_TRACE("MG_CPLD", "Performing CPLD scratch loopback test...");
    using namespace std::chrono;
    const uint16_t random_number = // Derived from current time
        uint16_t(system_clock::to_time_t(system_clock::now()) & 0xFFFF);
    set_scratch(random_number);
    const uint16_t actual = get_scratch();
    if (actual != random_number) {
        UHD_LOGGER_ERROR("MG_CPLD")
            << "CPLD scratch loopback failed! "
            << boost::format("Expected: 0x%04X Got: 0x%04X")
               % random_number % actual
        ;
        throw uhd::runtime_error("CPLD scratch loopback failed!");
    }
    UHD_LOG_TRACE("MG_CPLD", "CPLD scratch loopback test passed!");
}


void magnesium_cpld_ctrl::commit(const bool save_all)
{
    UHD_LOGGER_TRACE("MG_CPLD")
        << "Storing register cache "
        << (save_all ? "completely" : "selectively")
        << " to CPLD via SPI..."
    ;
    auto changed_addrs = save_all ?
        _regs.get_all_addrs() :
        _regs.get_changed_addrs<size_t>();
    for (const auto addr: changed_addrs) {
        _write_fn(addr, _regs.get_reg(addr));
    }
    _regs.save_state();
    UHD_LOG_TRACE("MG_CPLD",
        "Storing cache complete: " \
        "Updated " << changed_addrs.size() << " registers.");
}