Update Subghz

This commit is contained in:
VerstreuteSeele
2023-01-03 21:35:41 +01:00
parent de6967c4e2
commit c8e0501feb
9 changed files with 806 additions and 4 deletions

View File

@@ -305,7 +305,7 @@ int32_t nfc_app(void* p) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateNfcV); scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateNfcV);
DOLPHIN_DEED(DolphinDeedNfcEmulate); DOLPHIN_DEED(DolphinDeedNfcEmulate);
} else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) { } else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDeviceInfo); scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
} else { } else {
scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid); scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
DOLPHIN_DEED(DolphinDeedNfcEmulate); DOLPHIN_DEED(DolphinDeedNfcEmulate);

View File

@@ -50,6 +50,7 @@ typedef enum {
SubGhzCustomEventSceneAnalyzerLock, SubGhzCustomEventSceneAnalyzerLock,
SubGhzCustomEventSceneAnalyzerUnlock, SubGhzCustomEventSceneAnalyzerUnlock,
SubGhzCustomEventSceneSettingLock, SubGhzCustomEventSceneSettingLock,
SubGhzCustomEventSceneSettingError,
SubGhzCustomEventSceneExit, SubGhzCustomEventSceneExit,
SubGhzCustomEventSceneStay, SubGhzCustomEventSceneStay,

View File

@@ -45,6 +45,35 @@ const float raw_theshold_rssi_value[RAW_THRESHOLD_RSSI_COUNT] = {
-40.0f, -40.0f,
}; };
#define BANDWIDTH_COUNT 16
const char* const bandwidth_labels[BANDWIDTH_COUNT] = {
"58 kHz",
"68 kHz",
"81 kHz",
"102 kHz",
"116 kHz",
"135 kHz",
"162 kHz",
"203 kHz",
"232 kHz",
"270 kHz",
"325 kHz",
"406 kHz",
"464 kHz",
"541 kHz",
"650 kHz",
"812 kHz",
};
// Bandwidths values are ordered from F (58kHz) to 0 (812kHz)
#define BANDWIDTH_INDEX(value) ((uint8_t)15 - ((uint8_t)(value >> 4) & 0x0F))
#define MANCHESTER_FLAG_COUNT 2
const char* const manchester_flag_text[MANCHESTER_FLAG_COUNT] = {
"OFF",
"ON",
};
#define HOPPING_COUNT 2 #define HOPPING_COUNT 2
const char* const hopping_text[HOPPING_COUNT] = { const char* const hopping_text[HOPPING_COUNT] = {
"OFF", "OFF",
@@ -99,6 +128,18 @@ const uint32_t speaker_value[SPEAKER_COUNT] = {
SubGhzSpeakerStateEnable, SubGhzSpeakerStateEnable,
}; };
// Allow advanced edit only on specific preset
bool subghz_scene_receiver_config_can_edit_current_preset(SubGhz* subghz) {
SubGhzRadioPreset* preset = subghz->txrx->preset;
bool preset_name_allow_edit =
!strcmp(furi_string_get_cstr(preset->name), ADVANCED_AM_PRESET_NAME) ||
!strcmp(furi_string_get_cstr(preset->name), "CUSTOM");
return preset && preset_name_allow_edit &&
subghz_preset_custom_is_ook_modulation(preset->data, preset->data_size);
}
uint8_t subghz_scene_receiver_config_next_frequency(const uint32_t value, void* context) { uint8_t subghz_scene_receiver_config_next_frequency(const uint32_t value, void* context) {
furi_assert(context); furi_assert(context);
SubGhz* subghz = context; SubGhz* subghz = context;
@@ -129,6 +170,52 @@ uint8_t subghz_scene_receiver_config_next_preset(const char* preset_name, void*
return index; return index;
} }
// Advanced settings of preset may change if preset was changed.
// In that case - update values
static void subghz_scene_receiver_config_update_advanced(SubGhz* subghz) {
uint8_t value_index;
if(subghz->variable_item_bandwidth) {
value_index = BANDWIDTH_INDEX(subghz->txrx->raw_bandwidth);
variable_item_set_current_value_index(subghz->variable_item_bandwidth, value_index);
variable_item_set_current_value_text(
subghz->variable_item_bandwidth, bandwidth_labels[value_index]);
}
if(subghz->variable_item_datarate) {
variable_item_set_current_value_index(subghz->variable_item_datarate, 0);
char datarate_str[16] = {0};
subghz_preset_custom_printf_datarate(
subghz->txrx->raw_datarate, datarate_str, sizeof(datarate_str));
variable_item_set_current_value_text(subghz->variable_item_datarate, datarate_str);
}
if(subghz->variable_item_manchester) {
value_index = subghz->txrx->raw_manchester_enabled ? 1 : 0;
variable_item_set_current_value_index(subghz->variable_item_manchester, value_index);
variable_item_set_current_value_text(
subghz->variable_item_manchester, manchester_flag_text[value_index]);
}
}
// Apply advanced configuration to advanced am preset
static void subghz_scene_receiver_config_apply_advanced(SubGhz* subghz) {
if(subghz_scene_receiver_config_can_edit_current_preset(subghz)) {
SubGhzRadioPreset* preset = subghz->txrx->preset;
subghz_preset_custom_set_bandwidth(
preset->data, preset->data_size, subghz->txrx->raw_bandwidth);
subghz_preset_custom_set_machester_enable(
preset->data, preset->data_size, subghz->txrx->raw_manchester_enabled);
subghz_preset_custom_set_datarate(
preset->data, preset->data_size, subghz->txrx->raw_datarate);
}
}
uint8_t subghz_scene_receiver_config_hopper_value_index( uint8_t subghz_scene_receiver_config_hopper_value_index(
const uint32_t value, const uint32_t value,
const uint32_t values[], const uint32_t values[],
@@ -214,6 +301,8 @@ static void subghz_scene_receiver_config_set_preset(VariableItem* item) {
subghz->txrx->preset->frequency, subghz->txrx->preset->frequency,
subghz_setting_get_preset_data(subghz->setting, index), subghz_setting_get_preset_data(subghz->setting, index),
subghz_setting_get_preset_data_size(subghz->setting, index)); subghz_setting_get_preset_data_size(subghz->setting, index));
subghz_scene_receiver_config_update_advanced(subghz);
} }
static void subghz_scene_receiver_config_set_rssi_threshold(VariableItem* item) { static void subghz_scene_receiver_config_set_rssi_threshold(VariableItem* item) {
@@ -307,6 +396,107 @@ static void subghz_scene_receiver_config_set_raw_threshold_rssi(VariableItem* it
subghz->txrx->raw_threshold_rssi = raw_theshold_rssi_value[index]; subghz->txrx->raw_threshold_rssi = raw_theshold_rssi_value[index];
} }
static void subghz_scene_receiver_config_set_raw_ook_bandwidth(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
if(subghz_scene_receiver_config_can_edit_current_preset(subghz)) {
// update bandwidth value from selected index
uint8_t index = variable_item_get_current_value_index(item);
subghz->txrx->raw_bandwidth = subghz_preset_custom_bandwidth_values[index];
subghz_scene_receiver_config_update_advanced(subghz);
} else {
furi_string_set(
subghz->error_str, "Read-only\nsetting!\nUse '" ADVANCED_AM_PRESET_NAME "'\npreset.");
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubGhzCustomEventSceneSettingError);
}
}
static void subghz_scene_receiver_config_set_manchester_flag(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
if(subghz_scene_receiver_config_can_edit_current_preset(subghz)) {
// update enable flag from view
uint8_t index = variable_item_get_current_value_index(item);
subghz->txrx->raw_manchester_enabled = index == 0 ? false : true;
subghz_scene_receiver_config_update_advanced(subghz);
} else {
furi_string_set(
subghz->error_str, "Read-only\nsetting!\nUse '" ADVANCED_AM_PRESET_NAME "'\npreset.");
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubGhzCustomEventSceneSettingError);
}
}
static void subghz_scene_receiver_config_datarate_input_callback(void* context) {
furi_assert(context);
SubGhz* subghz = context;
float value = atoff(subghz->datarate_input_str);
if(value != 0 && value > 0) {
subghz->txrx->raw_datarate = value;
subghz_scene_receiver_config_update_advanced(subghz);
}
// show list view
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList);
}
static bool subghz_scene_receiver_config_datarate_input_validate(
const char* text,
FuriString* error,
void* context) {
UNUSED(context);
float value = atoff(text);
if(value == 0) {
furi_string_printf(error, "Cannot parse\r\nvalue");
} else if(value < 0) {
furi_string_printf(error, "Value\r\nshould be\r\ngreater\r\nthan 0");
} else {
return true;
}
return false;
}
static void subghz_scene_receiver_config_show_datarate_input(SubGhz* subghz) {
TextInput* text_input = subghz->text_input;
snprintf(
subghz->datarate_input_str,
sizeof(subghz->datarate_input_str),
"%.2f",
(double)subghz->txrx->raw_datarate);
text_input_set_header_text(text_input, "Datarate bauds (not kBauds)");
text_input_set_result_callback(
text_input,
subghz_scene_receiver_config_datarate_input_callback,
subghz,
subghz->datarate_input_str,
sizeof(subghz->datarate_input_str),
false);
text_input_set_validator(
text_input, subghz_scene_receiver_config_datarate_input_validate, NULL);
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdTextInput);
}
static void subghz_scene_receiver_config_set_datarate(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
if(subghz_scene_receiver_config_can_edit_current_preset(subghz)) {
// reset value index in order to show '>' symbol always
variable_item_set_current_value_index(item, 0);
subghz_scene_receiver_config_show_datarate_input(subghz);
} else {
furi_string_set(
subghz->error_str, "Read-only\nsetting!\nUse '" ADVANCED_AM_PRESET_NAME "'\npreset.");
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubGhzCustomEventSceneSettingError);
}
}
static void subghz_scene_receiver_config_var_list_enter_callback(void* context, uint32_t index) { static void subghz_scene_receiver_config_var_list_enter_callback(void* context, uint32_t index) {
furi_assert(context); furi_assert(context);
SubGhz* subghz = context; SubGhz* subghz = context;
@@ -437,6 +627,33 @@ void subghz_scene_receiver_config_on_enter(void* context) {
subghz->txrx->raw_threshold_rssi, raw_theshold_rssi_value, RAW_THRESHOLD_RSSI_COUNT); subghz->txrx->raw_threshold_rssi, raw_theshold_rssi_value, RAW_THRESHOLD_RSSI_COUNT);
variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, raw_theshold_rssi_text[value_index]); variable_item_set_current_value_text(item, raw_theshold_rssi_text[value_index]);
// Advanced MODEM settings. RW only for ADVANCED_AM_PRESET_NAME
// Bandwidth
subghz->variable_item_bandwidth = variable_item_list_add(
subghz->variable_item_list,
"Bandwidth:",
BANDWIDTH_COUNT,
subghz_scene_receiver_config_set_raw_ook_bandwidth,
subghz);
// Data rate (editable via OK click)
subghz->variable_item_datarate = variable_item_list_add(
subghz->variable_item_list,
"Data rate:",
2,
subghz_scene_receiver_config_set_datarate,
subghz);
// Manchester codec flag
subghz->variable_item_manchester = variable_item_list_add(
subghz->variable_item_list,
"Manch. Enc.:",
MANCHESTER_FLAG_COUNT,
subghz_scene_receiver_config_set_manchester_flag,
subghz);
subghz_scene_receiver_config_update_advanced(subghz);
} }
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList); view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList);
} }
@@ -450,6 +667,11 @@ bool subghz_scene_receiver_config_on_event(void* context, SceneManagerEvent even
subghz->lock = SubGhzLockOn; subghz->lock = SubGhzLockOn;
scene_manager_previous_scene(subghz->scene_manager); scene_manager_previous_scene(subghz->scene_manager);
consumed = true; consumed = true;
} else if(event.event == SubGhzCustomEventSceneSettingError) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowErrorSub);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneShowErrorSub, event.event);
consumed = true;
} }
} }
return consumed; return consumed;
@@ -457,6 +679,16 @@ bool subghz_scene_receiver_config_on_event(void* context, SceneManagerEvent even
void subghz_scene_receiver_config_on_exit(void* context) { void subghz_scene_receiver_config_on_exit(void* context) {
SubGhz* subghz = context; SubGhz* subghz = context;
// reset UI variable list items (next scene may be not RAW config)
subghz->variable_item_bandwidth = NULL;
subghz->variable_item_datarate = NULL;
subghz->variable_item_manchester = NULL;
text_input_set_validator(subghz->text_input, NULL, NULL);
// apply advanced preset variables (if applicable)
subghz_scene_receiver_config_apply_advanced(subghz);
variable_item_list_set_selected_item(subghz->variable_item_list, 0); variable_item_list_set_selected_item(subghz->variable_item_list, 0);
variable_item_list_reset(subghz->variable_item_list); variable_item_list_reset(subghz->variable_item_list);
subghz_last_settings_save(subghz->last_settings); subghz_last_settings_save(subghz->last_settings);

View File

@@ -26,8 +26,16 @@ bool subghz_scene_show_error_sub_on_event(void* context, SceneManagerEvent event
SubGhz* subghz = context; SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom) { if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubGhzCustomEventSceneShowErrorSub) { if(event.event == SubGhzCustomEventSceneShowErrorSub) {
if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneShowErrorSub) ==
SubGhzCustomEventSceneSettingError) {
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneReceiverConfig);
} else {
scene_manager_search_and_switch_to_previous_scene( scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart); subghz->scene_manager, SubGhzSceneStart);
}
return true; return true;
} }
} }

View File

@@ -12,6 +12,25 @@ enum SubmenuIndex {
SubmenuIndexReadRAW, SubmenuIndexReadRAW,
}; };
void subghz_scene_start_remove_advanced_preset(SubGhz* subghz) {
// delete operation is harmless
subghz_setting_delete_custom_preset(subghz->setting, ADVANCED_AM_PRESET_NAME);
}
void subghz_scene_start_load_advanced_preset(SubGhz* subghz) {
for(uint8_t i = 0; i < subghz_setting_get_preset_count(subghz->setting); i++) {
if(!strcmp(subghz_setting_get_preset_name(subghz->setting, i), ADVANCED_AM_PRESET_NAME)) {
return; // already exists
}
}
// Load custom advanced AM preset with configurable CFGMDM settings
FlipperFormat* advanced_am_preset = subghz_preset_custom_advanced_am_preset_alloc();
subghz_setting_load_custom_preset(
subghz->setting, ADVANCED_AM_PRESET_NAME, advanced_am_preset);
flipper_format_free(advanced_am_preset);
}
void subghz_scene_start_submenu_callback(void* context, uint32_t index) { void subghz_scene_start_submenu_callback(void* context, uint32_t index) {
SubGhz* subghz = context; SubGhz* subghz = context;
view_dispatcher_send_custom_event(subghz->view_dispatcher, index); view_dispatcher_send_custom_event(subghz->view_dispatcher, index);
@@ -73,12 +92,14 @@ bool subghz_scene_start_on_event(void* context, SceneManagerEvent event) {
return true; return true;
} else if(event.type == SceneManagerEventTypeCustom) { } else if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexReadRAW) { if(event.event == SubmenuIndexReadRAW) {
subghz_scene_start_load_advanced_preset(subghz);
scene_manager_set_scene_state( scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneStart, SubmenuIndexReadRAW); subghz->scene_manager, SubGhzSceneStart, SubmenuIndexReadRAW);
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE; subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReadRAW); scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReadRAW);
return true; return true;
} else if(event.event == SubmenuIndexRead) { } else if(event.event == SubmenuIndexRead) {
subghz_scene_start_remove_advanced_preset(subghz);
scene_manager_set_scene_state( scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneStart, SubmenuIndexRead); subghz->scene_manager, SubGhzSceneStart, SubmenuIndexRead);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiver); scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiver);

View File

@@ -30,6 +30,12 @@ void subghz_preset_init(
subghz->txrx->preset->frequency = frequency; subghz->txrx->preset->frequency = frequency;
subghz->txrx->preset->data = preset_data; subghz->txrx->preset->data = preset_data;
subghz->txrx->preset->data_size = preset_data_size; subghz->txrx->preset->data_size = preset_data_size;
subghz->txrx->raw_bandwidth =
subghz_preset_custom_get_bandwidth(preset_data, preset_data_size);
subghz->txrx->raw_manchester_enabled =
subghz_preset_custom_get_machester_enable(preset_data, preset_data_size);
subghz->txrx->raw_datarate = subghz_preset_custom_get_datarate(preset_data, preset_data_size);
} }
bool subghz_set_preset(SubGhz* subghz, const char* preset) { bool subghz_set_preset(SubGhz* subghz, const char* preset) {

View File

@@ -3,6 +3,7 @@
#include "helpers/subghz_types.h" #include "helpers/subghz_types.h"
#include "helpers/subghz_error_type.h" #include "helpers/subghz_error_type.h"
#include <lib/subghz/types.h> #include <lib/subghz/types.h>
#include <lib/subghz/helpers/subghz_config_preset_custom.h>
#include "subghz.h" #include "subghz.h"
#include "views/receiver.h" #include "views/receiver.h"
#include "views/transmitter.h" #include "views/transmitter.h"
@@ -78,6 +79,13 @@ struct SubGhzTxRx {
float raw_threshold_rssi; float raw_threshold_rssi;
uint8_t raw_threshold_rssi_low_count; uint8_t raw_threshold_rssi_low_count;
// one of the 16 possible bandwidth values
uint8_t raw_bandwidth;
// datarate in bauds
float raw_datarate;
// flag if manchester encoding/decoding enabled
bool raw_manchester_enabled;
}; };
typedef struct SubGhzTxRx SubGhzTxRx; typedef struct SubGhzTxRx SubGhzTxRx;
@@ -106,6 +114,13 @@ struct SubGhz {
SubGhzViewTransmitter* subghz_transmitter; SubGhzViewTransmitter* subghz_transmitter;
VariableItemList* variable_item_list; VariableItemList* variable_item_list;
// Advanced config items
VariableItem* variable_item_bandwidth; // specific config list view item: bandwidth
VariableItem* variable_item_datarate; // specific config list view item: data rate
VariableItem* variable_item_manchester; // specific config list view item: manchester enc flag
// Advanced config strings
char datarate_input_str[16];
SubGhzFrequencyAnalyzer* subghz_frequency_analyzer; SubGhzFrequencyAnalyzer* subghz_frequency_analyzer;
SubGhzReadRAW* subghz_read_raw; SubGhzReadRAW* subghz_read_raw;
bool raw_send_only; bool raw_send_only;

View File

@@ -0,0 +1,326 @@
#include "subghz_config_preset_custom.h"
#include <stdio.h>
#include <core/log.h>
#include <core/core_defines.h> // UNUSED()
#include <core/check.h> // furi_assert()
#include <math.h> // log2(), floor()
#include <flipper_format/flipper_format.h>
// https://www.ti.com/lit/ds/symlink/cc1101.pdf?ts=1671943815135
// page 35.
// 12 Data Rate Programming
//
#define DATARATE_FUNC_CHIP_FOSC 26000000.0 /* 26MHz */
#define DATARATE_FUNC_DIVIDER (1 << 28) /* 2 pow 28 */
#define DATARATE_FUNC_MULTIPLIER \
(DATARATE_FUNC_CHIP_FOSC / DATARATE_FUNC_DIVIDER) /* should be 0.09685754 */
#define DATARATE_EXP_FORMULA_DIVISIBLE (1 << 20) /* 2 pow 20 */
#define DATARATE_EXP_FORMULA_MULTIPLIER \
(DATARATE_EXP_FORMULA_DIVISIBLE / DATARATE_FUNC_CHIP_FOSC) /* should be 0.04032984 */
#define DATARATE_MNT_FORMULA_DIVISIBLE (1 << 28) /* 2 pow 28 */
#define DATARATE_MNT_FORMULA_MULTIPLIER \
(DATARATE_MNT_FORMULA_DIVISIBLE / DATARATE_FUNC_CHIP_FOSC) /* should be 10.3244406 */
//
#define SUGHZ_CONFIG_TAG "SubGHz_Config"
uint8_t furi_hal_subghz_preset_ook_custom_async_regs[PRESET_OOK_CUSTOM_ADVANCED_AM_SIZE] = {0};
/** Check if cursom preset is AM (OOK) modulation
*
* This will check MOD_FORMAT bits in CC1101_MDMCFG2 register
* If preset data doesn have this register - will return false.
* This function will not fail in any case
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_is_ook_modulation(const uint8_t* preset_data, uint8_t data_len) {
if(preset_data != NULL) {
for(uint8_t i = 2; i <= data_len; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG2) {
return (preset_data[i - 1] & 0b01110000) == 0x30;
}
}
}
return false;
}
/** Get bandwidth value from preset data.
*
* This will get HIGHER bits in CC1101_MDMCFG4 register
* If CC1101_MDMCFG4 is not found in preset data - will return
* CH_BANDWIDTH_INVALID (0xFF)
* If there is ANY low 4 bits in returned value - the value is invalid
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
uint8_t subghz_preset_custom_get_bandwidth(const uint8_t* preset_data, uint8_t data_len) {
if(preset_data != NULL) {
for(uint8_t i = 2; i <= data_len; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG4) {
return (preset_data[i - 1] & 0b11110000);
}
}
}
return CH_BANDWIDTH_INVALID;
}
/** Set bandwidth value to preset data.
*
* This will set HIGHER bits in CC1101_MDMCFG4 register
* If CC1101_MDMCFG4 is not found in preset data - will do nothing and return false
* If there are ANY low 4 bits in provided value - they will be ignored
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
* @param value New bandwidth value. See macros definition for possible values
*/
bool subghz_preset_custom_set_bandwidth(uint8_t* preset_data, uint8_t data_len, uint8_t value) {
if(preset_data != NULL) {
for(uint8_t i = 2; i <= data_len; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG4) {
preset_data[i - 1] = (preset_data[i - 1] & 0b00001111) | (0b11110000 & value);
return true;
}
}
}
return false;
}
/** Get data rate value from preset data.
*
* This will get DRATE_M and DRATE_E bits from CC1101_MDMCFG3 and CC1101_MDMCFG4 registers
* and calculate the value for 26MHz chip oscillator by formula from datasheet.
*
* If CC1101_MDMCFG[3:4] are not found in preset data - will return `-1`
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
float subghz_preset_custom_get_datarate(const uint8_t* preset_data, uint8_t data_len) {
if(preset_data != NULL) {
uint8_t mantissa = 0xFF;
uint8_t exponent = 0xFF; // Invalid, only 4 lower bits are singificant
uint8_t step = 0;
for(uint8_t i = 2; i <= data_len && step < 2; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG4) {
exponent = preset_data[i - 1] & 0b00001111;
step++;
} else if(preset_data[i - 2] == CC1101_MDMCFG3) {
mantissa = preset_data[i - 1];
step++;
}
}
if(step == 2) {
return (float)((256 + mantissa) * (1 << exponent) * DATARATE_FUNC_MULTIPLIER);
}
}
return -1;
}
/** Set data rate value to preset data.
*
* This will update DRATE_M and DRATE_E bits from CC1101_MDMCFG3 and CC1101_MDMCFG4 registers
* with calculated values for 26MHz chip oscillator by formula from datasheet.
*
* If CC1101_MDMCFG[3:4] are not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
* @param value value in kBaud
*/
bool subghz_preset_custom_set_datarate(uint8_t* preset_data, uint8_t data_len, float value) {
if(preset_data != NULL) {
uint8_t* pMantissa = NULL;
uint8_t* pExponent = NULL;
uint8_t step = 0;
for(uint8_t i = 2; i <= data_len && step < 2; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG4) {
pExponent = &preset_data[i - 1];
step++;
} else if(preset_data[i - 2] == CC1101_MDMCFG3) {
pMantissa = &preset_data[i - 1];
step++;
}
}
// Has both registers in data - calculate values
if(step == 2) {
// │ value * 2^20 │
// DRATE_E = │log2(──────────────)│
// └ Fosc ┘
double exponent = floor(log2(value * DATARATE_EXP_FORMULA_MULTIPLIER));
uint8_t datarate_e = (uint8_t)exponent;
// value * 2^28
// DRATE_M = (────────────────────) - 256
// Fosc * 2^DRATE_E
double mantissa =
floor((value * DATARATE_MNT_FORMULA_MULTIPLIER) / (1 << datarate_e) + 0.5) - 256;
// If DRATE_M is rounded to the nearest integer and becomes 256, increment DRATE_E and use DRATE_M = 0.
if(mantissa >= 256) {
mantissa = 0;
datarate_e += 1;
}
uint8_t datarate_m = (uint8_t)mantissa;
*pExponent = (*pExponent & 0b11110000) | (datarate_e & 0b00001111);
*pMantissa = datarate_m;
return true;
}
}
return false;
}
/** Print datarate value to string
*
* This is just convenience function
*
* @param datarate datarate obtained from `subghz_preset_custom_get_datarate` function
* @param string Target print buffer
* @param size Target print buffer size
*/
void subghz_preset_custom_printf_datarate(float datarate, char* string, uint8_t size) {
float kBaudRate = datarate / 1000.0f;
snprintf(
string,
size,
"%lu.%02lu kBd",
(uint32_t)(kBaudRate), // decimal part
(uint32_t)((kBaudRate - (uint32_t)kBaudRate) * 100) // fractional part multiplied by 100
);
}
/** Get Manchester encoding/decoding flag value from preset data.
*
* This will get MANCHESTER_EN (3-rd) bit in CC1101_MDMCFG2 register
* If CC1101_MDMCFG2 is not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_get_machester_enable(const uint8_t* preset_data, uint8_t data_len) {
if(preset_data != NULL) {
for(uint8_t i = 2; i <= data_len; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG2) {
return (preset_data[i - 1] & 0b00001000);
}
}
}
return false;
}
/** Set Manchester encoding/decoding flag value to preset data.
*
* This will set MANCHESTER_EN (3-rd) bit in CC1101_MDMCFG2 register
* If CC1101_MDMCFG2 is not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_set_machester_enable(uint8_t* preset_data, uint8_t data_len, bool value) {
if(preset_data != NULL) {
for(uint8_t i = 2; i <= data_len; i += 2) {
if(preset_data[i - 2] == CC1101_MDMCFG2) {
preset_data[i - 1] = (preset_data[i - 1] & 0b11110111) | (0b00001000 * value);
return true;
}
}
}
return false;
}
/**
* Initialize custom preset data
*/
void subghz_preset_custom_init_advanced_am_preset() {
FURI_LOG_D(SUGHZ_CONFIG_TAG, "Initializing AM preset with custom Modem configuration");
if(furi_hal_subghz_preset_ook_custom_async_regs[0]) {
// already initialized
FURI_LOG_D(SUGHZ_CONFIG_TAG, "Already initialized");
return;
}
// Copy default AM270 preset
memcpy(
&furi_hal_subghz_preset_ook_custom_async_regs,
&furi_hal_subghz_preset_ook_270khz_async_regs,
sizeof(furi_hal_subghz_preset_ook_270khz_async_regs));
const uint8_t ModemConfigStart = 4;
#if FURI_DEBUG
const uint8_t ModemConfigEnd = ModemConfigStart + MODEM_CONFIG_REGISTERS_COUNT;
for(uint8_t i = ModemConfigStart; i < ModemConfigEnd; ++i) {
// Check we'll overwrite correct settings
furi_assert(
furi_hal_subghz_preset_ook_custom_async_regs[i * 2 + 0] ==
furi_hal_subghz_custom_modulation_regs[i - ModemConfigStart][0]);
}
#endif
// Copy CUSTOM Modem preset
memcpy(
&furi_hal_subghz_preset_ook_custom_async_regs[ModemConfigStart * 2],
&furi_hal_subghz_custom_modulation_regs,
sizeof(furi_hal_subghz_custom_modulation_regs));
// Copy default AM270 patable
memcpy(
&furi_hal_subghz_preset_ook_custom_async_regs[sizeof(
furi_hal_subghz_preset_ook_270khz_async_regs)],
&furi_hal_subghz_preset_ook_async_patable,
sizeof(furi_hal_subghz_preset_ook_async_patable));
// Here at the end we should have
// <AM270 bytes> <CFGMDM regs> <AM270 bytes> 00 00 <AM270 patable>
#if FURI_DEBUG
FURI_LOG_D(SUGHZ_CONFIG_TAG, "Custom OOK preset created");
for(uint8_t i = 0; i < PRESET_OOK_CUSTOM_ADVANCED_AM_SIZE; i += 2) {
FURI_LOG_D(
SUGHZ_CONFIG_TAG,
"Register: 0x%hhX, Value: 0x%hhX",
furi_hal_subghz_preset_ook_custom_async_regs[i * 2 + 0],
furi_hal_subghz_preset_ook_custom_async_regs[i * 2 + 1]);
}
#endif
FURI_LOG_D(SUGHZ_CONFIG_TAG, "Done");
}
/**
* Create subghz preset file with custom am preset
* this is used for preset initialization if subghz app
*/
FlipperFormat* subghz_preset_custom_advanced_am_preset_alloc() {
FlipperFormat* advanced_am_preset = flipper_format_string_alloc();
subghz_preset_custom_init_advanced_am_preset();
flipper_format_write_hex(
advanced_am_preset,
(const char*)"Custom_preset_data",
(const uint8_t*)&furi_hal_subghz_preset_ook_custom_async_regs[0],
sizeof(furi_hal_subghz_preset_ook_custom_async_regs));
flipper_format_rewind(advanced_am_preset);
return advanced_am_preset;
}

View File

@@ -0,0 +1,193 @@
#pragma once
#include <furi_hal_subghz_configs.h>
#include <string.h> /* memcpy() */
#define ADVANCED_AM_PRESET_NAME "AM*"
// Awailable bandwidth values
// Setup in MDMCFG4 register
#define CH_BANDWIDTH_058 0b11110000
#define CH_BANDWIDTH_068 0b11100000
#define CH_BANDWIDTH_081 0b11010000
#define CH_BANDWIDTH_102 0b11000000
#define CH_BANDWIDTH_116 0b10110000
#define CH_BANDWIDTH_135 0b10100000
#define CH_BANDWIDTH_162 0b10010000
#define CH_BANDWIDTH_203 0b10000000
#define CH_BANDWIDTH_232 0b01110000
#define CH_BANDWIDTH_270 0b01100000
#define CH_BANDWIDTH_325 0b01010000
#define CH_BANDWIDTH_406 0b01000000
#define CH_BANDWIDTH_464 0b00110000
#define CH_BANDWIDTH_541 0b00100000
#define CH_BANDWIDTH_650 0b00010000
#define CH_BANDWIDTH_812 0b00000000
#define CH_BANDWIDTH_INVALID 0xFF
static const uint8_t subghz_preset_custom_bandwidth_values[] = {
CH_BANDWIDTH_058,
CH_BANDWIDTH_068,
CH_BANDWIDTH_081,
CH_BANDWIDTH_102,
CH_BANDWIDTH_116,
CH_BANDWIDTH_135,
CH_BANDWIDTH_162,
CH_BANDWIDTH_203,
CH_BANDWIDTH_232,
CH_BANDWIDTH_270,
CH_BANDWIDTH_325,
CH_BANDWIDTH_406,
CH_BANDWIDTH_464,
CH_BANDWIDTH_541,
CH_BANDWIDTH_650,
CH_BANDWIDTH_812,
};
#define CH_BANDWIDTH_NUM (sizeof(subghz_preset_custom_bandwidth_values) / sizeof(uint8_t))
#define DATARATE_EXPONENT_3_79_kBaud 0b00000111 // 7
#define DATARATE_MANTISSA_3_79_kBaud 0x32
#define CHANNEL_SPACING_25_EXPONENT 0b00000000 /* last bit */
#define CHANNEL_SPACING_25_MANTISSA 0x00
#define MODEM_CONFIG_REGISTERS_COUNT 5
#define PRESET_OOK_CUSTOM_ADVANCED_AM_SIZE \
sizeof(furi_hal_subghz_preset_ook_270khz_async_regs) + \
sizeof(furi_hal_subghz_preset_ook_async_patable)
extern uint8_t furi_hal_subghz_preset_ook_custom_async_regs[PRESET_OOK_CUSTOM_ADVANCED_AM_SIZE];
static const uint8_t furi_hal_subghz_custom_modulation_regs[MODEM_CONFIG_REGISTERS_COUNT][2] = {
// Channel spacing is 25kHz, no Forward Error Correction, 2 preamble bytes (will be ignored)
{CC1101_MDMCFG0, CHANNEL_SPACING_25_MANTISSA},
{CC1101_MDMCFG1, 0x00 | CHANNEL_SPACING_25_EXPONENT},
// [0:2] SYNC_MODE = 00 // No preamble/sync
// [3:3] MANCHESTER_EN = 0 // Disable
// [4:6] MOD_FORMAT = 03 // Format ASK/OOK
// [7:7] DEM_DCFILT_OFF = 0 // Enable
{CC1101_MDMCFG2, 0x30},
// 3.79 kBaud data rate (mantissa in 3rd register)
{CC1101_MDMCFG3, DATARATE_MANTISSA_3_79_kBaud},
// 270.8333 kHz Rx BW filer (hi) and 3.79 kBaud data rate (exponent in 4th register)
{CC1101_MDMCFG4, DATARATE_EXPONENT_3_79_kBaud | CH_BANDWIDTH_270},
};
#ifdef __cplusplus
extern "C" {
#endif
/** Check if cursom preset is AM (OOK) modulation
*
* This will check MOD_FORMAT bits in CC1101_MDMCFG2 register
* If preset data doesn have this register - will return false.
* This function will not fail in any case
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_is_ook_modulation(const uint8_t* preset_data, uint8_t data_len);
/** Get bandwidth value from preset data.
*
* This will get HIGHER bits in CC1101_MDMCFG4 register
* If CC1101_MDMCFG4 is not found in preset data - will return
* CH_BANDWIDTH_INVALID (0xFF)
* If there is ANY low 4 bits in returned value - the value is invalid
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
uint8_t subghz_preset_custom_get_bandwidth(const uint8_t* preset_data, uint8_t data_len);
/** Set bandwidth value to preset data.
*
* This will set HIGHER bits in CC1101_MDMCFG4 register
* If CC1101_MDMCFG4 is not found in preset data - will do nothing and return false
* If there are ANY low 4 bits in provided value - they will be ignored
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
* @param value New bandwidth value. See macros definition for possible values
*/
bool subghz_preset_custom_set_bandwidth(uint8_t* preset_data, uint8_t data_len, uint8_t value);
/** Get data rate value from preset data.
*
* This will get DRATE_M and DRATE_E bits from CC1101_MDMCFG3 and CC1101_MDMCFG4 registers
* and calculate the value for 26MHz chip oscillator by formula from datasheet.
*
* If CC1101_MDMCFG[3:4] are not found in preset data - will return `-1`
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
float subghz_preset_custom_get_datarate(const uint8_t* preset_data, uint8_t data_len);
/** Set data rate value to preset data.
*
* This will update DRATE_M and DRATE_E bits from CC1101_MDMCFG3 and CC1101_MDMCFG4 registers
* with calculated values for 26MHz chip oscillator by formula from datasheet.
*
* If CC1101_MDMCFG[3:4] are not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
* @param value value in kBaud
*/
bool subghz_preset_custom_set_datarate(uint8_t* preset_data, uint8_t data_len, float value);
/** Print datarate value to string
*
* This is just conviniece function
*
* @param datarate datarate obtained from `subghz_preset_custom_get_datarate` function
* @param string Target print buffer
* @param size Target print buffer size
*/
void subghz_preset_custom_printf_datarate(float datarate, char* string, uint8_t size);
/** Get Manchester encoding/decoding flag value from preset data.
*
* This will get MANCHESTER_EN (3-rd) bit in CC1101_MDMCFG2 register
* If CC1101_MDMCFG2 is not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_get_machester_enable(const uint8_t* preset_data, uint8_t data_len);
/** Set Manchester encoding/decoding flag value to preset data.
*
* This will set MANCHESTER_EN (3-rd) bit in CC1101_MDMCFG2 register
* If CC1101_MDMCFG2 is not found in preset data - will return false
*
* @param preset_data Custom preset data (registers and patable)
* @param data_len Data length
*/
bool subghz_preset_custom_set_machester_enable(uint8_t* preset_data, uint8_t data_len, bool value);
/**
* Initialize advanced am custom preset
*/
void subghz_preset_custom_init_advanced_am_preset();
/**
* Create subghz preset file with custom am preset
* this is used for preset initialization if subghz app
*/
struct FlipperFormat* subghz_preset_custom_advanced_am_preset_alloc();
#ifdef __cplusplus
}
#endif