aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/include/uhdlib/rfnoc/clock_iface.hpp
blob: 1a9f18bd5997c7cb24c317613fe3a8625e76fc92 (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
//
// Copyright 2019 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#ifndef INCLUDED_UHD_RFNOC_CLOCK_IFACE_HPP
#define INCLUDED_UHD_RFNOC_CLOCK_IFACE_HPP

#include <uhd/config.hpp>
#include <uhd/exception.hpp>
#include <uhd/utils/log.hpp>
#include <atomic>
#include <memory>
#include <string>

namespace uhd { namespace rfnoc {

class clock_iface
{
public:
    using sptr = std::shared_ptr<clock_iface>;

    clock_iface(const std::string& name) : _name(name), _is_mutable(true)
    {
        _is_running = false;
        _freq       = 0.0;
    }

    clock_iface(const std::string& name, const double freq, const bool is_mutable = true)
        : _name(name), _freq(freq), _is_mutable(is_mutable)
    {
        _is_running = false;
    }

    clock_iface()                       = delete;
    clock_iface(const clock_iface& rhs) = delete;
    clock_iface(clock_iface&& rhs)      = delete;

    clock_iface& operator=(const clock_iface& fraction) = delete;

    inline const std::string& get_name() const
    {
        return _name;
    }

    inline bool is_running() const
    {
        return _is_running;
    }

    inline bool is_mutable() const
    {
        return _is_mutable;
    }

    inline void set_running(bool is_running)
    {
        _is_running = is_running;
    }

    inline double get_freq() const
    {
        return _freq;
    }

    //! If the clock is immutable, this will throw if freq is different from the
    // current frequency.
    inline void set_freq(double freq)
    {
        if (!_is_mutable && freq != _freq.load()) {
            UHD_LOG_ERROR(_name, "Trying to change an immutable clock!");
            throw uhd::runtime_error("Trying to change an immutable clock!");
        }
        _freq = freq;
    }

private:
    const std::string _name;
    std::atomic<bool> _is_running;
    std::atomic<double> _freq;
    const bool _is_mutable;
};

}} // namespace uhd::rfnoc

#endif /* INCLUDED_UHD_RFNOC_CLOCK_IFACE_HPP */