From 08efeeccaf260f03ad8b7e797361a143c0c14e67 Mon Sep 17 00:00:00 2001 From: Adrian Stanea Date: Thu, 25 Apr 2024 14:15:53 +0300 Subject: [PATCH] examples: usage examples of triggered AnalogOut Signed-off-by: Adrian Stanea --- examples/analog/CMakeLists.txt | 2 + examples/analog/triggered_aout.cpp | 117 +++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 examples/analog/triggered_aout.cpp diff --git a/examples/analog/CMakeLists.txt b/examples/analog/CMakeLists.txt index 26f31e4b..636b45c0 100644 --- a/examples/analog/CMakeLists.txt +++ b/examples/analog/CMakeLists.txt @@ -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) @@ -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}) diff --git a/examples/analog/triggered_aout.cpp b/examples/analog/triggered_aout.cpp new file mode 100644 index 00000000..57a62abd --- /dev/null +++ b/examples/analog/triggered_aout.cpp @@ -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 . + * + */ +#define _USE_MATH_DEFINES +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 generate_square_wave(int num_samples) { + std::vector 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; +}