app_subghz.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. #include "custom_presets.h"
  5. #include <flipper_format/flipper_format_i.h>
  6. #include <furi_hal_rtc.h>
  7. #include <furi_hal_spi.h>
  8. #include <furi_hal_interrupt.h>
  9. void raw_sampling_worker_start(ProtoViewApp *app);
  10. void raw_sampling_worker_stop(ProtoViewApp *app);
  11. ProtoViewModulation ProtoViewModulations[] = {
  12. {"OOK 650Khz", FuriHalSubGhzPresetOok650Async, NULL},
  13. {"OOK 270Khz", FuriHalSubGhzPresetOok270Async, NULL},
  14. {"2FSK 2.38Khz", FuriHalSubGhzPreset2FSKDev238Async, NULL},
  15. {"2FSK 47.6Khz", FuriHalSubGhzPreset2FSKDev476Async, NULL},
  16. {"MSK", FuriHalSubGhzPresetMSK99_97KbAsync, NULL},
  17. {"GFSK", FuriHalSubGhzPresetGFSK9_99KbAsync, NULL},
  18. {"TPMS 1 (FSK)", 0, (uint8_t*)protoview_subghz_tpms1_async_regs},
  19. {"TPMS 2 (FSK)", 0, (uint8_t*)protoview_subghz_tpms2_async_regs},
  20. {"TPMS 3 (FSK)", 0, (uint8_t*)protoview_subghz_tpms3_async_regs},
  21. {NULL, 0, NULL} /* End of list sentinel. */
  22. };
  23. /* Called after the application initialization in order to setup the
  24. * subghz system and put it into idle state. If the user wants to start
  25. * receiving we will call radio_rx() to start a receiving worker and
  26. * associated thread. */
  27. void radio_begin(ProtoViewApp* app) {
  28. furi_assert(app);
  29. furi_hal_subghz_reset();
  30. furi_hal_subghz_idle();
  31. /* The CC1101 preset can be either one of the standard presets, if
  32. * the modulation "custom" field is NULL, or a custom preset we
  33. * defined in custom_presets.h. */
  34. if (ProtoViewModulations[app->modulation].custom == NULL)
  35. furi_hal_subghz_load_preset(ProtoViewModulations[app->modulation].preset);
  36. else
  37. furi_hal_subghz_load_custom_preset(ProtoViewModulations[app->modulation].custom);
  38. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  39. app->txrx->txrx_state = TxRxStateIDLE;
  40. }
  41. /* Setup subghz to start receiving using a background worker. */
  42. uint32_t radio_rx(ProtoViewApp* app) {
  43. furi_assert(app);
  44. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  45. furi_crash(TAG" Incorrect RX frequency.");
  46. }
  47. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  48. furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
  49. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  50. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  51. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  52. furi_hal_subghz_flush_rx();
  53. furi_hal_subghz_rx();
  54. if (!app->txrx->debug_timer_sampling) {
  55. furi_hal_subghz_start_async_rx(subghz_worker_rx_callback,
  56. app->txrx->worker);
  57. subghz_worker_start(app->txrx->worker);
  58. } else {
  59. raw_sampling_worker_start(app);
  60. }
  61. app->txrx->txrx_state = TxRxStateRx;
  62. return value;
  63. }
  64. /* Stop subghz worker (if active), put radio on idle state. */
  65. void radio_rx_end(ProtoViewApp* app) {
  66. furi_assert(app);
  67. if (app->txrx->txrx_state == TxRxStateRx) {
  68. if (!app->txrx->debug_timer_sampling) {
  69. if(subghz_worker_is_running(app->txrx->worker)) {
  70. subghz_worker_stop(app->txrx->worker);
  71. furi_hal_subghz_stop_async_rx();
  72. }
  73. } else {
  74. raw_sampling_worker_stop(app);
  75. }
  76. }
  77. furi_hal_subghz_idle();
  78. app->txrx->txrx_state = TxRxStateIDLE;
  79. }
  80. /* Put radio on sleep. */
  81. void radio_sleep(ProtoViewApp* app) {
  82. furi_assert(app);
  83. if (app->txrx->txrx_state == TxRxStateRx) {
  84. /* We can't go from having an active RX worker to sleeping.
  85. * Stop the RX subsystems first. */
  86. radio_rx_end(app);
  87. }
  88. furi_hal_subghz_sleep();
  89. app->txrx->txrx_state = TxRxStateSleep;
  90. }
  91. /* ============================= Raw sampling mode =============================
  92. * This is a special mode that uses a high frequency timer to sample the
  93. * CC1101 pin directly. It's useful for debugging purposes when we want
  94. * to get the raw data from the chip and completely bypass the subghz
  95. * Flipper system.
  96. * ===========================================================================*/
  97. void protoview_timer_isr(void *ctx) {
  98. ProtoViewApp *app = ctx;
  99. bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
  100. if (app->txrx->last_g0_value != level) {
  101. uint32_t now = DWT->CYCCNT;
  102. uint32_t dur = now - app->txrx->last_g0_change_time;
  103. dur /= furi_hal_cortex_instructions_per_microsecond();
  104. if (dur > 15000) dur = 15000;
  105. raw_samples_add(RawSamples, app->txrx->last_g0_value, dur);
  106. app->txrx->last_g0_value = level;
  107. app->txrx->last_g0_change_time = now;
  108. }
  109. LL_TIM_ClearFlag_UPDATE(TIM2);
  110. }
  111. void raw_sampling_worker_start(ProtoViewApp *app) {
  112. UNUSED(app);
  113. LL_TIM_InitTypeDef tim_init = {
  114. .Prescaler = 63, /* CPU frequency is ~64Mhz. */
  115. .CounterMode = LL_TIM_COUNTERMODE_UP,
  116. .Autoreload = 5, /* Sample every 5 us */
  117. };
  118. LL_TIM_Init(TIM2, &tim_init);
  119. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  120. LL_TIM_DisableCounter(TIM2);
  121. LL_TIM_SetCounter(TIM2, 0);
  122. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, protoview_timer_isr, app);
  123. LL_TIM_EnableIT_UPDATE(TIM2);
  124. LL_TIM_EnableCounter(TIM2);
  125. FURI_LOG_E(TAG, "Timer enabled");
  126. }
  127. void raw_sampling_worker_stop(ProtoViewApp *app) {
  128. UNUSED(app);
  129. FURI_CRITICAL_ENTER();
  130. LL_TIM_DisableCounter(TIM2);
  131. LL_TIM_DisableIT_UPDATE(TIM2);
  132. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  133. LL_TIM_DeInit(TIM2);
  134. FURI_CRITICAL_EXIT();
  135. }