Skip to content

Commit 55ae863

Browse files
committed
Add radio configuration update callback
1 parent 3f589ab commit 55ae863

6 files changed

Lines changed: 149 additions & 67 deletions

File tree

firmware/common/radio.c

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "platform_detect.h"
2929
#include "radio.h"
3030
#include "fixed_point.h"
31-
#include "hackrf_ui.h"
3231

3332
#define MIN(x, y) ((x) < (y) ? (x) : (y))
3433
#define MAX(x, y) ((x) > (y) ? (x) : (y))
@@ -225,11 +224,6 @@ static bool radio_update_sample_rate(radio_t* const radio, uint64_t* bank)
225224
new_rate = (rate != radio->config[RADIO_BANK_APPLIED][RADIO_SAMPLE_RATE]);
226225
if (new_rate) {
227226
radio->config[RADIO_BANK_APPLIED][RADIO_SAMPLE_RATE] = rate;
228-
if (rate != RADIO_UNSET) {
229-
/* Round to the nearest Hz for display. */
230-
const uint32_t rate_hz = (rate + (FP_ONE_HZ >> 1)) / FP_ONE_HZ;
231-
hackrf_ui()->set_sample_rate(rate_hz);
232-
}
233227
}
234228

235229
return (new_afe_rate || new_rate || new_n);
@@ -637,6 +631,7 @@ bool radio_update(radio_t* const radio)
637631
uint64_t tmp_bank[RADIO_NUM_REGS];
638632
nvic_disable_irq(NVIC_USB0_IRQ);
639633
uint32_t dirty = radio->regs_dirty;
634+
uint32_t changed = 0;
640635
if (dirty == 0) {
641636
nvic_enable_irq(NVIC_USB0_IRQ);
642637
return false;
@@ -645,56 +640,57 @@ bool radio_update(radio_t* const radio)
645640
memcpy(&tmp_bank[0], &(radio->config[RADIO_BANK_ACTIVE][0]), sizeof(tmp_bank));
646641
nvic_enable_irq(NVIC_USB0_IRQ);
647642

648-
bool dir = false;
649-
bool rate = false;
650-
bool freq = false;
651-
bool bw = false;
652-
bool gain = false;
653-
bool bias = false;
654-
bool trig = false;
655-
bool dc = false;
656-
657-
if ((dirty &
658-
((1 << RADIO_SAMPLE_RATE) | (1 << RADIO_RESAMPLE_TX) |
659-
(1 << RADIO_RESAMPLE_RX))) ||
643+
if ((dirty & RADIO_REG_GROUP_RATE) ||
660644
((detected_platform() == BOARD_ID_PRALINE) &&
661645
(dirty & (1 << RADIO_OPMODE)))) {
662-
rate = radio_update_sample_rate(radio, &tmp_bank[0]);
646+
if (radio_update_sample_rate(radio, &tmp_bank[0])) {
647+
changed |= RADIO_REG_GROUP_RATE;
648+
}
663649
}
664-
if ((dirty &
665-
((1 << RADIO_FREQUENCY_RF) | (1 << RADIO_FREQUENCY_IF) |
666-
(1 << RADIO_FREQUENCY_LO) | (1 << RADIO_IMAGE_REJECT) |
667-
(1 << RADIO_ROTATION))) ||
650+
if ((dirty & RADIO_REG_GROUP_FREQ) ||
668651
((detected_platform() == BOARD_ID_PRALINE) &&
669-
(rate || (dirty & (1 << RADIO_OPMODE))))) {
670-
freq = radio_update_frequency(radio, &tmp_bank[0]);
652+
((changed & RADIO_REG_GROUP_RATE) || (dirty & (1 << RADIO_OPMODE))))) {
653+
if (radio_update_frequency(radio, &tmp_bank[0])) {
654+
changed |= RADIO_REG_GROUP_FREQ;
655+
}
671656
}
672-
if ((dirty &
673-
((1 << RADIO_BB_BANDWIDTH_TX) | (1 << RADIO_BB_BANDWIDTH_RX) |
674-
(1 << RADIO_XCVR_TX_LPF) | (1 << RADIO_XCVR_RX_LPF) |
675-
(1 << RADIO_XCVR_RX_HPF) | (1 << RADIO_RX_NARROW_LPF))) ||
676-
((detected_platform() == BOARD_ID_PRALINE) && (rate || freq))) {
677-
bw = radio_update_bandwidth(radio, &tmp_bank[0]);
657+
if ((dirty & RADIO_REG_GROUP_BW) ||
658+
((detected_platform() == BOARD_ID_PRALINE) &&
659+
(changed & (RADIO_REG_GROUP_RATE | RADIO_REG_GROUP_FREQ)))) {
660+
if (radio_update_bandwidth(radio, &tmp_bank[0])) {
661+
changed |= RADIO_REG_GROUP_BW;
662+
}
678663
}
679-
if (dirty &
680-
((1 << RADIO_GAIN_TX_RF) | (1 << RADIO_GAIN_TX_IF) | (1 << RADIO_GAIN_RX_RF) |
681-
(1 << RADIO_GAIN_RX_IF) | (1 << RADIO_GAIN_RX_BB) | (1 << RADIO_OPMODE))) {
682-
gain = radio_update_gain(radio, &tmp_bank[0]);
664+
if (dirty & RADIO_REG_GROUP_GAIN) {
665+
if (radio_update_gain(radio, &tmp_bank[0])) {
666+
changed |= RADIO_REG_GROUP_GAIN;
667+
}
683668
}
684669
if (dirty & ((1 << RADIO_BIAS_TEE) | (1 << RADIO_OPMODE))) {
685-
bias = radio_update_bias_tee(radio, &tmp_bank[0]);
670+
if (radio_update_bias_tee(radio, &tmp_bank[0])) {
671+
changed |= RADIO_BIAS_TEE;
672+
}
686673
}
687674
if (dirty & (1 << RADIO_TRIGGER)) {
688-
trig = radio_update_trigger(radio, &tmp_bank[0]);
675+
if (radio_update_trigger(radio, &tmp_bank[0])) {
676+
changed |= RADIO_TRIGGER;
677+
}
689678
}
690679
if (dirty & (1 << RADIO_DC_BLOCK)) {
691-
dc = radio_update_dc_block(radio, &tmp_bank[0]);
680+
if (radio_update_dc_block(radio, &tmp_bank[0])) {
681+
changed |= RADIO_DC_BLOCK;
682+
}
692683
}
693684
if (dirty & (1 << RADIO_OPMODE)) {
694-
dir = radio_update_direction(radio, &tmp_bank[0]);
685+
if (radio_update_direction(radio, &tmp_bank[0])) {
686+
changed |= RADIO_OPMODE;
687+
}
695688
}
696689

697-
return trig || dir || rate || freq || bw || gain || bias || dc;
690+
if (radio->update_cb) {
691+
radio->update_cb(changed);
692+
}
693+
return (changed != 0);
698694
}
699695

700696
void radio_switch_opmode(radio_t* const radio, const transceiver_mode_t mode)

firmware/common/radio.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ typedef enum {
186186
#define RADIO_NUM_REGS (23)
187187
#define RADIO_UNSET (0xffffffffffffffff)
188188

189+
/* register groups for bitfield convenience */
190+
#define RADIO_REG_GROUP_RATE \
191+
((1 << RADIO_SAMPLE_RATE) | (1 << RADIO_RESAMPLE_TX) | (1 << RADIO_RESAMPLE_RX))
192+
#define RADIO_REG_GROUP_FREQ \
193+
((1 << RADIO_FREQUENCY_RF) | (1 << RADIO_FREQUENCY_IF) | \
194+
(1 << RADIO_FREQUENCY_LO) | (1 << RADIO_IMAGE_REJECT) | (1 << RADIO_ROTATION))
195+
#define RADIO_REG_GROUP_BW \
196+
((1 << RADIO_BB_BANDWIDTH_TX) | (1 << RADIO_BB_BANDWIDTH_RX) | \
197+
(1 << RADIO_XCVR_TX_LPF) | (1 << RADIO_XCVR_RX_LPF) | \
198+
(1 << RADIO_XCVR_RX_HPF) | (1 << RADIO_RX_NARROW_LPF))
199+
#define RADIO_REG_GROUP_GAIN \
200+
((1 << RADIO_GAIN_TX_RF) | (1 << RADIO_GAIN_TX_IF) | (1 << RADIO_GAIN_RX_RF) | \
201+
(1 << RADIO_GAIN_RX_IF) | (1 << RADIO_GAIN_RX_BB) | (1 << RADIO_OPMODE))
202+
189203
/**
190204
* Register bank RADIO_BANK_ACTIVE stores the active configuration. Active
191205
* register settings are copied to the applied register when applied.
@@ -216,11 +230,19 @@ typedef enum {
216230
*/
217231
typedef fp_40_24_t (*sample_rate_fn)(const fp_40_24_t sample_rate, const bool program);
218232

233+
/**
234+
* An optional callback may be provided that is called after each time the
235+
* radio configuration has been updated. The single argument is a bitfield
236+
* indicating registers that have been changed.
237+
*/
238+
typedef void (*update_fn)(const uint32_t changed_regs);
239+
219240
typedef struct radio_t {
220241
radio_config_mode_t config_mode;
221242
uint64_t config[RADIO_NUM_BANKS][RADIO_NUM_REGS];
222243
volatile uint32_t regs_dirty;
223244
sample_rate_fn sample_rate_cb;
245+
update_fn update_cb;
224246
} radio_t;
225247

226248
void radio_init(radio_t* const radio);

firmware/common/rf_path.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
#include <hackrf_core.h>
2929

30-
#include "hackrf_ui.h"
3130
#include "gpio_lpc.h"
3231
#include "platform_detect.h"
3332
#include "mixer.h"
@@ -547,8 +546,6 @@ void rf_path_set_direction(rf_path_t* const rf_path, const rf_path_direction_t d
547546
}
548547

549548
switchctrl_set(rf_path, rf_path->switchctrl);
550-
551-
hackrf_ui()->set_direction(direction);
552549
}
553550

554551
void rf_path_set_filter(rf_path_t* const rf_path, const rf_path_filter_t filter)
@@ -573,8 +570,6 @@ void rf_path_set_filter(rf_path_t* const rf_path, const rf_path_filter_t filter)
573570
}
574571

575572
switchctrl_set(rf_path, rf_path->switchctrl);
576-
577-
hackrf_ui()->set_filter(filter);
578573
}
579574

580575
void rf_path_set_lna(rf_path_t* const rf_path, const uint_fast8_t enable)
@@ -598,8 +593,6 @@ void rf_path_set_lna(rf_path_t* const rf_path, const uint_fast8_t enable)
598593
}
599594

600595
switchctrl_set(rf_path, rf_path->switchctrl);
601-
602-
hackrf_ui()->set_lna_power(enable);
603596
}
604597

605598
/* antenna port power control */
@@ -612,6 +605,4 @@ void rf_path_set_antenna(rf_path_t* const rf_path, const uint_fast8_t enable)
612605
}
613606

614607
switchctrl_set(rf_path, rf_path->switchctrl);
615-
616-
hackrf_ui()->set_antenna_bias(enable);
617608
}

firmware/common/tuning.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
*/
2323

2424
#include "tuning.h"
25-
#include "hackrf_ui.h"
2625
#include "hackrf_core.h"
2726
#include "mixer.h"
2827
#include "max2831.h"
2928
#include "max2837.h"
3029
#include "max2839.h"
3130
#include "sgpio.h"
32-
#include "operacake.h"
3331
#include "platform_detect.h"
3432

3533
#ifndef PRALINE
@@ -132,12 +130,6 @@ bool set_freq(const uint64_t freq)
132130
success = false;
133131
}
134132
max283x_set_mode(&max283x, prior_max283x_mode);
135-
if (success) {
136-
hackrf_ui()->set_frequency(freq);
137-
#ifdef HACKRF_ONE
138-
operacake_set_range(freq_mhz);
139-
#endif
140-
}
141133
return success;
142134
}
143135

@@ -197,8 +189,6 @@ bool tuning_set_frequency(
197189
}
198190

199191
max2831_set_mode(&max283x, prior_max2831_mode);
200-
hackrf_ui()->set_frequency(freq);
201-
operacake_set_range(freq_mhz);
202192
return true;
203193
}
204194
#endif

firmware/hackrf_usb/hackrf_usb.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,96 @@ static void m0_rom_to_ram(void)
256256
memcpy(dest, (uint32_t*) (base + src), len);
257257
}
258258

259+
void radio_changed(const uint32_t changed)
260+
{
261+
const uint64_t opmode = radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_OPMODE);
262+
263+
if (changed & (1 << RADIO_OPMODE)) {
264+
hackrf_ui()->set_direction(opmode);
265+
}
266+
if (changed & (1 << RADIO_SAMPLE_RATE)) {
267+
const uint64_t rate =
268+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_SAMPLE_RATE);
269+
hackrf_ui()->set_sample_rate(rate / FP_ONE_HZ);
270+
}
271+
if (changed & (1 << RADIO_FREQUENCY_RF)) {
272+
const uint64_t freq =
273+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_FREQUENCY_RF);
274+
hackrf_ui()->set_frequency(freq / FP_ONE_HZ);
275+
#if defined(HACKRF_ONE) || defined(PRALINE)
276+
operacake_set_range(freq / FP_ONE_MHZ);
277+
#endif
278+
}
279+
if (changed & (1 << RADIO_IMAGE_REJECT)) {
280+
const uint64_t img_reject =
281+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_IMAGE_REJECT);
282+
hackrf_ui()->set_filter(img_reject);
283+
}
284+
if (changed &
285+
((1 << RADIO_BB_BANDWIDTH_TX) | (1 << RADIO_BB_BANDWIDTH_RX) |
286+
(1 << RADIO_OPMODE))) {
287+
uint64_t bw;
288+
289+
switch (opmode) {
290+
case TRANSCEIVER_MODE_TX:
291+
case TRANSCEIVER_MODE_SS:
292+
bw = radio_reg_read(
293+
&radio,
294+
RADIO_BANK_APPLIED,
295+
RADIO_BB_BANDWIDTH_TX);
296+
break;
297+
default:
298+
bw = radio_reg_read(
299+
&radio,
300+
RADIO_BANK_APPLIED,
301+
RADIO_BB_BANDWIDTH_RX);
302+
}
303+
hackrf_ui()->set_filter_bw(bw);
304+
}
305+
if (changed &
306+
((1 << RADIO_GAIN_TX_RF) | (1 << RADIO_GAIN_RX_RF) | (1 << RADIO_OPMODE))) {
307+
const uint64_t tx_rf =
308+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_GAIN_TX_RF);
309+
const uint64_t rx_rf =
310+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_GAIN_RX_RF);
311+
bool enable;
312+
313+
switch (opmode) {
314+
case TRANSCEIVER_MODE_TX:
315+
case TRANSCEIVER_MODE_SS:
316+
enable = tx_rf;
317+
break;
318+
case TRANSCEIVER_MODE_RX:
319+
case TRANSCEIVER_MODE_RX_SWEEP:
320+
enable = rx_rf;
321+
break;
322+
default:
323+
enable = false;
324+
}
325+
hackrf_ui()->set_lna_power(enable);
326+
}
327+
if (changed & (1 << RADIO_GAIN_TX_IF)) {
328+
const uint64_t tx_if =
329+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_GAIN_TX_IF);
330+
hackrf_ui()->set_bb_tx_vga_gain(tx_if);
331+
}
332+
if (changed & (1 << RADIO_GAIN_RX_IF)) {
333+
const uint64_t rx_if =
334+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_GAIN_RX_IF);
335+
hackrf_ui()->set_bb_lna_gain(rx_if);
336+
}
337+
if (changed & (1 << RADIO_GAIN_RX_BB)) {
338+
const uint64_t rx_bb =
339+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_GAIN_RX_BB);
340+
hackrf_ui()->set_bb_vga_gain(rx_bb);
341+
}
342+
if (changed & (1 << RADIO_BIAS_TEE)) {
343+
const uint64_t enable =
344+
radio_reg_read(&radio, RADIO_BANK_APPLIED, RADIO_BIAS_TEE);
345+
hackrf_ui()->set_antenna_bias(enable);
346+
}
347+
}
348+
259349
int main(void)
260350
{
261351
// Copy M0 image from ROM before SPIFI is disabled
@@ -320,6 +410,7 @@ int main(void)
320410
fpga_spi_selftest();
321411
fpga_sgpio_selftest();
322412
#endif
413+
radio.update_cb = radio_changed;
323414
radio_init(&radio);
324415

325416
#if (defined HACKRF_ONE || defined PRALINE)

0 commit comments

Comments
 (0)