furi-hal-subghz.c 37 KB

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