1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::;
/// A HackRF operating in transmit mode.
///
/// To send data, first take a HackRF peripheral and call
/// [`HackRf::start_tx`], or use [`Transmit::new`] with it. Provide the maximum
/// block transfer size, in samples, that will be used for the duration of the
/// transmit.
///
/// Next, call [`get_buffer`][Transmit::get_buffer] to allocate a buffer, and
/// then fill that buffer with the next block of samples to transmit, up to the
/// maximum block transfer size chosen. Set up around half a million samples
/// worth of buffers before submitting any, ideally, or gaps in the transmit
/// sequence may occur.
///
/// Next, start calling [`submit`][Transmit::submit] to submit the blocks.
/// Continue the buffer filling and submitting process as long as needed,
/// preferrably pausing with [`next_complete`][Transmit::next_complete] when
/// there a more than half a million samples worth of buffers queued up. The
/// number of queued buffers can be checked with [`pending`][Transmit::pending].
/// Once buffers are queued up, the process of completing, grabbing a buffer,
/// and submitting it in a loop until finished.
///
/// When all buffers have been submitted, call [`flush`][Transmit::flush] to
/// queue up a final set of zero-filled buffers to flush out any remaining data
/// in the HackRF's internal buffers. Then continue calling
/// [`next_complete`][Transmit::next_complete] until
/// [`pending`][Transmit::pending] returns 0. Finally, exit transmit mode with
/// [`stop`][Transmit::stop]. Transmit mode can be exited without doing this
/// sequence, but not all samples queued up will necessarily be sent otherwise.
///
/// Putting it all together, here's an example program that transmits all the
/// data in a file:
///
/// ```no_run
/// use anyhow::Result;
/// use tokio::io::AsyncReadExt;
/// use waverave_hackrf::Buffer;
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let hackrf = waverave_hackrf::open_hackrf()?;
///
/// // Configure: 20MHz sample rate, turn on RF amp, set TX IF gain to 16 dB,
/// // and tune to 915 MHz.
/// hackrf.set_sample_rate(20e6).await?;
/// hackrf.set_txvga_gain(16).await?;
/// hackrf.set_freq(915_000_000).await?;
/// hackrf.set_amp_enable(true).await?;
///
/// // Open up a file for buffered reading.
/// let mut args = std::env::args();
/// args.next();
/// let file_name = args.next().unwrap_or_else(|| String::from("./tx.bin"));
/// let mut file = tokio::fs::File::open(&file_name).await?;
///
/// // Start transmitting, in bursts of 8192 samples
/// let mut hackrf_tx = hackrf.start_tx(8192).await.map_err(|e| e.err)?;
///
/// // Set up an asynchronous process that fills buffers and sends them on to
/// // the transmitter.
/// let (buf_send, mut buf_recv) = tokio::sync::mpsc::channel::<Buffer>(4);
/// let (data_send, mut data_recv) = tokio::sync::mpsc::channel::<Buffer>(4);
/// tokio::spawn(async move {
/// loop {
/// let Some(mut buf) = buf_recv.recv().await else {
/// break;
/// };
/// buf.extend_zeros(buf.remaining_capacity());
/// file.read_exact(buf.bytes_mut()).await?;
/// let Ok(_) = data_send.send(buf).await else {
/// break;
/// };
/// }
/// Ok::<(), anyhow::Error>(())
/// });
///
/// // Start filling up queue
/// let mut start = Vec::with_capacity(64);
/// while start.len() < 64 {
/// while buf_send.try_send(hackrf_tx.get_buffer()).is_ok() {}
/// let Some(buf) = data_recv.recv().await else {
/// break;
/// };
/// start.push(buf);
/// }
///
/// // Submit the whole starting queue in one go
/// for buf in start {
/// hackrf_tx.submit(buf);
/// }
///
/// // Continue filling the queue and submitting samples as often as possible
/// loop {
/// while buf_send.try_send(hackrf_tx.get_buffer()).is_ok() {}
/// hackrf_tx.next_complete().await?;
/// let Some(buf) = data_recv.recv().await else {
/// break;
/// };
/// hackrf_tx.submit(buf);
/// }
///
/// // Flush the remainder
/// hackrf_tx.flush();
/// while hackrf_tx.pending() > 0 {
/// hackrf_tx.next_complete().await?;
/// }
///
/// // Stop transmitting
/// hackrf_tx.stop().await?;
///
/// Ok(())
/// }
/// ```