app_subghz.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. void raw_sampling_worker_start(ProtoViewApp *app);
  7. void raw_sampling_worker_stop(ProtoViewApp *app);
  8. ProtoViewModulation ProtoViewModulations[] = {
  9. {"OOK 650Khz", FuriHalSubGhzPresetOok650Async, NULL},
  10. {"OOK 270Khz", FuriHalSubGhzPresetOok270Async, NULL},
  11. {"2FSK 2.38Khz", FuriHalSubGhzPreset2FSKDev238Async, NULL},
  12. {"2FSK 47.6Khz", FuriHalSubGhzPreset2FSKDev476Async, NULL},
  13. {"MSK", FuriHalSubGhzPresetMSK99_97KbAsync, NULL},
  14. {"GFSK", FuriHalSubGhzPresetGFSK9_99KbAsync, NULL},
  15. {"TPMS 1 (FSK)", 0, (uint8_t*)protoview_subghz_tpms1_async_regs},
  16. {"TPMS 2 (FSK)", 0, (uint8_t*)protoview_subghz_tpms2_async_regs},
  17. {NULL, 0, NULL} /* End of list sentinel. */
  18. };
  19. /* Called after the application initialization in order to setup the
  20. * subghz system and put it into idle state. If the user wants to start
  21. * receiving we will call radio_rx() to start a receiving worker and
  22. * associated thread. */
  23. void radio_begin(ProtoViewApp* app) {
  24. furi_assert(app);
  25. furi_hal_subghz_reset();
  26. furi_hal_subghz_idle();
  27. /* The CC1101 preset can be either one of the standard presets, if
  28. * the modulation "custom" field is NULL, or a custom preset we
  29. * defined in custom_presets.h. */
  30. if (ProtoViewModulations[app->modulation].custom == NULL)
  31. furi_hal_subghz_load_preset(ProtoViewModulations[app->modulation].preset);
  32. else
  33. furi_hal_subghz_load_custom_preset(ProtoViewModulations[app->modulation].custom);
  34. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  35. app->txrx->txrx_state = TxRxStateIDLE;
  36. }
  37. /* Setup subghz to start receiving using a background worker. */
  38. uint32_t radio_rx(ProtoViewApp* app) {
  39. furi_assert(app);
  40. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  41. furi_crash(TAG" Incorrect RX frequency.");
  42. }
  43. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  44. furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
  45. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  46. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  47. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  48. furi_hal_subghz_flush_rx();
  49. furi_hal_subghz_rx();
  50. if (!app->txrx->debug_direct_sampling) {
  51. furi_hal_subghz_start_async_rx(subghz_worker_rx_callback,
  52. app->txrx->worker);
  53. subghz_worker_start(app->txrx->worker);
  54. } else {
  55. raw_sampling_worker_start(app);
  56. }
  57. app->txrx->txrx_state = TxRxStateRx;
  58. return value;
  59. }
  60. /* Stop subghz worker (if active), put radio on idle state. */
  61. void radio_rx_end(ProtoViewApp* app) {
  62. furi_assert(app);
  63. if (app->txrx->txrx_state == TxRxStateRx) {
  64. if (!app->txrx->debug_direct_sampling) {
  65. if(subghz_worker_is_running(app->txrx->worker)) {
  66. subghz_worker_stop(app->txrx->worker);
  67. furi_hal_subghz_stop_async_rx();
  68. }
  69. } else {
  70. raw_sampling_worker_stop(app);
  71. }
  72. }
  73. furi_hal_subghz_idle();
  74. app->txrx->txrx_state = TxRxStateIDLE;
  75. }
  76. /* Put radio on sleep. */
  77. void radio_sleep(ProtoViewApp* app) {
  78. furi_assert(app);
  79. if (app->txrx->txrx_state == TxRxStateRx) {
  80. /* We can't go from having an active RX worker to sleeping.
  81. * Stop the RX subsystems first. */
  82. radio_rx_end(app);
  83. }
  84. furi_hal_subghz_sleep();
  85. app->txrx->txrx_state = TxRxStateSleep;
  86. }
  87. /* ============================= Raw sampling mode =============================
  88. * This is useful only for debugging: in this mode instead of using the
  89. * subghz thread, we read in a busy loop from the GDO0 pin of the CC1101
  90. * in order to get exactly what the chip is receiving. Then using the
  91. * CPU ticks counter we fill the buffer of data with the pulses level
  92. * and duration. */
  93. int32_t direct_sampling_thread(void *ctx) {
  94. ProtoViewApp *app = ctx;
  95. bool last_level = false;
  96. uint32_t last_change_time = DWT->CYCCNT;
  97. if (0) while(app->txrx->ds_thread_running) furi_delay_ms(1);
  98. while(app->txrx->ds_thread_running) {
  99. uint16_t d[50]; uint8_t l[50];
  100. for (uint32_t j = 0; j < 500; j++) {
  101. volatile uint32_t maxloops = 50000;
  102. while(maxloops-- && app->txrx->ds_thread_running) {
  103. bool l = furi_hal_gpio_read(&gpio_cc1101_g0);
  104. if (l != last_level) break;
  105. }
  106. if (maxloops == 0) {
  107. FURI_LOG_E(TAG, "Max loops reached in DS");
  108. furi_delay_tick(1);
  109. }
  110. /* g0 no longer equal to last level. */
  111. uint32_t now = DWT->CYCCNT;
  112. uint32_t dur = now - last_change_time;
  113. dur /= furi_hal_cortex_instructions_per_microsecond();
  114. if (dur > 20) {
  115. raw_samples_add(RawSamples, last_level, dur);
  116. if (j < 50) {
  117. l[j] = last_level;
  118. d[j] = dur;
  119. }
  120. } else {
  121. last_level = !last_level;
  122. continue;
  123. }
  124. last_level = !last_level; /* What g0 is now. */
  125. last_change_time = now;
  126. if (!app->txrx->ds_thread_running) break;
  127. }
  128. for (uint32_t j = 0; j < 50; j++)
  129. printf("%d=%u ", (unsigned int)l[j], (unsigned int)d[j]);
  130. printf("\n");
  131. furi_delay_ms(50);
  132. }
  133. FURI_LOG_E(TAG, "Exiting DS thread");
  134. return 0;
  135. }
  136. void raw_sampling_worker_start(ProtoViewApp *app) {
  137. if (app->txrx->ds_thread != NULL) return;
  138. app->txrx->ds_thread_running = true;
  139. app->txrx->ds_thread = furi_thread_alloc_ex("ProtoView DS", 2048, direct_sampling_thread, app);
  140. furi_thread_start(app->txrx->ds_thread);
  141. }
  142. void raw_sampling_worker_stop(ProtoViewApp *app) {
  143. if (app->txrx->ds_thread == NULL) return;
  144. app->txrx->ds_thread_running = false;
  145. furi_thread_join(app->txrx->ds_thread);
  146. furi_thread_free(app->txrx->ds_thread);
  147. app->txrx->ds_thread = NULL;
  148. }