app_subghz.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. ProtoViewModulation ProtoViewModulations[] = {
  7. {"OOK 650Khz", FuriHalSubGhzPresetOok650Async, NULL},
  8. {"OOK 270Khz", FuriHalSubGhzPresetOok270Async, NULL},
  9. {"2FSK 2.38Khz", FuriHalSubGhzPreset2FSKDev238Async, NULL},
  10. {"2FSK 47.6Khz", FuriHalSubGhzPreset2FSKDev476Async, NULL},
  11. {"MSK", FuriHalSubGhzPresetMSK99_97KbAsync, NULL},
  12. {"GFSK", FuriHalSubGhzPresetGFSK9_99KbAsync, NULL},
  13. {"FSK for TPMS", 0, (uint8_t*)protoview_subghz_tpms_async_regs},
  14. {NULL, 0, NULL} /* End of list sentinel. */
  15. };
  16. /* Called after the application initialization in order to setup the
  17. * subghz system and put it into idle state. If the user wants to start
  18. * receiving we will call radio_rx() to start a receiving worker and
  19. * associated thread. */
  20. void radio_begin(ProtoViewApp* app) {
  21. furi_assert(app);
  22. furi_hal_subghz_reset();
  23. furi_hal_subghz_idle();
  24. /* The CC1101 preset can be either one of the standard presets, if
  25. * the modulation "custom" field is NULL, or a custom preset we
  26. * defined in custom_presets.h. */
  27. if (ProtoViewModulations[app->modulation].custom == NULL)
  28. furi_hal_subghz_load_preset(ProtoViewModulations[app->modulation].preset);
  29. else
  30. furi_hal_subghz_load_custom_preset(ProtoViewModulations[app->modulation].custom);
  31. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  32. app->txrx->txrx_state = TxRxStateIDLE;
  33. }
  34. /* Setup subghz to start receiving using a background worker. */
  35. uint32_t radio_rx(ProtoViewApp* app) {
  36. furi_assert(app);
  37. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  38. furi_crash(TAG" Incorrect RX frequency.");
  39. }
  40. if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
  41. furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
  42. uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
  43. FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
  44. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  45. furi_hal_subghz_flush_rx();
  46. furi_hal_subghz_rx();
  47. furi_hal_subghz_start_async_rx(subghz_worker_rx_callback, app->txrx->worker);
  48. subghz_worker_start(app->txrx->worker);
  49. app->txrx->txrx_state = TxRxStateRx;
  50. return value;
  51. }
  52. /* Stop subghz worker (if active), put radio on idle state. */
  53. void radio_rx_end(ProtoViewApp* app) {
  54. furi_assert(app);
  55. if (app->txrx->txrx_state == TxRxStateRx) {
  56. if(subghz_worker_is_running(app->txrx->worker)) {
  57. subghz_worker_stop(app->txrx->worker);
  58. furi_hal_subghz_stop_async_rx();
  59. }
  60. }
  61. furi_hal_subghz_idle();
  62. app->txrx->txrx_state = TxRxStateIDLE;
  63. }
  64. /* Put radio on sleep. */
  65. void radio_sleep(ProtoViewApp* app) {
  66. furi_assert(app);
  67. if (app->txrx->txrx_state == TxRxStateRx) {
  68. /* We can't go from having an active RX worker to sleeping.
  69. * Stop the RX subsystems first. */
  70. radio_rx_end(app);
  71. }
  72. furi_hal_subghz_sleep();
  73. app->txrx->txrx_state = TxRxStateSleep;
  74. }