Skip to content

Commit 6cf468e

Browse files
Teo PerisanuTeo Perisanu
authored andcommitted
M2kHardwareTrigger: Add initial structure for trigger out.
Both DACs and pattern generator share common functionalities. These features will be available in a future firmware version, so we have separated these from the others, in a new class - M2kHwTrigger_v0.26. Signed-off-by: Teo Perisanu <[email protected]>
1 parent ccd324a commit 6cf468e

File tree

4 files changed

+188
-3
lines changed

4 files changed

+188
-3
lines changed

include/libm2k/enums.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ namespace libm2k {
176176
};
177177

178178

179+
/**
180+
* @enum M2K_TRIGGER_OUT_SOURCE
181+
* @brief Select the triggering source for a DeviceOut
182+
*/
183+
enum M2K_TRIGGER_OUT_SOURCE {
184+
SRC_OUT_NONE = 0, ///< SRC_OUT_NONE - no trigger events
185+
SRC_OUT_TRIGGER_IN = 1, ///< SRC_OUT_TRIGGER_IN - trigger events on the TI(trigger in) pin trigger the Output interface
186+
SRC_OUT_TRIGGER_OUT = 2, ///< SRC_OUT_TRIGGER_OUT - trigger events on the TO(trigger out) pin trigger the Output interface
187+
SRC_OUT_ANALOG_IN = 3, ///< SRC_OUT_ANALOG_IN - trigger events on the AnalogIn interface trigger the Output interface
188+
SRC_OUT_DIGITAL_IN = 4, ///< SRC_OUT_DIGITAL_IN - trigger events on the DigitalIn interface trigger the Output interface
189+
};
190+
191+
179192
/**
180193
* @struct SETTINGS
181194
* @brief Triggering system

src/m2k_impl.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <digital/m2kdigital_impl.hpp>
2727
#include "m2khardwaretrigger_impl.hpp"
2828
#include "m2khardwaretrigger_v0.24_impl.hpp"
29+
#include "m2khardwaretrigger_v0.26_impl.hpp"
2930
#include "m2kcalibration_impl.hpp"
3031
#include <libm2k/analog/dmm.hpp>
3132
#include "utils/channel.hpp"
@@ -69,11 +70,16 @@ M2kImpl::M2kImpl(std::string uri, iio_context* ctx, std::string name, bool sync)
6970

7071
m_firmware_version = getFirmwareVersion();
7172

72-
int diff = Utils::compareVersions(m_firmware_version, "v0.24");
73-
if (diff < 0) { //m_firmware_version < 0.24
73+
int diff_v024 = Utils::compareVersions(m_firmware_version, "v0.24");
74+
if (diff_v024 < 0) { //m_firmware_version < 0.24
7475
m_trigger = new M2kHardwareTriggerImpl(ctx);
7576
} else {
76-
m_trigger = new M2kHardwareTriggerV024Impl(ctx);
77+
int diff_v026 = Utils::compareVersions(m_firmware_version, "v0.26");
78+
if (diff_v026 < 0) { // firmware version 0.24, 0.25
79+
m_trigger = new M2kHardwareTriggerV024Impl(ctx);
80+
} else { //firmware version >= 0.26
81+
m_trigger = new M2kHardwareTriggerV026Impl(ctx);
82+
}
7783
}
7884

7985
if (!m_trigger) {

src/m2khardwaretrigger_v0.26_impl.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2020 Analog Devices Inc.
3+
*
4+
* This file is part of libm2k
5+
* (see http://www.github.com/analogdevicesinc/libm2k).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 2.1 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#include "m2khardwaretrigger_v0.26_impl.hpp"
23+
#include <libm2k/m2kexceptions.hpp>
24+
#include <algorithm>
25+
26+
27+
const std::string libm2k::M2kHardwareTriggerV026Impl::m_source_attr = "trigger_src";
28+
const std::string libm2k::M2kHardwareTriggerV026Impl::m_condition_attr = "trigger_condition";
29+
30+
31+
std::vector<std::string> libm2k::M2kHardwareTriggerV026Impl::m_trigger_source = {
32+
"none",
33+
"trigger_i_0",
34+
"trigger_i_1",
35+
"trigger-adc",
36+
"trigger-la",
37+
};
38+
39+
std::vector<std::string> libm2k::M2kHardwareTriggerV026Impl::m_trigger_cond = {
40+
"edge-rising",
41+
"edge-falling",
42+
"level-low",
43+
"level-high",
44+
"edge-any",
45+
"none"
46+
};
47+
48+
libm2k::M2kHardwareTriggerV026Impl::M2kHardwareTriggerV026Impl(struct iio_context *ctx, bool init) :
49+
M2kHardwareTriggerV024Impl(ctx)
50+
{
51+
if (init) {
52+
reset();
53+
}
54+
}
55+
56+
void libm2k::M2kHardwareTriggerV026Impl::reset()
57+
{
58+
M2kHardwareTriggerV024Impl::reset();
59+
}
60+
61+
void libm2k::M2kHardwareTriggerV026Impl::setTriggerOutSource(libm2k::M2K_TRIGGER_OUT_SOURCE src,
62+
const std::shared_ptr<libm2k::utils::DeviceOut>& device)
63+
{
64+
if (src >= m_trigger_source.size()) {
65+
THROW_M2K_EXCEPTION("M2kHardwareTrigger: "
66+
"the provided source is not supported on "
67+
"the current board; Check the firmware version.",
68+
libm2k::EXC_INVALID_PARAMETER);
69+
}
70+
std::string src_str = m_trigger_source[src];
71+
device->setStringValue(m_source_attr, src_str);
72+
}
73+
74+
libm2k::M2K_TRIGGER_OUT_SOURCE
75+
libm2k::M2kHardwareTriggerV026Impl::getTriggerOutSource(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const
76+
{
77+
std::string buf = device->getStringValue(m_source_attr);
78+
79+
auto it = std::find(m_trigger_source.begin(),
80+
m_trigger_source.end(), buf.c_str());
81+
if (it == m_trigger_source.end()) {
82+
THROW_M2K_EXCEPTION("Unexpected value read from attribute: " + m_source_attr, libm2k::EXC_INVALID_PARAMETER);
83+
}
84+
85+
return static_cast<libm2k::M2K_TRIGGER_OUT_SOURCE>(it - m_trigger_source.begin());
86+
}
87+
88+
void libm2k::M2kHardwareTriggerV026Impl::setTriggerOutCondition(
89+
libm2k::M2K_TRIGGER_CONDITION_DIGITAL cond, const std::shared_ptr<libm2k::utils::DeviceOut>& device)
90+
{
91+
if (cond >= m_trigger_cond.size()) {
92+
THROW_M2K_EXCEPTION("M2kHardwareTrigger: "
93+
"the provided condition is not supported on "
94+
"the current board; Check the firmware version.",
95+
libm2k::EXC_INVALID_PARAMETER);
96+
}
97+
std::string src_str = m_trigger_cond[cond];
98+
device->setStringValue(m_condition_attr, src_str);
99+
}
100+
101+
libm2k::M2K_TRIGGER_CONDITION_DIGITAL
102+
libm2k::M2kHardwareTriggerV026Impl::getTriggerOutCondition(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const
103+
{
104+
std::string buf = device->getStringValue(m_condition_attr);
105+
106+
auto it = std::find(m_trigger_cond.begin(),
107+
m_trigger_cond.end(), buf.c_str());
108+
if (it == m_trigger_cond.end()) {
109+
THROW_M2K_EXCEPTION("Unexpected value read from attribute: " + m_condition_attr, libm2k::EXC_INVALID_PARAMETER);
110+
}
111+
112+
return static_cast<libm2k::M2K_TRIGGER_CONDITION_DIGITAL>(it - m_trigger_cond.begin());
113+
}

src/m2khardwaretrigger_v0.26_impl.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2020 Analog Devices Inc.
3+
*
4+
* This file is part of libm2k
5+
* (see http://www.github.com/analogdevicesinc/libm2k).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 2.1 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef M2KOUTHARDWARETRIGGER_IMPL_HPP
23+
#define M2KOUTHARDWARETRIGGER_IMPL_HPP
24+
25+
#include "m2khardwaretrigger_v0.24_impl.hpp"
26+
#include "utils/deviceout.hpp"
27+
28+
29+
namespace libm2k {
30+
31+
class M2kHardwareTriggerV026Impl : public M2kHardwareTriggerV024Impl {
32+
public:
33+
explicit M2kHardwareTriggerV026Impl(struct iio_context *ctx, bool init = true);
34+
35+
~M2kHardwareTriggerV026Impl() override = default;
36+
37+
void reset() override;
38+
39+
protected:
40+
void setTriggerOutSource(M2K_TRIGGER_OUT_SOURCE src, const std::shared_ptr<libm2k::utils::DeviceOut>& device);
41+
M2K_TRIGGER_OUT_SOURCE getTriggerOutSource(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const;
42+
void setTriggerOutCondition(M2K_TRIGGER_CONDITION_DIGITAL cond, const std::shared_ptr<libm2k::utils::DeviceOut>& device);
43+
M2K_TRIGGER_CONDITION_DIGITAL getTriggerOutCondition(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const;
44+
45+
static std::vector<std::string> m_trigger_source;
46+
static std::vector<std::string> m_trigger_cond;
47+
48+
static const std::string m_source_attr;
49+
static const std::string m_condition_attr;
50+
};
51+
}
52+
53+
#endif //M2KOUTHARDWARETRIGGER_IMPL_HPP

0 commit comments

Comments
 (0)