app_subghz.c 2.7 KB

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