From 0f11a2bc9b22a1362245459ae45ae4f86cb9374e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 5 Sep 2023 10:11:39 +0200 Subject: Dexter: make channel_down wait for underflow to occur --- src/output/Dexter.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/output') diff --git a/src/output/Dexter.cpp b/src/output/Dexter.cpp index 8e2a6c5..26472e8 100644 --- a/src/output/Dexter.cpp +++ b/src/output/Dexter.cpp @@ -187,11 +187,33 @@ void Dexter::channel_down() etiLog.level(error) << "Failed to set dexter_dsp_tx.gain0 = 0: " << get_iio_error(r); } - // This will flush out the FIFO + // Setting stream0_start_clocks to 0 will flush out the FIFO, but we need to wait a bit before + // we "up" the channel again if ((r = iio_device_attr_write_longlong(m_dexter_dsp_tx, "stream0_start_clks", 0)) != 0) { etiLog.level(warn) << "Failed to set dexter_dsp_tx.stream0_start_clks = 0 : " << get_iio_error(r); } + long long underflows_old = 0; + + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &underflows_old)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.buffer_underflows0 : " << get_iio_error(r); + } + + long long underflows = underflows_old; + + // Limiting to 10*96ms is just a safety to avoid running into an infinite loop + for (size_t i = 0; underflows == underflows_old && i < 10; i++) { + if ((r = iio_device_attr_read_longlong(m_dexter_dsp_tx, "buffer_underflows0", &underflows)) != 0) { + etiLog.level(warn) << "Failed to read dexter_dsp_tx.buffer_underflows0 : " << get_iio_error(r); + } + + this_thread::sleep_for(chrono::milliseconds(96)); + } + + if (underflows == underflows_old) { + etiLog.level(warn) << "DEXTER CHANNEL_DOWN, no underflow detected! " << underflows; + } + m_channel_is_up = false; etiLog.level(debug) << "DEXTER CHANNEL_DOWN"; } -- cgit v1.2.3