furi_hal_subghz.c 36 KB

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