Debian Codegraph @

hdl/fpga/platforms/bladerf-micro/software/foxhunt/src/main.c

raw symbols SCIP
1 /* This file is part of the bladeRF project:
2 * http://www.github.com/nuand/bladeRF
3 *
4 * Copyright (c) 2018 Nuand LLC
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "foxhunt.h"
32 #include "morse.h"
33
34 #include "debug.h"
35 #include "devices.h"
36 #include "devices_rfic.h"
37
38 #define BLADERF_DEVICE_NAME "Foxhunt for the Nuand bladeRF 2.0 Micro"
39
40 #define SAMPLERATE 768000
41
42 #define MAX_MESSAGE_LENGTH 128
43 #define MAX_TONE_COUNT 128
44
45 // uncomment to enable bias tee power (e.g. for BT-100)
46 #define BIASTEE 1
47
48 // Check for buildability
49 #ifndef TONE_GENERATOR_0_BASE
50 #error tone generator not present
51 #endif
52
53 /**
54 * @brief Error-catching wrapper
55 *
56 * @param _fn The function to wrap
57 *
58 * @return returns 1 on failure, continues if successful
59 */
60 #define CALL(_fn) \
61 do { \
62 if (!_fn) { \
63 DBG("*** FAILED: %s() @ %s:0x%x\n", __FUNCTION__, __FILE__, \
64 __LINE__); \
65 return 1; \
66 } \
67 } while (0)
68
69 bool const REPEAT_MODE = true;
70
71 uint32_t const INTER_MESSAGE_GAP = 10; /* seconds */
72
73 struct message const MESSAGES[] = {
74 {
75 /* This should be exactly 100 dot lengths */
76 .text = "PARIS PARIS",
77 .wpm = 5,
78 .tone = 1000,
79 .carrier = 915000000,
80 .backoff_dB = 10,
81 },
82 // {
83 // /* This should be exactly 100 dot lengths */
84 // .text = "PARIS PARIS",
85 // .wpm = 5,
86 // .tone = 1000,
87 // .carrier = 433920000,
88 // .backoff_dB = 10,
89 // },
90 // {
91 // .text = "cq foxhunt de w2xh",
92 // .wpm = 5,
93 // .tone = 1000,
94 // .carrier = 147555000,
95 // .backoff_dB = 0,
96 // },
97 };
98
99 size_t queue_status()
100 {
101 return IORD_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_STATUS);
102 }
103
104 size_t queue_len()
105 {
106 return (queue_status() >> 8) & 0xFF;
107 }
108
109 bool queue_full()
110 {
111 return (queue_status() & 0x2);
112 }
113
114 bool queue_running()
115 {
116 return (queue_status() & 0x1);
117 }
118
119 /* dphase is 4096 / (f_clock / f_tone) */
120 float const DPHASE_COEFF = 4096.0 / (float)SAMPLERATE;
121
122 void queue_tone(unsigned int frequency, float duration)
123 {
124 uint32_t dur_clks;
125 int32_t dphase;
126
127 dur_clks = __round_int((float)SAMPLERATE * duration);
128 dphase = __round_int((float)frequency * DPHASE_COEFF);
129
130 /* Spin until queue has room */
131 while (queue_full()) {
132 usleep(1000);
133 }
134
135 IOWR_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_DPHASE, dphase);
136 IOWR_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_DURATION, dur_clks);
137
138 DBG("%s: read dphase=0x%x\n", __FUNCTION__,
139 IORD_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_DPHASE));
140 DBG("%s: read dur_clks=0x%x\n", __FUNCTION__,
141 IORD_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_DURATION));
142
143 IOWR_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_STATUS,
144 TONE_GEN_OFFSET_CTRL_APPEND);
145
146 DBG("%s: running=%x dphase=0x%x dur_clks=0x%x qlen=0x%x\n", __FUNCTION__,
147 queue_running(), dphase, dur_clks, queue_len());
148 }
149
150 /* Generate morse code signal */
151 void morse_tx(struct message msg)
152 {
153 size_t count;
154 element elements[MAX_MESSAGE_LENGTH];
155 struct tone tones[MAX_TONE_COUNT];
156
157 /** Dot length calculation:
158 *
159 * "PARIS" is 50 dot lengths, a "typical" word
160 * If it can be sent n times in a minute, speed is n WPM
161 *
162 * 1 WPM => 60 seconds / 50 dots
163 * n WPM => 60 seconds / (n*50) dots
164 */
165 float const base = 60.0 / 50.0;
166 float dot_time = base / msg.wpm;
167
168 int total_dots = 0;
169
170 count = to_morse(msg.text, elements, MAX_MESSAGE_LENGTH);
171 if (0 == count || count >= MAX_MESSAGE_LENGTH) {
172 DBG("%s: to_morse ret 0x%x\n", __FUNCTION__, count);
173 return;
174 }
175
176 count = to_tones(elements, tones, msg.tone, dot_time, MAX_TONE_COUNT);
177 if (0 == count || count >= MAX_TONE_COUNT) {
178 DBG("%s: to_tones ret 0x%x\n", __FUNCTION__, count);
179 return;
180 }
181
182 for (size_t i = 0; i < count; ++i) {
183 int dots = (int)(tones[i].duration / dot_time);
184 // DBG("play len=%x freq=%x\n", dots, tones[i].frequency);
185 queue_tone(tones[i].frequency, tones[i].duration);
186 total_dots += dots;
187 }
188
189 DBG("%s: total count=%x len=%x\n", __FUNCTION__, count, total_dots);
190 }
191
192 int main(void)
193 {
194 /* Standard output text */
195 DBG(BLADERF_DEVICE_NAME " FPGA v%x.%x.%x\n", FPGA_VERSION_MAJOR,
196 FPGA_VERSION_MINOR, FPGA_VERSION_PATCH);
197 DBG("Built " __DATE__ " " __TIME__ " with love <3\n");
198 #ifdef BLADERF_NIOS_LIBAD936X
199 DBG("libad936x found: This FPGA image has magic transgirl powers\n");
200 #else
201 DBG("FATAL ERROR: libad936x is required for this application.\n");
202 while (true) {
203 };
204 #endif // BLADERF_NIOS_LIBAD936X
205
206 /* Hardcoded channel */
207 bladerf_channel channel = BLADERF_CHANNEL_TX(0);
208
209 /* State */
210 bool run_nios = true; /* Set 'false' to drop out of the loop */
211 size_t msg_index = 0; /* Counter for message index */
212
213 bladerf_frequency prev_freq = 0;
214 bladerf_gain prev_gain = 0;
215
216 /* Using a guestimate here... */
217 ad56x1_vctcxo_trim_dac_write(0x1ec1);
218 usleep(1000000);
219
220 IOWR_32DIRECT(TONE_GENERATOR_0_BASE, TONE_GEN_OFFSET_STATUS,
221 TONE_GEN_OFFSET_CTRL_CLEAR);
222
223 DBG("=== Initializing RFIC ===\n");
224
225 /* Initialize RFIC.
226 *
227 * The CALL() macro will print an error and return 1 if the function returns
228 * false.
229 */
230 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_INIT,
231 RFIC_SYSTEM_CHANNEL,
232 BLADERF_RFIC_INIT_STATE_ON));
233
234 /* Set up filters */
235 for (size_t i = 0; i < 2; ++i) {
236 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_FILTER,
237 BLADERF_CHANNEL_RX(i),
238 BLADERF_RFIC_RXFIR_DEC4));
239 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_FILTER,
240 BLADERF_CHANNEL_TX(i),
241 BLADERF_RFIC_TXFIR_INT4));
242 }
243
244 /* Set Sample Rate */
245 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_SAMPLERATE, channel,
246 SAMPLERATE));
247
248 DBG("=== System Ready ===\n");
249
250 /**
251 * Main program loop.
252 */
253 while (run_nios) {
254 struct message msg = MESSAGES[msg_index];
255
256 DBG("--> Message %x ('%s')\n", msg_index, msg.text);
257
258 /* BEGIN TRANSMITTING MESSAGE */
259
260 /* Set Frequency */
261 if (msg.carrier != prev_freq) {
262 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_FREQUENCY,
263 channel, msg.carrier));
264 prev_freq = msg.carrier;
265 }
266
267 /* Set Gain */
268 if (msg.backoff_dB != prev_gain) {
269 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_GAIN, channel,
270 msg.backoff_dB * 1000));
271 prev_gain = msg.backoff_dB;
272 }
273
274 /* Begin TX on this channel if required */
275 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_ENABLE, channel,
276 true));
277
278 #ifdef BIASTEE
279 /* Enable bias tee on TX side */
280 uint32_t rffe_reg;
281
282 rffe_reg = rffe_csr_read();
283
284 rffe_reg |= (1 << 10); // tx_bias_en
285
286 rffe_csr_write(rffe_reg);
287 #endif // BIASTEE
288
289 /* Pause 1 second */
290 queue_tone(0, 1);
291
292 /* Do signal transmission */
293 morse_tx(msg);
294
295 /* If we have more messages, transmit them */
296 if ((msg_index + 1) < ARRAY_SIZE(MESSAGES)) {
297 ++msg_index;
298 } else {
299 msg_index = 0;
300 run_nios = REPEAT_MODE;
301 }
302
303 if (run_nios) {
304 /* Pause 1 second */
305 queue_tone(0, 1);
306
307 /* Wait for completion */
308 while (queue_running()) {
309 usleep(1000);
310 }
311
312 /* Stop transmitting */
313 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_ENABLE, channel,
314 false));
315
316 #ifdef BIASTEE
317 /* Disable bias tee on TX side */
318 rffe_reg = rffe_csr_read();
319
320 rffe_reg &= ~(1 << 10); // tx_bias_en
321
322 rffe_csr_write(rffe_reg);
323 #endif // BIASTEE
324
325 usleep(INTER_MESSAGE_GAP * 1e6);
326 }
327 }
328
329 DBG("=== RFIC standby ===\n");
330
331 CALL(rfic_command_write_immed(BLADERF_RFIC_COMMAND_INIT,
332 RFIC_SYSTEM_CHANNEL,
333 BLADERF_RFIC_INIT_STATE_STANDBY));
334
335 DBG("=== Time to die... ===\n");
336 return 0;
337 }