Skip to content

Commit

Permalink
examples: usage examples of triggered AnalogOut
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Stanea <[email protected]>
  • Loading branch information
Adrian-Stanea committed Jun 13, 2024
1 parent b25609c commit 33a4b06
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/analog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_executable(sync_stream_diff_frequencies "sync_stream_diff_frequencies.cpp")
add_executable(stream_test_adc "stream_test_adc.cpp")
add_executable(stream_test_dac "stream_test_dac.cpp")
add_executable(analog_out_set_voltage "analog_out_set_voltage.cpp")
add_executable(triggered_aout "triggered_aout.cpp")

target_link_libraries(analog_in_out PRIVATE libm2k::libm2k)
target_link_libraries(streaming_one_channel PRIVATE libm2k::libm2k)
Expand All @@ -29,6 +30,7 @@ target_link_libraries(sync_stream_diff_frequencies PRIVATE libm2k::libm2k)
target_link_libraries(stream_test_adc PRIVATE libm2k::libm2k)
target_link_libraries(stream_test_dac PRIVATE libm2k::libm2k)
target_link_libraries(analog_out_set_voltage PRIVATE libm2k::libm2k)
target_link_libraries(triggered_aout PRIVATE libm2k::libm2k)

if (PTHREAD_LIBRARIES)
target_link_libraries(sync_stream_diff_frequencies PRIVATE ${PTHREAD_LIBRARIES})
Expand Down
117 changes: 117 additions & 0 deletions examples/analog/triggered_aout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2024 Analog Devices Inc.
*
* This file is part of libm2k
* (see http://www.github.com/analogdevicesinc/libm2k).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define _USE_MATH_DEFINES
#include <iostream>
#include <thread>
#include <math.h>
#include <libm2k/m2k.hpp>
#include <libm2k/contextbuilder.hpp>
#include <libm2k/analog/m2kpowersupply.hpp>
#include <libm2k/analog/m2kanalogin.hpp>
#include <libm2k/analog/m2kanalogout.hpp>
#include <libm2k/digital/m2kdigital.hpp>

using namespace std;
using namespace libm2k;
using namespace libm2k::analog;
using namespace libm2k::digital;
using namespace libm2k::context;

#define NUM_SAMPLES (1024)

#define CFG_TRIGGER_TI
// #define CFG_TRIGGER_ADC
// #define CFG_TRIGGER_LA

// Buffers are sent one at a time with each trigger event
// #define BUFFER_REARM

std::vector<double> generate_square_wave(int num_samples) {
std::vector<double> samples(num_samples);
for (int i = 0; i < num_samples; i++) {
samples[i] = (i % 2 == 0) ? 1.0 : -1.0;
}
return samples;
}

int main(int argc, char* argv[])
{
M2k *ctx = m2kOpen("ip:192.168.2.1");
if (!ctx) {
std::cout << "Connection Error: No ADALM2000 device available/connected to your PC." << std::endl;
return 1;
} else {
std::cout << "Connection Success: ADALM2000 device connected." << std::endl;
}

M2kAnalogIn *ain = ctx->getAnalogIn();
M2kAnalogOut *aout = ctx->getAnalogOut();
M2kDigital *dig = ctx->getDigital();
M2kHardwareTrigger *trig = aout->getTrigger();

ctx->reset();
ctx->calibrateADC();
ctx->calibrateDAC();

#ifdef BUFFER_REARM
aout->setBufferRearmOnTrigger(true);
#endif

#ifdef CFG_TRIGGER_TI
trig->setAnalogOutTriggerSource(TRIGGER_TI);
trig->setAnalogOutTriggerCondition(RISING_EDGE_OUT);
trig->setAnalogOutTriggerStatus(START);
#endif

#ifdef CFG_TRIGGER_ADC
ain->setSampleRate(ain->getMaximumSamplerate()); // fastest response
trig->setAnalogSource(CHANNEL_1);
trig->setAnalogCondition(CHANNEL_1, RISING_EDGE_ANALOG);
trig->setAnalogLevel(CHANNEL_1, 0.5);
trig->setAnalogDelay(-100);
trig->setAnalogMode(CHANNEL_1, ANALOG);

trig->setAnalogOutTriggerSource(TRIGGER_ADC);
trig->setAnalogOutTriggerStatus(START);
#endif

#ifdef CFG_TRIGGER_LA
dig->setSampleRateIn(75000000); // fastest response
dig->setDirection(DIO_CHANNEL_0, DIO_INPUT);
dig->enableChannel(DIO_CHANNEL_0, true);
dig->setOutputMode(DIO_CHANNEL_0, DIO_PUSHPULL);
trig->setDigitalCondition(DIO_CHANNEL_0, RISING_EDGE_DIGITAL);

trig->setAnalogOutTriggerSource(TRIGGER_LA);
trig->setAnalogOutTriggerStatus(START);
#endif

// Setup analog output
aout->setSampleRate(0, aout->getMaximumSamplerate(0));
aout->enableChannel(0, true);
aout->setCyclic(true);

auto waveform = generate_square_wave(NUM_SAMPLES);
aout->push({waveform, waveform});

contextClose(ctx);
return 0;
}

0 comments on commit 33a4b06

Please sign in to comment.