SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
PlutoTX.hxx
Go to the documentation of this file.
1#pragma once
2/*
3Copyright (c) 2026 Matthew H. Reilly (kb1vc)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
50
51#include "RadioTX.hxx"
52#include "SoDaBase.hxx"
53#include "Params.hxx"
54#include <SoDa/ReSampler.hxx>
55#include <iio.h>
56#include <memory>
57#include <vector>
58#include <complex>
59#include <thread>
60#include <mutex>
61#include <condition_variable>
62#include <deque>
63
64namespace SoDa {
65 class PlutoTX;
66 typedef std::shared_ptr<PlutoTX> PlutoTXPtr;
67
68 class PlutoTX : public RadioTX {
69 protected:
71
72 public:
74
75 static PlutoTXPtr make(ParamsPtr params) {
76 auto ret = std::shared_ptr<PlutoTX>(new PlutoTX(params));
77 ret->self = ret;
78 ret->registerThread(ret);
79 return ret;
80 }
81
82 void init() override;
83
90 bool transmitSwitch(bool tx_on) override;
91
97 bool put(CBufPtr buf) override;
98
99 RadioTXPtr getSelfPtr() override { return self.lock(); }
100
101 private:
102 void pushToHW(const std::vector<std::complex<float>> & block);
105 void enqueue(std::vector<std::complex<float>> block);
106
107 // Kernel DMA slots; IO thread pre-fills (N_KERNEL_BUFS-1) on TX arm.
108 // 120000 samples / 2.5 MSPS = 48 ms each → 4 × 48 ms = 192 ms headroom.
109 static constexpr unsigned int N_KERNEL_BUFS = 5;
110 // Software queue cap: prevents RadioTX from flooding the IO thread.
111 static constexpr unsigned int MAX_SW_QUEUE = 16;
112
113 // IIO streaming objects — owned by the IO thread after init().
114 iio_context * ctx;
115 iio_device * dev;
116 iio_channel * tx_i_chan;
117 iio_channel * tx_q_chan;
118 iio_buffer * txbuf;
119
120 unsigned int hw_buf_size;
121 unsigned int rs_in_size;
122
123 SoDa::ReSamplerPtr hw_resampler;
124
125 std::vector<std::complex<float>> accu;
126 std::vector<std::complex<float>> rs_in;
127 std::vector<std::complex<float>> hw_cf;
128
129 std::weak_ptr<PlutoTX> self;
130
131 // Async IO: dedicated thread drains tx_queue via iio_buffer_push() so
132 // RadioTX::put() never blocks on USB scheduling jitter.
133 std::thread io_thread;
134 std::mutex tx_mutex;
135 std::condition_variable tx_cv;
136 std::deque<std::vector<std::complex<float>>> tx_queue;
137 bool io_running{false};
138 };
139}
The Baseclass for all SoDa objects, and useful commonly used classes.
bool transmitSwitch(bool tx_on) override
Arm or disarm the IIO TX stream.
static PlutoTXPtr make(ParamsPtr params)
Definition PlutoTX.hxx:75
std::vector< std::complex< float > > hw_cf
resampler output (2.5 MSPS)
Definition PlutoTX.hxx:127
std::weak_ptr< PlutoTX > self
Definition PlutoTX.hxx:129
unsigned int rs_in_size
Definition PlutoTX.hxx:121
static constexpr unsigned int N_KERNEL_BUFS
Definition PlutoTX.hxx:109
std::condition_variable tx_cv
Definition PlutoTX.hxx:135
iio_channel * tx_q_chan
Definition PlutoTX.hxx:117
std::thread io_thread
Definition PlutoTX.hxx:133
void pushToHW(const std::vector< std::complex< float > > &block)
PlutoTX(ParamsPtr params)
bool put(CBufPtr buf) override
Accumulate complex<float> samples; when a full resampler block is available, interpolate 4:1 and push...
iio_device * dev
Definition PlutoTX.hxx:115
iio_context * ctx
Definition PlutoTX.hxx:114
void ioThreadFn()
std::deque< std::vector< std::complex< float > > > tx_queue
Definition PlutoTX.hxx:136
unsigned int hw_buf_size
Definition PlutoTX.hxx:120
iio_buffer * txbuf
Definition PlutoTX.hxx:118
void enqueue(std::vector< std::complex< float > > block)
RadioTXPtr getSelfPtr() override
Definition PlutoTX.hxx:99
iio_channel * tx_i_chan
Definition PlutoTX.hxx:116
std::mutex tx_mutex
Definition PlutoTX.hxx:134
SoDa::ReSamplerPtr hw_resampler
Definition PlutoTX.hxx:123
void flushZeros()
void init() override
Some initialization must occurr after all the components are created.
std::vector< std::complex< float > > accu
625 kSPS accumulator
Definition PlutoTX.hxx:125
std::vector< std::complex< float > > rs_in
resampler input block
Definition PlutoTX.hxx:126
static constexpr unsigned int MAX_SW_QUEUE
Definition PlutoTX.hxx:111
RadioTX(ParamsPtr params, const std::string &name)
Constructor for RF Transmit/modulator process.
std::shared_ptr< PlutoTX > PlutoTXPtr
Definition PlutoTX.hxx:66
std::shared_ptr< Params > ParamsPtr
Definition Params.hxx:42
std::shared_ptr< CBuf > CBufPtr
Definition SoDaBase.hxx:64
std::shared_ptr< RadioTX > RadioTXPtr
Definition RadioTX.hxx:57