furi_hal_subghz.c 36 KB

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