furi-hal-subghz.c 39 KB

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