app_subghz.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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", FuriHalSubGhzPresetOok650Async, NULL, 30},
  13. {"OOK 270Khz", "FuriHalSubGhzPresetOok270Async", FuriHalSubGhzPresetOok270Async, NULL, 30},
  14. {"2FSK 2.38Khz",
  15. "FuriHalSubGhzPreset2FSKDev238Async",
  16. FuriHalSubGhzPreset2FSKDev238Async,
  17. NULL,
  18. 30},
  19. {"2FSK 47.6Khz",
  20. "FuriHalSubGhzPreset2FSKDev476Async",
  21. FuriHalSubGhzPreset2FSKDev476Async,
  22. NULL,
  23. 30},
  24. {"TPMS 1 (FSK)", NULL, 0, (uint8_t*)protoview_subghz_tpms1_fsk_async_regs, 30},
  25. {"TPMS 2 (OOK)", NULL, 0, (uint8_t*)protoview_subghz_tpms2_ook_async_regs, 30},
  26. {"TPMS 3 (GFSK)", NULL, 0, (uint8_t*)protoview_subghz_tpms3_gfsk_async_regs, 30},
  27. {"OOK 40kBaud", NULL, 0, (uint8_t*)protoview_subghz_40k_ook_async_regs, 15},
  28. {"FSK 40kBaud", NULL, 0, (uint8_t*)protoview_subghz_40k_fsk_async_regs, 15},
  29. {NULL, NULL, 0, NULL, 0} /* End of list sentinel. */
  30. };
  31. /* Called after the application initialization in order to setup the
  32. * subghz system and put it into idle state. */
  33. void radio_begin(ProtoViewApp* app) {
  34. furi_assert(app);
  35. subghz_devices_reset(app->radio_device);
  36. subghz_devices_idle(app->radio_device);
  37. /* The CC1101 preset can be either one of the standard presets, if
  38. * the modulation "custom" field is NULL, or a custom preset we
  39. * defined in custom_presets.h. */
  40. if(ProtoViewModulations[app->modulation].custom == NULL) {
  41. subghz_devices_load_preset(
  42. app->radio_device, ProtoViewModulations[app->modulation].preset, NULL);
  43. } else {
  44. subghz_devices_load_preset(
  45. app->radio_device,
  46. FuriHalSubGhzPresetCustom,
  47. ProtoViewModulations[app->modulation].custom);
  48. }
  49. furi_hal_gpio_init(
  50. subghz_devices_get_data_gpio(app->radio_device), GpioModeInput, GpioPullNo, GpioSpeedLow);
  51. app->txrx->txrx_state = TxRxStateIDLE;
  52. }
  53. /* ================================= Reception ============================== */
  54. /* We avoid the subghz provided abstractions and put the data in our
  55. * simple abstraction: the RawSamples circular buffer. */
  56. void protoview_rx_callback(bool level, uint32_t duration, void* context) {
  57. UNUSED(context);
  58. /* Add data to the circular buffer. */
  59. raw_samples_add(RawSamples, level, duration);
  60. // FURI_LOG_E(TAG, "FEED: %d %d", (int)level, (int)duration);
  61. return;
  62. }
  63. /* Setup the CC1101 to start receiving using a background worker. */
  64. uint32_t radio_rx(ProtoViewApp* app) {
  65. furi_assert(app);
  66. if(!subghz_devices_is_frequency_valid(app->radio_device, app->frequency)) {
  67. furi_crash(TAG " Incorrect RX frequency.");
  68. }
  69. if(app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  70. subghz_devices_idle(app->radio_device); /* Put it into idle state in case it is sleeping. */
  71. uint32_t value = subghz_devices_set_frequency(app->radio_device, app->frequency);
  72. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  73. subghz_devices_flush_rx(app->radio_device);
  74. subghz_devices_set_rx(app->radio_device);
  75. if(!app->txrx->debug_timer_sampling) {
  76. subghz_devices_start_async_rx(app->radio_device, protoview_rx_callback, NULL);
  77. } else {
  78. furi_hal_gpio_init(
  79. subghz_devices_get_data_gpio(app->radio_device),
  80. GpioModeInput,
  81. GpioPullNo,
  82. GpioSpeedLow);
  83. raw_sampling_worker_start(app);
  84. }
  85. app->txrx->txrx_state = TxRxStateRx;
  86. return value;
  87. }
  88. /* Stop receiving (if active) and put the radio on idle state. */
  89. void radio_rx_end(ProtoViewApp* app) {
  90. furi_assert(app);
  91. if(app->txrx->txrx_state == TxRxStateRx) {
  92. if(!app->txrx->debug_timer_sampling) {
  93. subghz_devices_stop_async_rx(app->radio_device);
  94. } else {
  95. raw_sampling_worker_stop(app);
  96. }
  97. }
  98. subghz_devices_idle(app->radio_device);
  99. app->txrx->txrx_state = TxRxStateIDLE;
  100. }
  101. /* Put radio on sleep. */
  102. void radio_sleep(ProtoViewApp* app) {
  103. furi_assert(app);
  104. if(app->txrx->txrx_state == TxRxStateRx) {
  105. /* Stop the asynchronous receiving system before putting the
  106. * chip into sleep. */
  107. radio_rx_end(app);
  108. }
  109. subghz_devices_sleep(app->radio_device);
  110. app->txrx->txrx_state = TxRxStateSleep;
  111. }
  112. /* =============================== Transmission ============================= */
  113. /* This function suspends the current RX state, switches to TX mode,
  114. * transmits the signal provided by the callback data_feeder, and later
  115. * restores the RX state if there was one. */
  116. void radio_tx_signal(ProtoViewApp* app, FuriHalSubGhzAsyncTxCallback data_feeder, void* ctx) {
  117. TxRxState oldstate = app->txrx->txrx_state;
  118. if(oldstate == TxRxStateRx) radio_rx_end(app);
  119. radio_begin(app);
  120. subghz_devices_idle(app->radio_device);
  121. uint32_t value = subghz_devices_set_frequency(app->radio_device, app->frequency);
  122. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  123. subghz_devices_start_async_tx(app->radio_device, data_feeder, ctx);
  124. while(!subghz_devices_is_async_complete_tx(app->radio_device)) furi_delay_ms(10);
  125. subghz_devices_stop_async_tx(app->radio_device);
  126. subghz_devices_idle(app->radio_device);
  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(subghz_devices_get_data_gpio(app->radio_device));
  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. furi_hal_bus_enable(FuriHalBusTIM2);
  153. LL_TIM_InitTypeDef tim_init = {
  154. .Prescaler = 63, /* CPU frequency is ~64Mhz. */
  155. .CounterMode = LL_TIM_COUNTERMODE_UP,
  156. .Autoreload = 5, /* Sample every 5 us */
  157. };
  158. LL_TIM_Init(TIM2, &tim_init);
  159. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  160. LL_TIM_DisableCounter(TIM2);
  161. LL_TIM_SetCounter(TIM2, 0);
  162. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, protoview_timer_isr, app);
  163. LL_TIM_EnableIT_UPDATE(TIM2);
  164. LL_TIM_EnableCounter(TIM2);
  165. FURI_LOG_E(TAG, "Timer enabled");
  166. }
  167. void raw_sampling_worker_stop(ProtoViewApp* app) {
  168. UNUSED(app);
  169. FURI_CRITICAL_ENTER();
  170. LL_TIM_DisableCounter(TIM2);
  171. LL_TIM_DisableIT_UPDATE(TIM2);
  172. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  173. furi_hal_bus_disable(FuriHalBusTIM2);
  174. FURI_CRITICAL_EXIT();
  175. }