lmx2594.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #include "lmx2594.h"
  2. const uint32_t lmx2594_rst[2] = {
  3. 0x00251e,
  4. 0x00251c
  5. };
  6. uint32_t lmx2594regs[LMX_COUNT] = {
  7. 0x700000,
  8. 0x6F0000,
  9. 0x6E0000,
  10. 0x6D0000,
  11. 0x6C0000,
  12. 0x6B0000,
  13. 0x6A0000,
  14. 0x690021,
  15. 0x680000,
  16. 0x670000,
  17. 0x660000,
  18. 0x650011,
  19. 0x640000,
  20. 0x630000,
  21. 0x620000,
  22. 0x610888,
  23. 0x600000,
  24. 0x5F0000,
  25. 0x5E0000,
  26. 0x5D0000,
  27. 0x5C0000,
  28. 0x5B0000,
  29. 0x5A0000,
  30. 0x590000,
  31. 0x580000,
  32. 0x570000,
  33. 0x560000,
  34. 0x550000,
  35. 0x540000,
  36. 0x530000,
  37. 0x520000,
  38. 0x510000,
  39. 0x500000,
  40. 0x4F0000,
  41. 0x4E0105,
  42. 0x4D0000,
  43. 0x4C000C,
  44. 0x4B0C40,
  45. 0x4A0000,
  46. 0x49003F,
  47. 0x480001,
  48. 0x470081,
  49. 0x46C350,
  50. 0x450000,
  51. 0x4403E8,
  52. 0x430000,
  53. 0x4201F4,
  54. 0x410000,
  55. 0x401388,
  56. 0x3F0000,
  57. 0x3E0322,
  58. 0x3D00A8,
  59. 0x3C03E8,
  60. 0x3B0001,
  61. 0x3A9001,
  62. 0x390020,
  63. 0x380000,
  64. 0x370000,
  65. 0x360000,
  66. 0x350000,
  67. 0x340820,
  68. 0x330080,
  69. 0x320000,
  70. 0x314180,
  71. 0x300300,
  72. 0x2F0300,
  73. 0x2E07FD,
  74. 0x2DC8DF,
  75. 0x2C1F20,
  76. 0x2B0000,
  77. 0x2A0000,
  78. 0x290000,
  79. 0x280000,
  80. 0x2703E8,
  81. 0x260000,
  82. 0x250104,
  83. 0x240032,
  84. 0x230004,
  85. 0x220000,
  86. 0x211E21,
  87. 0x200393,
  88. 0x1F43EC,
  89. 0x1E318C,
  90. 0x1D318C,
  91. 0x1C0488,
  92. 0x1B0002,
  93. 0x1A0DB0,
  94. 0x190C2B,
  95. 0x18071A,
  96. 0x17007C,
  97. 0x160001,
  98. 0x150401,
  99. 0x14D848,
  100. 0x1327B7,
  101. 0x120064,
  102. 0x11012C,
  103. 0x100080,
  104. 0x0F064F,
  105. 0x0E1E70,
  106. 0x0D4000,
  107. 0x0C5001,
  108. 0x0B0018,
  109. 0x0A10D8,
  110. 0x090604,
  111. 0x082000,
  112. 0x0740B2,
  113. 0x06C802,
  114. 0x0500C8,
  115. 0x041443,
  116. 0x030642,
  117. 0x020500,
  118. 0x01080B,
  119. 0x00251C
  120. };
  121. void lmx2594_init(void *bar1) {
  122. // Header for LMX Reset
  123. uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
  124. *ptr_rst = LMX2594_RST_HEADER;
  125. // Reset Data
  126. for (int m = 0; m < 2; m++) {
  127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  128. *ptr = lmx2594_rst[m];
  129. }
  130. // Header for init data
  131. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  132. *ptr = InitLMX2594Header;
  133. // Init data
  134. for (int i = 0; i < LMX_COUNT; i++) {
  135. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  136. *ptr = lmx2594regs[i];
  137. }
  138. }
  139. /*-------------------------LMX2594 Frequency Set-------------------------*/
  140. int lmx_freq_set_main_band(void *bar1, double freq, double f_pd) {
  141. double N_div;
  142. N_div = freq / f_pd;
  143. int vco_core;
  144. double f_coremin;
  145. double f_coremax;
  146. int c_core_min;
  147. int c_core_max;
  148. int a_core_min;
  149. int a_core_max;
  150. uint16_t vco_cap_ctrl_strt;
  151. uint16_t vco_daciset_strt;
  152. // divide whole part and fractional part
  153. uint32_t N = (uint32_t) N_div;
  154. // In frac part there is separate denominator and numerator
  155. // If frac part is 0 then the denominator is 1000 and numerator is 0
  156. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  157. uint32_t frac_d = 524287;
  158. // If frac part is 0 then the denominator is 1000 and numerator is 0
  159. if (frac_n == 0) {
  160. frac_n = 0;
  161. frac_d = 524287;
  162. }
  163. // Partial assist for the calibration
  164. //Determine a VCO core and other parameters
  165. if (freq >= 7500e6 && freq <= 8600e6) {
  166. vco_core = 1;
  167. f_coremin = 7500e6;
  168. f_coremax = 8600e6;
  169. c_core_min = 164;
  170. c_core_max = 12;
  171. a_core_min = 299;
  172. a_core_max = 240;
  173. }
  174. else if (freq > 8600e6 && freq < 9800e6) {
  175. vco_core = 2;
  176. f_coremin = 8600e6;
  177. f_coremax = 9800e6;
  178. c_core_min = 165;
  179. c_core_max = 16;
  180. a_core_min = 356;
  181. a_core_max = 247;
  182. }
  183. else if (freq >= 9800e6 && freq <= 10800e6) {
  184. vco_core = 3;
  185. f_coremin = 9800e6;
  186. f_coremax = 10800e6;
  187. c_core_min = 158;
  188. c_core_max = 19;
  189. a_core_min = 324;
  190. a_core_max = 224;
  191. }
  192. else if (freq > 10800e6 && freq <= 12000e6) {
  193. vco_core = 4;
  194. f_coremin = 10800e6;
  195. f_coremax = 12000e6;
  196. c_core_min = 140;
  197. c_core_max = 0;
  198. a_core_min = 383;
  199. a_core_max = 244;
  200. }
  201. else if (freq > 12000e6 && freq <= 12900e6) {
  202. vco_core = 5;
  203. f_coremin = 12000e6;
  204. f_coremax = 12900e6;
  205. c_core_min = 183;
  206. c_core_max = 36;
  207. a_core_min = 205;
  208. a_core_max = 146;
  209. }
  210. else if (freq > 12900e6 && freq <= 13900e6) {
  211. vco_core = 6;
  212. f_coremin = 12900e6;
  213. f_coremax = 13900e6;
  214. c_core_min = 155;
  215. c_core_max = 6;
  216. a_core_min = 242;
  217. a_core_max = 163;
  218. }
  219. else if (freq > 13900e6 && freq <= 15000e6) {
  220. vco_core = 7;
  221. f_coremin = 13900e6;
  222. f_coremax = 15000e6;
  223. c_core_min = 175;
  224. c_core_max = 19;
  225. a_core_min = 323;
  226. a_core_max = 244;
  227. };
  228. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  229. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  230. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  231. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  232. // Calibration assist
  233. //Set the VCO_CORE
  234. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  235. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  236. // Set the VCO_CAP_CTRL
  237. lmx2594regs[112 - VCO_CAP_CTRL] = lmx2594regs[112 - VCO_CAP_CTRL] & (~BITM_LMX2594_R19_VCO_CAP_CTRL);
  238. lmx2594regs[112 - VCO_CAP_CTRL] = lmx2594regs[112 - VCO_CAP_CTRL] | (vco_cap_ctrl_strt << BITP_LMX2594_R19_VCO_CAP_CTRL);
  239. // Set the VCO_DACISET
  240. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  241. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  242. // Recommended sequnce for changin freq
  243. // 1. Change the N-div value
  244. // 2. Change the PLL numerator and denominator
  245. // 3. Program FCAL_EN bit
  246. // Clear the required parts of the register
  247. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  248. // Set the MASH_ORDER to 3
  249. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  250. // Set PF_DLY_SEL to 3
  251. if (freq <= 10e9) {
  252. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  253. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  254. printf("PFD_DLY_SEL = %d\n", 3);
  255. }
  256. else if (freq > 10e9) {
  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] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  259. printf("PFD_DLY_SEL = %d\n", 4);
  260. }
  261. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  262. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  263. //CLear the lower 16 bits of the register
  264. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  265. // Next 16 bits of the register
  266. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  267. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  268. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] & (~0xFFFF);
  269. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] | (frac_n >> 16);
  270. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  271. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] & (~0xFFFF);
  272. // Next 16 bits of the numerator
  273. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] | (frac_n & 0xFFFF);
  274. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  275. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] & (~0xFFFF);
  276. // most significant 16 bits of the denominator
  277. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] | (frac_d >> 16);
  278. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  279. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] & (~0xFFFF);
  280. // Next 16 bits of the denominator
  281. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] | (frac_d & 0xFFFF);
  282. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  283. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  284. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  285. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  286. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  287. // Program the FCAL_EN bit
  288. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  289. // Show the all the upper 16 bits of the register lmx2594regs[PLL_N_S]
  290. // Determine which regs are changed and send only those
  291. uint32_t lmx_change_freq_regs[] = {
  292. lmx2594regs[112 - VCO_SEL],
  293. lmx2594regs[112 - VCO_CAP_CTRL],
  294. lmx2594regs[112 - VCO_DACISET],
  295. lmx2594regs[112-MASH_ORDER],
  296. lmx2594regs[112-PFD_DLY_SEL],
  297. lmx2594regs[112-PLL_N_S],
  298. lmx2594regs[112-PLL_N_M],
  299. lmx2594regs[112-PLL_DEN_S],
  300. lmx2594regs[112-PLL_DEN_M],
  301. lmx2594regs[112-PLL_NUM_S],
  302. lmx2594regs[112-PLL_NUM_M],
  303. lmx2594regs[112 - CHDIV],
  304. lmx2594regs[112 - CHDIV_DIV2],
  305. lmx2594regs[112-OUTA_MUX],
  306. lmx2594regs[112-FCAL_ADDR]
  307. };
  308. // Create a header for the LMX2594 with the appropriate number of words
  309. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  310. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  311. *ptr = LMX_HEADER;
  312. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  313. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  314. *data_ptr = lmx_change_freq_regs[i];
  315. }
  316. usleep(1);
  317. printf("N_div = %f\n", N_div);
  318. printf("f_vco = %f\n", freq);
  319. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  320. printf("N = %d\n", N);
  321. printf("frac_n = %d\n", frac_n);
  322. printf("frac_d = %d\n", frac_d);
  323. return 0;
  324. }
  325. int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
  326. double f_vco = 2 * freq;
  327. int chan_div = 2;
  328. uint8_t ch_div_reg = 0; // 2
  329. double vco_div = 7.5e9 / freq;
  330. double N_div;
  331. int vco_core;
  332. double f_coremin;
  333. double f_coremax;
  334. int c_core_min;
  335. int c_core_max;
  336. int a_core_min;
  337. int a_core_max;
  338. uint16_t vco_cap_ctrl_strt;
  339. uint16_t vco_daciset_strt;
  340. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  341. if (f_vco < 7.5e9) {
  342. if (vco_div > 2 && vco_div <= 4)
  343. chan_div = 4; // 4
  344. f_vco = freq * chan_div;
  345. if (vco_div > 4 && vco_div <= 6) {
  346. chan_div = 6; // 6
  347. f_vco = freq * chan_div;
  348. }
  349. if (vco_div > 6 && vco_div <= 8) {
  350. chan_div = 8; // 8
  351. f_vco = freq * chan_div;
  352. }
  353. if (vco_div > 8 && vco_div <= 12) {
  354. chan_div = 12; // 12
  355. f_vco = freq * chan_div;
  356. }
  357. if (vco_div > 12 && vco_div <= 16) {
  358. chan_div = 16; // 16
  359. f_vco = freq * chan_div;
  360. }
  361. if (vco_div > 16 && vco_div <= 24) {
  362. chan_div = 24; // 24
  363. f_vco = freq * chan_div;
  364. }
  365. if (vco_div > 24 && vco_div <= 32) {
  366. chan_div = 32; // 32
  367. f_vco = freq * chan_div;
  368. }
  369. if (vco_div > 32 && vco_div <= 48) {
  370. chan_div = 48; // 48
  371. f_vco = freq * chan_div;
  372. }
  373. if (vco_div > 48 && vco_div <= 64) {
  374. chan_div = 64; // 64
  375. f_vco = freq * chan_div;
  376. }
  377. if (vco_div > 64 && vco_div <= 72) {
  378. chan_div = 72; // 72
  379. f_vco = freq * chan_div;
  380. }
  381. if (vco_div > 72 && vco_div <= 96) {
  382. chan_div = 96; // 96
  383. f_vco = freq * chan_div;
  384. }
  385. if (vco_div > 96 && vco_div <= 128) {
  386. chan_div = 128; // 128
  387. f_vco = freq * chan_div;
  388. }
  389. if (vco_div > 128 && vco_div <= 192) {
  390. chan_div = 192; // 192
  391. f_vco = freq * chan_div;
  392. }
  393. if (vco_div > 192 && vco_div <= 256) {
  394. chan_div = 256; // 256
  395. f_vco = freq * chan_div;
  396. }
  397. if (vco_div > 256 && vco_div <= 384) {
  398. chan_div = 384; // 384
  399. f_vco = freq * chan_div;
  400. }
  401. if (vco_div > 384 && vco_div <= 512) {
  402. chan_div = 512; // 512
  403. f_vco = freq * chan_div;
  404. }
  405. if (vco_div > 512 && vco_div <= 768) {
  406. chan_div = 768; // 768
  407. f_vco = freq * chan_div;
  408. }
  409. switch (chan_div) {
  410. case 2:
  411. ch_div_reg = 0;
  412. break;
  413. case 4:
  414. ch_div_reg = 1;
  415. break;
  416. case 6:
  417. ch_div_reg = 2;
  418. break;
  419. case 8:
  420. ch_div_reg = 3;
  421. break;
  422. case 12:
  423. ch_div_reg = 4;
  424. break;
  425. case 16:
  426. ch_div_reg = 5;
  427. break;
  428. case 24:
  429. ch_div_reg = 6;
  430. break;
  431. case 32:
  432. ch_div_reg = 7;
  433. break;
  434. case 48:
  435. ch_div_reg = 8;
  436. break;
  437. case 64:
  438. ch_div_reg = 9;
  439. break;
  440. case 72:
  441. ch_div_reg = 10;
  442. break;
  443. case 96:
  444. ch_div_reg = 11;
  445. break;
  446. case 128:
  447. ch_div_reg = 12;
  448. break;
  449. case 192:
  450. ch_div_reg = 13;
  451. break;
  452. case 256:
  453. ch_div_reg = 14;
  454. break;
  455. case 384:
  456. ch_div_reg = 15;
  457. break;
  458. case 512:
  459. ch_div_reg = 16;
  460. break;
  461. case 768:
  462. ch_div_reg = 17;
  463. break;
  464. }
  465. } else {
  466. ch_div_reg = 0;
  467. f_vco = freq * 2;
  468. }
  469. N_div = f_vco / f_pd;
  470. // divide whole part and fractional part
  471. uint32_t N = (uint32_t) N_div;
  472. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  473. uint32_t frac_d = 524287;
  474. // If frac part is 0 then the denominator is 1000 and numerator is 0
  475. if (frac_n == 0) {
  476. frac_n = 0;
  477. frac_d = 524287;
  478. }
  479. // Partial assist for the calibration
  480. //Determine a VCO core and other parameters
  481. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  482. vco_core = 1;
  483. f_coremin = 7500e6;
  484. f_coremax = 8600e6;
  485. c_core_min = 164;
  486. c_core_max = 12;
  487. a_core_min = 299;
  488. a_core_max = 240;
  489. }
  490. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  491. vco_core = 2;
  492. f_coremin = 8600e6;
  493. f_coremax = 9800e6;
  494. c_core_min = 165;
  495. c_core_max = 16;
  496. a_core_min = 356;
  497. a_core_max = 247;
  498. }
  499. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  500. vco_core = 3;
  501. f_coremin = 9800e6;
  502. f_coremax = 10800e6;
  503. c_core_min = 158;
  504. c_core_max = 19;
  505. a_core_min = 324;
  506. a_core_max = 224;
  507. }
  508. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  509. vco_core = 4;
  510. f_coremin = 10800e6;
  511. f_coremax = 12000e6;
  512. c_core_min = 140;
  513. c_core_max = 0;
  514. a_core_min = 383;
  515. a_core_max = 244;
  516. }
  517. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  518. vco_core = 5;
  519. f_coremin = 12000e6;
  520. f_coremax = 12900e6;
  521. c_core_min = 183;
  522. c_core_max = 36;
  523. a_core_min = 205;
  524. a_core_max = 146;
  525. }
  526. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  527. vco_core = 6;
  528. f_coremin = 12900e6;
  529. f_coremax = 13900e6;
  530. c_core_min = 155;
  531. c_core_max = 6;
  532. a_core_min = 242;
  533. a_core_max = 163;
  534. }
  535. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  536. vco_core = 7;
  537. f_coremin = 13900e6;
  538. f_coremax = 15000e6;
  539. c_core_min = 175;
  540. c_core_max = 19;
  541. a_core_min = 323;
  542. a_core_max = 244;
  543. };
  544. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  545. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  546. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  547. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  548. // Calibration assist
  549. //Set the VCO_CORE
  550. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  551. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  552. // Set the VCO_CAP_CTRL
  553. lmx2594regs[112 - VCO_CAP_CTRL] = lmx2594regs[112 - VCO_CAP_CTRL] & (~BITM_LMX2594_R19_VCO_CAP_CTRL);
  554. lmx2594regs[112 - VCO_CAP_CTRL] = lmx2594regs[112 - VCO_CAP_CTRL] | (vco_cap_ctrl_strt << BITP_LMX2594_R19_VCO_CAP_CTRL);
  555. // Set the VCO_DACISET
  556. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  557. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  558. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  559. // Set the MASH_ORDER to 3
  560. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  561. // Set PF_DLY_SEL to appropriate value
  562. if (f_vco <=10e9){
  563. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  564. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  565. printf("PFD_DLY_SEL = %d\n", 3);
  566. }
  567. else if (f_vco > 10e9) {
  568. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  569. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  570. printf("PFD_DLY_SEL = %d\n", 4);
  571. }
  572. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  573. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  574. //CLear the lower 16 bits of the register
  575. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  576. // Next 16 bits of the register
  577. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  578. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  579. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  580. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  581. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  582. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  583. // Next 16 bits of the numerator
  584. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  585. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  586. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  587. // most significant 16 bits of the denominator
  588. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  589. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  590. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  591. // Next 16 bits of the denominator
  592. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  593. // Program the CHDIV value
  594. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  595. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  596. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  597. // If the ch_div > 2 then set the SEG1_EN bit
  598. if (chan_div > 2) {
  599. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  600. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  601. }
  602. else {
  603. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  604. }
  605. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  606. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  607. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  608. // Program the FCAL_EN bit
  609. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  610. uint32_t lmx_change_freq_regs[] = {
  611. lmx2594regs[112 - VCO_SEL],
  612. lmx2594regs[112 - VCO_CAP_CTRL],
  613. lmx2594regs[112 - VCO_DACISET],
  614. lmx2594regs[112-MASH_ORDER],
  615. lmx2594regs[112-PFD_DLY_SEL],
  616. lmx2594regs[112 - PLL_N_S],
  617. lmx2594regs[112 - PLL_N_M],
  618. lmx2594regs[112 - PLL_DEN_S],
  619. lmx2594regs[112 - PLL_DEN_M],
  620. lmx2594regs[112 - PLL_NUM_S],
  621. lmx2594regs[112 - PLL_NUM_M],
  622. lmx2594regs[112 - CHDIV],
  623. lmx2594regs[112 - CHDIV_DIV2],
  624. lmx2594regs[112 - OUTA_MUX],
  625. lmx2594regs[112 - FCAL_ADDR]
  626. };
  627. // Create a header for the LMX2594 with the appropriate number of words
  628. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  629. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  630. *ptr = LMX_HEADER;
  631. // Send the data
  632. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  633. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  634. *data_ptr = lmx_change_freq_regs[i];
  635. }
  636. usleep(1);
  637. printf("N_div = %f\n", N_div);
  638. printf("f_vco = %f\n", f_vco);
  639. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  640. printf("N = %d\n", N);
  641. printf("frac_n = %d\n", frac_n);
  642. printf("frac_d = %d\n", frac_d);
  643. printf("chan_div = %d\n", chan_div);
  644. printf("chan_div_reg = %d\n", ch_div_reg);
  645. return 0;
  646. }
  647. int lmx_freq_set(void *bar1, double freq) {
  648. double f_pd = 200e6;
  649. double N_div = 0;
  650. if (freq < 10e6 || freq > 15e9) {
  651. printf("Frequency range is 10 MHz to 15 GHz\n");
  652. return -1;
  653. }
  654. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  655. if (freq >= 7.5e9 && freq <= 15e9) {
  656. lmx_freq_set_main_band(bar1, freq, f_pd);
  657. }
  658. else if (freq < 7.5e9) {
  659. lmx_freq_set_out_of_band(bar1, freq, f_pd);
  660. }
  661. return 0;
  662. }
  663. uint32_t lmx_ld_status(void *bar1) {
  664. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  665. uint32_t read_value = *read_ptr;
  666. return read_value;
  667. }