lmx2594.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. #include "lmx2594.h"
  2. #include <math.h>
  3. const uint32_t lmx2594_rst[] = {
  4. 0x002516,
  5. 0x002514
  6. };
  7. uint32_t lmx2594regs[LMX_COUNT] = {
  8. 0x700000,
  9. 0x6F0000,
  10. 0x6E0000,
  11. 0x6D0000,
  12. 0x6C0000,
  13. 0x6B0000,
  14. 0x6A0000,
  15. 0x690021,
  16. 0x680000,
  17. 0x670000,
  18. 0x660000,
  19. 0x650011,
  20. 0x640000,
  21. 0x630000,
  22. 0x620000,
  23. 0x610888,
  24. 0x600000,
  25. 0x5F0000,
  26. 0x5E0000,
  27. 0x5D0000,
  28. 0x5C0000,
  29. 0x5B0000,
  30. 0x5A0000,
  31. 0x590000,
  32. 0x580000,
  33. 0x570000,
  34. 0x560000,
  35. 0x550000,
  36. 0x540000,
  37. 0x530000,
  38. 0x520000,
  39. 0x510000,
  40. 0x500000,
  41. 0x4F0000,
  42. 0x4E016F,
  43. 0x4D0000,
  44. 0x4C000C,
  45. 0x4B0800,
  46. 0x4A0000,
  47. 0x49003F,
  48. 0x480001,
  49. 0x470081,
  50. 0x46C350,
  51. 0x450000,
  52. 0x4403E8,
  53. 0x430000,
  54. 0x4201F4,
  55. 0x410000,
  56. 0x401388,
  57. 0x3F0000,
  58. 0x3E0322,
  59. 0x3D00A8,
  60. 0x3C03E8,
  61. 0x3B0001,
  62. 0x3A9001,
  63. 0x390020,
  64. 0x380000,
  65. 0x370000,
  66. 0x360000,
  67. 0x350000,
  68. 0x340820,
  69. 0x330080,
  70. 0x320000,
  71. 0x314180,
  72. 0x300300,
  73. 0x2F0300,
  74. 0x2E07FC,
  75. 0x2DC8DF,
  76. 0x2C1FA0,
  77. 0x2B0000,
  78. 0x2A0000,
  79. 0x290000,
  80. 0x280000,
  81. 0x2703E8,
  82. 0x260000,
  83. 0x250104,
  84. 0x240032,
  85. 0x230004,
  86. 0x220000,
  87. 0x211E21,
  88. 0x200393,
  89. 0x1F43EC,
  90. 0x1E318C,
  91. 0x1D318C,
  92. 0x1C0488,
  93. 0x1B0002,
  94. 0x1A0DB0,
  95. 0x190C2B,
  96. 0x18071A,
  97. 0x17007C,
  98. 0x160001,
  99. 0x150401,
  100. 0x14F848,
  101. 0x1327B7,
  102. 0x120064,
  103. 0x11012C,
  104. 0x100080,
  105. 0x0F064F,
  106. 0x0E1E40,
  107. 0x0D4000,
  108. 0x0C5001,
  109. 0x0B0018,
  110. 0x0A10D8,
  111. 0x090604,
  112. 0x082000,
  113. 0x0740B2,
  114. 0x06C802,
  115. 0x0500C8,
  116. 0x041243,
  117. 0x030642,
  118. 0x020500,
  119. 0x010808,
  120. 0x00251C
  121. };
  122. void auto_cal(void *bar1) {
  123. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  124. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | LMX2594_R0_FCAL_EN;
  125. uint32_t *ptr_header = bar1+LMX_BASE_ADDR;
  126. *ptr_header = ((0 << 23) | (DeviceIdLmx2594 << 18) | (0x1 << 1) | 1);
  127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  128. *ptr = lmx2594regs[112-FCAL_ADDR];
  129. }
  130. void lmx2594_init(void *bar1) {
  131. // Header for LMX Reset
  132. uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
  133. *ptr_rst = LMX2594_RST_HEADER;
  134. // Reset Data
  135. for (int m = 0; m < (sizeof(lmx2594_rst))/4; m++) {
  136. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  137. *ptr = lmx2594_rst[m];
  138. }
  139. // Header for init data
  140. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  141. *ptr = InitLMX2594Header;
  142. // Init data
  143. for (int i = 0; i < LMX_COUNT; i++) {
  144. *ptr = lmx2594regs[i];
  145. }
  146. usleep(10);
  147. auto_cal(bar1);
  148. }
  149. /*-------------------------LMX2594 Frequency Set-------------------------*/
  150. int lmx_freq_set_main_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  151. double N_div;
  152. printf("f_pd before = %f\n",f_pd);
  153. N_div = lmx_freq / f_pd;
  154. uint32_t N = (uint32_t) N_div;
  155. if (lmx_freq <= 12500e6) {
  156. if (N < 28){
  157. N= 28;
  158. }
  159. }
  160. else if (lmx_freq > 12500e6) {
  161. if (N <32) {
  162. N = 32;
  163. }
  164. };
  165. int vco_core;
  166. double f_coremin;
  167. double f_coremax;
  168. int c_core_min;
  169. int c_core_max;
  170. int a_core_min;
  171. int a_core_max;
  172. uint16_t vco_cap_ctrl_strt;
  173. uint16_t vco_daciset_strt;
  174. // Partial assist for the calibration
  175. //Determine a VCO core and other parameters
  176. if (lmx_freq >= 7500e6 && lmx_freq <= 8600e6) {
  177. vco_core = 1;
  178. f_coremin = 7500e6;
  179. f_coremax = 8600e6;
  180. c_core_min = 164;
  181. c_core_max = 12;
  182. a_core_min = 299;
  183. a_core_max = 240;
  184. }
  185. else if (lmx_freq > 8600e6 && lmx_freq < 9800e6) {
  186. vco_core = 2;
  187. f_coremin = 8600e6;
  188. f_coremax = 9800e6;
  189. c_core_min = 165;
  190. c_core_max = 16;
  191. a_core_min = 356;
  192. a_core_max = 247;
  193. }
  194. else if (lmx_freq >= 9800e6 && lmx_freq <= 10800e6) {
  195. vco_core = 3;
  196. f_coremin = 9800e6;
  197. f_coremax = 10800e6;
  198. c_core_min = 158;
  199. c_core_max = 19;
  200. a_core_min = 324;
  201. a_core_max = 224;
  202. }
  203. else if (lmx_freq > 10800e6 && lmx_freq <= 12000e6) {
  204. vco_core = 4;
  205. f_coremin = 10800e6;
  206. f_coremax = 12000e6;
  207. c_core_min = 140;
  208. c_core_max = 0;
  209. a_core_min = 383;
  210. a_core_max = 244;
  211. }
  212. else if (lmx_freq > 12000e6 && lmx_freq <= 12900e6) {
  213. vco_core = 5;
  214. f_coremin = 12000e6;
  215. f_coremax = 12900e6;
  216. c_core_min = 183;
  217. c_core_max = 36;
  218. a_core_min = 205;
  219. a_core_max = 146;
  220. }
  221. else if (lmx_freq > 12900e6 && lmx_freq <= 13900e6) {
  222. vco_core = 6;
  223. f_coremin = 12900e6;
  224. f_coremax = 13900e6;
  225. c_core_min = 155;
  226. c_core_max = 6;
  227. a_core_min = 242;
  228. a_core_max = 163;
  229. }
  230. else if (lmx_freq > 13900e6 && lmx_freq <= 15000e6) {
  231. vco_core = 7;
  232. f_coremin = 13900e6;
  233. f_coremax = 15000e6;
  234. c_core_min = 175;
  235. c_core_max = 19;
  236. a_core_min = 323;
  237. a_core_max = 244;
  238. };
  239. if (lmx_freq >=11900e6 && lmx_freq <=12100e6) {
  240. vco_daciset_strt = 300;
  241. vco_core = 4;
  242. vco_cap_ctrl_strt = 1;
  243. }
  244. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (lmx_freq - f_coremin) / (f_coremax - f_coremin));
  245. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (lmx_freq - f_coremin) / (f_coremax - f_coremin));
  246. //Set the VCO_CORE
  247. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  248. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  249. // Set the VCO_CAP_CTRL
  250. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  251. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  252. // Set the VCO_DACISET
  253. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  254. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  255. // Set the PF_DLY_SEL
  256. if (lmx_freq <= 12500e6) {
  257. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  258. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  259. }
  260. else if (lmx_freq > 12500e6) {
  261. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  262. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  263. };
  264. int cal_clk_div;
  265. //SET the FCAL_HPFD_ADJ
  266. if (f_pd <= 100e6) {
  267. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  268. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  269. }
  270. else if (f_pd > 100e6 && f_pd <= 150e6) {
  271. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  272. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  273. }
  274. else if (f_pd > 150e6 && f_pd <= 200e6) {
  275. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  276. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  277. }
  278. else if (f_pd > 200e6) {
  279. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  280. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  281. }
  282. // SET the CAL_CLK_DIV value
  283. if (f_pd <= 200e6 ) {
  284. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  285. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  286. cal_clk_div = 0;
  287. }
  288. else if (f_pd > 200e6 && f_pd <= 400e6) {
  289. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  290. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  291. cal_clk_div = 1;
  292. }
  293. else if (f_pd > 400e6 && f_pd < 800e6) {
  294. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  295. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  296. cal_clk_div = 2;
  297. };
  298. //Calculate the ACAL_CMP_DLY
  299. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  300. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
  301. //Set the ACAL_CMP_DLY value
  302. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  303. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  304. // SET the N_DIV
  305. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  306. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  307. //CLear the lower 16 bits of the register
  308. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  309. // Next 16 bits of the register
  310. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  311. // Clear the SEG1_EN bit
  312. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  313. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  314. // Removed unnecessary commented-out LMX_HEADER definition
  315. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  316. // Program the FCAL_EN bit
  317. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  318. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  319. uint32_t lmx_change_freq_regs[] = {
  320. lmx2594regs[112 - VCO_SEL],
  321. lmx2594regs[112 - CAP_CTRL_START],
  322. lmx2594regs[112 - VCO_DACISET],
  323. lmx2594regs[112-PFD_DLY_SEL],
  324. lmx2594regs[112-R4_ADDR],
  325. lmx2594regs[112-R1_ADDR],
  326. lmx2594regs[112-CHDIV_DIV2],
  327. lmx2594regs[112-PLL_N_S],
  328. lmx2594regs[112-PLL_N_M],
  329. lmx2594regs[112 - OUTA_MUX],
  330. lmx2594regs[112-FCAL_ADDR]
  331. };
  332. // Create a header for the LMX2594 with the appropriate number of words
  333. // uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  334. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  335. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  336. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  337. *ptr = LMX_HEADER;
  338. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  339. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  340. *data_ptr = lmx_change_freq_regs[i];
  341. }
  342. return 0;
  343. }
  344. int lmx_freq_set_out_of_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  345. double f_vco = 2 * lmx_freq;
  346. int chan_div = 2;
  347. uint8_t ch_div_reg = 0; // 2
  348. double vco_div = 7.5e9 / lmx_freq;
  349. int vco_core;
  350. double f_coremin;
  351. double f_coremax;
  352. int c_core_min;
  353. int c_core_max;
  354. int a_core_min;
  355. int a_core_max;
  356. uint16_t vco_cap_ctrl_strt;
  357. uint16_t vco_daciset_strt;
  358. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  359. if (f_vco < 7.5e9) {
  360. if (vco_div > 2 && vco_div <= 4) {
  361. chan_div = 4; // 4
  362. f_vco = lmx_freq * chan_div;
  363. }
  364. else if (vco_div > 4 && vco_div <= 6) {
  365. chan_div = 6; // 6
  366. f_vco = lmx_freq * chan_div;
  367. }
  368. else if (vco_div > 6 && vco_div <= 8) {
  369. chan_div = 8; // 8
  370. f_vco = lmx_freq * chan_div;
  371. }
  372. else if (vco_div > 8 && vco_div <= 12) {
  373. chan_div = 12; // 12
  374. f_vco = lmx_freq * chan_div;
  375. }
  376. else if (vco_div > 12 && vco_div <= 16) {
  377. chan_div = 16; // 16
  378. f_vco = lmx_freq * chan_div;
  379. }
  380. else if (vco_div > 16 && vco_div <= 24) {
  381. chan_div = 24; // 24
  382. f_vco = lmx_freq * chan_div;
  383. }
  384. else if (vco_div > 24 && vco_div <= 32) {
  385. chan_div = 32; // 32
  386. f_vco = lmx_freq * chan_div;
  387. }
  388. else if (vco_div > 32 && vco_div <= 48) {
  389. chan_div = 48; // 48
  390. f_vco = lmx_freq * chan_div;
  391. }
  392. else if (vco_div > 48 && vco_div <= 64) {
  393. chan_div = 64; // 64
  394. f_vco = lmx_freq * chan_div;
  395. }
  396. else if (vco_div > 64 && vco_div <= 72) {
  397. chan_div = 72; // 72
  398. f_vco = lmx_freq * chan_div;
  399. }
  400. else if (vco_div > 72 && vco_div <= 96) {
  401. chan_div = 96; // 96
  402. f_vco = lmx_freq * chan_div;
  403. }
  404. else if (vco_div > 96 && vco_div <= 128) {
  405. chan_div = 128; // 128
  406. f_vco = lmx_freq * chan_div;
  407. }
  408. else if (vco_div > 128 && vco_div <= 192) {
  409. chan_div = 192; // 192
  410. f_vco = lmx_freq * chan_div;
  411. }
  412. else if (vco_div > 192 && vco_div <= 256) {
  413. chan_div = 256; // 256
  414. f_vco = lmx_freq * chan_div;
  415. }
  416. else if (vco_div > 256 && vco_div <= 384) {
  417. chan_div = 384; // 384
  418. f_vco = lmx_freq * chan_div;
  419. }
  420. else if (vco_div > 384 && vco_div <= 512) {
  421. chan_div = 512; // 512
  422. f_vco = lmx_freq * chan_div;
  423. }
  424. else if (vco_div > 512 && vco_div <= 768) {
  425. chan_div = 768; // 768
  426. f_vco = lmx_freq * chan_div;
  427. }
  428. switch (chan_div) {
  429. case 2:
  430. ch_div_reg = 0;
  431. break;
  432. case 4:
  433. ch_div_reg = 1;
  434. break;
  435. case 6:
  436. ch_div_reg = 2;
  437. break;
  438. case 8:
  439. ch_div_reg = 3;
  440. break;
  441. case 12:
  442. ch_div_reg = 4;
  443. break;
  444. case 16:
  445. ch_div_reg = 5;
  446. break;
  447. case 24:
  448. ch_div_reg = 6;
  449. break;
  450. case 32:
  451. ch_div_reg = 7;
  452. break;
  453. case 48:
  454. ch_div_reg = 8;
  455. break;
  456. case 64:
  457. ch_div_reg = 9;
  458. break;
  459. case 72:
  460. ch_div_reg = 10;
  461. break;
  462. case 96:
  463. ch_div_reg = 11;
  464. break;
  465. case 128:
  466. ch_div_reg = 12;
  467. break;
  468. case 192:
  469. ch_div_reg = 13;
  470. break;
  471. case 256:
  472. ch_div_reg = 14;
  473. break;
  474. case 384:
  475. ch_div_reg = 15;
  476. break;
  477. case 512:
  478. ch_div_reg = 16;
  479. break;
  480. case 768:
  481. ch_div_reg = 17;
  482. break;
  483. }
  484. }
  485. else {
  486. ch_div_reg = 0;
  487. f_vco = lmx_freq * 2;
  488. }
  489. double N_div = f_vco / f_pd;
  490. uint32_t N = (uint32_t) N_div;
  491. if (f_vco <= 12500e6) {
  492. if (N < 28){
  493. N= 28;
  494. };
  495. }
  496. else if (f_vco > 12500e6) {
  497. if (N <32) {
  498. N = 32;
  499. }
  500. };
  501. // Partial assist for the calibration
  502. //Determine a VCO core and other parameters
  503. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  504. vco_core = 1;
  505. f_coremin = 7500e6;
  506. f_coremax = 8600e6;
  507. c_core_min = 164;
  508. c_core_max = 12;
  509. a_core_min = 299;
  510. a_core_max = 240;
  511. }
  512. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  513. vco_core = 2;
  514. f_coremin = 8600e6;
  515. f_coremax = 9800e6;
  516. c_core_min = 165;
  517. c_core_max = 16;
  518. a_core_min = 356;
  519. a_core_max = 247;
  520. }
  521. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  522. vco_core = 3;
  523. f_coremin = 9800e6;
  524. f_coremax = 10800e6;
  525. c_core_min = 158;
  526. c_core_max = 19;
  527. a_core_min = 324;
  528. a_core_max = 224;
  529. }
  530. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  531. vco_core = 4;
  532. f_coremin = 10800e6;
  533. f_coremax = 12000e6;
  534. c_core_min = 140;
  535. c_core_max = 0;
  536. a_core_min = 383;
  537. a_core_max = 244;
  538. }
  539. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  540. vco_core = 5;
  541. f_coremin = 12000e6;
  542. f_coremax = 12900e6;
  543. c_core_min = 183;
  544. c_core_max = 36;
  545. a_core_min = 205;
  546. a_core_max = 146;
  547. }
  548. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  549. vco_core = 6;
  550. f_coremin = 12900e6;
  551. f_coremax = 13900e6;
  552. c_core_min = 155;
  553. c_core_max = 6;
  554. a_core_min = 242;
  555. a_core_max = 163;
  556. }
  557. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  558. vco_core = 7;
  559. f_coremin = 13900e6;
  560. f_coremax = 15000e6;
  561. c_core_min = 175;
  562. c_core_max = 19;
  563. a_core_min = 323;
  564. a_core_max = 244;
  565. };
  566. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  567. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  568. if (f_vco >=11900e6 && f_vco <=12100e6) {
  569. vco_daciset_strt = 300;
  570. vco_core = 4;
  571. vco_cap_ctrl_strt = 1;
  572. }
  573. // Calibration assist
  574. //Set the VCO_CORE
  575. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  576. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  577. // Set the VCO_CAP_CTRL_START
  578. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  579. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  580. // Set the VCO_DACISET
  581. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  582. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  583. // Set the PFD_DLY_SEL to appropriate value
  584. if (f_vco <= 12500e6) {
  585. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  586. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  587. // printf("PFD_DLY_SEL = %d\n", 1);
  588. }
  589. else if (f_vco > 12500e6) {
  590. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  591. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  592. // printf("PFD_DLY_SEL = %d\n", 2);
  593. }
  594. if (f_pd <= 100e6) {
  595. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  596. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  597. }
  598. else if (f_pd > 100e6 && f_pd <= 150e6) {
  599. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  600. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  601. }
  602. else if (f_pd > 150e6 && f_pd <= 200e6) {
  603. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  604. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  605. }
  606. else if (f_pd > 200e6) {
  607. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  608. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  609. };
  610. // SET the CAL_CLK_DIV value
  611. int cal_clk_div;
  612. if (f_pd <= 200e6 ) {
  613. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  614. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  615. cal_clk_div =0;
  616. }
  617. else if (f_pd > 200e6 && f_pd <= 400e6) {
  618. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  619. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  620. cal_clk_div =1;
  621. }
  622. else if (f_pd > 400e6 && f_pd < 800e6) {
  623. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  624. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  625. cal_clk_div = 2;
  626. };
  627. //Calculate the ACAL_CMP_DLY
  628. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  629. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));;
  630. //Set the ACAL_CMP_DLY value
  631. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  632. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  633. // Set the N value
  634. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  635. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  636. //CLear the lower 16 bits of the register
  637. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  638. // Next 16 bits of the register
  639. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  640. // Program the CHDIV value
  641. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  642. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  643. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  644. // If the ch_div > 2 then set the SEG1_EN bit
  645. if (chan_div > 2) {
  646. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  647. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  648. }
  649. else {
  650. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  651. }
  652. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  653. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  654. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  655. // Program the FCAL_EN bit
  656. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  657. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  658. uint32_t lmx_change_freq_regs []={
  659. lmx2594regs[112 - VCO_SEL],
  660. lmx2594regs[112 - CAP_CTRL_START],
  661. lmx2594regs[112 - VCO_DACISET],
  662. lmx2594regs[112-PFD_DLY_SEL],
  663. lmx2594regs[112-R4_ADDR],
  664. lmx2594regs[112-R1_ADDR],
  665. lmx2594regs[112 - PLL_N_S],
  666. lmx2594regs[112 - PLL_N_M],
  667. lmx2594regs[112 - CHDIV],
  668. lmx2594regs[112 - CHDIV_DIV2],
  669. lmx2594regs[112 - OUTA_MUX],
  670. lmx2594regs[112 - FCAL_ADDR]
  671. };
  672. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  673. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  674. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  675. *ptr = LMX_HEADER;
  676. // Send the data
  677. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  678. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  679. *data_ptr = lmx_change_freq_regs[i];
  680. }
  681. // char filename[100];
  682. // sprintf(filename, "%f.txt", lmx_freq);
  683. // FILE * f = fopen(filename, "w");
  684. // for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  685. // fprintf(f, "0x%08X\n", lmx2594regs[i]);
  686. // }
  687. // fclose(f);
  688. // printf("N_div = %f\n", N_div);
  689. // printf("f_vco = %f\n", f_vco);
  690. // printf("N = %d\n", N);
  691. // printf("chan_div = %d\n", chan_div);
  692. // printf("chan_div_reg = %d\n", ch_div_reg);
  693. return 0;
  694. }
  695. double lmx_lower_bond_set (double freq, double f_pd) {
  696. double f_max2870 = 4e9;
  697. double lmx_req_freq = f_max2870-freq; // 4 GHz - freq
  698. return lmx_req_freq;
  699. }
  700. double lmx_get_freq(double freq) {
  701. if (freq >= 100e3 && freq <= 1000e6) {
  702. double f_max2870 = 4e9;
  703. double lmx_freq = f_max2870-freq; // 4 GHz - freq
  704. return lmx_freq;
  705. }
  706. else if (freq > 1000e6 && freq <= 15e9) {
  707. return freq;
  708. }
  709. else if (freq > 15e9 && freq <=27e9) {
  710. return freq/2;
  711. }
  712. else if (freq > 27e9 && freq <= 45e9) {
  713. return freq/4;
  714. }
  715. }
  716. int lmx_freq_set(void *bar1, double freq,double f_pd) {
  717. // Set the 4 Mosi mode
  718. usleep(1);
  719. uint32_t cfg_reg = get_cfg_reg();
  720. SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
  721. uint32_t *spi_mode = bar1 +CFG_REG_ADDR;
  722. *spi_mode = cfg_reg;
  723. usleep(1);
  724. *spi_mode = SET_REGISTER_PARAM(cfg_reg,CFG_REG_RST_FOR_FPGA_BITM, CFG_REG_RST_FOR_FPGA_BITP,CFG_REG_RST_FOR_FPGA_ON);
  725. usleep(1);
  726. *spi_mode = SET_REGISTER_PARAM(cfg_reg,CFG_REG_RST_FOR_FPGA_BITM, CFG_REG_RST_FOR_FPGA_BITP,CFG_REG_RST_FOR_FPGA_OFF);
  727. if (freq < 100e3 || freq > 45e9) {
  728. printf("Frequency range is 100 kHz to 45 GHz\n");
  729. return -1;
  730. }
  731. double lmx_freq = lmx_get_freq(freq);
  732. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  733. if (lmx_freq >= 7.5e9 && lmx_freq <= 15e9) {
  734. // lmx_freq_set_main_band(bar1, freq, f_pd);
  735. lmx_freq_set_main_band_int_mode(bar1, lmx_freq, f_pd);
  736. }
  737. else if (lmx_freq < 7.5e9) {
  738. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  739. lmx_freq_set_out_of_band_int_mode(bar1, lmx_freq, f_pd);
  740. }
  741. // Return the 1 MOSI mode
  742. usleep(1);
  743. *spi_mode = SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_1MOSI);
  744. // Switch the keys
  745. usleep(1);
  746. key_switch(bar1, freq,lmx_freq);
  747. return 0;
  748. }
  749. uint32_t lmx_ld_status(void *bar1) {
  750. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  751. uint32_t read_value = *read_ptr;
  752. return read_value;
  753. }