lmx2594.c 23 KB

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