mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-22 20:43:07 +00:00
SubGhz: refactoring frequency analyzer and MegaCode display changes (#1221)
* SubGhz: MegaCode display changes * SubGhz: refactoring frequency analyzer * SubGhz: use one stage detection in frequency analyzer, tune bw, datarate and etc * SubGhz: tune analyzer threshold * SubGhz: raise frequency analyzer threshold and rssi sampling config * SubGhz: fix frequency analyzer, small step frequency analysis * SubGhz: subghz_frequency_analyzer_worker * SubGhz: fix SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD * SubGhz: debug logging in frequency analyzer, increase MAGN_TARGET to max value * SubGhz: reduce RSSI delay in frequency scanner * SubGhz: fix delays, remove trace logging from frequency analyzer * SubGhz: cleanup variable names and add comments Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
parent
f6384116a1
commit
d38dba4a26
2 changed files with 121 additions and 43 deletions
|
@ -1,24 +1,22 @@
|
|||
#include "subghz_frequency_analyzer_worker.h"
|
||||
#include <lib/drivers/cc1101_regs.h>
|
||||
#include <lib/drivers/cc1101.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#include "../subghz_i.h"
|
||||
|
||||
#define TAG "SubghzFrequencyAnalyzerWorker"
|
||||
|
||||
#define SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD -95.0f
|
||||
|
||||
static const uint8_t subghz_preset_ook_58khz[][2] = {
|
||||
{CC1101_FIFOTHR, 0x47}, // The only important bit is ADC_RETENTION, FIFO Tx=33 Rx=32
|
||||
{CC1101_MDMCFG4, 0xF5}, // Rx BW filter is 58.035714kHz
|
||||
{CC1101_TEST2, 0x81}, // FIFOTHR ADC_RETENTION=1 matched value
|
||||
{CC1101_TEST1, 0x35}, // FIFOTHR ADC_RETENTION=1 matched value
|
||||
{CC1101_MDMCFG4, 0b11110111}, // Rx BW filter is 58.035714kHz
|
||||
/* End */
|
||||
{0, 0},
|
||||
};
|
||||
|
||||
static const uint8_t subghz_preset_ook_650khz[][2] = {
|
||||
{CC1101_FIFOTHR, 0x07}, // The only important bit is ADC_RETENTION
|
||||
{CC1101_MDMCFG4, 0x17}, // Rx BW filter is 650.000kHz
|
||||
{CC1101_TEST2, 0x88},
|
||||
{CC1101_TEST1, 0x31},
|
||||
{CC1101_MDMCFG4, 0b00010111}, // Rx BW filter is 650.000kHz
|
||||
/* End */
|
||||
{0, 0},
|
||||
};
|
||||
|
@ -27,7 +25,7 @@ struct SubGhzFrequencyAnalyzerWorker {
|
|||
FuriThread* thread;
|
||||
|
||||
volatile bool worker_running;
|
||||
uint8_t count_repet;
|
||||
uint8_t sample_hold_counter;
|
||||
FrequencyRSSI frequency_rssi_buf;
|
||||
SubGhzSetting* setting;
|
||||
|
||||
|
@ -37,6 +35,16 @@ struct SubGhzFrequencyAnalyzerWorker {
|
|||
void* context;
|
||||
};
|
||||
|
||||
static void subghz_frequency_analyzer_worker_load_registers(const uint8_t data[][2]) {
|
||||
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
|
||||
size_t i = 0;
|
||||
while(data[i][0]) {
|
||||
cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, data[i][0], data[i][1]);
|
||||
i++;
|
||||
}
|
||||
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
|
||||
}
|
||||
|
||||
// running average with adaptive coefficient
|
||||
static uint32_t subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
|
||||
SubGhzFrequencyAnalyzerWorker* instance,
|
||||
|
@ -62,32 +70,75 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
|
|||
SubGhzFrequencyAnalyzerWorker* instance = context;
|
||||
|
||||
FrequencyRSSI frequency_rssi = {.frequency = 0, .rssi = 0};
|
||||
float rssi;
|
||||
uint32_t frequency;
|
||||
uint32_t frequency_start;
|
||||
float rssi = 0;
|
||||
uint32_t frequency = 0;
|
||||
CC1101Status status;
|
||||
|
||||
//Start CC1101
|
||||
furi_hal_subghz_reset();
|
||||
furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
|
||||
furi_hal_subghz_set_frequency(433920000);
|
||||
furi_hal_subghz_flush_rx();
|
||||
|
||||
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
|
||||
cc1101_flush_rx(&furi_hal_spi_bus_handle_subghz);
|
||||
cc1101_flush_tx(&furi_hal_spi_bus_handle_subghz);
|
||||
cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW);
|
||||
cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_MDMCFG3,
|
||||
0b11111111); // symbol rate
|
||||
cc1101_write_reg(
|
||||
&furi_hal_spi_bus_handle_subghz,
|
||||
CC1101_AGCCTRL2,
|
||||
0b00000111); // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAGN_TARGET 42 dB
|
||||
cc1101_write_reg(
|
||||
&furi_hal_spi_bus_handle_subghz,
|
||||
CC1101_AGCCTRL1,
|
||||
0b00001000); // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 1000 - Absolute carrier sense threshold disabled
|
||||
cc1101_write_reg(
|
||||
&furi_hal_spi_bus_handle_subghz,
|
||||
CC1101_AGCCTRL0,
|
||||
0b00110000); // 00 - No hysteresis, medium asymmetric dead zone, medium gain ; 11 - 64 samples agc; 00 - Normal AGC, 00 - 4dB boundary
|
||||
|
||||
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
|
||||
|
||||
furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
|
||||
furi_hal_subghz_rx();
|
||||
|
||||
while(instance->worker_running) {
|
||||
osDelay(10);
|
||||
|
||||
float rssi_min = 26.0f;
|
||||
float rssi_avg = 0;
|
||||
size_t rssi_avg_samples = 0;
|
||||
|
||||
frequency_rssi.rssi = -127.0f;
|
||||
furi_hal_subghz_idle();
|
||||
furi_hal_subghz_load_registers(subghz_preset_ook_650khz);
|
||||
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_650khz);
|
||||
|
||||
// First stage: coarse scan
|
||||
for(size_t i = 0; i < subghz_setting_get_frequency_count(instance->setting); i++) {
|
||||
if(furi_hal_subghz_is_frequency_valid(
|
||||
subghz_setting_get_frequency(instance->setting, i))) {
|
||||
furi_hal_subghz_idle();
|
||||
frequency = furi_hal_subghz_set_frequency(
|
||||
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
|
||||
cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
|
||||
frequency = cc1101_set_frequency(
|
||||
&furi_hal_spi_bus_handle_subghz,
|
||||
subghz_setting_get_frequency(instance->setting, i));
|
||||
furi_hal_subghz_rx();
|
||||
osDelay(3);
|
||||
|
||||
cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
|
||||
do {
|
||||
status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
|
||||
} while(status.STATE != CC1101StateIDLE);
|
||||
|
||||
cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
|
||||
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
|
||||
|
||||
// delay will be in range between 1 and 2ms
|
||||
osDelay(2);
|
||||
|
||||
rssi = furi_hal_subghz_get_rssi();
|
||||
|
||||
rssi_avg += rssi;
|
||||
rssi_avg_samples++;
|
||||
|
||||
if(rssi < rssi_min) rssi_min = rssi;
|
||||
|
||||
if(frequency_rssi.rssi < rssi) {
|
||||
frequency_rssi.rssi = rssi;
|
||||
frequency_rssi.frequency = frequency;
|
||||
|
@ -95,19 +146,41 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
|
|||
}
|
||||
}
|
||||
|
||||
if(frequency_rssi.rssi > -90.0) {
|
||||
// -0.5 ... 433.92 ... +0.5
|
||||
frequency_start = frequency_rssi.frequency - 250000;
|
||||
//step 10KHz
|
||||
FURI_LOG_T(
|
||||
TAG,
|
||||
"RSSI: avg %f, max %f at %u, min %f",
|
||||
(double)(rssi_avg / rssi_avg_samples),
|
||||
(double)frequency_rssi.rssi,
|
||||
frequency_rssi.frequency,
|
||||
(double)rssi_min);
|
||||
|
||||
// Second stage: fine scan
|
||||
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
|
||||
FURI_LOG_D(TAG, "~:%u:%f", frequency_rssi.frequency, (double)frequency_rssi.rssi);
|
||||
|
||||
frequency_rssi.rssi = -127.0;
|
||||
furi_hal_subghz_idle();
|
||||
furi_hal_subghz_load_registers(subghz_preset_ook_58khz);
|
||||
for(uint32_t i = frequency_start; i < frequency_start + 500000; i += 10000) {
|
||||
subghz_frequency_analyzer_worker_load_registers(subghz_preset_ook_58khz);
|
||||
//-0.3 ... 433.92 ... +0.3 step 10KHz
|
||||
for(uint32_t i = frequency_rssi.frequency - 300000;
|
||||
i < frequency_rssi.frequency + 300000;
|
||||
i += 20000) {
|
||||
if(furi_hal_subghz_is_frequency_valid(i)) {
|
||||
furi_hal_subghz_idle();
|
||||
frequency = furi_hal_subghz_set_frequency(i);
|
||||
furi_hal_subghz_rx();
|
||||
osDelay(3);
|
||||
furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
|
||||
cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
|
||||
frequency = cc1101_set_frequency(&furi_hal_spi_bus_handle_subghz, i);
|
||||
|
||||
cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
|
||||
do {
|
||||
status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
|
||||
} while(status.STATE != CC1101StateIDLE);
|
||||
|
||||
cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
|
||||
furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
|
||||
|
||||
// delay will be in range between 1 and 2ms
|
||||
osDelay(2);
|
||||
|
||||
rssi = furi_hal_subghz_get_rssi();
|
||||
if(frequency_rssi.rssi < rssi) {
|
||||
frequency_rssi.rssi = rssi;
|
||||
|
@ -117,20 +190,24 @@ static int32_t subghz_frequency_analyzer_worker_thread(void* context) {
|
|||
}
|
||||
}
|
||||
|
||||
if(frequency_rssi.rssi > -90.0) {
|
||||
instance->count_repet = 20;
|
||||
// Deliver results
|
||||
if(frequency_rssi.rssi > SUBGHZ_FREQUENCY_ANALYZER_THRESHOLD) {
|
||||
FURI_LOG_D(TAG, "=:%u:%f", frequency_rssi.frequency, (double)frequency_rssi.rssi);
|
||||
|
||||
instance->sample_hold_counter = 20;
|
||||
if(instance->filVal) {
|
||||
frequency_rssi.frequency =
|
||||
subghz_frequency_analyzer_worker_expRunningAverageAdaptive(
|
||||
instance, frequency_rssi.frequency);
|
||||
}
|
||||
if(instance->pair_callback)
|
||||
// Deliver callback
|
||||
if(instance->pair_callback) {
|
||||
instance->pair_callback(
|
||||
instance->context, frequency_rssi.frequency, frequency_rssi.rssi);
|
||||
|
||||
}
|
||||
} else {
|
||||
if(instance->count_repet > 0) {
|
||||
instance->count_repet--;
|
||||
if(instance->sample_hold_counter > 0) {
|
||||
instance->sample_hold_counter--;
|
||||
} else {
|
||||
instance->filVal = 0;
|
||||
if(instance->pair_callback) instance->pair_callback(instance->context, 0, 0);
|
||||
|
|
|
@ -401,13 +401,14 @@ void subghz_protocol_decoder_megacode_get_string(void* context, string_t output)
|
|||
string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%06lX\r\n"
|
||||
"Sn:%04lX Btn:%X\r\n"
|
||||
"Facility:%X\r\n",
|
||||
"Key:0x%06lX\r\n"
|
||||
"Sn:0x%04lX - %d\r\n"
|
||||
"Facility:%X Btn:%X\r\n",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
(uint32_t)instance->generic.data,
|
||||
instance->generic.serial,
|
||||
instance->generic.btn,
|
||||
instance->generic.cnt);
|
||||
instance->generic.serial,
|
||||
instance->generic.cnt,
|
||||
instance->generic.btn);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue