furi-hal-subghz.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. #include "furi-hal-subghz.h"
  2. #include "furi-hal-version.h"
  3. #include <furi-hal-gpio.h>
  4. #include <furi-hal-spi.h>
  5. #include <furi-hal-interrupt.h>
  6. #include <furi-hal-resources.h>
  7. #include <furi.h>
  8. #include <cc1101.h>
  9. #include <stdio.h>
  10. #define __STDC_FORMAT_MACROS
  11. #include <inttypes.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 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, // 10dBm 0xC0, 7dBm 0xC8, 5dBm 0x84, 0dBm 0x60, -10dBm 0x34, -15dBm 0x1D, -20dBm 0x0E, -30dBm 0x12
  264. 0x00,
  265. 0x00,
  266. 0x00,
  267. 0x00,
  268. 0x00,
  269. 0x00};
  270. static const uint8_t furi_hal_subghz_preset_2fsk_async_patable[8] = {
  271. 0xC0, // 10dBm 0xC0, 7dBm 0xC8, 5dBm 0x84, 0dBm 0x60, -10dBm 0x34, -15dBm 0x1D, -20dBm 0x0E, -30dBm 0x12
  272. 0x00,
  273. 0x00,
  274. 0x00,
  275. 0x00,
  276. 0x00,
  277. 0x00,
  278. 0x00};
  279. static const uint8_t furi_hal_subghz_preset_msk_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_gfsk_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. void furi_hal_subghz_init() {
  298. furi_assert(furi_hal_subghz_state == SubGhzStateInit);
  299. furi_hal_subghz_state = SubGhzStateIdle;
  300. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  301. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  302. hal_gpio_init(&FURI_HAL_SUBGHZ_TX_GPIO, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  303. #endif
  304. // Reset
  305. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  306. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  307. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  308. // Prepare GD0 for power on self test
  309. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  310. // GD0 low
  311. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW);
  312. while(hal_gpio_read(&gpio_cc1101_g0) != false)
  313. ;
  314. // GD0 high
  315. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHW | CC1101_IOCFG_INV);
  316. while(hal_gpio_read(&gpio_cc1101_g0) != true)
  317. ;
  318. // Reset GD0 to floating state
  319. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  320. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  321. // RF switches
  322. hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  323. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  324. // Go to sleep
  325. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  326. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  327. FURI_LOG_I(TAG, "Init OK");
  328. }
  329. void furi_hal_subghz_sleep() {
  330. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  331. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  332. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  333. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  334. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  335. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  336. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  337. }
  338. void furi_hal_subghz_dump_state() {
  339. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  340. printf(
  341. "[furi_hal_subghz] cc1101 chip %d, version %d\r\n",
  342. cc1101_get_partnumber(&furi_hal_spi_bus_handle_subghz),
  343. cc1101_get_version(&furi_hal_spi_bus_handle_subghz));
  344. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  345. }
  346. void furi_hal_subghz_load_preset(FuriHalSubGhzPreset preset) {
  347. if(preset == FuriHalSubGhzPresetOok650Async) {
  348. furi_hal_subghz_load_registers(furi_hal_subghz_preset_ook_650khz_async_regs);
  349. furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
  350. } else if(preset == FuriHalSubGhzPresetOok270Async) {
  351. furi_hal_subghz_load_registers(furi_hal_subghz_preset_ook_270khz_async_regs);
  352. furi_hal_subghz_load_patable(furi_hal_subghz_preset_ook_async_patable);
  353. } else if(preset == FuriHalSubGhzPreset2FSKDev238Async) {
  354. furi_hal_subghz_load_registers(furi_hal_subghz_preset_2fsk_dev2_38khz_async_regs);
  355. furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
  356. } else if(preset == FuriHalSubGhzPreset2FSKDev476Async) {
  357. furi_hal_subghz_load_registers(furi_hal_subghz_preset_2fsk_dev4_76khz_async_regs);
  358. furi_hal_subghz_load_patable(furi_hal_subghz_preset_2fsk_async_patable);
  359. } else if(preset == FuriHalSubGhzPresetMSK99_97KbAsync) {
  360. furi_hal_subghz_load_registers(furi_hal_subghz_preset_msk_99_97kb_async_regs);
  361. furi_hal_subghz_load_patable(furi_hal_subghz_preset_msk_async_patable);
  362. } else if(preset == FuriHalSubGhzPresetGFSK9_99KbAsync) {
  363. furi_hal_subghz_load_registers(furi_hal_subghz_preset_gfsk_9_99kb_async_regs);
  364. furi_hal_subghz_load_patable(furi_hal_subghz_preset_gfsk_async_patable);
  365. } else{
  366. furi_crash(NULL);
  367. }
  368. }
  369. void furi_hal_subghz_load_registers(const uint8_t data[][2]) {
  370. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  371. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  372. uint32_t i = 0;
  373. while(data[i][0]) {
  374. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, data[i][0], data[i][1]);
  375. i++;
  376. }
  377. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  378. }
  379. void furi_hal_subghz_load_patable(const uint8_t data[8]) {
  380. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  381. cc1101_set_pa_table(&furi_hal_spi_bus_handle_subghz, data);
  382. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  383. }
  384. void furi_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
  385. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  386. cc1101_flush_tx(&furi_hal_spi_bus_handle_subghz);
  387. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_FIFO, size);
  388. cc1101_write_fifo(&furi_hal_spi_bus_handle_subghz, data, size);
  389. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  390. }
  391. void furi_hal_subghz_flush_rx() {
  392. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  393. cc1101_flush_rx(&furi_hal_spi_bus_handle_subghz);
  394. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  395. }
  396. void furi_hal_subghz_flush_tx() {
  397. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  398. cc1101_flush_tx(&furi_hal_spi_bus_handle_subghz);
  399. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  400. }
  401. bool furi_hal_subghz_rx_pipe_not_empty() {
  402. CC1101RxBytes status[1];
  403. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  404. cc1101_read_reg(&furi_hal_spi_bus_handle_subghz, (CC1101_STATUS_RXBYTES) | CC1101_BURST, (uint8_t*)status);
  405. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  406. // TODO: you can add a buffer overflow flag if needed
  407. if(status->NUM_RXBYTES > 0) {
  408. return true;
  409. } else {
  410. return false;
  411. }
  412. }
  413. bool furi_hal_subghz_is_rx_data_crc_valid() {
  414. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  415. uint8_t data[1];
  416. cc1101_read_reg(&furi_hal_spi_bus_handle_subghz, CC1101_STATUS_LQI | CC1101_BURST, data);
  417. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  418. if(((data[0] >> 7) & 0x01)) {
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. void furi_hal_subghz_read_packet(uint8_t* data, uint8_t* size) {
  425. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  426. cc1101_read_fifo(&furi_hal_spi_bus_handle_subghz, data, size);
  427. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  428. }
  429. void furi_hal_subghz_shutdown() {
  430. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  431. // Reset and shutdown
  432. cc1101_shutdown(&furi_hal_spi_bus_handle_subghz);
  433. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  434. }
  435. void furi_hal_subghz_reset() {
  436. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  437. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  438. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  439. cc1101_reset(&furi_hal_spi_bus_handle_subghz);
  440. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  441. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  442. }
  443. void furi_hal_subghz_idle() {
  444. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  445. cc1101_switch_to_idle(&furi_hal_spi_bus_handle_subghz);
  446. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  447. }
  448. void furi_hal_subghz_rx() {
  449. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  450. cc1101_switch_to_rx(&furi_hal_spi_bus_handle_subghz);
  451. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  452. }
  453. bool furi_hal_subghz_tx() {
  454. if(furi_hal_subghz_regulation != SubGhzRegulationTxRx) return false;
  455. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  456. cc1101_switch_to_tx(&furi_hal_spi_bus_handle_subghz);
  457. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  458. return true;
  459. }
  460. float furi_hal_subghz_get_rssi() {
  461. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  462. int32_t rssi_dec = cc1101_get_rssi(&furi_hal_spi_bus_handle_subghz);
  463. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  464. float rssi = rssi_dec;
  465. if(rssi_dec >= 128) {
  466. rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
  467. } else {
  468. rssi = (rssi / 2.0f) - 74.0f;
  469. }
  470. return rssi;
  471. }
  472. uint8_t furi_hal_subghz_get_lqi() {
  473. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  474. uint8_t data[1];
  475. cc1101_read_reg(&furi_hal_spi_bus_handle_subghz, CC1101_STATUS_LQI | CC1101_BURST, data);
  476. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  477. return data[0] & 0x7F;
  478. }
  479. bool furi_hal_subghz_is_frequency_valid(uint32_t value) {
  480. if(!(value >= 299999755 && value <= 348000335) &&
  481. !(value >= 386999938 && value <= 464000000) &&
  482. !(value >= 778999847 && value <= 928000000)) {
  483. return false;
  484. }
  485. return true;
  486. }
  487. uint32_t furi_hal_subghz_set_frequency_and_path(uint32_t value) {
  488. value = furi_hal_subghz_set_frequency(value);
  489. if(value >= 299999755 && value <= 348000335) {
  490. furi_hal_subghz_set_path(FuriHalSubGhzPath315);
  491. } else if(value >= 386999938 && value <= 464000000) {
  492. furi_hal_subghz_set_path(FuriHalSubGhzPath433);
  493. } else if(value >= 778999847 && value <= 928000000) {
  494. furi_hal_subghz_set_path(FuriHalSubGhzPath868);
  495. } else {
  496. furi_crash(NULL);
  497. }
  498. return value;
  499. }
  500. bool furi_hal_subghz_is_tx_allowed(uint32_t value) {
  501. //checking regional settings
  502. bool is_allowed = false;
  503. switch(furi_hal_version_get_hw_region()) {
  504. case FuriHalVersionRegionEuRu:
  505. //433,05..434,79; 868,15..868,55
  506. if(!(value >= 433050000 && value <= 434790000) &&
  507. !(value >= 868150000 && value <= 868550000)) {
  508. } else {
  509. is_allowed = true;
  510. }
  511. break;
  512. case FuriHalVersionRegionUsCaAu:
  513. //304,10..315,25; 433,05..434,79; 915,00..928,00
  514. if(!(value >= 304100000 && value <= 315250000) &&
  515. !(value >= 433050000 && value <= 434790000) &&
  516. !(value >= 915000000 && value <= 928000000)) {
  517. } else {
  518. is_allowed = true;
  519. }
  520. break;
  521. case FuriHalVersionRegionJp:
  522. //312,00..315,25; 920,50..923,50
  523. if(!(value >= 312000000 && value <= 315250000) &&
  524. !(value >= 920500000 && value <= 923500000)) {
  525. } else {
  526. is_allowed = true;
  527. }
  528. break;
  529. default:
  530. is_allowed = true;
  531. break;
  532. }
  533. return is_allowed;
  534. }
  535. uint32_t furi_hal_subghz_set_frequency(uint32_t value) {
  536. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  537. if(furi_hal_subghz_is_tx_allowed(value)) {
  538. furi_hal_subghz_regulation = SubGhzRegulationTxRx;
  539. } else {
  540. furi_hal_subghz_regulation = SubGhzRegulationOnlyRx;
  541. }
  542. uint32_t real_frequency = cc1101_set_frequency(&furi_hal_spi_bus_handle_subghz, value);
  543. cc1101_calibrate(&furi_hal_spi_bus_handle_subghz);
  544. while(true) {
  545. CC1101Status status = cc1101_get_status(&furi_hal_spi_bus_handle_subghz);
  546. if(status.STATE == CC1101StateIDLE) break;
  547. }
  548. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  549. return real_frequency;
  550. }
  551. void furi_hal_subghz_set_path(FuriHalSubGhzPath path) {
  552. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_subghz);
  553. if(path == FuriHalSubGhzPath433) {
  554. hal_gpio_write(&gpio_rf_sw_0, 0);
  555. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
  556. } else if(path == FuriHalSubGhzPath315) {
  557. hal_gpio_write(&gpio_rf_sw_0, 1);
  558. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  559. } else if(path == FuriHalSubGhzPath868) {
  560. hal_gpio_write(&gpio_rf_sw_0, 1);
  561. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW | CC1101_IOCFG_INV);
  562. } else if(path == FuriHalSubGhzPathIsolate) {
  563. hal_gpio_write(&gpio_rf_sw_0, 0);
  564. cc1101_write_reg(&furi_hal_spi_bus_handle_subghz, CC1101_IOCFG2, CC1101IocfgHW);
  565. } else {
  566. furi_crash(NULL);
  567. }
  568. furi_hal_spi_release(&furi_hal_spi_bus_handle_subghz);
  569. }
  570. volatile uint32_t furi_hal_subghz_capture_delta_duration = 0;
  571. volatile FuriHalSubGhzCaptureCallback furi_hal_subghz_capture_callback = NULL;
  572. volatile void* furi_hal_subghz_capture_callback_context = NULL;
  573. static void furi_hal_subghz_capture_ISR() {
  574. // Channel 1
  575. if(LL_TIM_IsActiveFlag_CC1(TIM2)) {
  576. LL_TIM_ClearFlag_CC1(TIM2);
  577. furi_hal_subghz_capture_delta_duration = LL_TIM_IC_GetCaptureCH1(TIM2);
  578. if(furi_hal_subghz_capture_callback) {
  579. furi_hal_subghz_capture_callback(
  580. true,
  581. furi_hal_subghz_capture_delta_duration,
  582. (void*)furi_hal_subghz_capture_callback_context);
  583. }
  584. }
  585. // Channel 2
  586. if(LL_TIM_IsActiveFlag_CC2(TIM2)) {
  587. LL_TIM_ClearFlag_CC2(TIM2);
  588. if(furi_hal_subghz_capture_callback) {
  589. furi_hal_subghz_capture_callback(
  590. false,
  591. LL_TIM_IC_GetCaptureCH2(TIM2) - furi_hal_subghz_capture_delta_duration,
  592. (void*)furi_hal_subghz_capture_callback_context);
  593. }
  594. }
  595. }
  596. void furi_hal_subghz_start_async_rx(FuriHalSubGhzCaptureCallback callback, void* context) {
  597. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  598. furi_hal_subghz_state = SubGhzStateAsyncRx;
  599. furi_hal_subghz_capture_callback = callback;
  600. furi_hal_subghz_capture_callback_context = context;
  601. hal_gpio_init_ex(
  602. &gpio_cc1101_g0, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn1TIM2);
  603. // Timer: base
  604. FURI_CRITICAL_ENTER();
  605. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  606. FURI_CRITICAL_EXIT();
  607. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  608. TIM_InitStruct.Prescaler = 64 - 1;
  609. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  610. TIM_InitStruct.Autoreload = 0x7FFFFFFE;
  611. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV4;
  612. LL_TIM_Init(TIM2, &TIM_InitStruct);
  613. // Timer: advanced
  614. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  615. LL_TIM_DisableARRPreload(TIM2);
  616. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_TI2FP2);
  617. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_RESET);
  618. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  619. LL_TIM_EnableMasterSlaveMode(TIM2);
  620. LL_TIM_DisableDMAReq_TRIG(TIM2);
  621. LL_TIM_DisableIT_TRIG(TIM2);
  622. // Timer: channel 1 indirect
  623. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_INDIRECTTI);
  624. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  625. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
  626. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
  627. // Timer: channel 2 direct
  628. LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
  629. LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  630. LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
  631. LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV32_N8);
  632. // ISR setup
  633. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_subghz_capture_ISR);
  634. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  635. NVIC_EnableIRQ(TIM2_IRQn);
  636. // Interrupts and channels
  637. LL_TIM_EnableIT_CC1(TIM2);
  638. LL_TIM_EnableIT_CC2(TIM2);
  639. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
  640. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  641. // Enable NVIC
  642. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  643. NVIC_EnableIRQ(TIM2_IRQn);
  644. // Start timer
  645. LL_TIM_SetCounter(TIM2, 0);
  646. LL_TIM_EnableCounter(TIM2);
  647. // Switch to RX
  648. furi_hal_subghz_rx();
  649. }
  650. void furi_hal_subghz_stop_async_rx() {
  651. furi_assert(furi_hal_subghz_state == SubGhzStateAsyncRx);
  652. furi_hal_subghz_state = SubGhzStateIdle;
  653. // Shutdown radio
  654. furi_hal_subghz_idle();
  655. FURI_CRITICAL_ENTER();
  656. LL_TIM_DeInit(TIM2);
  657. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  658. FURI_CRITICAL_EXIT();
  659. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  660. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  661. }
  662. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL (256)
  663. #define API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF (API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL / 2)
  664. #define API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME 333
  665. typedef struct {
  666. uint32_t* buffer;
  667. bool flip_flop;
  668. FuriHalSubGhzAsyncTxCallback callback;
  669. void* callback_context;
  670. uint64_t duty_high;
  671. uint64_t duty_low;
  672. } FuriHalSubGhzAsyncTx;
  673. static FuriHalSubGhzAsyncTx furi_hal_subghz_async_tx = {0};
  674. static void furi_hal_subghz_async_tx_refill(uint32_t* buffer, size_t samples) {
  675. while(samples > 0) {
  676. bool is_odd = samples % 2;
  677. LevelDuration ld =
  678. furi_hal_subghz_async_tx.callback(furi_hal_subghz_async_tx.callback_context);
  679. if(level_duration_is_wait(ld)) {
  680. return;
  681. } else if(level_duration_is_reset(ld)) {
  682. // One more even sample required to end at low level
  683. if(is_odd) {
  684. *buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  685. buffer++;
  686. samples--;
  687. furi_hal_subghz_async_tx.duty_low += API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  688. }
  689. break;
  690. } else {
  691. // Inject guard time if level is incorrect
  692. bool level = level_duration_get_level(ld);
  693. if(is_odd == level) {
  694. *buffer = API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  695. buffer++;
  696. samples--;
  697. if (!level) {
  698. furi_hal_subghz_async_tx.duty_high += API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  699. } else {
  700. furi_hal_subghz_async_tx.duty_low += API_HAL_SUBGHZ_ASYNC_TX_GUARD_TIME;
  701. }
  702. }
  703. uint32_t duration = level_duration_get_duration(ld);
  704. furi_assert(duration > 0);
  705. *buffer = duration;
  706. buffer++;
  707. samples--;
  708. if (level) {
  709. furi_hal_subghz_async_tx.duty_high += duration;
  710. } else {
  711. furi_hal_subghz_async_tx.duty_low += duration;
  712. }
  713. }
  714. }
  715. memset(buffer, 0, samples * sizeof(uint32_t));
  716. }
  717. static void furi_hal_subghz_async_tx_dma_isr() {
  718. furi_assert(furi_hal_subghz_state == SubGhzStateAsyncTx);
  719. if(LL_DMA_IsActiveFlag_HT1(DMA1)) {
  720. LL_DMA_ClearFlag_HT1(DMA1);
  721. furi_hal_subghz_async_tx_refill(
  722. furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
  723. }
  724. if(LL_DMA_IsActiveFlag_TC1(DMA1)) {
  725. LL_DMA_ClearFlag_TC1(DMA1);
  726. furi_hal_subghz_async_tx_refill(
  727. furi_hal_subghz_async_tx.buffer + API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF,
  728. API_HAL_SUBGHZ_ASYNC_TX_BUFFER_HALF);
  729. }
  730. }
  731. static void furi_hal_subghz_async_tx_timer_isr() {
  732. if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) {
  733. LL_TIM_ClearFlag_UPDATE(TIM2);
  734. if(LL_TIM_GetAutoReload(TIM2) == 0) {
  735. if(furi_hal_subghz_state == SubGhzStateAsyncTx) {
  736. furi_hal_subghz_state = SubGhzStateAsyncTxLast;
  737. //forcibly pulls the pin to the ground so that there is no carrier
  738. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullDown, GpioSpeedLow);
  739. } else {
  740. furi_hal_subghz_state = SubGhzStateAsyncTxEnd;
  741. LL_TIM_DisableCounter(TIM2);
  742. }
  743. }
  744. }
  745. }
  746. bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void* context) {
  747. furi_assert(furi_hal_subghz_state == SubGhzStateIdle);
  748. furi_assert(callback);
  749. //If transmission is prohibited by regional settings
  750. if(furi_hal_subghz_regulation != SubGhzRegulationTxRx) return false;
  751. furi_hal_subghz_async_tx.callback = callback;
  752. furi_hal_subghz_async_tx.callback_context = context;
  753. furi_hal_subghz_state = SubGhzStateAsyncTx;
  754. furi_hal_subghz_async_tx.duty_low = 0;
  755. furi_hal_subghz_async_tx.duty_high = 0;
  756. furi_hal_subghz_async_tx.buffer =
  757. furi_alloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
  758. furi_hal_subghz_async_tx_refill(
  759. furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL);
  760. // Connect CC1101_GD0 to TIM2 as output
  761. hal_gpio_init_ex(
  762. &gpio_cc1101_g0, GpioModeAltFunctionPushPull, GpioPullDown, GpioSpeedLow, GpioAltFn1TIM2);
  763. // Configure DMA
  764. LL_DMA_InitTypeDef dma_config = {0};
  765. dma_config.PeriphOrM2MSrcAddress = (uint32_t) & (TIM2->ARR);
  766. dma_config.MemoryOrM2MDstAddress = (uint32_t)furi_hal_subghz_async_tx.buffer;
  767. dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
  768. dma_config.Mode = LL_DMA_MODE_CIRCULAR;
  769. dma_config.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  770. dma_config.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
  771. dma_config.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
  772. dma_config.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
  773. dma_config.NbData = API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL;
  774. dma_config.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP;
  775. dma_config.Priority = LL_DMA_MODE_NORMAL;
  776. LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &dma_config);
  777. furi_hal_interrupt_set_dma_channel_isr(
  778. DMA1, LL_DMA_CHANNEL_1, furi_hal_subghz_async_tx_dma_isr);
  779. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  780. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_1);
  781. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  782. // Configure TIM2
  783. FURI_CRITICAL_ENTER();
  784. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  785. FURI_CRITICAL_EXIT();
  786. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  787. TIM_InitStruct.Prescaler = 64 - 1;
  788. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  789. TIM_InitStruct.Autoreload = 1000;
  790. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  791. LL_TIM_Init(TIM2, &TIM_InitStruct);
  792. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  793. LL_TIM_EnableARRPreload(TIM2);
  794. // Configure TIM2 CH2
  795. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  796. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_TOGGLE;
  797. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  798. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  799. TIM_OC_InitStruct.CompareValue = 0;
  800. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  801. LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
  802. LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2);
  803. LL_TIM_DisableMasterSlaveMode(TIM2);
  804. furi_hal_interrupt_set_timer_isr(TIM2, furi_hal_subghz_async_tx_timer_isr);
  805. LL_TIM_EnableIT_UPDATE(TIM2);
  806. LL_TIM_EnableDMAReq_UPDATE(TIM2);
  807. LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH2);
  808. // Start counter
  809. LL_TIM_GenerateEvent_UPDATE(TIM2);
  810. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  811. hal_gpio_write(&FURI_HAL_SUBGHZ_TX_GPIO, true);
  812. #endif
  813. furi_hal_subghz_tx();
  814. // Enable NVIC
  815. NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  816. NVIC_EnableIRQ(TIM2_IRQn);
  817. LL_TIM_SetCounter(TIM2, 0);
  818. LL_TIM_EnableCounter(TIM2);
  819. return true;
  820. }
  821. bool furi_hal_subghz_is_async_tx_complete() {
  822. return furi_hal_subghz_state == SubGhzStateAsyncTxEnd;
  823. }
  824. void furi_hal_subghz_stop_async_tx() {
  825. furi_assert(
  826. furi_hal_subghz_state == SubGhzStateAsyncTx ||
  827. furi_hal_subghz_state == SubGhzStateAsyncTxLast ||
  828. furi_hal_subghz_state == SubGhzStateAsyncTxEnd);
  829. // Shutdown radio
  830. furi_hal_subghz_idle();
  831. #ifdef FURI_HAL_SUBGHZ_TX_GPIO
  832. hal_gpio_write(&FURI_HAL_SUBGHZ_TX_GPIO, false);
  833. #endif
  834. // Deinitialize Timer
  835. FURI_CRITICAL_ENTER();
  836. LL_TIM_DeInit(TIM2);
  837. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2);
  838. furi_hal_interrupt_set_timer_isr(TIM2, NULL);
  839. // Deinitialize DMA
  840. LL_DMA_DeInit(DMA1, LL_DMA_CHANNEL_1);
  841. furi_hal_interrupt_set_dma_channel_isr(DMA1, LL_DMA_CHANNEL_1, NULL);
  842. // Deinitialize GPIO
  843. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  844. FURI_CRITICAL_EXIT();
  845. free(furi_hal_subghz_async_tx.buffer);
  846. float duty_cycle = 100.0f * (float)furi_hal_subghz_async_tx.duty_high / ((float)furi_hal_subghz_async_tx.duty_low + (float)furi_hal_subghz_async_tx.duty_high);
  847. FURI_LOG_D(TAG, "Async TX Radio stats: on %0.0fus, off %0.0fus, DutyCycle: %0.0f%%", (float)furi_hal_subghz_async_tx.duty_high, (float)furi_hal_subghz_async_tx.duty_low, duty_cycle);
  848. furi_hal_subghz_state = SubGhzStateIdle;
  849. }