app_subghz.c 3.3 KB

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