app_subghz.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. {NULL, 0, NULL} /* End of list sentinel. */
  21. };
  22. /* Called after the application initialization in order to setup the
  23. * subghz system and put it into idle state. If the user wants to start
  24. * receiving we will call radio_rx() to start a receiving worker and
  25. * associated thread. */
  26. void radio_begin(ProtoViewApp* app) {
  27. furi_assert(app);
  28. furi_hal_subghz_reset();
  29. furi_hal_subghz_idle();
  30. /* The CC1101 preset can be either one of the standard presets, if
  31. * the modulation "custom" field is NULL, or a custom preset we
  32. * defined in custom_presets.h. */
  33. if (ProtoViewModulations[app->modulation].custom == NULL)
  34. furi_hal_subghz_load_preset(ProtoViewModulations[app->modulation].preset);
  35. else
  36. furi_hal_subghz_load_custom_preset(ProtoViewModulations[app->modulation].custom);
  37. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  38. app->txrx->txrx_state = TxRxStateIDLE;
  39. }
  40. /* Setup subghz to start receiving using a background worker. */
  41. uint32_t radio_rx(ProtoViewApp* app) {
  42. furi_assert(app);
  43. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  44. furi_crash(TAG" Incorrect RX frequency.");
  45. }
  46. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  47. furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
  48. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  49. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  50. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  51. furi_hal_subghz_flush_rx();
  52. furi_hal_subghz_rx();
  53. if (!app->txrx->debug_direct_sampling) {
  54. furi_hal_subghz_start_async_rx(subghz_worker_rx_callback,
  55. app->txrx->worker);
  56. subghz_worker_start(app->txrx->worker);
  57. } else {
  58. raw_sampling_worker_start(app);
  59. }
  60. app->txrx->txrx_state = TxRxStateRx;
  61. return value;
  62. }
  63. /* Stop subghz worker (if active), put radio on idle state. */
  64. void radio_rx_end(ProtoViewApp* app) {
  65. furi_assert(app);
  66. if (app->txrx->txrx_state == TxRxStateRx) {
  67. if (!app->txrx->debug_direct_sampling) {
  68. if(subghz_worker_is_running(app->txrx->worker)) {
  69. subghz_worker_stop(app->txrx->worker);
  70. furi_hal_subghz_stop_async_rx();
  71. }
  72. } else {
  73. raw_sampling_worker_stop(app);
  74. }
  75. }
  76. furi_hal_subghz_idle();
  77. app->txrx->txrx_state = TxRxStateIDLE;
  78. }
  79. /* Put radio on sleep. */
  80. void radio_sleep(ProtoViewApp* app) {
  81. furi_assert(app);
  82. if (app->txrx->txrx_state == TxRxStateRx) {
  83. /* We can't go from having an active RX worker to sleeping.
  84. * Stop the RX subsystems first. */
  85. radio_rx_end(app);
  86. }
  87. furi_hal_subghz_sleep();
  88. app->txrx->txrx_state = TxRxStateSleep;
  89. }
  90. /* ============================= Raw sampling mode =============================
  91. * This is useful only for debugging: in this mode instead of using the
  92. * subghz thread, we read in a busy loop from the GDO0 pin of the CC1101
  93. * in order to get exactly what the chip is receiving. Then using the
  94. * CPU ticks counter we fill the buffer of data with the pulses level
  95. * and duration. */
  96. int32_t direct_sampling_thread(void *ctx) {
  97. ProtoViewApp *app = ctx;
  98. bool last_level = false;
  99. uint32_t last_change_time = DWT->CYCCNT;
  100. if (0) while(app->txrx->ds_thread_running) furi_delay_ms(1);
  101. while(app->txrx->ds_thread_running) {
  102. uint16_t d[50]; uint8_t l[50];
  103. for (uint32_t j = 0; j < 500; j++) {
  104. volatile uint32_t maxloops = 50000;
  105. while(maxloops-- && app->txrx->ds_thread_running) {
  106. bool l = furi_hal_gpio_read(&gpio_cc1101_g0);
  107. if (l != last_level) break;
  108. }
  109. if (maxloops == 0) {
  110. FURI_LOG_E(TAG, "Max loops reached in DS");
  111. furi_delay_tick(1);
  112. }
  113. /* g0 no longer equal to last level. */
  114. uint32_t now = DWT->CYCCNT;
  115. uint32_t dur = now - last_change_time;
  116. dur /= furi_hal_cortex_instructions_per_microsecond();
  117. if (dur > 20) {
  118. raw_samples_add(RawSamples, last_level, dur);
  119. if (j < 50) {
  120. l[j] = last_level;
  121. d[j] = dur;
  122. }
  123. } else {
  124. last_level = !last_level;
  125. continue;
  126. }
  127. last_level = !last_level; /* What g0 is now. */
  128. last_change_time = now;
  129. if (!app->txrx->ds_thread_running) break;
  130. }
  131. for (uint32_t j = 0; j < 50; j++)
  132. printf("%d=%u ", (unsigned int)l[j], (unsigned int)d[j]);
  133. printf("\n");
  134. furi_delay_ms(50);
  135. }
  136. FURI_LOG_E(TAG, "Exiting DS thread");
  137. return 0;
  138. }
  139. #if 0
  140. void raw_sampling_worker_start(ProtoViewApp *app) {
  141. if (app->txrx->ds_thread != NULL) return;
  142. app->txrx->ds_thread_running = true;
  143. app->txrx->ds_thread = furi_thread_alloc_ex("ProtoView DS", 2048, direct_sampling_thread, app);
  144. furi_thread_start(app->txrx->ds_thread);
  145. }
  146. void raw_sampling_worker_stop(ProtoViewApp *app) {
  147. if (app->txrx->ds_thread == NULL) return;
  148. app->txrx->ds_thread_running = false;
  149. furi_thread_join(app->txrx->ds_thread);
  150. furi_thread_free(app->txrx->ds_thread);
  151. app->txrx->ds_thread = NULL;
  152. }
  153. #endif
  154. void protoview_timer_isr(void *ctx) {
  155. ProtoViewApp *app = ctx;
  156. bool level = furi_hal_gpio_read(&gpio_cc1101_g0);
  157. if (app->txrx->last_g0_value != level) {
  158. uint32_t now = DWT->CYCCNT;
  159. uint32_t dur = now - app->txrx->last_g0_change_time;
  160. dur /= furi_hal_cortex_instructions_per_microsecond();
  161. if (dur > 15000) dur = 15000;
  162. raw_samples_add(RawSamples, app->txrx->last_g0_value, dur);
  163. app->txrx->last_g0_value = level;
  164. app->txrx->last_g0_change_time = now;
  165. }
  166. LL_TIM_ClearFlag_UPDATE(TIM2);
  167. }
  168. void raw_sampling_worker_start(ProtoViewApp *app) {
  169. UNUSED(app);
  170. LL_TIM_InitTypeDef tim_init = {
  171. .Prescaler = 63,
  172. .CounterMode = LL_TIM_COUNTERMODE_UP,
  173. .Autoreload = 5, /* Sample every 5 us */
  174. };
  175. LL_TIM_Init(TIM2, &tim_init);
  176. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  177. LL_TIM_DisableCounter(TIM2);
  178. LL_TIM_SetCounter(TIM2, 0);
  179. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, protoview_timer_isr, app);
  180. LL_TIM_EnableIT_UPDATE(TIM2);
  181. LL_TIM_EnableCounter(TIM2);
  182. FURI_LOG_E(TAG, "Timer enabled");
  183. }
  184. void raw_sampling_worker_stop(ProtoViewApp *app) {
  185. UNUSED(app);
  186. FURI_CRITICAL_ENTER();
  187. LL_TIM_DisableCounter(TIM2);
  188. LL_TIM_DisableIT_UPDATE(TIM2);
  189. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  190. LL_TIM_DeInit(TIM2);
  191. FURI_CRITICAL_EXIT();
  192. }