app_subghz.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_timer_start(ProtoViewApp *app);
  10. void raw_sampling_timer_stop(ProtoViewApp *app);
  11. ProtoViewModulation ProtoViewModulations[] = {
  12. {"OOK 650Khz", "FuriHalSubGhzPresetOok650Async",
  13. FuriHalSubGhzPresetOok650Async, NULL, 30},
  14. {"OOK 270Khz", "FuriHalSubGhzPresetOok270Async",
  15. FuriHalSubGhzPresetOok270Async, NULL, 30},
  16. {"2FSK 2.38Khz", "FuriHalSubGhzPreset2FSKDev238Async",
  17. FuriHalSubGhzPreset2FSKDev238Async, NULL, 30},
  18. {"2FSK 47.6Khz", "FuriHalSubGhzPreset2FSKDev476Async",
  19. FuriHalSubGhzPreset2FSKDev476Async, NULL, 30},
  20. {"TPMS 1 (FSK)", NULL,
  21. 0, (uint8_t*)protoview_subghz_tpms1_fsk_async_regs, 30},
  22. {"TPMS 2 (OOK)", NULL,
  23. 0, (uint8_t*)protoview_subghz_tpms2_ook_async_regs, 30},
  24. {"TPMS 3 (GFSK)", NULL,
  25. 0, (uint8_t*)protoview_subghz_tpms3_gfsk_async_regs, 30},
  26. {"OOK 40kBaud", NULL,
  27. 0, (uint8_t*)protoview_subghz_40k_ook_async_regs, 15},
  28. {"FSK 40kBaud", NULL,
  29. 0, (uint8_t*)protoview_subghz_40k_fsk_async_regs, 15},
  30. {NULL, NULL, 0, NULL, 0} /* End of list sentinel. */
  31. };
  32. /* Called after the application initialization in order to setup the
  33. * subghz system and put it into idle state. */
  34. void radio_begin(ProtoViewApp* app) {
  35. furi_assert(app);
  36. furi_hal_subghz_reset();
  37. furi_hal_subghz_idle();
  38. /* Power circuits are noisy. Suppressing the charge while we use
  39. * ProtoView will improve the RF performances. */
  40. furi_hal_power_suppress_charge_enter();
  41. /* The CC1101 preset can be either one of the standard presets, if
  42. * the modulation "custom" field is NULL, or a custom preset we
  43. * defined in custom_presets.h. */
  44. if (ProtoViewModulations[app->modulation].custom == NULL) {
  45. furi_hal_subghz_load_preset(
  46. ProtoViewModulations[app->modulation].preset);
  47. } else {
  48. furi_hal_subghz_load_custom_preset(
  49. ProtoViewModulations[app->modulation].custom);
  50. }
  51. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  52. app->txrx->txrx_state = TxRxStateIDLE;
  53. }
  54. /* ================================= Reception ============================== */
  55. /* We avoid the subghz provided abstractions and put the data in our
  56. * simple abstraction: the RawSamples circular buffer. */
  57. void protoview_rx_callback(bool level, uint32_t duration, void *context) {
  58. UNUSED(context);
  59. /* Add data to the circular buffer. */
  60. raw_samples_add(RawSamples, level, duration);
  61. // FURI_LOG_E(TAG, "FEED: %d %d", (int)level, (int)duration);
  62. return;
  63. }
  64. /* Setup the CC1101 to start receiving using a background worker. */
  65. uint32_t radio_rx(ProtoViewApp* app) {
  66. furi_assert(app);
  67. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  68. furi_crash(TAG" Incorrect RX frequency.");
  69. }
  70. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  71. furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
  72. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  73. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  74. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  75. furi_hal_subghz_flush_rx();
  76. furi_hal_subghz_rx();
  77. if (!app->txrx->debug_timer_sampling) {
  78. furi_hal_subghz_start_async_rx(protoview_rx_callback, NULL);
  79. } else {
  80. raw_sampling_worker_start(app);
  81. }
  82. app->txrx->txrx_state = TxRxStateRx;
  83. return value;
  84. }
  85. /* Stop receiving (if active) and put the radio on idle state. */
  86. void radio_rx_end(ProtoViewApp* app) {
  87. furi_assert(app);
  88. if (app->txrx->txrx_state == TxRxStateRx) {
  89. if (!app->txrx->debug_timer_sampling) {
  90. furi_hal_subghz_stop_async_rx();
  91. } else {
  92. raw_sampling_worker_stop(app);
  93. }
  94. }
  95. furi_hal_subghz_idle();
  96. app->txrx->txrx_state = TxRxStateIDLE;
  97. }
  98. /* Put radio on sleep. */
  99. void radio_sleep(ProtoViewApp* app) {
  100. furi_assert(app);
  101. if (app->txrx->txrx_state == TxRxStateRx) {
  102. /* Stop the asynchronous receiving system before putting the
  103. * chip into sleep. */
  104. radio_rx_end(app);
  105. }
  106. furi_hal_subghz_sleep();
  107. app->txrx->txrx_state = TxRxStateSleep;
  108. furi_hal_power_suppress_charge_exit();
  109. }
  110. /* =============================== Transmission ============================= */
  111. /* This function suspends the current RX state, switches to TX mode,
  112. * transmits the signal provided by the callback data_feeder, and later
  113. * restores the RX state if there was one. */
  114. void radio_tx_signal(ProtoViewApp *app, FuriHalSubGhzAsyncTxCallback data_feeder, void *ctx) {
  115. TxRxState oldstate = app->txrx->txrx_state;
  116. if (oldstate == TxRxStateRx) radio_rx_end(app);
  117. radio_begin(app);
  118. furi_hal_subghz_idle();
  119. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  120. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  121. furi_hal_gpio_write(&gpio_cc1101_g0, false);
  122. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  123. furi_hal_subghz_start_async_tx(data_feeder, ctx);
  124. while(!furi_hal_subghz_is_async_tx_complete()) furi_delay_ms(10);
  125. furi_hal_subghz_stop_async_tx();
  126. furi_hal_subghz_idle();
  127. radio_begin(app);
  128. if (oldstate == TxRxStateRx) radio_rx(app);
  129. }
  130. /* ============================= Raw sampling mode =============================
  131. * This is a special mode that uses a high frequency timer to sample the
  132. * CC1101 pin directly. It's useful for debugging purposes when we want
  133. * to get the raw data from the chip and completely bypass the subghz
  134. * Flipper system.
  135. * ===========================================================================*/
  136. void protoview_timer_isr(void *ctx) {
  137. ProtoViewApp *app = ctx;
  138. bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
  139. if (app->txrx->last_g0_value != level) {
  140. uint32_t now = DWT->CYCCNT;
  141. uint32_t dur = now - app->txrx->last_g0_change_time;
  142. dur /= furi_hal_cortex_instructions_per_microsecond();
  143. if (dur > 15000) dur = 15000;
  144. raw_samples_add(RawSamples, app->txrx->last_g0_value, dur);
  145. app->txrx->last_g0_value = level;
  146. app->txrx->last_g0_change_time = now;
  147. }
  148. LL_TIM_ClearFlag_UPDATE(TIM2);
  149. }
  150. void raw_sampling_worker_start(ProtoViewApp *app) {
  151. UNUSED(app);
  152. LL_TIM_InitTypeDef tim_init = {
  153. .Prescaler = 63, /* CPU frequency is ~64Mhz. */
  154. .CounterMode = LL_TIM_COUNTERMODE_UP,
  155. .Autoreload = 5, /* Sample every 5 us */
  156. };
  157. LL_TIM_Init(TIM2, &tim_init);
  158. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  159. LL_TIM_DisableCounter(TIM2);
  160. LL_TIM_SetCounter(TIM2, 0);
  161. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, protoview_timer_isr, app);
  162. LL_TIM_EnableIT_UPDATE(TIM2);
  163. LL_TIM_EnableCounter(TIM2);
  164. FURI_LOG_E(TAG, "Timer enabled");
  165. }
  166. void raw_sampling_worker_stop(ProtoViewApp *app) {
  167. UNUSED(app);
  168. FURI_CRITICAL_ENTER();
  169. LL_TIM_DisableCounter(TIM2);
  170. LL_TIM_DisableIT_UPDATE(TIM2);
  171. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  172. LL_TIM_DeInit(TIM2);
  173. FURI_CRITICAL_EXIT();
  174. }