-
Notifications
You must be signed in to change notification settings - Fork 32
/
rx.cpp
349 lines (292 loc) · 10.6 KB
/
rx.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "pico/multicore.h"
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include <string.h>
#include "rx.h"
#include "nco.h"
//buffers and dma for ADC
int rx::adc_dma_ping;
int rx::adc_dma_pong;
dma_channel_config rx::ping_cfg;
dma_channel_config rx::pong_cfg;
uint16_t rx::ping_samples[adc_block_size];
uint16_t rx::pong_samples[adc_block_size];
//buffers and dma for PWM audio output
int rx::audio_pwm_slice_num;
int rx::pwm_dma_ping;
int rx::pwm_dma_pong;
dma_channel_config rx::audio_ping_cfg;
dma_channel_config rx::audio_pong_cfg;
int16_t rx::ping_audio[adc_block_size];
int16_t rx::pong_audio[adc_block_size];
bool rx::audio_running;
uint16_t rx::num_ping_samples;
uint16_t rx::num_pong_samples;
//dma for capture
int rx::capture_dma;
dma_channel_config rx::capture_cfg;
void rx::dma_handler() {
// adc ping #### ####
// adc pong #### ####
// processing ping ###
// processing pong ###
// pwm_ping ####
// pwm_pong ####
if(dma_hw->ints0 & (1u << adc_dma_ping))
{
dma_channel_configure(adc_dma_ping, &ping_cfg, ping_samples, &adc_hw->fifo, adc_block_size, false);
if(audio_running){
dma_channel_configure(pwm_dma_pong, &audio_pong_cfg, &pwm_hw->slice[audio_pwm_slice_num].cc, pong_audio, num_pong_samples, true);
}
dma_hw->ints0 = 1u << adc_dma_ping;
}
if(dma_hw->ints0 & (1u << adc_dma_pong))
{
dma_channel_configure(adc_dma_pong, &pong_cfg, pong_samples, &adc_hw->fifo, adc_block_size, false);
dma_channel_configure(pwm_dma_ping, &audio_ping_cfg, &pwm_hw->slice[audio_pwm_slice_num].cc, ping_audio, num_ping_samples, true);
if(!audio_running){
audio_running = true;
}
dma_hw->ints0 = 1u << adc_dma_pong;
}
}
void rx::access(bool s)
{
sem_acquire_blocking(&settings_semaphore);
settings_changed |= s;
}
void rx::release()
{
sem_release(&settings_semaphore);
}
void rx::apply_settings()
{
if(sem_try_acquire(&settings_semaphore))
{
suspend = settings_to_apply.suspend;
if(settings_changed)
{
//apply frequency
tuned_frequency_Hz = settings_to_apply.tuned_frequency_Hz;
uint32_t system_clock_rate;
nco_frequency_Hz = nco_set_frequency(pio, sm, tuned_frequency_Hz, system_clock_rate);
offset_frequency_Hz = tuned_frequency_Hz - nco_frequency_Hz;
if(tuned_frequency_Hz > 16.0e6)
{
gpio_put(2, 0);
gpio_put(3, 0);
gpio_put(4, 0);
}
else if(tuned_frequency_Hz > 8.0e6)
{
gpio_put(2, 1);
gpio_put(3, 0);
gpio_put(4, 0);
}
else if(tuned_frequency_Hz > 4.0e6)
{
gpio_put(2, 0);
gpio_put(3, 1);
gpio_put(4, 0);
}
else if(tuned_frequency_Hz > 2.0e6)
{
gpio_put(2, 1);
gpio_put(3, 1);
gpio_put(4, 0);
}
else
{
gpio_put(2, 0);
gpio_put(3, 0);
gpio_put(4, 1);
}
//apply pwm_max
pwm_max = (system_clock_rate/audio_sample_rate)-1;
rx_dsp_inst.set_pwm_max(pwm_max);
pwm_set_wrap(audio_pwm_slice_num, pwm_max);
//apply frequency offset
rx_dsp_inst.set_frequency_offset_Hz(offset_frequency_Hz);
//apply CW sidetone
rx_dsp_inst.set_cw_sidetone_Hz(settings_to_apply.cw_sidetone_Hz);
//apply gain calibration
rx_dsp_inst.set_gain_cal_dB(settings_to_apply.gain_cal);
//apply AGC speed
rx_dsp_inst.set_agc_speed(settings_to_apply.agc_speed);
//apply Automatic Notch Filter
rx_dsp_inst.set_auto_notch(settings_to_apply.enable_auto_notch);
//apply mode
rx_dsp_inst.set_mode(settings_to_apply.mode, settings_to_apply.bandwidth);
//apply volume
rx_dsp_inst.set_volume(settings_to_apply.volume);
//apply squelch
rx_dsp_inst.set_squelch(settings_to_apply.squelch);
//apply swap iq
rx_dsp_inst.set_swap_iq(settings_to_apply.swap_iq);
}
//update status
status.signal_strength_dBm = rx_dsp_inst.get_signal_strength_dBm();
status.busy_time = busy_time;
status.battery = battery;
status.temp = temp;
settings_changed = false;
sem_release(&settings_semaphore);
}
}
void rx::get_spectrum(float spectrum[])
{
rx_dsp_inst.get_spectrum(spectrum);
}
rx::rx(rx_settings & settings_to_apply, rx_status & status) : settings_to_apply(settings_to_apply), status(status)
{
settings_to_apply.suspend = false;
suspend = false;
//Configure PIO to act as quadrature oscilator
pio = pio0;
offset = pio_add_program(pio, &nco_program);
sm = pio_claim_unused_sm(pio, true);
nco_program_init(pio, sm, offset);
//configure SMPS into power save mode
const uint PSU_PIN = 23;
gpio_init(PSU_PIN);
gpio_set_function(PSU_PIN, GPIO_FUNC_SIO);
gpio_set_dir(PSU_PIN, GPIO_OUT);
gpio_put(PSU_PIN, 1);
//ADC Configuration
adc_init();
adc_gpio_init(26);//I channel (0) - configure pin for ADC use
adc_gpio_init(27);//Q channel (1) - configure pin for ADC use
adc_gpio_init(29);//Battery - configure pin for ADC use
adc_set_temp_sensor_enabled(true);
adc_set_clkdiv(0); //flat out
//band select
gpio_init(2);//band 0
gpio_init(3);//band 1
gpio_init(4);//band 2
gpio_set_function(2, GPIO_FUNC_SIO);
gpio_set_function(3, GPIO_FUNC_SIO);
gpio_set_function(4, GPIO_FUNC_SIO);
gpio_set_dir(2, GPIO_OUT);
gpio_set_dir(3, GPIO_OUT);
gpio_set_dir(4, GPIO_OUT);
// Configure DMA for ADC transfers
adc_dma_ping = dma_claim_unused_channel(true);
adc_dma_pong = dma_claim_unused_channel(true);
ping_cfg = dma_channel_get_default_config(adc_dma_ping);
pong_cfg = dma_channel_get_default_config(adc_dma_pong);
channel_config_set_transfer_data_size(&ping_cfg, DMA_SIZE_16);
channel_config_set_read_increment(&ping_cfg, false);
channel_config_set_write_increment(&ping_cfg, true);
channel_config_set_dreq(&ping_cfg, DREQ_ADC);// Pace transfers based on availability of ADC samples
channel_config_set_chain_to(&ping_cfg, adc_dma_pong);
channel_config_set_transfer_data_size(&pong_cfg, DMA_SIZE_16);
channel_config_set_read_increment(&pong_cfg, false);
channel_config_set_write_increment(&pong_cfg, true);
channel_config_set_dreq(&pong_cfg, DREQ_ADC);// Pace transfers based on availability of ADC samples
channel_config_set_chain_to(&pong_cfg, adc_dma_ping);
//settings semaphore
sem_init(&settings_semaphore, 1, 1);
//audio output
const uint AUDIO_PIN = 16;
gpio_set_function(AUDIO_PIN, GPIO_FUNC_PWM);
gpio_set_drive_strength(AUDIO_PIN, GPIO_DRIVE_STRENGTH_12MA);
audio_pwm_slice_num = pwm_gpio_to_slice_num(AUDIO_PIN);
pwm_config config = pwm_get_default_config();
pwm_config_set_clkdiv(&config, 1.f);
pwm_max = 500;
pwm_config_set_wrap(&config, pwm_max);
pwm_init(audio_pwm_slice_num, &config, true);
//configure DMA for audio transfers
pwm_dma_ping = dma_claim_unused_channel(true);
pwm_dma_pong = dma_claim_unused_channel(true);
audio_ping_cfg = dma_channel_get_default_config(pwm_dma_ping);
audio_pong_cfg = dma_channel_get_default_config(pwm_dma_pong);
channel_config_set_transfer_data_size(&audio_ping_cfg, DMA_SIZE_16);
channel_config_set_read_increment(&audio_ping_cfg, true);
channel_config_set_write_increment(&audio_ping_cfg, false);
channel_config_set_dreq(&audio_ping_cfg, DREQ_PWM_WRAP0 + audio_pwm_slice_num);
channel_config_set_transfer_data_size(&audio_pong_cfg, DMA_SIZE_16);
channel_config_set_read_increment(&audio_pong_cfg, true);
channel_config_set_write_increment(&audio_pong_cfg, false);
channel_config_set_dreq(&audio_pong_cfg, DREQ_PWM_WRAP0 + audio_pwm_slice_num);
//configure DMA for audio transfers
capture_dma = dma_claim_unused_channel(true);
capture_cfg = dma_channel_get_default_config(pwm_dma_ping);
channel_config_set_transfer_data_size(&capture_cfg, DMA_SIZE_16);
channel_config_set_read_increment(&capture_cfg, true);
channel_config_set_write_increment(&capture_cfg, true);
dma_set_irq0_channel_mask_enabled((1u<<adc_dma_ping) | (1u<<adc_dma_pong), true);
irq_set_exclusive_handler(DMA_IRQ_0, dma_handler);
irq_set_enabled(DMA_IRQ_0, true);
}
void rx::read_batt_temp()
{
adc_select_input(3);
battery = 0;
for(uint8_t i=0; i<16; i++)
{
battery += adc_read();
}
adc_select_input(4);
temp = 0;
for(uint8_t i=0; i<16; i++)
{
temp += adc_read();
}
}
void rx::run()
{
while(true)
{
//read other adc channels when streaming is not running
uint32_t timeout = 1000;
read_batt_temp();
//supress audio output until first block has completed
audio_running = false;
hw_clear_bits(&adc_hw->fcs, ADC_FCS_UNDER_BITS);
hw_clear_bits(&adc_hw->fcs, ADC_FCS_OVER_BITS);
adc_fifo_setup(true, true, 1, false, false);
adc_select_input(0);
adc_set_round_robin(3);
dma_channel_configure(adc_dma_ping, &ping_cfg, ping_samples, &adc_hw->fifo, adc_block_size, false);
dma_channel_configure(adc_dma_pong, &pong_cfg, pong_samples, &adc_hw->fifo, adc_block_size, false);
dma_channel_set_irq0_enabled(adc_dma_ping, true);
dma_channel_set_irq0_enabled(adc_dma_pong, true);
dma_start_channel_mask(1u << adc_dma_ping);
adc_run(true);
while(true)
{
//exchange data with UI (runing in core 0)
apply_settings();
//periodically (or when requested) suspend streaming
if(timeout-- == 0 || suspend)
{
dma_channel_cleanup(adc_dma_ping);
dma_channel_cleanup(adc_dma_pong);
dma_channel_cleanup(pwm_dma_ping);
dma_channel_cleanup(pwm_dma_pong);
adc_run(false);
adc_fifo_drain();
adc_set_round_robin(0);
adc_fifo_setup(false, false, 1, false, false);
break;
}
//process adc data as each block completes
dma_channel_wait_for_finish_blocking(adc_dma_ping);
uint32_t start_time = time_us_32();
num_ping_samples = rx_dsp_inst.process_block(ping_samples, ping_audio);
busy_time = time_us_32()-start_time;
dma_channel_wait_for_finish_blocking(adc_dma_pong);
num_pong_samples = rx_dsp_inst.process_block(pong_samples, pong_audio);
}
//suspended state
while(true)
{
apply_settings();
if(!suspend)
{
break;
}
}
}
}