furi_hal_subghz.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * @file furi_hal_subghz.h
  3. * SubGhz HAL API
  4. */
  5. #pragma once
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <stddef.h>
  9. #include <toolbox/level_duration.h>
  10. #include <furi_hal_gpio.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** Low level buffer dimensions and guard times */
  15. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL (256)
  16. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF (API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL / 2)
  17. #define API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME 999
  18. /** Radio Presets */
  19. typedef enum {
  20. FuriHalSubGhzPresetIDLE, /**< default configuration */
  21. FuriHalSubGhzPresetOok270Async, /**< OOK, bandwidth 270kHz, asynchronous */
  22. FuriHalSubGhzPresetOok650Async, /**< OOK, bandwidth 650kHz, asynchronous */
  23. FuriHalSubGhzPreset2FSKDev238Async, /**< FM, deviation 2.380371 kHz, asynchronous */
  24. FuriHalSubGhzPreset2FSKDev476Async, /**< FM, deviation 47.60742 kHz, asynchronous */
  25. FuriHalSubGhzPresetMSK99_97KbAsync, /**< MSK, deviation 47.60742 kHz, 99.97Kb/s, asynchronous */
  26. FuriHalSubGhzPresetGFSK9_99KbAsync, /**< GFSK, deviation 19.042969 kHz, 9.996Kb/s, asynchronous */
  27. FuriHalSubGhzPresetCustom, /**Custom Preset*/
  28. } FuriHalSubGhzPreset;
  29. /** Switchable Radio Paths */
  30. typedef enum {
  31. FuriHalSubGhzPathIsolate, /**< Isolate Radio from antenna */
  32. FuriHalSubGhzPath433, /**< Center Frequency: 433MHz. Path 1: SW1RF1-SW2RF2, LCLCL */
  33. FuriHalSubGhzPath315, /**< Center Frequency: 315MHz. Path 2: SW1RF2-SW2RF1, LCLCLCL */
  34. FuriHalSubGhzPath868, /**< Center Frequency: 868MHz. Path 3: SW1RF3-SW2RF3, LCLC */
  35. } FuriHalSubGhzPath;
  36. /** SubGhz state */
  37. typedef enum {
  38. SubGhzStateInit, /**< Init pending */
  39. SubGhzStateIdle, /**< Idle, energy save mode */
  40. SubGhzStateAsyncRx, /**< Async RX started */
  41. SubGhzStateAsyncTx, /**< Async TX started, DMA and timer is on */
  42. SubGhzStateAsyncTxLast, /**< Async TX continue, DMA completed and timer got last value to go */
  43. SubGhzStateAsyncTxEnd, /**< Async TX complete, cleanup needed */
  44. } SubGhzState;
  45. /** SubGhz regulation, receive transmission on the current frequency for the
  46. * region */
  47. typedef enum {
  48. SubGhzRegulationOnlyRx, /**only Rx*/
  49. SubGhzRegulationTxRx, /**TxRx*/
  50. } SubGhzRegulation;
  51. /* Mirror RX/TX async modulation signal to specified pin
  52. *
  53. * @warning Configures pin to output mode. Make sure it is not connected
  54. * directly to power or ground.
  55. *
  56. * @param[in] pin pointer to the gpio pin structure or NULL to disable
  57. */
  58. void furi_hal_subghz_set_async_mirror_pin(const GpioPin* pin);
  59. /** Initialize and switch to power save mode Used by internal API-HAL
  60. * initialization routine Can be used to reinitialize device to safe state and
  61. * send it to sleep
  62. */
  63. void furi_hal_subghz_init();
  64. /** Send device to sleep mode
  65. */
  66. void furi_hal_subghz_sleep();
  67. /** Dump info to stdout
  68. */
  69. void furi_hal_subghz_dump_state();
  70. /** Load registers from preset by preset name
  71. *
  72. * @param preset to load
  73. */
  74. void furi_hal_subghz_load_preset(FuriHalSubGhzPreset preset);
  75. /** Load custom registers from preset
  76. *
  77. * @param preset_data registers to load
  78. */
  79. void furi_hal_subghz_load_custom_preset(uint8_t* preset_data);
  80. /** Load registers
  81. *
  82. * @param data Registers data
  83. */
  84. void furi_hal_subghz_load_registers(uint8_t* data);
  85. /** Load PATABLE
  86. *
  87. * @param data 8 uint8_t values
  88. */
  89. void furi_hal_subghz_load_patable(const uint8_t data[8]);
  90. /** Write packet to FIFO
  91. *
  92. * @param data bytes array
  93. * @param size size
  94. */
  95. void furi_hal_subghz_write_packet(const uint8_t* data, uint8_t size);
  96. /** Check if receive pipe is not empty
  97. *
  98. * @return true if not empty
  99. */
  100. bool furi_hal_subghz_rx_pipe_not_empty();
  101. /** Check if received data crc is valid
  102. *
  103. * @return true if valid
  104. */
  105. bool furi_hal_subghz_is_rx_data_crc_valid();
  106. /** Read packet from FIFO
  107. *
  108. * @param data pointer
  109. * @param size size
  110. */
  111. void furi_hal_subghz_read_packet(uint8_t* data, uint8_t* size);
  112. /** Flush rx FIFO buffer
  113. */
  114. void furi_hal_subghz_flush_rx();
  115. /** Flush tx FIFO buffer
  116. */
  117. void furi_hal_subghz_flush_tx();
  118. /** Shutdown Issue SPWD command
  119. * @warning registers content will be lost
  120. */
  121. void furi_hal_subghz_shutdown();
  122. /** Reset Issue reset command
  123. * @warning registers content will be lost
  124. */
  125. void furi_hal_subghz_reset();
  126. /** Switch to Idle
  127. */
  128. void furi_hal_subghz_idle();
  129. /** Switch to Receive
  130. */
  131. void furi_hal_subghz_rx();
  132. /** Switch to Transmit
  133. *
  134. * @return true if the transfer is allowed by belonging to the region
  135. */
  136. bool furi_hal_subghz_tx();
  137. /** Get RSSI value in dBm
  138. *
  139. * @return RSSI value
  140. */
  141. float furi_hal_subghz_get_rssi();
  142. /** Get LQI
  143. *
  144. * @return LQI value
  145. */
  146. uint8_t furi_hal_subghz_get_lqi();
  147. /** Check if frequency is in valid range
  148. *
  149. * @param value frequency in Hz
  150. *
  151. * @return true if frequency is valid, otherwise false
  152. */
  153. bool furi_hal_subghz_is_frequency_valid(uint32_t value);
  154. /** Set frequency and path This function automatically selects antenna matching
  155. * network
  156. *
  157. * @param value frequency in Hz
  158. *
  159. * @return real frequency in Hz
  160. */
  161. uint32_t furi_hal_subghz_set_frequency_and_path(uint32_t value);
  162. /** Set frequency
  163. *
  164. * @param value frequency in Hz
  165. *
  166. * @return real frequency in Hz
  167. */
  168. uint32_t furi_hal_subghz_set_frequency(uint32_t value);
  169. /** Set path
  170. *
  171. * @param path path to use
  172. */
  173. void furi_hal_subghz_set_path(FuriHalSubGhzPath path);
  174. /* High Level API */
  175. /** Signal Timings Capture callback */
  176. typedef void (*FuriHalSubGhzCaptureCallback)(bool level, uint32_t duration, void* context);
  177. /** Enable signal timings capture Initializes GPIO and TIM2 for timings capture
  178. *
  179. * @param callback FuriHalSubGhzCaptureCallback
  180. * @param context callback context
  181. */
  182. void furi_hal_subghz_start_async_rx(FuriHalSubGhzCaptureCallback callback, void* context);
  183. /** Disable signal timings capture Resets GPIO and TIM2
  184. */
  185. void furi_hal_subghz_stop_async_rx();
  186. /** Async TX callback type
  187. * @param context callback context
  188. * @return LevelDuration
  189. */
  190. typedef LevelDuration (*FuriHalSubGhzAsyncTxCallback)(void* context);
  191. /** Start async TX Initializes GPIO, TIM2 and DMA1 for signal output
  192. *
  193. * @param callback FuriHalSubGhzAsyncTxCallback
  194. * @param context callback context
  195. *
  196. * @return true if the transfer is allowed by belonging to the region
  197. */
  198. bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void* context);
  199. /** Wait for async transmission to complete
  200. *
  201. * @return true if TX complete
  202. */
  203. bool furi_hal_subghz_is_async_tx_complete();
  204. /** Stop async transmission and cleanup resources Resets GPIO, TIM2, and DMA1
  205. */
  206. void furi_hal_subghz_stop_async_tx();
  207. #ifdef __cplusplus
  208. }
  209. #endif