app_subghz.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. subghz_devices_reset(app->radio_device);
  37. subghz_devices_idle(app->radio_device);
  38. /* The CC1101 preset can be either one of the standard presets, if
  39. * the modulation "custom" field is NULL, or a custom preset we
  40. * defined in custom_presets.h. */
  41. if (ProtoViewModulations[app->modulation].custom == NULL) {
  42. subghz_devices_load_preset(
  43. app->radio_device, ProtoViewModulations[app->modulation].preset, NULL);
  44. } else {
  45. subghz_devices_load_preset(
  46. app->radio_device,
  47. FuriHalSubGhzPresetCustom,
  48. ProtoViewModulations[app->modulation].custom);
  49. }
  50. furi_hal_gpio_init(
  51. subghz_devices_get_data_gpio(app->radio_device), 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(!subghz_devices_is_frequency_valid(app->radio_device, app->frequency)) {
  68. furi_crash(TAG" Incorrect RX frequency.");
  69. }
  70. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  71. subghz_devices_idle(app->radio_device); /* Put it into idle state in case it is sleeping. */
  72. uint32_t value = subghz_devices_set_frequency(app->radio_device, app->frequency);
  73. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  74. subghz_devices_flush_rx(app->radio_device);
  75. subghz_devices_set_rx(app->radio_device);
  76. if (!app->txrx->debug_timer_sampling) {
  77. subghz_devices_start_async_rx(app->radio_device, protoview_rx_callback, NULL);
  78. } else {
  79. furi_hal_gpio_init(
  80. subghz_devices_get_data_gpio(app->radio_device),
  81. GpioModeInput,
  82. GpioPullNo,
  83. GpioSpeedLow);
  84. raw_sampling_worker_start(app);
  85. }
  86. app->txrx->txrx_state = TxRxStateRx;
  87. return value;
  88. }
  89. /* Stop receiving (if active) and put the radio on idle state. */
  90. void radio_rx_end(ProtoViewApp* app) {
  91. furi_assert(app);
  92. if (app->txrx->txrx_state == TxRxStateRx) {
  93. if (!app->txrx->debug_timer_sampling) {
  94. subghz_devices_stop_async_rx(app->radio_device);
  95. } else {
  96. raw_sampling_worker_stop(app);
  97. }
  98. }
  99. subghz_devices_idle(app->radio_device);
  100. app->txrx->txrx_state = TxRxStateIDLE;
  101. }
  102. /* Put radio on sleep. */
  103. void radio_sleep(ProtoViewApp* app) {
  104. furi_assert(app);
  105. if (app->txrx->txrx_state == TxRxStateRx) {
  106. /* Stop the asynchronous receiving system before putting the
  107. * chip into sleep. */
  108. radio_rx_end(app);
  109. }
  110. subghz_devices_sleep(app->radio_device);
  111. app->txrx->txrx_state = TxRxStateSleep;
  112. }
  113. /* =============================== Transmission ============================= */
  114. /* This function suspends the current RX state, switches to TX mode,
  115. * transmits the signal provided by the callback data_feeder, and later
  116. * restores the RX state if there was one. */
  117. void radio_tx_signal(ProtoViewApp *app, FuriHalSubGhzAsyncTxCallback data_feeder, void *ctx) {
  118. TxRxState oldstate = app->txrx->txrx_state;
  119. if (oldstate == TxRxStateRx) radio_rx_end(app);
  120. radio_begin(app);
  121. subghz_devices_idle(app->radio_device);
  122. uint32_t value = subghz_devices_set_frequency(app->radio_device, app->frequency);
  123. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  124. subghz_devices_start_async_tx(app->radio_device, data_feeder, ctx);
  125. while(!subghz_devices_is_async_complete_tx(app->radio_device)) furi_delay_ms(10);
  126. subghz_devices_stop_async_tx(app->radio_device);
  127. subghz_devices_idle(app->radio_device);
  128. radio_begin(app);
  129. if (oldstate == TxRxStateRx) radio_rx(app);
  130. }
  131. /* ============================= Raw sampling mode =============================
  132. * This is a special mode that uses a high frequency timer to sample the
  133. * CC1101 pin directly. It's useful for debugging purposes when we want
  134. * to get the raw data from the chip and completely bypass the subghz
  135. * Flipper system.
  136. * ===========================================================================*/
  137. void protoview_timer_isr(void *ctx) {
  138. ProtoViewApp *app = ctx;
  139. bool level = furi_hal_gpio_read(subghz_devices_get_data_gpio(app->radio_device));
  140. if (app->txrx->last_g0_value != level) {
  141. uint32_t now = DWT->CYCCNT;
  142. uint32_t dur = now - app->txrx->last_g0_change_time;
  143. dur /= furi_hal_cortex_instructions_per_microsecond();
  144. if (dur > 15000) dur = 15000;
  145. raw_samples_add(RawSamples, app->txrx->last_g0_value, dur);
  146. app->txrx->last_g0_value = level;
  147. app->txrx->last_g0_change_time = now;
  148. }
  149. LL_TIM_ClearFlag_UPDATE(TIM2);
  150. }
  151. void raw_sampling_worker_start(ProtoViewApp *app) {
  152. UNUSED(app);
  153. furi_hal_bus_enable(FuriHalBusTIM2);
  154. LL_TIM_InitTypeDef tim_init = {
  155. .Prescaler = 63, /* CPU frequency is ~64Mhz. */
  156. .CounterMode = LL_TIM_COUNTERMODE_UP,
  157. .Autoreload = 5, /* Sample every 5 us */
  158. };
  159. LL_TIM_Init(TIM2, &tim_init);
  160. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  161. LL_TIM_DisableCounter(TIM2);
  162. LL_TIM_SetCounter(TIM2, 0);
  163. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, protoview_timer_isr, app);
  164. LL_TIM_EnableIT_UPDATE(TIM2);
  165. LL_TIM_EnableCounter(TIM2);
  166. FURI_LOG_E(TAG, "Timer enabled");
  167. }
  168. void raw_sampling_worker_stop(ProtoViewApp *app) {
  169. UNUSED(app);
  170. FURI_CRITICAL_ENTER();
  171. LL_TIM_DisableCounter(TIM2);
  172. LL_TIM_DisableIT_UPDATE(TIM2);
  173. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  174. furi_hal_bus_disable(FuriHalBusTIM2);
  175. FURI_CRITICAL_EXIT();
  176. }