app_subghz.c 2.2 KB

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