app_subghz.c 2.6 KB

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