Skip to content

Commit cb08b84

Browse files
authored
Spectrum analyzer: new ext radio driver (#2)
* Sub Analyzer app: UPD to new driver * Sub Analyzer: fix working on start
1 parent d208b69 commit cb08b84

File tree

4 files changed

+153
-41
lines changed

4 files changed

+153
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "radio_device_loader.h"
2+
3+
#include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
4+
#include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
5+
6+
static void radio_device_loader_power_on() {
7+
uint8_t attempts = 0;
8+
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
9+
furi_hal_power_enable_otg();
10+
//CC1101 power-up time
11+
furi_delay_ms(10);
12+
}
13+
}
14+
15+
static void radio_device_loader_power_off() {
16+
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
17+
}
18+
19+
bool radio_device_loader_is_connect_external(const char* name) {
20+
bool is_connect = false;
21+
bool is_otg_enabled = furi_hal_power_is_otg_enabled();
22+
23+
if(!is_otg_enabled) {
24+
radio_device_loader_power_on();
25+
}
26+
27+
const SubGhzDevice* device = subghz_devices_get_by_name(name);
28+
if(device) {
29+
is_connect = subghz_devices_is_connect(device);
30+
}
31+
32+
if(!is_otg_enabled) {
33+
radio_device_loader_power_off();
34+
}
35+
return is_connect;
36+
}
37+
38+
const SubGhzDevice* radio_device_loader_set(
39+
const SubGhzDevice* current_radio_device,
40+
SubGhzRadioDeviceType radio_device_type) {
41+
const SubGhzDevice* radio_device;
42+
43+
if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
44+
radio_device_loader_is_connect_external(SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
45+
radio_device_loader_power_on();
46+
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
47+
subghz_devices_begin(radio_device);
48+
} else if(current_radio_device == NULL) {
49+
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
50+
} else {
51+
radio_device_loader_end(current_radio_device);
52+
radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
53+
}
54+
55+
return radio_device;
56+
}
57+
58+
void radio_device_loader_end(const SubGhzDevice* radio_device) {
59+
furi_assert(radio_device);
60+
radio_device_loader_power_off();
61+
if(radio_device != subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME)) {
62+
subghz_devices_end(radio_device);
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <lib/subghz/devices/devices.h>
4+
5+
/** SubGhzRadioDeviceType */
6+
typedef enum {
7+
SubGhzRadioDeviceTypeInternal,
8+
SubGhzRadioDeviceTypeExternalCC1101,
9+
} SubGhzRadioDeviceType;
10+
11+
const SubGhzDevice* radio_device_loader_set(
12+
const SubGhzDevice* current_radio_device,
13+
SubGhzRadioDeviceType radio_device_type);
14+
15+
void radio_device_loader_end(const SubGhzDevice* radio_device);

applications/external/spectrum_analyzer/spectrum_analyzer.c

+6-17
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,6 @@ void spectrum_analyzer_free(SpectrumAnalyzer* instance) {
389389

390390
free(instance->model);
391391
free(instance);
392-
393-
furi_hal_subghz_idle();
394-
furi_hal_subghz_sleep();
395-
396-
// Disable power for External CC1101 if it was enabled and module is connected
397-
furi_hal_subghz_disable_ext_power();
398-
// Reinit SPI handles for internal radio / nfc
399-
furi_hal_subghz_init_radio_type(SubGhzRadioInternal);
400392
}
401393

402394
int32_t spectrum_analyzer_app(void* p) {
@@ -405,21 +397,18 @@ int32_t spectrum_analyzer_app(void* p) {
405397
SpectrumAnalyzer* spectrum_analyzer = spectrum_analyzer_alloc();
406398
InputEvent input;
407399

408-
// Enable power for External CC1101 if it is connected
409-
furi_hal_subghz_enable_ext_power();
410-
// Auto switch to internal radio if external radio is not available
411-
furi_delay_ms(15);
412-
if(!furi_hal_subghz_check_radio()) {
413-
furi_hal_subghz_select_radio_type(SubGhzRadioInternal);
414-
furi_hal_subghz_init_radio_type(SubGhzRadioInternal);
415-
}
416-
417400
furi_hal_power_suppress_charge_enter();
418401

419402
FURI_LOG_D("Spectrum", "Main Loop - Starting worker");
420403
furi_delay_ms(50);
421404

422405
spectrum_analyzer_worker_start(spectrum_analyzer->worker);
406+
spectrum_analyzer_calculate_frequencies(spectrum_analyzer->model);
407+
spectrum_analyzer_worker_set_frequencies(
408+
spectrum_analyzer->worker,
409+
spectrum_analyzer->model->channel0_frequency,
410+
spectrum_analyzer->model->spacing,
411+
spectrum_analyzer->model->width);
423412

424413
FURI_LOG_D("Spectrum", "Main Loop - Wait on queue");
425414
furi_delay_ms(50);

applications/external/spectrum_analyzer/spectrum_analyzer_worker.c

+68-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <furi_hal.h>
55
#include <furi.h>
66

7+
#include "helpers/radio_device_loader.h"
8+
79
#include <lib/drivers/cc1101_regs.h>
810

911
struct SpectrumAnalyzerWorker {
@@ -13,6 +15,8 @@ struct SpectrumAnalyzerWorker {
1315
SpectrumAnalyzerWorkerCallback callback;
1416
void* callback_context;
1517

18+
const SubGhzDevice* radio_device;
19+
1620
uint32_t channel0_frequency;
1721
uint32_t spacing;
1822
uint8_t width;
@@ -44,7 +48,9 @@ void spectrum_analyzer_worker_set_filter(SpectrumAnalyzerWorker* instance) {
4448
filter_config[0][1] = 0x6C; /* 196 kHz / .8 = 245 kHz --> 270 kHz */
4549
break;
4650
}
47-
furi_hal_subghz_load_registers((uint8_t*)filter_config);
51+
52+
UNUSED(filter_config);
53+
// furi_hal_subghz_load_registers((uint8_t*)filter_config);
4854
}
4955

5056
static int32_t spectrum_analyzer_worker_thread(void* context) {
@@ -54,32 +60,53 @@ static int32_t spectrum_analyzer_worker_thread(void* context) {
5460
FURI_LOG_D("SpectrumWorker", "spectrum_analyzer_worker_thread: Start");
5561

5662
// Start CC1101
57-
furi_hal_subghz_reset();
58-
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
59-
furi_hal_subghz_set_frequency(433920000);
60-
furi_hal_subghz_flush_rx();
61-
furi_hal_subghz_rx();
62-
63-
static const uint8_t radio_config[][2] = {
64-
{CC1101_FSCTRL1, 0x12},
65-
{CC1101_FSCTRL0, 0x00},
66-
67-
{CC1101_AGCCTRL2, 0xC0},
68-
69-
{CC1101_MDMCFG4, 0x6C},
70-
{CC1101_TEST2, 0x88},
71-
{CC1101_TEST1, 0x31},
72-
{CC1101_TEST0, 0x09},
63+
subghz_devices_reset(instance->radio_device);
64+
subghz_devices_load_preset(instance->radio_device, FuriHalSubGhzPresetOok650Async, NULL);
65+
subghz_devices_set_frequency(instance->radio_device, 433920000);
66+
subghz_devices_flush_rx(instance->radio_device);
67+
subghz_devices_set_rx(instance->radio_device);
68+
69+
const uint8_t radio_config[] = {
70+
71+
CC1101_FSCTRL0,
72+
0x00,
73+
CC1101_FSCTRL1,
74+
0x12,
75+
76+
CC1101_AGCCTRL2,
77+
0xC0,
78+
79+
CC1101_MDMCFG4,
80+
0x6C,
81+
CC1101_TEST2,
82+
0x88,
83+
CC1101_TEST1,
84+
0x31,
85+
CC1101_TEST0,
86+
0x09,
87+
7388
/* End */
74-
{0, 0},
89+
0,
90+
0,
91+
92+
// ook_async_patable
93+
0x00,
94+
0xC0, // 12dBm 0xC0, 10dBm 0xC5, 7dBm 0xCD, 5dBm 0x86, 0dBm 0x50, -6dBm 0x37, -10dBm 0x26, -15dBm 0x1D, -20dBm 0x17, -30dBm 0x03
95+
0x00,
96+
0x00,
97+
0x00,
98+
0x00,
99+
0x00,
100+
0x00,
75101
};
76102

77103
while(instance->should_work) {
78104
furi_delay_ms(50);
79105

80106
// FURI_LOG_T("SpectrumWorker", "spectrum_analyzer_worker_thread: Worker Loop");
81-
furi_hal_subghz_idle();
82-
furi_hal_subghz_load_registers((uint8_t*)radio_config);
107+
subghz_devices_idle(instance->radio_device);
108+
subghz_devices_load_preset(
109+
instance->radio_device, FuriHalSubGhzPresetCustom, (uint8_t*)radio_config);
83110

84111
// TODO: Check filter!
85112
// spectrum_analyzer_worker_set_filter(instance);
@@ -90,25 +117,31 @@ static int32_t spectrum_analyzer_worker_thread(void* context) {
90117
for(uint8_t ch_offset = 0, chunk = 0; ch_offset < CHUNK_SIZE;
91118
++chunk >= NUM_CHUNKS && ++ch_offset && (chunk = 0)) {
92119
uint8_t ch = chunk * CHUNK_SIZE + ch_offset;
93-
furi_hal_subghz_set_frequency(instance->channel0_frequency + (ch * instance->spacing));
94120

95-
furi_hal_subghz_rx();
121+
if(subghz_devices_is_frequency_valid(
122+
instance->radio_device,
123+
instance->channel0_frequency + (ch * instance->spacing)))
124+
subghz_devices_set_frequency(
125+
instance->radio_device,
126+
instance->channel0_frequency + (ch * instance->spacing));
127+
128+
subghz_devices_set_rx(instance->radio_device);
96129
furi_delay_ms(3);
97130

98131
// dec dBm
99132
//max_ss = 127 -> -10.5
100133
//max_ss = 0 -> -74.0
101134
//max_ss = 255 -> -74.5
102135
//max_ss = 128 -> -138.0
103-
instance->channel_ss[ch] = (furi_hal_subghz_get_rssi() + 138) * 2;
136+
instance->channel_ss[ch] = (subghz_devices_get_rssi(instance->radio_device) + 138) * 2;
104137

105138
if(instance->channel_ss[ch] > instance->max_rssi_dec) {
106139
instance->max_rssi_dec = instance->channel_ss[ch];
107140
instance->max_rssi = (instance->channel_ss[ch] / 2) - 138;
108141
instance->max_rssi_channel = ch;
109142
}
110143

111-
furi_hal_subghz_idle();
144+
subghz_devices_idle(instance->radio_device);
112145
}
113146

114147
// FURI_LOG_T("SpectrumWorker", "channel_ss[0]: %u", instance->channel_ss[0]);
@@ -138,6 +171,11 @@ SpectrumAnalyzerWorker* spectrum_analyzer_worker_alloc() {
138171
furi_thread_set_context(instance->thread, instance);
139172
furi_thread_set_callback(instance->thread, spectrum_analyzer_worker_thread);
140173

174+
subghz_devices_init();
175+
176+
instance->radio_device =
177+
radio_device_loader_set(instance->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
178+
141179
FURI_LOG_D("Spectrum", "spectrum_analyzer_worker_alloc: End");
142180

143181
return instance;
@@ -147,6 +185,12 @@ void spectrum_analyzer_worker_free(SpectrumAnalyzerWorker* instance) {
147185
FURI_LOG_D("Spectrum", "spectrum_analyzer_worker_free");
148186
furi_assert(instance);
149187
furi_thread_free(instance->thread);
188+
189+
subghz_devices_sleep(instance->radio_device);
190+
radio_device_loader_end(instance->radio_device);
191+
192+
subghz_devices_deinit();
193+
150194
free(instance);
151195
}
152196

0 commit comments

Comments
 (0)