furi-hal-subghz.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. #include "furi-hal-subghz.h"
  2. #include "furi-hal-version.h"
  3. #include <furi-hal-gpio.h>
  4. #include <furi-hal-spi.h>
  5. #include <furi-hal-interrupt.h>
  6. #include <furi-hal-resources.h>
  7. #include <furi.h>
  8. #include <cc1101.h>
  9. #include <stdio.h>
  10. #define TAG "FuriHalSubGhz"
  11. static volatile SubGhzState furi_hal_subghz_state = SubGhzStateInit;
  12. static volatile SubGhzRegulation furi_hal_subghz_regulation = SubGhzRegulationTxRx;
  13. static const uint8_t furi_hal_subghz_preset_ook_270khz_async_regs[][2] = {
  14. // https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz-group/sub-1-ghz/f/sub-1-ghz-forum/382066/cc1101---don-t-know-the-correct-registers-configuration
  15. /* GPIO GD0 */
  16. {CC1101_IOCFG0, 0x0D}, // GD0 as async serial data output/input
  17. /* FIFO and internals */
  18. {CC1101_FIFOTHR, 0x47}, // The only important bit is ADC_RETENTION, FIFO Tx=33 Rx=32
  19. /* Packet engine */
  20. {CC1101_PKTCTRL0, 0x32}, // Async, continious, no whitening
  21. /* Frequency Synthesizer Control */
  22. {CC1101_FSCTRL1, 0x06}, // IF = (26*10^6) / (2^10) * 0x06 = 152343.75Hz
  23. // Modem Configuration
  24. {CC1101_MDMCFG0, 0x00}, // Channel spacing is 25kHz
  25. {CC1101_MDMCFG1, 0x00}, // Channel spacing is 25kHz
  26. {CC1101_MDMCFG2, 0x30}, // Format ASK/OOK, No preamble/sync
  27. {CC1101_MDMCFG3, 0x32}, // Data rate is 3.79372 kBaud
  28. {CC1101_MDMCFG4, 0x67}, // Rx BW filter is 270.833333kHz
  29. /* Main Radio Control State Machine */
  30. {CC1101_MCSM0, 0x18}, // Autocalibrate on idle-to-rx/tx, PO_TIMEOUT is 64 cycles(149-155us)
  31. /* Frequency Offset Compensation Configuration */
  32. {CC1101_FOCCFG,
  33. 0x18}, // no frequency offset compensation, POST_K same as PRE_K, PRE_K is 4K, GATE is off
  34. /* Automatic Gain Control */
  35. {CC1101_AGCCTRL0,
  36. 0x40}, // 01 - Low hysteresis, small asymmetric dead zone, medium gain; 00 - 8 samples agc; 00 - Normal AGC, 00 - 4dB boundary
  37. {CC1101_AGCCTRL1,
  38. 0x00}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
  39. {CC1101_AGCCTRL2, 0x03}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 011 - MAIN_TARGET 24 dB
  40. /* Wake on radio and timeouts control */
  41. {CC1101_WORCTRL, 0xFB}, // WOR_RES is 2^15 periods (0.91 - 0.94 s) 16.5 - 17.2 hours
  42. /* Frontend configuration */
  43. {CC1101_FREND0, 0x11}, // Adjusts current TX LO buffer + high is PATABLE[1]
  44. {CC1101_FREND1, 0xB6}, //
  45. /* Frequency Synthesizer Calibration, valid for 433.92 */
  46. {CC1101_FSCAL3, 0xE9},
  47. {CC1101_FSCAL2, 0x2A},
  48. {CC1101_FSCAL1, 0x00},
  49. {CC1101_FSCAL0, 0x1F},
  50. /* Magic f4ckery */
  51. {CC1101_TEST2, 0x81}, // FIFOTHR ADC_RETENTION=1 matched value
  52. {CC1101_TEST1, 0x35}, // FIFOTHR ADC_RETENTION=1 matched value
  53. {CC1101_TEST0, 0x09}, // VCO selection calibration stage is disabled
  54. /* End */
  55. {0, 0},
  56. };
  57. static const uint8_t furi_hal_subghz_preset_ook_650khz_async_regs[][2] = {
  58. // https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz-group/sub-1-ghz/f/sub-1-ghz-forum/382066/cc1101---don-t-know-the-correct-registers-configuration
  59. /* GPIO GD0 */
  60. {CC1101_IOCFG0, 0x0D}, // GD0 as async serial data output/input
  61. /* FIFO and internals */
  62. {CC1101_FIFOTHR, 0x07}, // The only important bit is ADC_RETENTION
  63. /* Packet engine */
  64. {CC1101_PKTCTRL0, 0x32}, // Async, continious, no whitening
  65. /* Frequency Synthesizer Control */
  66. {CC1101_FSCTRL1, 0x06}, // IF = (26*10^6) / (2^10) * 0x06 = 152343.75Hz
  67. // Modem Configuration
  68. {CC1101_MDMCFG0, 0x00}, // Channel spacing is 25kHz
  69. {CC1101_MDMCFG1, 0x00}, // Channel spacing is 25kHz
  70. {CC1101_MDMCFG2, 0x30}, // Format ASK/OOK, No preamble/sync
  71. {CC1101_MDMCFG3, 0x32}, // Data rate is 3.79372 kBaud
  72. {CC1101_MDMCFG4, 0x17}, // Rx BW filter is 650.000kHz
  73. /* Main Radio Control State Machine */
  74. {CC1101_MCSM0, 0x18}, // Autocalibrate on idle-to-rx/tx, PO_TIMEOUT is 64 cycles(149-155us)
  75. /* Frequency Offset Compensation Configuration */
  76. {CC1101_FOCCFG,
  77. 0x18}, // no frequency offset compensation, POST_K same as PRE_K, PRE_K is 4K, GATE is off
  78. /* Automatic Gain Control */
  79. // {CC1101_AGCTRL0,0x40}, // 01 - Low hysteresis, small asymmetric dead zone, medium gain; 00 - 8 samples agc; 00 - Normal AGC, 00 - 4dB boundary
  80. // {CC1101_AGCTRL1,0x00}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
  81. // {CC1101_AGCCTRL2, 0x03}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 011 - MAIN_TARGET 24 dB
  82. //MAGN_TARGET for RX filter BW =< 100 kHz is 0x3. For higher RX filter BW's MAGN_TARGET is 0x7.
  83. {CC1101_AGCCTRL0,
  84. 0x91}, // 10 - Medium hysteresis, medium asymmetric dead zone, medium gain ; 01 - 16 samples agc; 00 - Normal AGC, 01 - 8dB boundary
  85. {CC1101_AGCCTRL1,
  86. 0x0}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
  87. {CC1101_AGCCTRL2, 0x07}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAIN_TARGET 42 dB
  88. /* Wake on radio and timeouts control */
  89. {CC1101_WORCTRL, 0xFB}, // WOR_RES is 2^15 periods (0.91 - 0.94 s) 16.5 - 17.2 hours
  90. /* Frontend configuration */
  91. {CC1101_FREND0, 0x11}, // Adjusts current TX LO buffer + high is PATABLE[1]
  92. {CC1101_FREND1, 0xB6}, //
  93. /* Frequency Synthesizer Calibration, valid for 433.92 */
  94. {CC1101_FSCAL3, 0xE9},
  95. {CC1101_FSCAL2, 0x2A},
  96. {CC1101_FSCAL1, 0x00},
  97. {CC1101_FSCAL0, 0x1F},
  98. /* Magic f4ckery */
  99. {CC1101_TEST2, 0x88},
  100. {CC1101_TEST1, 0x31},
  101. {CC1101_TEST0, 0x09}, // VCO selection calibration stage is disabled
  102. /* End */
  103. {0, 0},
  104. };
  105. static const uint8_t furi_hal_subghz_preset_2fsk_dev2_38khz_async_regs[][2] = {
  106. /* GPIO GD0 */
  107. {CC1101_IOCFG0, 0x0D}, // GD0 as async serial data output/input
  108. /* Frequency Synthesizer Control */
  109. {CC1101_FSCTRL1, 0x06}, // IF = (26*10^6) / (2^10) * 0x06 = 152343.75Hz
  110. /* Packet engine */
  111. {CC1101_PKTCTRL0, 0x32}, // Async, continious, no whitening
  112. {CC1101_PKTCTRL1, 0x04},
  113. // // Modem Configuration
  114. {CC1101_MDMCFG0, 0x00},
  115. {CC1101_MDMCFG1, 0x02},
  116. {CC1101_MDMCFG2, 0x04}, // Format 2-FSK/FM, No preamble/sync, Disable (current optimized)
  117. {CC1101_MDMCFG3, 0x83}, // Data rate is 4.79794 kBaud
  118. {CC1101_MDMCFG4, 0x67}, //Rx BW filter is 270.833333 kHz
  119. {CC1101_DEVIATN, 0x04}, //Deviation 2.380371 kHz
  120. /* Main Radio Control State Machine */
  121. {CC1101_MCSM0, 0x18}, // Autocalibrate on idle-to-rx/tx, PO_TIMEOUT is 64 cycles(149-155us)
  122. /* Frequency Offset Compensation Configuration */
  123. {CC1101_FOCCFG,
  124. 0x16}, // no frequency offset compensation, POST_K same as PRE_K, PRE_K is 4K, GATE is off
  125. /* Automatic Gain Control */
  126. {CC1101_AGCCTRL0,
  127. 0x91}, //10 - Medium hysteresis, medium asymmetric dead zone, medium gain ; 01 - 16 samples agc; 00 - Normal AGC, 01 - 8dB boundary
  128. {CC1101_AGCCTRL1,
  129. 0x00}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
  130. {CC1101_AGCCTRL2, 0x07}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAIN_TARGET 42 dB
  131. /* Wake on radio and timeouts control */
  132. {CC1101_WORCTRL, 0xFB}, // WOR_RES is 2^15 periods (0.91 - 0.94 s) 16.5 - 17.2 hours
  133. /* Frontend configuration */
  134. {CC1101_FREND0, 0x10}, // Adjusts current TX LO buffer
  135. {CC1101_FREND1, 0x56},
  136. /* Frequency Synthesizer Calibration, valid for 433.92 */
  137. {CC1101_FSCAL3, 0xE9},
  138. {CC1101_FSCAL2, 0x2A},
  139. {CC1101_FSCAL1, 0x00},
  140. {CC1101_FSCAL0, 0x1F},
  141. /* Magic f4ckery */
  142. {CC1101_TEST2, 0x81}, // FIFOTHR ADC_RETENTION=1 matched value
  143. {CC1101_TEST1, 0x35}, // FIFOTHR ADC_RETENTION=1 matched value
  144. {CC1101_TEST0, 0x09}, // VCO selection calibration stage is disabled
  145. /* End */
  146. {0, 0},
  147. };
  148. static const uint8_t furi_hal_subghz_preset_2fsk_dev4_76khz_async_regs[][2] = {
  149. /* GPIO GD0 */
  150. {CC1101_IOCFG0, 0x0D}, // GD0 as async serial data output/input
  151. /* Frequency Synthesizer Control */
  152. {CC1101_FSCTRL1, 0x06}, // IF = (26*10^6) / (2^10) * 0x06 = 152343.75Hz
  153. /* Packet engine */
  154. {CC1101_PKTCTRL0, 0x32}, // Async, continious, no whitening
  155. {CC1101_PKTCTRL1, 0x04},
  156. // // Modem Configuration
  157. {CC1101_MDMCFG0, 0x00},
  158. {CC1101_MDMCFG1, 0x02},
  159. {CC1101_MDMCFG2, 0x04}, // Format 2-FSK/FM, No preamble/sync, Disable (current optimized)
  160. {CC1101_MDMCFG3, 0x83}, // Data rate is 4.79794 kBaud
  161. {CC1101_MDMCFG4, 0x67}, //Rx BW filter is 270.833333 kHz
  162. {CC1101_DEVIATN, 0x14}, //Deviation 4.760742 kHz
  163. /* Main Radio Control State Machine */
  164. {CC1101_MCSM0, 0x18}, // Autocalibrate on idle-to-rx/tx, PO_TIMEOUT is 64 cycles(149-155us)
  165. /* Frequency Offset Compensation Configuration */
  166. {CC1101_FOCCFG,
  167. 0x16}, // no frequency offset compensation, POST_K same as PRE_K, PRE_K is 4K, GATE is off
  168. /* Automatic Gain Control */
  169. {CC1101_AGCCTRL0,
  170. 0x91}, //10 - Medium hysteresis, medium asymmetric dead zone, medium gain ; 01 - 16 samples agc; 00 - Normal AGC, 01 - 8dB boundary
  171. {CC1101_AGCCTRL1,
  172. 0x00}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
  173. {CC1101_AGCCTRL2, 0x07}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAIN_TARGET 42 dB
  174. /* Wake on radio and timeouts control */
  175. {CC1101_WORCTRL, 0xFB}, // WOR_RES is 2^15 periods (0.91 - 0.94 s) 16.5 - 17.2 hours
  176. /* Frontend configuration */
  177. {CC1101_FREND0, 0x10}, // Adjusts current TX LO buffer
  178. {CC1101_FREND1, 0x56},
  179. /* Frequency Synthesizer Calibration, valid for 433.92 */
  180. {CC1101_FSCAL3, 0xE9},
  181. {CC1101_FSCAL2, 0x2A},
  182. {CC1101_FSCAL1, 0x00},
  183. {CC1101_FSCAL0, 0x1F},
  184. /* Magic f4ckery */
  185. {CC1101_TEST2, 0x81}, // FIFOTHR ADC_RETENTION=1 matched value
  186. {CC1101_TEST1, 0x35}, // FIFOTHR ADC_RETENTION=1 matched value
  187. {CC1101_TEST0, 0x09}, // VCO selection calibration stage is disabled
  188. /* End */
  189. {0, 0},
  190. };
  191. static const uint8_t furi_hal_subghz_preset_msk_99_97kb_async_regs[][2] = {
  192. /* GPIO GD0 */
  193. {CC1101_IOCFG0, 0x06},
  194. {CC1101_FIFOTHR, 0x07}, // The only important bit is ADC_RETENTION
  195. {CC1101_SYNC1, 0x46},
  196. {CC1101_SYNC0, 0x4C},
  197. {CC1101_ADDR, 0x00},
  198. {CC1101_PKTLEN, 0x00},
  199. {CC1101_CHANNR, 0x00},
  200. {CC1101_PKTCTRL0, 0x05},
  201. {CC1101_FSCTRL0, 0x23},
  202. {CC1101_FSCTRL1, 0x06},
  203. {CC1101_MDMCFG0, 0xF8},
  204. {CC1101_MDMCFG1, 0x22},
  205. {CC1101_MDMCFG2, 0x72},
  206. {CC1101_MDMCFG3, 0xF8},
  207. {CC1101_MDMCFG4, 0x5B},
  208. {CC1101_DEVIATN, 0x47},
  209. {CC1101_MCSM0, 0x18},
  210. {CC1101_FOCCFG, 0x16},
  211. {CC1101_AGCCTRL0, 0xB2},
  212. {CC1101_AGCCTRL1, 0x00},
  213. {CC1101_AGCCTRL2, 0xC7},
  214. {CC1101_FREND0, 0x10},
  215. {CC1101_FREND1, 0x56},
  216. {CC1101_FSCAL3, 0xE9},
  217. {CC1101_FSCAL2, 0x2A},
  218. {CC1101_FSCAL1, 0x00},
  219. {CC1101_FSCAL0, 0x1F},
  220. {CC1101_BSCFG, 0x1C},
  221. {CC1101_FSTEST, 0x59},
  222. {CC1101_TEST2, 0x81},
  223. {CC1101_TEST1, 0x35},
  224. {CC1101_TEST0, 0x09},
  225. /* End */
  226. {0, 0},
  227. };
  228. static const uint8_t furi_hal_subghz_preset_ook_async_patable[8] = {
  229. 0x00,
  230. 0xC0, // 10dBm 0xC0, 7dBm 0xC8, 5dBm 0x84, 0dBm 0x60, -10dBm 0x34, -15dBm 0x1D, -20dBm 0x0E, -30dBm 0x12
  231. 0x00,
  232. 0x00,
  233. 0x00,
  234. 0x00,
  235. 0x00,
  236. 0x00};
  237. static const uint8_t furi_hal_subghz_preset_2fsk_async_patable[8] = {
  238. 0xC0, // 10dBm 0xC0, 7dBm 0xC8, 5dBm 0x84, 0dBm 0x60, -10dBm 0x34, -15dBm 0x1D, -20dBm 0x0E, -30dBm 0x12
  239. 0x00,
  240. 0x00,
  241. 0x00,
  242. 0x00,
  243. 0x00,
  244. 0x00,
  245. 0x00};
  246. static const uint8_t furi_hal_subghz_preset_msk_async_patable[8] = {
  247. 0xC0, // 10dBm 0xC0, 7dBm 0xC8, 5dBm 0x84, 0dBm 0x60, -10dBm 0x34, -15dBm 0x1D, -20dBm 0x0E, -30dBm 0x12
  248. 0x00,
  249. 0x00,
  250. 0x00,
  251. 0x00,
  252. 0x00,
  253. 0x00,
  254. 0x00};
  255. void furi_hal_subghz_init() {
  256. furi_assert(furi_hal_subghz_state == SubGhzStateInit);
  257. furi_hal_subghz_state = SubGhzStateIdle;
  258. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  259. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  260. hal_gpio_init(&FURI_HAL_SUBGHZ_TX_GPIO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  261. #endif
  262. // Reset
  263. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  264. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  265. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  266. // Prepare GD0 for power on self test
  267. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  268. // GD0 low
  269. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW);
  270. while(hal_gpio_read(&gpio_cc1101_g0) != false)
  271. ;
  272. // GD0 high
  273. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW | CC1101_IOCFG_INV);
  274. while(hal_gpio_read(&gpio_cc1101_g0) != true)
  275. ;
  276. // Reset GD0 to floating state
  277. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  278. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  279. // RF switches
  280. hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  281. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  282. // Go to sleep
  283. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  284. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  285. FURI_LOG_I(TAG, "Init OK");
  286. }
  287. void furi_hal_subghz_sleep() {
  288. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  289. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  290. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  291. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  292. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  293. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  294. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  295. }
  296. void furi_hal_subghz_dump_state() {
  297. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  298. printf(
  299. "[furi_hal_subghz] cc1101 chip %d, version %d\r\n",
  300. cc1101_get_partnumber(&furi_hal_spi_bus_handle_subghz),
  301. cc1101_get_version(&furi_hal_spi_bus_handle_subghz));
  302. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  303. }
  304. void furi_hal_subghz_load_preset(FuriHalSubGhzPreset preset) {
  305. if(preset == FuriHalSubGhzPresetOok650Async) {
  306. furi_hal_subghz_load_registers(furi_hal_subghz_preset_ook_650khz_async_regs);
  307. furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
  308. } else if(preset == FuriHalSubGhzPresetOok270Async) {
  309. furi_hal_subghz_load_registers(furi_hal_subghz_preset_ook_270khz_async_regs);
  310. furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
  311. } else if(preset == FuriHalSubGhzPreset2FSKDev238Async) {
  312. furi_hal_subghz_load_registers(furi_hal_subghz_preset_2fsk_dev2_38khz_async_regs);
  313. furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
  314. } else if(preset == FuriHalSubGhzPreset2FSKDev476Async) {
  315. furi_hal_subghz_load_registers(furi_hal_subghz_preset_2fsk_dev4_76khz_async_regs);
  316. furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
  317. } else if(preset == FuriHalSubGhzPresetMSK99_97KbAsync) {
  318. furi_hal_subghz_load_registers(furi_hal_subghz_preset_msk_99_97kb_async_regs);
  319. furi_hal_subghz_load_patable(furi_hal_subghz_preset_msk_async_patable);
  320. } else {
  321. furi_crash(NULL);
  322. }
  323. }
  324. void furi_hal_subghz_load_registers(const uint8_t data[][2]) {
  325. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  326. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  327. uint32_t i = 0;
  328. while(data[i][0]) {
  329. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, data[i][0], data[i][1]);
  330. i++;
  331. }
  332. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  333. }
  334. void furi_hal_subghz_load_patable(const uint8_t data[8]) {
  335. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  336. cc1101_set_pa_table(&furi_hal_spi_bus_handle_subghz, data);
  337. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  338. }
  339. void furi_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
  340. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  341. cc1101_flush_tx(&furi_hal_spi_bus_handle_subghz);
  342. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_FIFO, size);
  343. cc1101_write_fifo(&furi_hal_spi_bus_handle_subghz, data, size);
  344. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  345. }
  346. void furi_hal_subghz_flush_rx() {
  347. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  348. cc1101_flush_rx(&furi_hal_spi_bus_handle_subghz);
  349. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  350. }
  351. bool furi_hal_subghz_rx_pipe_not_empty() {
  352. CC1101RxBytes status[1];
  353. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  354. cc1101_read_reg(&furi_hal_spi_bus_handle_subghz, (CC1101_STATUS_RXBYTES) | CC1101_BURST, (uint8_t*)status);
  355. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  356. // TODO: you can add a buffer overflow flag if needed
  357. if(status->NUM_RXBYTES > 0) {
  358. return true;
  359. } else {
  360. return false;
  361. }
  362. }
  363. bool furi_hal_subghz_is_rx_data_crc_valid() {
  364. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  365. uint8_t data[1];
  366. cc1101_read_reg(&furi_hal_spi_bus_handle_subghz, CC1101_STATUS_LQI | CC1101_BURST, data);
  367. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  368. if(((data[0] >> 7) & 0x01)) {
  369. return true;
  370. } else {
  371. return false;
  372. }
  373. }
  374. void furi_hal_subghz_read_packet(uint8_t* data, uint8_t* size) {
  375. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  376. cc1101_read_fifo(&furi_hal_spi_bus_handle_subghz, data, size);
  377. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  378. }
  379. void furi_hal_subghz_shutdown() {
  380. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  381. // Reset and shutdown
  382. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  383. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  384. }
  385. void furi_hal_subghz_reset() {
  386. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  387. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  388. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  389. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  390. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  391. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  392. }
  393. void furi_hal_subghz_idle() {
  394. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  395. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  396. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  397. }
  398. void furi_hal_subghz_rx() {
  399. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  400. cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
  401. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  402. }
  403. bool furi_hal_subghz_tx() {
  404. if(furi_hal_subghz_regulation != SubGhzRegulationTxRx) return false;
  405. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  406. cc1101_switch_to_tx(&furi_hal_spi_bus_handle_subghz);
  407. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  408. return true;
  409. }
  410. float furi_hal_subghz_get_rssi() {
  411. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  412. int32_t rssi_dec = cc1101_get_rssi(&furi_hal_spi_bus_handle_subghz);
  413. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  414. float rssi = rssi_dec;
  415. if(rssi_dec >= 128) {
  416. rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
  417. } else {
  418. rssi = (rssi / 2.0f) - 74.0f;
  419. }
  420. return rssi;
  421. }
  422. bool furi_hal_subghz_is_frequency_valid(uint32_t value) {
  423. if(!(value >= 299999755 && value <= 348000335) &&
  424. !(value >= 386999938 && value <= 464000000) &&
  425. !(value >= 778999847 && value <= 928000000)) {
  426. return false;
  427. }
  428. return true;
  429. }
  430. uint32_t furi_hal_subghz_set_frequency_and_path(uint32_t value) {
  431. value = furi_hal_subghz_set_frequency(value);
  432. if(value >= 299999755 && value <= 348000335) {
  433. furi_hal_subghz_set_path(FuriHalSubGhzPath315);
  434. } else if(value >= 386999938 && value <= 464000000) {
  435. furi_hal_subghz_set_path(FuriHalSubGhzPath433);
  436. } else if(value >= 778999847 && value <= 928000000) {
  437. furi_hal_subghz_set_path(FuriHalSubGhzPath868);
  438. } else {
  439. furi_crash(NULL);
  440. }
  441. return value;
  442. }
  443. bool furi_hal_subghz_is_tx_allowed(uint32_t value) {
  444. //checking regional settings
  445. bool is_allowed = false;
  446. switch(furi_hal_version_get_hw_region()) {
  447. case FuriHalVersionRegionEuRu:
  448. //433,05..434,79; 868,15..868,55
  449. if(!(value >= 433050000 && value <= 434790000) &&
  450. !(value >= 868150000 && value <= 868550000)) {
  451. } else {
  452. is_allowed = true;
  453. }
  454. break;
  455. case FuriHalVersionRegionUsCaAu:
  456. //304,10..315,25; 433,05..434,79; 915,00..928,00
  457. if(!(value >= 304100000 && value <= 315250000) &&
  458. !(value >= 433050000 && value <= 434790000) &&
  459. !(value >= 915000000 && value <= 928000000)) {
  460. } else {
  461. is_allowed = true;
  462. }
  463. break;
  464. case FuriHalVersionRegionJp:
  465. //312,00..315,25; 920,50..923,50
  466. if(!(value >= 312000000 && value <= 315250000) &&
  467. !(value >= 920500000 && value <= 923500000)) {
  468. } else {
  469. is_allowed = true;
  470. }
  471. break;
  472. default:
  473. is_allowed = true;
  474. break;
  475. }
  476. return is_allowed;
  477. }
  478. uint32_t furi_hal_subghz_set_frequency(uint32_t value) {
  479. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  480. if(furi_hal_subghz_is_tx_allowed(value)) {
  481. furi_hal_subghz_regulation = SubGhzRegulationTxRx;
  482. } else {
  483. furi_hal_subghz_regulation = SubGhzRegulationOnlyRx;
  484. }
  485. uint32_t real_frequency = cc1101_set_frequency(&furi_hal_spi_bus_handle_subghz, value);
  486. cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
  487. while(true) {
  488. CC1101Status status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
  489. if(status.STATE == CC1101StateIDLE) break;
  490. }
  491. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  492. return real_frequency;
  493. }
  494. void furi_hal_subghz_set_path(FuriHalSubGhzPath path) {
  495. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  496. if(path == FuriHalSubGhzPath433) {
  497. hal_gpio_write(&gpio_rf_sw_0, 0);
  498. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
  499. } else if(path == FuriHalSubGhzPath315) {
  500. hal_gpio_write(&gpio_rf_sw_0, 1);
  501. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  502. } else if(path == FuriHalSubGhzPath868) {
  503. hal_gpio_write(&gpio_rf_sw_0, 1);
  504. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
  505. } else if(path == FuriHalSubGhzPathIsolate) {
  506. hal_gpio_write(&gpio_rf_sw_0, 0);
  507. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  508. } else {
  509. furi_crash(NULL);
  510. }
  511. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  512. }
  513. volatile uint32_t furi_hal_subghz_capture_delta_duration = 0;
  514. volatile FuriHalSubGhzCaptureCallback furi_hal_subghz_capture_callback = NULL;
  515. volatile void* furi_hal_subghz_capture_callback_context = NULL;
  516. static void furi_hal_subghz_capture_ISR() {
  517. // Channel 1
  518. if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
  519. LL_TIM_ClearFlag_CC1(TIM2);
  520. furi_hal_subghz_capture_delta_duration = LL_TIM_IC_GetCaptureCH1(TIM2);
  521. if(furi_hal_subghz_capture_callback) {
  522. furi_hal_subghz_capture_callback(
  523. true,
  524. furi_hal_subghz_capture_delta_duration,
  525. (void*)furi_hal_subghz_capture_callback_context);
  526. }
  527. }
  528. // Channel 2
  529. if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
  530. LL_TIM_ClearFlag_CC2(TIM2);
  531. if(furi_hal_subghz_capture_callback) {
  532. furi_hal_subghz_capture_callback(
  533. false,
  534. LL_TIM_IC_GetCaptureCH2(TIM2) - furi_hal_subghz_capture_delta_duration,
  535. (void*)furi_hal_subghz_capture_callback_context);
  536. }
  537. }
  538. }
  539. void furi_hal_subghz_start_async_rx(FuriHalSubGhzCaptureCallback callback, void* context) {
  540. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  541. furi_hal_subghz_state = SubGhzStateAsyncRx;
  542. furi_hal_subghz_capture_callback = callback;
  543. furi_hal_subghz_capture_callback_context = context;
  544. hal_gpio_init_ex(
  545. &gpio_cc1101_g0, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
  546. // Timer: base
  547. FURI_CRITICAL_ENTER();
  548. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  549. FURI_CRITICAL_EXIT();
  550. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  551. TIM_InitStruct.Prescaler = 64 - 1;
  552. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  553. TIM_InitStruct.Autoreload = 0x7FFFFFFE;
  554. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV4;
  555. LL_TIM_Init(TIM2, &TIM_InitStruct);
  556. // Timer: advanced
  557. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  558. LL_TIM_DisableARRPreload(TIM2);
  559. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI2FP2);
  560. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
  561. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  562. LL_TIM_EnableMasterSlaveMode(TIM2);
  563. LL_TIM_DisableDMAReq_TRIG(TIM2);
  564. LL_TIM_DisableIT_TRIG(TIM2);
  565. // Timer: channel 1 indirect
  566. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_INDIRECTTI);
  567. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  568. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
  569. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  570. // Timer: channel 2 direct
  571. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
  572. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  573. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
  574. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV32_N8);
  575. // ISR setup
  576. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_subghz_capture_ISR);
  577. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  578. NVIC_EnableIRQ(TIM2_IRQn);
  579. // Interrupts and channels
  580. LL_TIM_EnableIT_CC1(TIM2);
  581. LL_TIM_EnableIT_CC2(TIM2);
  582. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  583. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  584. // Enable NVIC
  585. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  586. NVIC_EnableIRQ(TIM2_IRQn);
  587. // Start timer
  588. LL_TIM_SetCounter(TIM2, 0);
  589. LL_TIM_EnableCounter(TIM2);
  590. // Switch to RX
  591. furi_hal_subghz_rx();
  592. }
  593. void furi_hal_subghz_stop_async_rx() {
  594. furi_assert(furi_hal_subghz_state == SubGhzStateAsyncRx);
  595. furi_hal_subghz_state = SubGhzStateIdle;
  596. // Shutdown radio
  597. furi_hal_subghz_idle();
  598. FURI_CRITICAL_ENTER();
  599. LL_TIM_DeInit(TIM2);
  600. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  601. FURI_CRITICAL_EXIT();
  602. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  603. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  604. }
  605. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL (256)
  606. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF (API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL / 2)
  607. #define API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME 333
  608. typedef struct {
  609. uint32_t* buffer;
  610. bool flip_flop;
  611. FuriHalSubGhzAsyncTxCallback callback;
  612. void* callback_context;
  613. } FuriHalSubGhzAsyncTx;
  614. static FuriHalSubGhzAsyncTx furi_hal_subghz_async_tx = {0};
  615. static void furi_hal_subghz_async_tx_refill(uint32_t* buffer, size_t samples) {
  616. while(samples > 0) {
  617. bool is_odd = samples % 2;
  618. LevelDuration ld =
  619. furi_hal_subghz_async_tx.callback(furi_hal_subghz_async_tx.callback_context);
  620. if(level_duration_is_wait(ld)) return;
  621. if(level_duration_is_reset(ld)) {
  622. // One more even sample required to end at low level
  623. if(is_odd) {
  624. *buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  625. buffer++;
  626. samples--;
  627. }
  628. break;
  629. } else {
  630. // Inject guard time if level is incorrect
  631. if(is_odd == level_duration_get_level(ld)) {
  632. *buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  633. buffer++;
  634. samples--;
  635. }
  636. uint32_t duration = level_duration_get_duration(ld);
  637. furi_assert(duration > 0);
  638. *buffer = duration;
  639. buffer++;
  640. samples--;
  641. }
  642. }
  643. memset(buffer, 0, samples * sizeof(uint32_t));
  644. }
  645. static void furi_hal_subghz_async_tx_dma_isr() {
  646. furi_assert(furi_hal_subghz_state == SubGhzStateAsyncTx);
  647. if(LL_DMA_IsActiveFlag_HT1(DMA1)) {
  648. LL_DMA_ClearFlag_HT1(DMA1);
  649. furi_hal_subghz_async_tx_refill(
  650. furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
  651. }
  652. if(LL_DMA_IsActiveFlag_TC1(DMA1)) {
  653. LL_DMA_ClearFlag_TC1(DMA1);
  654. furi_hal_subghz_async_tx_refill(
  655. furi_hal_subghz_async_tx.buffer + API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF,
  656. API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
  657. }
  658. }
  659. static void furi_hal_subghz_async_tx_timer_isr() {
  660. if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) {
  661. LL_TIM_ClearFlag_UPDATE(TIM2);
  662. if(LL_TIM_GetAutoReload(TIM2) == 0) {
  663. if(furi_hal_subghz_state == SubGhzStateAsyncTx) {
  664. furi_hal_subghz_state = SubGhzStateAsyncTxLast;
  665. //forcibly pulls the pin to the ground so that there is no carrier
  666. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullDown, GpioSpeedLow);
  667. } else {
  668. furi_hal_subghz_state = SubGhzStateAsyncTxEnd;
  669. LL_TIM_DisableCounter(TIM2);
  670. }
  671. }
  672. }
  673. }
  674. bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void* context) {
  675. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  676. furi_assert(callback);
  677. //If transmission is prohibited by regional settings
  678. if(furi_hal_subghz_regulation != SubGhzRegulationTxRx) return false;
  679. furi_hal_subghz_async_tx.callback = callback;
  680. furi_hal_subghz_async_tx.callback_context = context;
  681. furi_hal_subghz_state = SubGhzStateAsyncTx;
  682. furi_hal_subghz_async_tx.buffer =
  683. furi_alloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
  684. furi_hal_subghz_async_tx_refill(
  685. furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL);
  686. // Connect CC1101_GD0 to TIM2 as output
  687. hal_gpio_init_ex(
  688. &gpio_cc1101_g0, GpioModeAltFunctionPushPull, GpioPullDown, GpioSpeedLow, GpioAltFn1TIM2);
  689. // Configure DMA
  690. LL_DMA_InitTypeDef dma_config = {0};
  691. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR);
  692. dma_config.MemoryOrM2MDstAddress = (uint32_t)furi_hal_subghz_async_tx.buffer;
  693. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  694. dma_config.Mode = LL_DMA_MODE_CIRCULAR;
  695. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  696. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  697. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  698. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
  699. dma_config.NbData = API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL;
  700. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP;
  701. dma_config.Priority = LL_DMA_MODE_NORMAL;
  702. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
  703. furi_hal_interrupt_set_dma_channel_isr(
  704. DMA1, LL_DMA_CHANNEL_1, furi_hal_subghz_async_tx_dma_isr);
  705. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  706. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_1);
  707. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  708. // Configure TIM2
  709. FURI_CRITICAL_ENTER();
  710. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  711. FURI_CRITICAL_EXIT();
  712. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  713. TIM_InitStruct.Prescaler = 64 - 1;
  714. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  715. TIM_InitStruct.Autoreload = 1000;
  716. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  717. LL_TIM_Init(TIM2, &TIM_InitStruct);
  718. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  719. LL_TIM_EnableARRPreload(TIM2);
  720. // Configure TIM2 CH2
  721. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  722. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
  723. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  724. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  725. TIM_OC_InitStruct.CompareValue = 0;
  726. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  727. LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
  728. LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2);
  729. LL_TIM_DisableMasterSlaveMode(TIM2);
  730. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_subghz_async_tx_timer_isr);
  731. LL_TIM_EnableIT_UPDATE(TIM2);
  732. LL_TIM_EnableDMAReq_UPDATE(TIM2);
  733. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  734. // Start counter
  735. LL_TIM_GenerateEvent_UPDATE(TIM2);
  736. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  737. hal_gpio_write(&FURI_HAL_SUBGHZ_TX_GPIO, true);
  738. #endif
  739. furi_hal_subghz_tx();
  740. // Enable NVIC
  741. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  742. NVIC_EnableIRQ(TIM2_IRQn);
  743. LL_TIM_SetCounter(TIM2, 0);
  744. LL_TIM_EnableCounter(TIM2);
  745. return true;
  746. }
  747. bool furi_hal_subghz_is_async_tx_complete() {
  748. return furi_hal_subghz_state == SubGhzStateAsyncTxEnd;
  749. }
  750. void furi_hal_subghz_stop_async_tx() {
  751. furi_assert(
  752. furi_hal_subghz_state == SubGhzStateAsyncTx ||
  753. furi_hal_subghz_state == SubGhzStateAsyncTxLast ||
  754. furi_hal_subghz_state == SubGhzStateAsyncTxEnd);
  755. // Shutdown radio
  756. furi_hal_subghz_idle();
  757. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  758. hal_gpio_write(&FURI_HAL_SUBGHZ_TX_GPIO, false);
  759. #endif
  760. // Deinitialize Timer
  761. FURI_CRITICAL_ENTER();
  762. LL_TIM_DeInit(TIM2);
  763. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  764. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  765. // Deinitialize DMA
  766. LL_DMA_DeInit(DMA1, LL_DMA_CHANNEL_1);
  767. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
  768. // Deinitialize GPIO
  769. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  770. FURI_CRITICAL_EXIT();
  771. free(furi_hal_subghz_async_tx.buffer);
  772. furi_hal_subghz_state = SubGhzStateIdle;
  773. }