File: gpio_atr_3000.cpp

package info (click to toggle)
uhd 4.3.0.0%2Bds1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 175,384 kB
  • sloc: cpp: 271,145; ansic: 103,960; python: 95,906; vhdl: 55,838; tcl: 14,117; xml: 8,535; makefile: 2,706; sh: 2,432; pascal: 230; javascript: 120; csh: 94; asm: 20; perl: 11
file content (462 lines) | stat: -rw-r--r-- 16,063 bytes parent folder | download
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// Copyright 2011,2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/types/dict.hpp>
#include <uhd/utils/soft_register.hpp>
#include <uhdlib/usrp/cores/gpio_atr_3000.hpp>
#include <unordered_map>

using namespace uhd;
using namespace usrp;

//-------------------------------------------------------------
// gpio_atr_3000
//-------------------------------------------------------------

namespace {
// Special RB addr value to indicate no readback
// This value is invalid as a real address because it is not a multiple of 4
static constexpr wb_iface::wb_addr_type READBACK_DISABLED = 0xFFFFFFFF;
}; // namespace

namespace uhd { namespace usrp { namespace gpio_atr {

bool gpio_atr_offsets::is_writeonly() const
{
    return readback == READBACK_DISABLED;
}

gpio_atr_offsets gpio_atr_offsets::make_default(
    const uhd::wb_iface::wb_addr_type base,
    const uhd::wb_iface::wb_addr_type rb_addr,
    const size_t stride)
{
    gpio_atr_offsets offsets {
        base, // Idle
        static_cast<uhd::wb_iface::wb_addr_type>(base + stride), // RX
        static_cast<uhd::wb_iface::wb_addr_type>(base + stride * 2), // TX
        static_cast<uhd::wb_iface::wb_addr_type>(base + stride * 3), // Full Duplex
        static_cast<uhd::wb_iface::wb_addr_type>(base + stride * 4), // DDR
        static_cast<uhd::wb_iface::wb_addr_type>(base + stride * 5), // Disabled
        rb_addr, // Readback
    };
    return offsets;
}

gpio_atr_offsets gpio_atr_offsets::make_write_only(
    const uhd::wb_iface::wb_addr_type base,
    const size_t stride)
{
    return make_default(base, READBACK_DISABLED, stride);
}

class gpio_atr_3000_impl : public gpio_atr_3000
{
public:
    gpio_atr_3000_impl(wb_iface::sptr iface,
        const gpio_atr_offsets registers)
        : _iface(iface)
        , _rb_addr(registers.readback)
        , _atr_idle_reg(registers.idle, _atr_disable_reg)
        , _atr_rx_reg(registers.rx)
        , _atr_tx_reg(registers.tx)
        , _atr_fdx_reg(registers.duplex)
        , _ddr_reg(registers.ddr)
        , _atr_disable_reg(registers.disable)
    {
        _atr_idle_reg.initialize(*_iface, true);
        _atr_rx_reg.initialize(*_iface, true);
        _atr_tx_reg.initialize(*_iface, true);
        _atr_fdx_reg.initialize(*_iface, true);
        _ddr_reg.initialize(*_iface, true);
        _atr_disable_reg.initialize(*_iface, true);
        for (const auto& attr : gpio_attr_map) {
            if (attr.first == usrp::gpio_atr::GPIO_SRC
                || attr.first == usrp::gpio_atr::GPIO_READBACK) {
                // Don't set the SRC and READBACK, they're handled elsewhere.
                continue;
            }
            _attr_reg_state.emplace(attr.first, 0);
        }
    }

    void set_atr_mode(const gpio_atr_mode_t mode, const uint32_t mask) override
    {
        // Each bit in the "ATR Disable" register determines whether the respective bit in
        // the GPIO output bus is driven by the ATR engine or a static register. For each
        // bit position, a 1 means that the bit is static and 0 means that the bit is
        // driven by the ATR state machine. This setting will only get applied to all bits
        // in the "mask" that are 1. All other bits will retain their old value.
        auto value = (mode == MODE_ATR) ? ~MASK_SET_ALL : MASK_SET_ALL;
        _atr_disable_reg.set_with_mask(value, mask);
        _atr_disable_reg.flush();
        // The attr state is inverted from _atr_disable_reg. In _atr_disable_reg,
        // 1 == disable, whereas in CTRL, 1 means enable ATR.
        _update_attr_state(GPIO_CTRL, ~value, mask);
    }

    void set_gpio_ddr(const gpio_ddr_t dir, const uint32_t mask) override
    {
        // Each bit in the "DDR" register determines whether the respective bit in the
        // GPIO bus is an input or an output. For each bit position, a 1 means that the
        // bit is an output and 0 means that the bit is an input. This setting will only
        // get applied to all bits in the "mask" that are 1. All other bits will retain
        // their old value.
        auto value = (dir == DDR_INPUT) ? ~MASK_SET_ALL : MASK_SET_ALL;
        _ddr_reg.set_with_mask(value, mask);
        _ddr_reg.flush();
        _update_attr_state(GPIO_DDR, value, mask);
    }

    void set_atr_reg(const gpio_atr_reg_t atr,
        const uint32_t value,
        const uint32_t mask = MASK_SET_ALL) override
    {
        // Set the value of the specified ATR register. For bits with ATR Disable set to
        // 1, the IDLE register will hold the output state This setting will only get
        // applied to all bits in the "mask" that are 1. All other bits will retain their
        // old value.
        masked_reg_t* reg = NULL;
        gpio_attr_t attr;
        switch (atr) {
            case ATR_REG_IDLE:
                reg  = &_atr_idle_reg;
                attr = GPIO_ATR_0X;

                break;
            case ATR_REG_RX_ONLY:
                reg  = &_atr_rx_reg;
                attr = GPIO_ATR_RX;

                break;
            case ATR_REG_TX_ONLY:
                reg  = &_atr_tx_reg;
                attr = GPIO_ATR_TX;

                break;
            case ATR_REG_FULL_DUPLEX:
                reg  = &_atr_fdx_reg;
                attr = GPIO_ATR_XX;

                break;
            default:
                reg  = &_atr_idle_reg;
                attr = GPIO_ATR_0X;
                break;
        }
        // For protection we only write to bits that have the mode ATR by masking the user
        // specified "mask" with ~atr_disable.
        reg->set_with_mask(value, mask);
        reg->flush();
        _update_attr_state(attr, value, mask);
    }

    void set_gpio_out(const uint32_t value, const uint32_t mask = MASK_SET_ALL) override
    {
        // Set the value of the specified GPIO output register.
        // This setting will only get applied to all bits in the "mask" that are 1. All
        // other bits will retain their old value.

        // For protection we only write to bits that have the mode GPIO by masking the
        // user specified "mask" with atr_disable.
        _atr_idle_reg.set_gpio_out_with_mask(value, mask);
        _atr_idle_reg.flush();
        _update_attr_state(GPIO_OUT, value, mask);
    }

    uint32_t read_gpio() override
    {
        // Read the state of the GPIO pins
        // If a pin is configured as an input, reads the actual value of the pin
        // If a pin is configured as an output, reads the last value written to the pin
        if (_rb_addr != READBACK_DISABLED) {
            return _iface->peek32(_rb_addr);
        } else {
            throw uhd::runtime_error("read_gpio not supported for write-only interface.");
        }
    }

    uint32_t get_attr_reg(const gpio_attr_t attr) override
    {
        if (attr == GPIO_SRC) {
            throw uhd::runtime_error("Can't get GPIO source by GPIO ATR interface.");
        }
        if (attr == GPIO_READBACK) {
            return read_gpio();
        }
        if (!_attr_reg_state.count(attr)) {
            throw uhd::runtime_error("Invalid GPIO attr!");
        }
        // Return the cached value of the requested attr
        return _attr_reg_state.at(attr);
    }

    inline void set_gpio_attr(const gpio_attr_t attr, const uint32_t value) override
    {
        // An attribute based API to configure all settings for the GPIO bus in one
        // function call. This API does not have a mask so it configures all bits at the
        // same time.
        switch (attr) {
            case GPIO_SRC:
                throw uhd::runtime_error("Can't set GPIO source by GPIO ATR interface.");
            case GPIO_CTRL:
                set_atr_mode(
                    MODE_ATR, value); // Configure mode=ATR for all bits that are set
                set_atr_mode(
                    MODE_GPIO, ~value); // Configure mode=GPIO for all bits that are unset
                break;
            case GPIO_DDR:
                set_gpio_ddr(
                    DDR_OUTPUT, value); // Configure as output for all bits that are set
                set_gpio_ddr(
                    DDR_INPUT, ~value); // Configure as input for all bits that are unset
                break;
            case GPIO_OUT:
                // Only set bits that are driven statically
                set_gpio_out(value);
                break;
            case GPIO_ATR_0X:
                // Only set bits that are driven by the ATR engine
                set_atr_reg(ATR_REG_IDLE, value);
                break;
            case GPIO_ATR_RX:
                // Only set bits that are driven by the ATR engine
                set_atr_reg(ATR_REG_RX_ONLY, value);
                break;
            case GPIO_ATR_TX:
                // Only set bits that are driven by the ATR engine
                set_atr_reg(ATR_REG_TX_ONLY, value);
                break;
            case GPIO_ATR_XX:
                // Only set bits that are driven by the ATR engine
                set_atr_reg(ATR_REG_FULL_DUPLEX, value);
                break;
            case GPIO_READBACK:
                // This is readonly register, ignore on set.
                break;
            default:
                UHD_THROW_INVALID_CODE_PATH();
        }
    }

protected:
    class masked_reg_t : public uhd::soft_reg32_wo_t
    {
    public:
        masked_reg_t(const wb_iface::wb_addr_type offset) : uhd::soft_reg32_wo_t(offset)
        {
            uhd::soft_reg32_wo_t::set(REGISTER, 0);
        }

        virtual void set_with_mask(const uint32_t value, const uint32_t mask)
        {
            uhd::soft_reg32_wo_t::set(REGISTER,
                (value & mask) | (uhd::soft_reg32_wo_t::get(REGISTER) & (~mask)));
        }

        virtual uint32_t get()
        {
            return uhd::soft_reg32_wo_t::get(uhd::soft_reg32_wo_t::REGISTER);
        }

        void flush() override
        {
            uhd::soft_reg32_wo_t::flush();
        }
    };

    class atr_idle_reg_t : public masked_reg_t
    {
    public:
        atr_idle_reg_t(const wb_iface::wb_addr_type offset, masked_reg_t& atr_disable_reg)
            : masked_reg_t(offset)
            , _atr_idle_cache(0)
            , _gpio_out_cache(0)
            , _atr_disable_reg(atr_disable_reg)
        {
        }

        void set_with_mask(const uint32_t value, const uint32_t mask) override
        {
            _atr_idle_cache = (value & mask) | (_atr_idle_cache & (~mask));
        }

        uint32_t get() override
        {
            return _atr_idle_cache;
        }

        void set_gpio_out_with_mask(const uint32_t value, const uint32_t mask)
        {
            _gpio_out_cache = (value & mask) | (_gpio_out_cache & (~mask));
        }

        virtual uint32_t get_gpio_out()
        {
            return _gpio_out_cache;
        }

        void flush() override
        {
            set(REGISTER,
                (_atr_idle_cache & (~_atr_disable_reg.get()))
                    | (_gpio_out_cache & _atr_disable_reg.get()));
            masked_reg_t::flush();
        }

    private:
        uint32_t _atr_idle_cache;
        uint32_t _gpio_out_cache;
        masked_reg_t& _atr_disable_reg;
    };

    std::unordered_map<gpio_attr_t, uint32_t, std::hash<size_t>> _attr_reg_state;
    wb_iface::sptr _iface;
    wb_iface::wb_addr_type _rb_addr;
    atr_idle_reg_t _atr_idle_reg;
    masked_reg_t _atr_rx_reg;
    masked_reg_t _atr_tx_reg;
    masked_reg_t _atr_fdx_reg;
    masked_reg_t _ddr_reg;
    masked_reg_t _atr_disable_reg;

    void _update_attr_state(
        const gpio_attr_t attr, const uint32_t val, const uint32_t mask)
    {
        _attr_reg_state[attr] = (_attr_reg_state.at(attr) & ~mask) | (val & mask);
    }
};

gpio_atr_3000::sptr gpio_atr_3000::make(wb_iface::sptr iface, gpio_atr_offsets registers)
{
    gpio_atr_3000::sptr gpio_iface = std::make_shared<gpio_atr_3000_impl>(
        iface, registers);
    if (registers.is_writeonly()) {
        gpio_iface->set_gpio_ddr(DDR_OUTPUT, MASK_SET_ALL);
    }
    gpio_iface->set_gpio_ddr(DDR_OUTPUT, MASK_SET_ALL);
    return gpio_iface;
}

//-------------------------------------------------------------
// db_gpio_atr_3000
//-------------------------------------------------------------

class db_gpio_atr_3000_impl : public gpio_atr_3000_impl, public db_gpio_atr_3000
{
public:
    db_gpio_atr_3000_impl(wb_iface::sptr iface, const gpio_atr_offsets registers)
        : gpio_atr_3000_impl(iface, registers)
    { /* NOP */
    }

    inline void set_pin_ctrl(
        const db_unit_t unit, const uint32_t value, const uint32_t mask) override
    {
        gpio_atr_3000_impl::set_atr_mode(MODE_ATR, compute_mask(unit, value & mask));
        gpio_atr_3000_impl::set_atr_mode(MODE_GPIO, compute_mask(unit, (~value) & mask));
    }

    inline uint32_t get_pin_ctrl(const db_unit_t unit) override
    {
        return (~_atr_disable_reg.get()) >> compute_shift(unit);
    }

    using gpio_atr_3000_impl::set_gpio_ddr;
    inline void set_gpio_ddr(
        const db_unit_t unit, const uint32_t value, const uint32_t mask) override
    {
        gpio_atr_3000_impl::set_gpio_ddr(DDR_OUTPUT, compute_mask(unit, value & mask));
        gpio_atr_3000_impl::set_gpio_ddr(DDR_INPUT, compute_mask(unit, (~value) & mask));
    }

    inline uint32_t get_gpio_ddr(const db_unit_t unit) override
    {
        return _ddr_reg.get() >> compute_shift(unit);
    }

    using gpio_atr_3000_impl::set_atr_reg;
    inline void set_atr_reg(const db_unit_t unit,
        const gpio_atr_reg_t atr,
        const uint32_t value,
        const uint32_t mask) override
    {
        gpio_atr_3000_impl::set_atr_reg(
            atr, value << compute_shift(unit), compute_mask(unit, mask));
    }

    inline uint32_t get_atr_reg(const db_unit_t unit, const gpio_atr_reg_t atr) override
    {
        masked_reg_t* reg = NULL;
        switch (atr) {
            case ATR_REG_IDLE:
                reg = &_atr_idle_reg;
                break;
            case ATR_REG_RX_ONLY:
                reg = &_atr_rx_reg;
                break;
            case ATR_REG_TX_ONLY:
                reg = &_atr_tx_reg;
                break;
            case ATR_REG_FULL_DUPLEX:
                reg = &_atr_fdx_reg;
                break;
            default:
                reg = &_atr_idle_reg;
                break;
        }
        return (reg->get() & compute_mask(unit, MASK_SET_ALL)) >> compute_shift(unit);
    }

    using gpio_atr_3000_impl::set_gpio_out;
    inline void set_gpio_out(
        const db_unit_t unit, const uint32_t value, const uint32_t mask) override
    {
        gpio_atr_3000_impl::set_gpio_out(
            static_cast<uint32_t>(value) << compute_shift(unit),
            compute_mask(unit, mask));
    }

    inline uint32_t get_gpio_out(const db_unit_t unit) override
    {
        return (_atr_idle_reg.get_gpio_out() & compute_mask(unit, MASK_SET_ALL))
               >> compute_shift(unit);
    }

    using gpio_atr_3000_impl::read_gpio;
    inline uint32_t read_gpio(const db_unit_t unit) override
    {
        return (gpio_atr_3000_impl::read_gpio() & compute_mask(unit, MASK_SET_ALL))
               >> compute_shift(unit);
    }

private:
    inline uint32_t compute_shift(const db_unit_t unit)
    {
        switch (unit) {
            case dboard_iface::UNIT_RX:
                return 0;
            case dboard_iface::UNIT_TX:
                return 16;
            default:
                return 0;
        }
    }

    inline uint32_t compute_mask(const db_unit_t unit, const uint32_t mask)
    {
        uint32_t tmp_mask = (unit == dboard_iface::UNIT_BOTH) ? mask : (mask & 0xFFFF);
        return tmp_mask << (compute_shift(unit));
    }
};

db_gpio_atr_3000::sptr db_gpio_atr_3000::make(wb_iface::sptr iface, gpio_atr_offsets registers)
{
    return std::make_shared<db_gpio_atr_3000_impl>(iface, registers);
}

}}} // namespace uhd::usrp::gpio_atr