2023-02-07 19:33:05 +03:00
|
|
|
#include <furi_hal_subghz.h>
|
|
|
|
#include <furi_hal_subghz_configs.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
#include <furi_hal_version.h>
|
|
|
|
#include <furi_hal_rtc.h>
|
2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal_spi.h>
|
|
|
|
#include <furi_hal_interrupt.h>
|
|
|
|
#include <furi_hal_resources.h>
|
2023-02-02 22:47:50 +03:00
|
|
|
#include <furi_hal_power.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-03-30 18:23:40 +03:00
|
|
|
#include <stm32wbxx_ll_dma.h>
|
|
|
|
|
2022-07-29 17:48:51 +03:00
|
|
|
#include <lib/flipper_format/flipper_format.h>
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
#include <furi.h>
|
|
|
|
#include <cc1101.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalSubGhz"
|
2023-02-02 22:47:50 +03:00
|
|
|
//Initialisation timeout (ms)
|
|
|
|
#define INIT_TIMEOUT 10
|
2021-11-12 16:04:35 +03:00
|
|
|
|
2022-12-17 01:20:10 +03:00
|
|
|
static uint32_t furi_hal_subghz_debug_gpio_buff[2];
|
2022-06-14 05:22:17 +03:00
|
|
|
|
2023-02-08 07:41:22 +03:00
|
|
|
/* DMA Channels definition */
|
|
|
|
#define SUBGHZ_DMA DMA2
|
|
|
|
#define SUBGHZ_DMA_CH1_CHANNEL LL_DMA_CHANNEL_1
|
|
|
|
#define SUBGHZ_DMA_CH2_CHANNEL LL_DMA_CHANNEL_2
|
|
|
|
#define SUBGHZ_DMA_CH1_IRQ FuriHalInterruptIdDma2Ch1
|
|
|
|
#define SUBGHZ_DMA_CH1_DEF SUBGHZ_DMA, SUBGHZ_DMA_CH1_CHANNEL
|
|
|
|
#define SUBGHZ_DMA_CH2_DEF SUBGHZ_DMA, SUBGHZ_DMA_CH2_CHANNEL
|
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
volatile FuriHalSubGhz furi_hal_subghz = {
|
|
|
|
.state = SubGhzStateInit,
|
|
|
|
.regulation = SubGhzRegulationTxRx,
|
|
|
|
.preset = FuriHalSubGhzPresetIDLE,
|
2022-12-17 01:20:10 +03:00
|
|
|
.async_mirror_pin = NULL,
|
2023-02-02 22:47:50 +03:00
|
|
|
.radio_type = SubGhzRadioInternal,
|
|
|
|
.spi_bus_handle = &furi_hal_spi_bus_handle_subghz,
|
|
|
|
.cc1101_g0_pin = &gpio_cc1101_g0,
|
2023-02-25 22:28:35 +03:00
|
|
|
.rolling_counter_mult = 1,
|
2023-03-09 04:02:31 +03:00
|
|
|
.ext_module_power_disabled = false,
|
2021-12-08 16:42:01 +03:00
|
|
|
};
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
bool furi_hal_subghz_set_radio_type(SubGhzRadioType state) {
|
|
|
|
furi_hal_subghz.radio_type = state;
|
|
|
|
furi_hal_spi_bus_handle_deinit(furi_hal_subghz.spi_bus_handle);
|
2023-02-12 02:41:11 +03:00
|
|
|
|
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioInternal) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_subghz.spi_bus_handle = &furi_hal_spi_bus_handle_subghz;
|
|
|
|
furi_hal_subghz.cc1101_g0_pin = &gpio_cc1101_g0;
|
2023-02-12 02:41:11 +03:00
|
|
|
} else {
|
|
|
|
furi_hal_subghz.spi_bus_handle = &furi_hal_spi_bus_handle_subghz_ext;
|
|
|
|
furi_hal_subghz.cc1101_g0_pin = &gpio_cc1101_g0_ext;
|
2023-02-02 22:47:50 +03:00
|
|
|
}
|
2023-02-12 02:41:11 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_bus_handle_init(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_subghz_init_check();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzRadioType furi_hal_subghz_get_radio_type(void) {
|
|
|
|
return furi_hal_subghz.radio_type;
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:28:35 +03:00
|
|
|
uint8_t furi_hal_subghz_get_rolling_counter_mult(void) {
|
|
|
|
return furi_hal_subghz.rolling_counter_mult;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_set_rolling_counter_mult(uint8_t mult) {
|
|
|
|
furi_hal_subghz.rolling_counter_mult = mult;
|
|
|
|
}
|
|
|
|
|
2023-03-09 04:02:31 +03:00
|
|
|
void furi_hal_subghz_set_external_power_disable(bool state) {
|
|
|
|
furi_hal_subghz.ext_module_power_disabled = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_get_external_power_disable(void) {
|
|
|
|
return furi_hal_subghz.ext_module_power_disabled;
|
|
|
|
}
|
|
|
|
|
2022-12-17 01:20:10 +03:00
|
|
|
void furi_hal_subghz_set_async_mirror_pin(const GpioPin* pin) {
|
|
|
|
furi_hal_subghz.async_mirror_pin = pin;
|
|
|
|
}
|
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
void furi_hal_subghz_init(void) {
|
|
|
|
furi_hal_subghz_init_check();
|
|
|
|
}
|
|
|
|
|
2023-03-08 01:09:17 +03:00
|
|
|
bool furi_hal_subghz_enable_ext_power(void) {
|
2023-03-09 04:02:31 +03:00
|
|
|
if(furi_hal_subghz.ext_module_power_disabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-08 01:09:17 +03:00
|
|
|
if(furi_hal_subghz.radio_type != SubGhzRadioInternal) {
|
|
|
|
uint8_t attempts = 0;
|
2023-03-13 18:43:50 +03:00
|
|
|
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
|
2023-03-08 01:09:17 +03:00
|
|
|
furi_hal_power_enable_otg();
|
|
|
|
//CC1101 power-up time
|
2023-03-13 18:43:50 +03:00
|
|
|
furi_delay_ms(10);
|
2023-03-08 01:09:17 +03:00
|
|
|
}
|
2023-02-02 22:47:50 +03:00
|
|
|
}
|
2023-03-08 01:09:17 +03:00
|
|
|
return furi_hal_power_is_otg_enabled();
|
2023-02-02 22:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_disable_ext_power(void) {
|
2023-03-09 01:23:57 +03:00
|
|
|
if(furi_hal_power_is_otg_enabled()) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_power_disable_otg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_check_radio(void) {
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2023-03-06 10:08:59 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
uint8_t ver = cc1101_get_version(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
|
|
|
|
|
|
|
if((ver != 0) && (ver != 255)) {
|
|
|
|
FURI_LOG_D(TAG, "Radio check ok");
|
|
|
|
} else {
|
2023-03-26 14:59:21 +03:00
|
|
|
FURI_LOG_D(TAG, "Radio check failed, revert to default");
|
2023-03-06 10:08:59 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_init_check(void) {
|
|
|
|
bool result = true;
|
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateInit);
|
|
|
|
furi_hal_subghz.state = SubGhzStateIdle;
|
|
|
|
furi_hal_subghz.preset = FuriHalSubGhzPresetIDLE;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_SUBGHZ_TX_GPIO
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_init(&FURI_HAL_SUBGHZ_TX_GPIO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Reset
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
cc1101_reset(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Prepare GD0 for power on self test
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
// GD0 low
|
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHW);
|
|
|
|
uint32_t test_start_time = furi_get_tick();
|
|
|
|
while(furi_hal_gpio_read(furi_hal_subghz.cc1101_g0_pin) != false && result) {
|
|
|
|
if(furi_get_tick() - test_start_time > INIT_TIMEOUT) {
|
|
|
|
result = false;
|
2023-02-02 22:47:50 +03:00
|
|
|
}
|
2023-02-12 02:41:11 +03:00
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
// GD0 high
|
|
|
|
cc1101_write_reg(
|
|
|
|
furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHW | CC1101_IOCFG_INV);
|
|
|
|
test_start_time = furi_get_tick();
|
|
|
|
while(furi_hal_gpio_read(furi_hal_subghz.cc1101_g0_pin) != true && result) {
|
|
|
|
if(furi_get_tick() - test_start_time > INIT_TIMEOUT) {
|
|
|
|
result = false;
|
|
|
|
}
|
2023-02-10 02:09:29 +03:00
|
|
|
}
|
2023-02-12 02:41:11 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
// Reset GD0 to floating state
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
|
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// RF switches
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Go to sleep
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_shutdown(furi_hal_subghz.spi_bus_handle);
|
|
|
|
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
if(result) {
|
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
|
|
|
} else {
|
2023-03-26 14:59:21 +03:00
|
|
|
FURI_LOG_E(TAG, "Selected CC1101 module init failed, revert to default");
|
2023-02-02 22:47:50 +03:00
|
|
|
}
|
|
|
|
return result;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_sleep() {
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateIdle);
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
|
|
|
|
cc1101_switch_to_idle(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
|
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_shutdown(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_hal_subghz.preset = FuriHalSubGhzPresetIDLE;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_dump_state() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
printf(
|
|
|
|
"[furi_hal_subghz] cc1101 chip %d, version %d\r\n",
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_get_partnumber(furi_hal_subghz.spi_bus_handle),
|
|
|
|
cc1101_get_version(furi_hal_subghz.spi_bus_handle));
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_load_preset(FuriHalSubGhzPreset preset) {
|
|
|
|
if(preset == FuriHalSubGhzPresetOok650Async) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers((uint8_t*)furi_hal_subghz_preset_ook_650khz_async_regs);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
|
2021-09-15 18:24:19 +03:00
|
|
|
} else if(preset == FuriHalSubGhzPresetOok270Async) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers((uint8_t*)furi_hal_subghz_preset_ook_270khz_async_regs);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
|
2021-10-10 17:35:10 +03:00
|
|
|
} else if(preset == FuriHalSubGhzPreset2FSKDev238Async) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers(
|
|
|
|
(uint8_t*)furi_hal_subghz_preset_2fsk_dev2_38khz_async_regs);
|
2021-10-10 17:35:10 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
|
|
|
|
} else if(preset == FuriHalSubGhzPreset2FSKDev476Async) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers(
|
|
|
|
(uint8_t*)furi_hal_subghz_preset_2fsk_dev47_6khz_async_regs);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
|
2021-12-01 18:44:39 +03:00
|
|
|
} else if(preset == FuriHalSubGhzPresetMSK99_97KbAsync) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers((uint8_t*)furi_hal_subghz_preset_msk_99_97kb_async_regs);
|
2021-12-01 18:44:39 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_msk_async_patable);
|
2021-12-08 16:42:01 +03:00
|
|
|
} else if(preset == FuriHalSubGhzPresetGFSK9_99KbAsync) {
|
2022-07-26 17:16:59 +03:00
|
|
|
furi_hal_subghz_load_registers((uint8_t*)furi_hal_subghz_preset_gfsk_9_99kb_async_regs);
|
2021-12-08 16:42:01 +03:00
|
|
|
furi_hal_subghz_load_patable(furi_hal_subghz_preset_gfsk_async_patable);
|
2021-12-22 14:05:14 +03:00
|
|
|
} else {
|
2022-06-01 01:35:31 +03:00
|
|
|
furi_crash("SubGhz: Missing config.");
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_hal_subghz.preset = preset;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:16:59 +03:00
|
|
|
void furi_hal_subghz_load_custom_preset(uint8_t* preset_data) {
|
|
|
|
//load config
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_reset(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
uint32_t i = 0;
|
2022-07-26 17:16:59 +03:00
|
|
|
uint8_t pa[8] = {0};
|
|
|
|
while(preset_data[i]) {
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, preset_data[i], preset_data[i + 1]);
|
2022-07-26 17:16:59 +03:00
|
|
|
i += 2;
|
|
|
|
}
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2022-07-26 17:16:59 +03:00
|
|
|
|
|
|
|
//load pa table
|
|
|
|
memcpy(&pa[0], &preset_data[i + 2], 8);
|
|
|
|
furi_hal_subghz_load_patable(pa);
|
|
|
|
furi_hal_subghz.preset = FuriHalSubGhzPresetCustom;
|
|
|
|
|
|
|
|
//show debug
|
|
|
|
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
|
|
|
|
i = 0;
|
|
|
|
FURI_LOG_D(TAG, "Loading custom preset");
|
|
|
|
while(preset_data[i]) {
|
|
|
|
FURI_LOG_D(TAG, "Reg[%lu]: %02X=%02X", i, preset_data[i], preset_data[i + 1]);
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
for(uint8_t y = i; y < i + 10; y++) {
|
2022-10-07 16:35:15 +03:00
|
|
|
FURI_LOG_D(TAG, "PA[%u]: %02X", y, preset_data[y]);
|
2022-07-26 17:16:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_load_registers(uint8_t* data) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_reset(furi_hal_subghz.spi_bus_handle);
|
2022-07-26 17:16:59 +03:00
|
|
|
uint32_t i = 0;
|
|
|
|
while(data[i]) {
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, data[i], data[i + 1]);
|
2022-07-26 17:16:59 +03:00
|
|
|
i += 2;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_load_patable(const uint8_t data[8]) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_set_pa_table(furi_hal_subghz.spi_bus_handle, data);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_flush_tx(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_FIFO, size);
|
|
|
|
cc1101_write_fifo(furi_hal_subghz.spi_bus_handle, data, size);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_flush_rx() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_flush_rx(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
void furi_hal_subghz_flush_tx() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_flush_tx(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-12-08 16:42:01 +03:00
|
|
|
}
|
|
|
|
|
2021-12-01 18:44:39 +03:00
|
|
|
bool furi_hal_subghz_rx_pipe_not_empty() {
|
|
|
|
CC1101RxBytes status[1];
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-12-22 14:05:14 +03:00
|
|
|
cc1101_read_reg(
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_subghz.spi_bus_handle, (CC1101_STATUS_RXBYTES) | CC1101_BURST, (uint8_t*)status);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-12-01 18:44:39 +03:00
|
|
|
// TODO: you can add a buffer overflow flag if needed
|
|
|
|
if(status->NUM_RXBYTES > 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_is_rx_data_crc_valid() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-12-01 18:44:39 +03:00
|
|
|
uint8_t data[1];
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_read_reg(furi_hal_subghz.spi_bus_handle, CC1101_STATUS_LQI | CC1101_BURST, data);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(((data[0] >> 7) & 0x01)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
void furi_hal_subghz_read_packet(uint8_t* data, uint8_t* size) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_read_fifo(furi_hal_subghz.spi_bus_handle, data, size);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_shutdown() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
// Reset and shutdown
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_shutdown(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_reset() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
cc1101_switch_to_idle(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_reset(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_idle() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_switch_to_idle(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_rx() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_switch_to_rx(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-28 03:05:40 +03:00
|
|
|
bool furi_hal_subghz_tx() {
|
2022-06-14 05:22:17 +03:00
|
|
|
if(furi_hal_subghz.regulation != SubGhzRegulationTxRx) return false;
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
cc1101_switch_to_tx(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-28 03:05:40 +03:00
|
|
|
return true;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
float furi_hal_subghz_get_rssi() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
int32_t rssi_dec = cc1101_get_rssi(furi_hal_subghz.spi_bus_handle);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
float rssi = rssi_dec;
|
|
|
|
if(rssi_dec >= 128) {
|
|
|
|
rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
|
|
|
|
} else {
|
|
|
|
rssi = (rssi / 2.0f) - 74.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rssi;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
uint8_t furi_hal_subghz_get_lqi() {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-12-08 16:42:01 +03:00
|
|
|
uint8_t data[1];
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_read_reg(furi_hal_subghz.spi_bus_handle, CC1101_STATUS_LQI | CC1101_BURST, data);
|
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-12-08 16:42:01 +03:00
|
|
|
return data[0] & 0x7F;
|
|
|
|
}
|
|
|
|
|
2022-07-29 17:48:51 +03:00
|
|
|
/*
|
2022-08-03 00:32:55 +03:00
|
|
|
Modified by @tkerby & MX to the full YARD Stick One extended range of 281-361 MHz, 378-481 MHz, and 749-962 MHz.
|
2022-07-29 17:48:51 +03:00
|
|
|
These changes are at your own risk. The PLL may not lock and FZ devs have warned of possible damage!
|
|
|
|
*/
|
2022-08-03 00:32:55 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
bool furi_hal_subghz_is_frequency_valid(uint32_t value) {
|
2022-08-03 00:32:55 +03:00
|
|
|
if(!(value >= 281000000 && value <= 361000000) &&
|
|
|
|
!(value >= 378000000 && value <= 481000000) &&
|
|
|
|
!(value >= 749000000 && value <= 962000000)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t furi_hal_subghz_set_frequency_and_path(uint32_t value) {
|
|
|
|
// Set these values to the extended frequency range only. They dont define if you can transmit but do select the correct RF path
|
|
|
|
value = furi_hal_subghz_set_frequency(value);
|
|
|
|
if(value >= 281000000 && value <= 361000000) {
|
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPath315);
|
|
|
|
} else if(value >= 378000000 && value <= 481000000) {
|
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPath433);
|
|
|
|
} else if(value >= 749000000 && value <= 962000000) {
|
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPath868);
|
|
|
|
} else {
|
|
|
|
furi_crash("SubGhz: Incorrect frequency during set.");
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_is_tx_allowed(uint32_t value) {
|
2022-07-29 17:48:51 +03:00
|
|
|
bool is_extended = false;
|
|
|
|
|
2022-08-03 00:32:55 +03:00
|
|
|
// TODO: !!! Move file check to another place
|
2022-08-13 17:58:46 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-07-29 17:48:51 +03:00
|
|
|
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
|
|
|
|
|
|
|
|
if(flipper_format_file_open_existing(fff_data_file, "/ext/subghz/assets/dangerous_settings")) {
|
|
|
|
flipper_format_read_bool(
|
|
|
|
fff_data_file, "yes_i_want_to_destroy_my_flipper", &is_extended, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
flipper_format_free(fff_data_file);
|
2022-08-13 17:58:46 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-07-29 17:48:51 +03:00
|
|
|
|
2023-02-12 02:48:20 +03:00
|
|
|
if(!(value >= 299999755 && value <= 350000335) && // was increased from 348 to 350
|
|
|
|
!(value >= 386999938 && value <= 467750000) && // was increased from 464 to 467.75
|
2022-07-29 17:48:51 +03:00
|
|
|
!(value >= 778999847 && value <= 928000000) && !(is_extended)) {
|
|
|
|
FURI_LOG_I(TAG, "Frequency blocked - outside default range");
|
|
|
|
return false;
|
|
|
|
} else if(
|
|
|
|
!(value >= 281000000 && value <= 361000000) &&
|
|
|
|
!(value >= 378000000 && value <= 481000000) &&
|
|
|
|
!(value >= 749000000 && value <= 962000000) && is_extended) {
|
|
|
|
FURI_LOG_I(TAG, "Frequency blocked - outside dangerous range");
|
2021-09-10 05:19:02 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-28 03:05:40 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-01 18:44:39 +03:00
|
|
|
uint32_t furi_hal_subghz_set_frequency(uint32_t value) {
|
2022-08-03 00:32:55 +03:00
|
|
|
furi_hal_subghz.regulation = SubGhzRegulationTxRx;
|
2021-09-28 03:05:40 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
|
|
|
uint32_t real_frequency = cc1101_set_frequency(furi_hal_subghz.spi_bus_handle, value);
|
|
|
|
cc1101_calibrate(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
while(true) {
|
2023-02-02 22:47:50 +03:00
|
|
|
CC1101Status status = cc1101_get_status(furi_hal_subghz.spi_bus_handle);
|
2021-09-15 18:24:19 +03:00
|
|
|
if(status.STATE == CC1101StateIDLE) break;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
return real_frequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_set_path(FuriHalSubGhzPath path) {
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_acquire(furi_hal_subghz.spi_bus_handle);
|
2021-09-15 18:24:19 +03:00
|
|
|
if(path == FuriHalSubGhzPath433) {
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_write(&gpio_rf_sw_0, 0);
|
2021-12-22 14:05:14 +03:00
|
|
|
cc1101_write_reg(
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_subghz.spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
|
2021-09-15 18:24:19 +03:00
|
|
|
} else if(path == FuriHalSubGhzPath315) {
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_write(&gpio_rf_sw_0, 1);
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW);
|
2021-09-15 18:24:19 +03:00
|
|
|
} else if(path == FuriHalSubGhzPath868) {
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_write(&gpio_rf_sw_0, 1);
|
2021-12-22 14:05:14 +03:00
|
|
|
cc1101_write_reg(
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_subghz.spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
|
2021-09-15 18:24:19 +03:00
|
|
|
} else if(path == FuriHalSubGhzPathIsolate) {
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_write(&gpio_rf_sw_0, 0);
|
2023-02-02 22:47:50 +03:00
|
|
|
cc1101_write_reg(furi_hal_subghz.spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW);
|
2021-09-10 05:19:02 +03:00
|
|
|
} else {
|
2022-04-09 21:47:14 +03:00
|
|
|
furi_crash("SubGhz: Incorrect path during set.");
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_spi_release(furi_hal_subghz.spi_bus_handle);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-12-17 01:20:10 +03:00
|
|
|
static bool furi_hal_subghz_start_debug() {
|
|
|
|
bool ret = false;
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL) {
|
|
|
|
furi_hal_gpio_init(
|
|
|
|
furi_hal_subghz.async_mirror_pin,
|
|
|
|
GpioModeOutputPushPull,
|
|
|
|
GpioPullNo,
|
|
|
|
GpioSpeedVeryHigh);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool furi_hal_subghz_stop_debug() {
|
|
|
|
bool ret = false;
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL) {
|
|
|
|
furi_hal_gpio_init(
|
|
|
|
furi_hal_subghz.async_mirror_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-10 02:09:29 +03:00
|
|
|
volatile uint32_t furi_hal_subghz_capture_delta_duration = 0;
|
2021-09-10 05:19:02 +03:00
|
|
|
volatile FuriHalSubGhzCaptureCallback furi_hal_subghz_capture_callback = NULL;
|
|
|
|
volatile void* furi_hal_subghz_capture_callback_context = NULL;
|
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
static void furi_hal_subghz_capture_int_ISR() {
|
|
|
|
// Channel 1
|
|
|
|
if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
|
|
|
|
LL_TIM_ClearFlag_CC1(TIM2);
|
|
|
|
furi_hal_subghz_capture_delta_duration = LL_TIM_IC_GetCaptureCH1(TIM2);
|
|
|
|
if(furi_hal_subghz_capture_callback) {
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL)
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.async_mirror_pin, false);
|
|
|
|
|
|
|
|
furi_hal_subghz_capture_callback(
|
|
|
|
true,
|
|
|
|
furi_hal_subghz_capture_delta_duration,
|
|
|
|
(void*)furi_hal_subghz_capture_callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Channel 2
|
|
|
|
if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
|
|
|
|
LL_TIM_ClearFlag_CC2(TIM2);
|
|
|
|
if(furi_hal_subghz_capture_callback) {
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL)
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.async_mirror_pin, true);
|
|
|
|
|
|
|
|
furi_hal_subghz_capture_callback(
|
|
|
|
false,
|
|
|
|
LL_TIM_IC_GetCaptureCH2(TIM2) - furi_hal_subghz_capture_delta_duration,
|
|
|
|
(void*)furi_hal_subghz_capture_callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 02:09:29 +03:00
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
static void furi_hal_subghz_capture_ext_ISR() {
|
|
|
|
if(!furi_hal_gpio_read(furi_hal_subghz.cc1101_g0_pin)) {
|
|
|
|
if(furi_hal_subghz_capture_callback) {
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL)
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.async_mirror_pin, false);
|
2022-12-17 01:20:10 +03:00
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
furi_hal_subghz_capture_callback(
|
|
|
|
true, TIM2->CNT, (void*)furi_hal_subghz_capture_callback_context);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2023-02-02 22:47:50 +03:00
|
|
|
} else {
|
2023-02-12 02:41:11 +03:00
|
|
|
if(furi_hal_subghz_capture_callback) {
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL)
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.async_mirror_pin, true);
|
|
|
|
|
|
|
|
furi_hal_subghz_capture_callback(
|
|
|
|
false, TIM2->CNT, (void*)furi_hal_subghz_capture_callback_context);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-12 02:41:11 +03:00
|
|
|
TIM2->CNT = 6;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_start_async_rx(FuriHalSubGhzCaptureCallback callback, void* context) {
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateIdle);
|
|
|
|
furi_hal_subghz.state = SubGhzStateAsyncRx;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
furi_hal_subghz_capture_callback = callback;
|
|
|
|
furi_hal_subghz_capture_callback_context = context;
|
|
|
|
|
|
|
|
// Timer: base
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
2021-09-15 18:24:19 +03:00
|
|
|
TIM_InitStruct.Prescaler = 64 - 1;
|
2021-09-10 05:19:02 +03:00
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 0x7FFFFFFE;
|
2023-02-12 02:41:11 +03:00
|
|
|
// Clock division for capture filter (for internal radio)
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV4;
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
|
|
|
|
|
|
|
// Timer: advanced
|
|
|
|
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_DisableARRPreload(TIM2);
|
2023-02-12 02:41:11 +03:00
|
|
|
LL_TIM_DisableDMAReq_TRIG(TIM2);
|
|
|
|
LL_TIM_DisableIT_TRIG(TIM2);
|
|
|
|
|
2023-02-10 02:09:29 +03:00
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioInternal) {
|
|
|
|
LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI2FP2);
|
|
|
|
LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
|
|
|
|
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
|
|
|
LL_TIM_EnableMasterSlaveMode(TIM2);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-10 02:09:29 +03:00
|
|
|
// Timer: channel 1 indirect
|
|
|
|
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_INDIRECTTI);
|
|
|
|
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
|
|
|
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
|
|
|
|
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
|
|
|
|
|
|
|
|
// Timer: channel 2 direct
|
|
|
|
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
|
|
|
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
|
|
|
|
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
|
|
|
|
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV32_N8);
|
|
|
|
|
|
|
|
// ISR setup
|
2023-02-12 02:41:11 +03:00
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, furi_hal_subghz_capture_int_ISR, NULL);
|
2023-02-10 02:09:29 +03:00
|
|
|
|
|
|
|
// Interrupts and channels
|
|
|
|
LL_TIM_EnableIT_CC1(TIM2);
|
|
|
|
LL_TIM_EnableIT_CC2(TIM2);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
2023-02-12 02:41:11 +03:00
|
|
|
|
|
|
|
furi_hal_gpio_init_ex(
|
|
|
|
furi_hal_subghz.cc1101_g0_pin,
|
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullNo,
|
|
|
|
GpioSpeedLow,
|
|
|
|
GpioAltFn1TIM2);
|
|
|
|
} else {
|
|
|
|
furi_hal_gpio_init(
|
|
|
|
furi_hal_subghz.cc1101_g0_pin,
|
|
|
|
GpioModeInterruptRiseFall,
|
|
|
|
GpioPullUp,
|
|
|
|
GpioSpeedVeryHigh);
|
|
|
|
furi_hal_gpio_add_int_callback(
|
|
|
|
furi_hal_subghz.cc1101_g0_pin,
|
|
|
|
furi_hal_subghz_capture_ext_ISR,
|
|
|
|
furi_hal_subghz_capture_callback);
|
|
|
|
furi_hal_gpio_enable_int_callback(furi_hal_subghz.cc1101_g0_pin);
|
2023-02-10 02:09:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
// Start timer
|
|
|
|
LL_TIM_SetCounter(TIM2, 0);
|
|
|
|
LL_TIM_EnableCounter(TIM2);
|
|
|
|
|
2022-12-17 01:20:10 +03:00
|
|
|
// Start debug
|
|
|
|
furi_hal_subghz_start_debug();
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
// Switch to RX
|
|
|
|
furi_hal_subghz_rx();
|
2023-02-08 16:20:42 +03:00
|
|
|
|
2023-02-08 17:48:07 +03:00
|
|
|
// Clear the variable after the end of the session
|
2023-02-10 02:09:29 +03:00
|
|
|
furi_hal_subghz_capture_delta_duration = 0;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_stop_async_rx() {
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateAsyncRx);
|
|
|
|
furi_hal_subghz.state = SubGhzStateIdle;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Shutdown radio
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_DeInit(TIM2);
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2022-12-17 01:20:10 +03:00
|
|
|
// Stop debug
|
|
|
|
furi_hal_subghz_stop_debug();
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2023-02-10 02:09:29 +03:00
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioInternal) {
|
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
|
2023-02-12 02:41:11 +03:00
|
|
|
} else {
|
|
|
|
furi_hal_gpio_disable_int_callback(furi_hal_subghz.cc1101_g0_pin);
|
|
|
|
furi_hal_gpio_remove_int_callback(furi_hal_subghz.cc1101_g0_pin);
|
2023-02-10 02:09:29 +03:00
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t* buffer;
|
2022-11-30 14:41:23 +03:00
|
|
|
LevelDuration carry_ld;
|
2021-09-10 05:19:02 +03:00
|
|
|
FuriHalSubGhzAsyncTxCallback callback;
|
|
|
|
void* callback_context;
|
2021-12-15 15:23:16 +03:00
|
|
|
uint64_t duty_high;
|
|
|
|
uint64_t duty_low;
|
2021-09-10 05:19:02 +03:00
|
|
|
} FuriHalSubGhzAsyncTx;
|
|
|
|
|
|
|
|
static FuriHalSubGhzAsyncTx furi_hal_subghz_async_tx = {0};
|
|
|
|
|
|
|
|
static void furi_hal_subghz_async_tx_refill(uint32_t* buffer, size_t samples) {
|
2022-11-30 14:41:23 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateAsyncTx);
|
2021-09-15 18:24:19 +03:00
|
|
|
while(samples > 0) {
|
2021-09-10 05:19:02 +03:00
|
|
|
bool is_odd = samples % 2;
|
2022-11-30 14:41:23 +03:00
|
|
|
LevelDuration ld;
|
|
|
|
if(level_duration_is_reset(furi_hal_subghz_async_tx.carry_ld)) {
|
|
|
|
ld = furi_hal_subghz_async_tx.callback(furi_hal_subghz_async_tx.callback_context);
|
|
|
|
} else {
|
|
|
|
ld = furi_hal_subghz_async_tx.carry_ld;
|
|
|
|
furi_hal_subghz_async_tx.carry_ld = level_duration_reset();
|
|
|
|
}
|
2021-12-15 15:23:16 +03:00
|
|
|
|
|
|
|
if(level_duration_is_wait(ld)) {
|
2022-11-30 14:41:23 +03:00
|
|
|
*buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
|
|
|
|
buffer++;
|
|
|
|
samples--;
|
2021-12-15 15:23:16 +03:00
|
|
|
} else if(level_duration_is_reset(ld)) {
|
2022-11-30 14:41:23 +03:00
|
|
|
*buffer = 0;
|
|
|
|
buffer++;
|
|
|
|
samples--;
|
2023-02-08 07:41:22 +03:00
|
|
|
LL_DMA_DisableIT_HT(SUBGHZ_DMA_CH1_DEF);
|
|
|
|
LL_DMA_DisableIT_TC(SUBGHZ_DMA_CH1_DEF);
|
2022-11-30 14:41:23 +03:00
|
|
|
LL_TIM_EnableIT_UPDATE(TIM2);
|
2021-09-10 05:19:02 +03:00
|
|
|
break;
|
|
|
|
} else {
|
2021-12-15 15:23:16 +03:00
|
|
|
bool level = level_duration_get_level(ld);
|
2022-11-30 14:41:23 +03:00
|
|
|
|
|
|
|
// Inject guard time if level is incorrect
|
|
|
|
if(is_odd != level) {
|
2021-09-10 05:19:02 +03:00
|
|
|
*buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
|
|
|
|
buffer++;
|
|
|
|
samples--;
|
2022-11-30 14:41:23 +03:00
|
|
|
if(is_odd) {
|
2021-12-15 15:23:16 +03:00
|
|
|
furi_hal_subghz_async_tx.duty_high += API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
|
|
|
|
} else {
|
|
|
|
furi_hal_subghz_async_tx.duty_low += API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
|
|
|
|
}
|
2022-11-30 14:41:23 +03:00
|
|
|
|
|
|
|
// Special case: prevent buffer overflow if sample is last
|
|
|
|
if(samples == 0) {
|
|
|
|
furi_hal_subghz_async_tx.carry_ld = ld;
|
|
|
|
break;
|
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t duration = level_duration_get_duration(ld);
|
2021-11-01 23:35:54 +03:00
|
|
|
furi_assert(duration > 0);
|
2021-09-10 05:19:02 +03:00
|
|
|
*buffer = duration;
|
|
|
|
buffer++;
|
|
|
|
samples--;
|
2021-12-15 15:23:16 +03:00
|
|
|
|
2022-11-30 14:41:23 +03:00
|
|
|
if(is_odd) {
|
2021-12-15 15:23:16 +03:00
|
|
|
furi_hal_subghz_async_tx.duty_high += duration;
|
|
|
|
} else {
|
|
|
|
furi_hal_subghz_async_tx.duty_low += duration;
|
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void furi_hal_subghz_async_tx_dma_isr() {
|
2022-11-30 14:41:23 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateAsyncTx);
|
2023-02-08 07:41:22 +03:00
|
|
|
|
|
|
|
#if SUBGHZ_DMA_CH1_CHANNEL == LL_DMA_CHANNEL_1
|
|
|
|
if(LL_DMA_IsActiveFlag_HT1(SUBGHZ_DMA)) {
|
|
|
|
LL_DMA_ClearFlag_HT1(SUBGHZ_DMA);
|
2021-09-15 18:24:19 +03:00
|
|
|
furi_hal_subghz_async_tx_refill(
|
|
|
|
furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2023-02-08 07:41:22 +03:00
|
|
|
if(LL_DMA_IsActiveFlag_TC1(SUBGHZ_DMA)) {
|
|
|
|
LL_DMA_ClearFlag_TC1(SUBGHZ_DMA);
|
2021-09-15 18:24:19 +03:00
|
|
|
furi_hal_subghz_async_tx_refill(
|
|
|
|
furi_hal_subghz_async_tx.buffer + API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF,
|
|
|
|
API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2023-02-08 07:41:22 +03:00
|
|
|
#else
|
|
|
|
#error Update this code. Would you kindly?
|
|
|
|
#endif
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void furi_hal_subghz_async_tx_timer_isr() {
|
|
|
|
if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) {
|
|
|
|
LL_TIM_ClearFlag_UPDATE(TIM2);
|
2021-09-15 18:24:19 +03:00
|
|
|
if(LL_TIM_GetAutoReload(TIM2) == 0) {
|
2022-06-14 05:22:17 +03:00
|
|
|
if(furi_hal_subghz.state == SubGhzStateAsyncTx) {
|
|
|
|
furi_hal_subghz.state = SubGhzStateAsyncTxLast;
|
2023-02-08 07:41:22 +03:00
|
|
|
LL_DMA_DisableChannel(SUBGHZ_DMA_CH1_DEF);
|
2022-11-30 14:41:23 +03:00
|
|
|
} else if(furi_hal_subghz.state == SubGhzStateAsyncTxLast) {
|
|
|
|
furi_hal_subghz.state = SubGhzStateAsyncTxEnd;
|
2021-09-28 03:05:40 +03:00
|
|
|
//forcibly pulls the pin to the ground so that there is no carrier
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_gpio_init(
|
|
|
|
furi_hal_subghz.cc1101_g0_pin, GpioModeInput, GpioPullDown, GpioSpeedLow);
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_DisableCounter(TIM2);
|
2022-11-30 14:41:23 +03:00
|
|
|
} else {
|
|
|
|
furi_crash(NULL);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 03:05:40 +03:00
|
|
|
bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void* context) {
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_assert(furi_hal_subghz.state == SubGhzStateIdle);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_assert(callback);
|
|
|
|
|
2021-09-28 03:05:40 +03:00
|
|
|
//If transmission is prohibited by regional settings
|
2022-06-14 05:22:17 +03:00
|
|
|
if(furi_hal_subghz.regulation != SubGhzRegulationTxRx) return false;
|
2021-09-28 03:05:40 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_subghz_async_tx.callback = callback;
|
|
|
|
furi_hal_subghz_async_tx.callback_context = context;
|
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_hal_subghz.state = SubGhzStateAsyncTx;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-12-15 15:23:16 +03:00
|
|
|
furi_hal_subghz_async_tx.duty_low = 0;
|
|
|
|
furi_hal_subghz_async_tx.duty_high = 0;
|
|
|
|
|
2021-09-15 18:24:19 +03:00
|
|
|
furi_hal_subghz_async_tx.buffer =
|
2022-02-18 22:53:46 +03:00
|
|
|
malloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2023-02-12 02:41:11 +03:00
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioInternal) {
|
2023-02-10 02:09:29 +03:00
|
|
|
// Connect CC1101_GD0 to TIM2 as output
|
|
|
|
furi_hal_gpio_init_ex(
|
2023-02-12 02:41:11 +03:00
|
|
|
furi_hal_subghz.cc1101_g0_pin,
|
2023-02-10 02:09:29 +03:00
|
|
|
GpioModeAltFunctionPushPull,
|
|
|
|
GpioPullDown,
|
|
|
|
GpioSpeedLow,
|
|
|
|
GpioAltFn1TIM2);
|
2023-02-12 02:41:11 +03:00
|
|
|
} else {
|
2023-02-13 00:43:58 +03:00
|
|
|
//Signal generation with mem-to-mem DMA
|
2023-02-12 02:41:11 +03:00
|
|
|
furi_hal_gpio_write(furi_hal_subghz.cc1101_g0_pin, true);
|
|
|
|
furi_hal_gpio_init(
|
|
|
|
furi_hal_subghz.cc1101_g0_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
|
2023-02-10 02:09:29 +03:00
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Configure DMA
|
|
|
|
LL_DMA_InitTypeDef dma_config = {0};
|
2021-09-15 18:24:19 +03:00
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR);
|
2021-09-10 05:19:02 +03:00
|
|
|
dma_config.MemoryOrM2MDstAddress = (uint32_t)furi_hal_subghz_async_tx.buffer;
|
|
|
|
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
|
|
|
dma_config.Mode = LL_DMA_MODE_CIRCULAR;
|
|
|
|
dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
|
|
|
|
dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
|
|
|
|
dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
|
|
|
|
dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
|
|
|
|
dma_config.NbData = API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL;
|
|
|
|
dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP;
|
|
|
|
dma_config.Priority = LL_DMA_MODE_NORMAL;
|
2023-02-08 07:41:22 +03:00
|
|
|
LL_DMA_Init(SUBGHZ_DMA_CH1_DEF, &dma_config);
|
|
|
|
furi_hal_interrupt_set_isr(SUBGHZ_DMA_CH1_IRQ, furi_hal_subghz_async_tx_dma_isr, NULL);
|
|
|
|
LL_DMA_EnableIT_TC(SUBGHZ_DMA_CH1_DEF);
|
|
|
|
LL_DMA_EnableIT_HT(SUBGHZ_DMA_CH1_DEF);
|
|
|
|
LL_DMA_EnableChannel(SUBGHZ_DMA_CH1_DEF);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Configure TIM2
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
2021-09-15 18:24:19 +03:00
|
|
|
TIM_InitStruct.Prescaler = 64 - 1;
|
2021-09-10 05:19:02 +03:00
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 1000;
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
|
|
|
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_EnableARRPreload(TIM2);
|
|
|
|
|
|
|
|
// Configure TIM2 CH2
|
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
|
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.CompareValue = 0;
|
2022-11-30 14:41:23 +03:00
|
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_LOW;
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
|
|
|
|
LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2);
|
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM2);
|
|
|
|
|
2022-03-29 20:37:23 +03:00
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, furi_hal_subghz_async_tx_timer_isr, NULL);
|
2022-03-28 16:42:31 +03:00
|
|
|
|
2022-11-30 14:41:23 +03:00
|
|
|
furi_hal_subghz_async_tx_refill(
|
|
|
|
furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL);
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_EnableDMAReq_UPDATE(TIM2);
|
|
|
|
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
|
|
|
|
|
|
|
|
// Start counter
|
|
|
|
LL_TIM_GenerateEvent_UPDATE(TIM2);
|
|
|
|
#ifdef FURI_HAL_SUBGHZ_TX_GPIO
|
2022-03-30 18:23:40 +03:00
|
|
|
furi_hal_gpio_write(&FURI_HAL_SUBGHZ_TX_GPIO, true);
|
2021-09-10 05:19:02 +03:00
|
|
|
#endif
|
|
|
|
furi_hal_subghz_tx();
|
|
|
|
|
|
|
|
LL_TIM_SetCounter(TIM2, 0);
|
|
|
|
LL_TIM_EnableCounter(TIM2);
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2023-02-13 00:43:58 +03:00
|
|
|
// Start debug
|
|
|
|
if(furi_hal_subghz_start_debug() || furi_hal_subghz.radio_type == SubGhzRadioExternal) {
|
|
|
|
const GpioPin* gpio = furi_hal_subghz.cc1101_g0_pin;
|
|
|
|
//Preparing bit mask
|
|
|
|
//Debug pin is may be only PORTB! (PB0, PB1, .., PB15)
|
|
|
|
furi_hal_subghz_debug_gpio_buff[0] = 0;
|
|
|
|
furi_hal_subghz_debug_gpio_buff[1] = 0;
|
|
|
|
|
|
|
|
//Mirror pin (for example, speaker)
|
|
|
|
if(furi_hal_subghz.async_mirror_pin != NULL) {
|
|
|
|
furi_hal_subghz_debug_gpio_buff[0] |= (uint32_t)furi_hal_subghz.async_mirror_pin->pin
|
|
|
|
<< GPIO_NUMBER;
|
|
|
|
furi_hal_subghz_debug_gpio_buff[1] |= furi_hal_subghz.async_mirror_pin->pin;
|
|
|
|
gpio = furi_hal_subghz.async_mirror_pin;
|
|
|
|
}
|
2023-02-10 00:56:29 +03:00
|
|
|
|
2023-02-13 00:43:58 +03:00
|
|
|
//G0 singnal generation for external radio
|
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioExternal) {
|
|
|
|
furi_hal_subghz_debug_gpio_buff[0] |= (uint32_t)furi_hal_subghz.cc1101_g0_pin->pin
|
|
|
|
<< GPIO_NUMBER;
|
|
|
|
furi_hal_subghz_debug_gpio_buff[1] |= furi_hal_subghz.cc1101_g0_pin->pin;
|
|
|
|
}
|
2023-02-10 02:09:29 +03:00
|
|
|
|
|
|
|
dma_config.MemoryOrM2MDstAddress = (uint32_t)furi_hal_subghz_debug_gpio_buff;
|
|
|
|
dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (gpio->port->BSRR);
|
|
|
|
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
|
|
|
dma_config.Mode = LL_DMA_MODE_CIRCULAR;
|
|
|
|
dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
|
|
|
|
dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
|
|
|
|
dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
|
|
|
|
dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
|
|
|
|
dma_config.NbData = 2;
|
|
|
|
dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP;
|
|
|
|
dma_config.Priority = LL_DMA_PRIORITY_VERYHIGH;
|
|
|
|
LL_DMA_Init(SUBGHZ_DMA_CH2_DEF, &dma_config);
|
|
|
|
LL_DMA_SetDataLength(SUBGHZ_DMA_CH2_DEF, 2);
|
|
|
|
LL_DMA_EnableChannel(SUBGHZ_DMA_CH2_DEF);
|
|
|
|
}
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2021-09-28 03:05:40 +03:00
|
|
|
return true;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_subghz_is_async_tx_complete() {
|
2022-06-14 05:30:51 +03:00
|
|
|
return furi_hal_subghz.state == SubGhzStateAsyncTxEnd;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_subghz_stop_async_tx() {
|
|
|
|
furi_assert(
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_hal_subghz.state == SubGhzStateAsyncTx ||
|
|
|
|
furi_hal_subghz.state == SubGhzStateAsyncTxLast ||
|
|
|
|
furi_hal_subghz.state == SubGhzStateAsyncTxEnd);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Shutdown radio
|
|
|
|
furi_hal_subghz_idle();
|
2023-02-10 02:09:29 +03:00
|
|
|
if(furi_hal_subghz.radio_type == SubGhzRadioExternal) {
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.cc1101_g0_pin, false);
|
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Deinitialize Timer
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_TIM_DeInit(TIM2);
|
2022-03-29 20:37:23 +03:00
|
|
|
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Deinitialize DMA
|
2023-02-08 07:41:22 +03:00
|
|
|
LL_DMA_DeInit(SUBGHZ_DMA_CH1_DEF);
|
2022-03-29 20:37:23 +03:00
|
|
|
|
2023-02-08 07:41:22 +03:00
|
|
|
furi_hal_interrupt_set_isr(SUBGHZ_DMA_CH1_IRQ, NULL, NULL);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
// Deinitialize GPIO
|
2023-02-02 22:47:50 +03:00
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2023-02-10 00:56:29 +03:00
|
|
|
// Stop debug
|
|
|
|
furi_hal_subghz_stop_debug();
|
|
|
|
|
2023-02-10 02:09:29 +03:00
|
|
|
if(((furi_hal_subghz.async_mirror_pin != NULL) &&
|
|
|
|
(furi_hal_subghz.radio_type == SubGhzRadioInternal)) ||
|
|
|
|
(furi_hal_subghz.radio_type == SubGhzRadioExternal)) {
|
|
|
|
LL_DMA_DisableChannel(SUBGHZ_DMA_CH2_DEF);
|
|
|
|
}
|
2022-08-27 11:06:25 +03:00
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
free(furi_hal_subghz_async_tx.buffer);
|
|
|
|
|
2021-12-22 14:05:14 +03:00
|
|
|
float duty_cycle =
|
|
|
|
100.0f * (float)furi_hal_subghz_async_tx.duty_high /
|
|
|
|
((float)furi_hal_subghz_async_tx.duty_low + (float)furi_hal_subghz_async_tx.duty_high);
|
|
|
|
FURI_LOG_D(
|
|
|
|
TAG,
|
|
|
|
"Async TX Radio stats: on %0.0fus, off %0.0fus, DutyCycle: %0.0f%%",
|
2022-05-11 12:45:01 +03:00
|
|
|
(double)furi_hal_subghz_async_tx.duty_high,
|
|
|
|
(double)furi_hal_subghz_async_tx.duty_low,
|
|
|
|
(double)duty_cycle);
|
2021-12-15 15:23:16 +03:00
|
|
|
|
2022-06-14 05:22:17 +03:00
|
|
|
furi_hal_subghz.state = SubGhzStateIdle;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|