aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/e300/e300_sensor_manager.cpp
blob: 527cfb91a44171052c5ae36a4f22fe290584f084 (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
//
// Copyright 2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "e300_sensor_manager.hpp"

#include <boost/thread.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/format.hpp>

#include <cstring>
#include <uhd/exception.hpp>
#include <uhd/utils/byteswap.hpp>
#include <uhd/usrp/gps_ctrl.hpp>

namespace uhd { namespace usrp { namespace e300 {

class e300_sensor_proxy : public e300_sensor_manager
{
public:
    e300_sensor_proxy(
        uhd::transport::zero_copy_if::sptr xport) : _xport(xport)
    {
    }

    std::vector<std::string> get_sensors()
    {
        return boost::assign::list_of("temp")("gps_locked")("gps_time")("ref_locked");
    }

    uhd::sensor_value_t get_sensor(const std::string &key)
    {
        if (key == "temp")
            return get_mb_temp();
        else if (key == "gps_locked")
            return get_gps_lock();
        else if (key == "gps_time")
            return get_gps_time();
        else if (key == "ref_locked")
            return get_ref_lock();
        else
            throw uhd::lookup_error(
                str(boost::format("Invalid sensor %s requested.") % key));
    }

    uhd::sensor_value_t get_mb_temp(void)
    {
        boost::mutex::scoped_lock(_mutex);
        sensor_transaction_t transaction;
        transaction.which = uhd::htonx<boost::uint32_t>(ZYNQ_TEMP);
        {
            uhd::transport::managed_send_buffer::sptr buff
                = _xport->get_send_buff(1.0);
            if (not buff or buff->size() < sizeof(transaction)) {
                throw uhd::runtime_error("sensor proxy send timeout");
            }
            std::memcpy(
                buff->cast<void *>(),
                &transaction,
                sizeof(transaction));
            buff->commit(sizeof(transaction));
        }
        {
            uhd::transport::managed_recv_buffer::sptr buff
                = _xport->get_recv_buff(1.0);

            if (not buff or buff->size() < sizeof(transaction))
                throw uhd::runtime_error("sensor proxy recv timeout");

            std::memcpy(
                &transaction,
                buff->cast<const void *>(),
                sizeof(transaction));
        }
        UHD_ASSERT_THROW(uhd::ntohx<boost::uint32_t>(transaction.which) == ZYNQ_TEMP);
        // TODO: Use proper serialization here ...
        return sensor_value_t(
            "temp",
            e300_sensor_manager::unpack_float_from_uint32_t(
                uhd::ntohx(transaction.value)),
            "C");
    }

    uhd::sensor_value_t get_gps_time(void)
    {
        boost::mutex::scoped_lock(_mutex);
        sensor_transaction_t transaction;
        transaction.which = uhd::htonx<boost::uint32_t>(GPS_TIME);
        {
            uhd::transport::managed_send_buffer::sptr buff
                = _xport->get_send_buff(1.0);
            if (not buff or buff->size() < sizeof(transaction)) {
                throw uhd::runtime_error("sensor proxy send timeout");
            }
            std::memcpy(
                buff->cast<void *>(),
                &transaction,
                sizeof(transaction));
            buff->commit(sizeof(transaction));
        }
        {
            uhd::transport::managed_recv_buffer::sptr buff
                = _xport->get_recv_buff(1.0);

            if (not buff or buff->size() < sizeof(transaction))
                throw uhd::runtime_error("sensor proxy recv timeout");

            std::memcpy(
                &transaction,
                buff->cast<const void *>(),
                sizeof(transaction));
        }
        UHD_ASSERT_THROW(uhd::ntohx<boost::uint32_t>(transaction.which) == GPS_TIME);
        // TODO: Use proper serialization here ...
        return sensor_value_t("GPS epoch time", int(uhd::ntohx<boost::uint32_t>(transaction.value)), "seconds");
    }

    bool get_gps_found(void)
    {
        boost::mutex::scoped_lock(_mutex);
        sensor_transaction_t transaction;
        transaction.which = uhd::htonx<boost::uint32_t>(GPS_FOUND);
        {
            uhd::transport::managed_send_buffer::sptr buff
                = _xport->get_send_buff(1.0);
            if (not buff or buff->size() < sizeof(transaction)) {
                throw uhd::runtime_error("sensor proxy send timeout");
            }
            std::memcpy(
                buff->cast<void *>(),
                &transaction,
                sizeof(transaction));
            buff->commit(sizeof(transaction));
        }
        {
            uhd::transport::managed_recv_buffer::sptr buff
                = _xport->get_recv_buff(1.0);

            if (not buff or buff->size() < sizeof(transaction))
                throw uhd::runtime_error("sensor proxy recv timeout");

            std::memcpy(
                &transaction,
                buff->cast<const void *>(),
                sizeof(transaction));
        }
        UHD_ASSERT_THROW(uhd::ntohx<boost::uint32_t>(transaction.which) == GPS_FOUND);
        // TODO: Use proper serialization here ...
        return (uhd::ntohx(transaction.value) > 0);
    }

    uhd::sensor_value_t get_gps_lock(void)
    {
        boost::mutex::scoped_lock(_mutex);
        sensor_transaction_t transaction;
        transaction.which = uhd::htonx<boost::uint32_t>(GPS_LOCK);
        {
            uhd::transport::managed_send_buffer::sptr buff
                = _xport->get_send_buff(1.0);
            if (not buff or buff->size() < sizeof(transaction)) {
                throw uhd::runtime_error("sensor proxy send timeout");
            }
            std::memcpy(
                buff->cast<void *>(),
                &transaction,
                sizeof(transaction));
            buff->commit(sizeof(transaction));
        }
        {
            uhd::transport::managed_recv_buffer::sptr buff
                = _xport->get_recv_buff(1.0);

            if (not buff or buff->size() < sizeof(transaction))
                throw uhd::runtime_error("sensor proxy recv timeout");

            std::memcpy(
                &transaction,
                buff->cast<const void *>(),
                sizeof(transaction));
        }
        UHD_ASSERT_THROW(uhd::ntohx<boost::uint32_t>(transaction.which) == GPS_LOCK);
        // TODO: Use proper serialization here ...
        return sensor_value_t("GPS lock status", (uhd::ntohx(transaction.value) > 0), "locked", "unlocked");
    }

    uhd::sensor_value_t get_ref_lock(void)
    {
        boost::mutex::scoped_lock(_mutex);
        sensor_transaction_t transaction;
        transaction.which = uhd::htonx<boost::uint32_t>(REF_LOCK);
        {
            uhd::transport::managed_send_buffer::sptr buff
                = _xport->get_send_buff(1.0);
            if (not buff or buff->size() < sizeof(transaction)) {
                throw uhd::runtime_error("sensor proxy send timeout");
            }
            std::memcpy(
                buff->cast<void *>(),
                &transaction,
                sizeof(transaction));
            buff->commit(sizeof(transaction));
        }
        {
            uhd::transport::managed_recv_buffer::sptr buff
                = _xport->get_recv_buff(1.0);

            if (not buff or buff->size() < sizeof(transaction))
                throw uhd::runtime_error("sensor proxy recv timeout");

            std::memcpy(
                &transaction,
                buff->cast<const void *>(),
                sizeof(transaction));
        }
        UHD_ASSERT_THROW(uhd::ntohx<boost::uint32_t>(transaction.which) == REF_LOCK);
        // TODO: Use proper serialization here ...
        return sensor_value_t("Ref", (uhd::ntohx(transaction.value) > 0), "locked", "unlocked");
    }

private:
    uhd::transport::zero_copy_if::sptr _xport;
    boost::mutex                       _mutex;
};

}}} // namespace

using namespace uhd::usrp::e300;

e300_sensor_manager::sptr e300_sensor_manager::make_proxy(
    uhd::transport::zero_copy_if::sptr xport)
{
    return sptr(new e300_sensor_proxy(xport));
}

#ifdef E300_NATIVE
#include "e300_fifo_config.hpp"

namespace uhd { namespace usrp { namespace e300 {

static const std::string E300_TEMP_SYSFS = "iio:device0";

class e300_sensor_local : public e300_sensor_manager
{
public:
    e300_sensor_local(uhd::gps_ctrl::sptr gps_ctrl, global_regs::sptr global_regs) :
        _gps_ctrl(gps_ctrl), _global_regs(global_regs)
    {
    }

    std::vector<std::string> get_sensors()
    {
        return boost::assign::list_of("temp")("gps_locked")("gps_time")("ref_locked");
    }

    uhd::sensor_value_t get_sensor(const std::string &key)
    {
        if (key == "temp")
            return get_mb_temp();
        else if (key == "gps_locked")
            return get_gps_lock();
        else if (key == "gps_time")
            return get_gps_time();
        else if (key == "ref_locked")
            return get_ref_lock();
        else
            throw uhd::lookup_error(
                str(boost::format("Invalid sensor %s requested.") % key));
    }

    uhd::sensor_value_t get_mb_temp(void)
    {
        double scale = boost::lexical_cast<double>(
            e300_get_sysfs_attr(E300_TEMP_SYSFS, "in_temp0_scale"));
        unsigned long raw = boost::lexical_cast<unsigned long>(
            e300_get_sysfs_attr(E300_TEMP_SYSFS, "in_temp0_raw"));
        unsigned long offset = boost::lexical_cast<unsigned long>(
            e300_get_sysfs_attr(E300_TEMP_SYSFS, "in_temp0_offset"));
        return sensor_value_t("temp", (raw + offset) * scale / 1000, "C");
    }

    bool get_gps_found(void)
    {
        return _gps_ctrl->gps_detected();
    }

    uhd::sensor_value_t get_gps_lock(void)
    {
        return _gps_ctrl->get_sensor("gps_locked");
    }

    uhd::sensor_value_t get_gps_time(void)
    {
        return _gps_ctrl->get_sensor("gps_time");
    }

    uhd::sensor_value_t get_ref_lock(void)
    {
        //PPSLOOP_LOCKED_MASK is asserted in the following cases:
        //- (Time source = GPS or External) AND (Loop is locked and is in fine adj mode)
        //- Time source is Internal
        static const boost::uint32_t PPSLOOP_LOCKED_MASK = 0x04;
        static const boost::uint32_t REFPLL_LOCKED_MASK = 0x20;

        const boost::uint32_t status =
            _global_regs->peek32(global_regs::RB32_CORE_MISC);
        bool ref_locked = (status & PPSLOOP_LOCKED_MASK) && (status & REFPLL_LOCKED_MASK);

        return sensor_value_t("Ref", ref_locked, "locked", "unlocked");
    }

private:
    gps_ctrl::sptr      _gps_ctrl;
    global_regs::sptr   _global_regs;
};
}}}

using namespace uhd::usrp::e300;
e300_sensor_manager::sptr e300_sensor_manager::make_local(
    uhd::gps_ctrl::sptr gps_ctrl, global_regs::sptr global_regs)
{
    return sptr(new e300_sensor_local(gps_ctrl, global_regs));
}

#else
using namespace uhd::usrp::e300;
e300_sensor_manager::sptr e300_sensor_manager::make_local(
    uhd::gps_ctrl::sptr, global_regs::sptr)
{
    throw uhd::assertion_error("e300_sensor_manager::make_local() !E300_NATIVE");
}
#endif