lmx2594.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. 0x250204,
  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. double lmx_freq; // Frequency of the LMX2594
  123. void lmx2594_init(void *bar1) {
  124. // Header for LMX Reset
  125. uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
  126. *ptr_rst = LMX2594_RST_HEADER;
  127. // Reset Data
  128. for (int m = 0; m < (sizeof(lmx2594_rst))/4; m++) {
  129. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  130. *ptr = lmx2594_rst[m];
  131. }
  132. // Header for init data
  133. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  134. *ptr = InitLMX2594Header;
  135. // Init data
  136. for (int i = 0; i < LMX_COUNT; i++) {
  137. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  138. *ptr = lmx2594regs[i];
  139. }
  140. FILE * f = fopen("init.txt", "w");
  141. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  142. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  143. }
  144. fclose(f);
  145. }
  146. /*-------------------------LMX2594 Frequency Set-------------------------*/
  147. int lmx_freq_set_main_band(void *bar1, double freq, double f_pd) {
  148. double N_div;
  149. N_div = freq / f_pd;
  150. int vco_core;
  151. double f_coremin;
  152. double f_coremax;
  153. int c_core_min;
  154. int c_core_max;
  155. int a_core_min;
  156. int a_core_max;
  157. uint16_t vco_cap_ctrl_strt;
  158. uint16_t vco_daciset_strt;
  159. // divide whole part and fractional part
  160. uint32_t N = (uint32_t) N_div;
  161. // In frac part there is separate denominator and numerator
  162. // If frac part is 0 then the denominator is 1000 and numerator is 0
  163. uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
  164. uint32_t frac_d = 4294967295-1;
  165. // If frac part is 0 then the denominator is 1000 and numerator is 0
  166. if (frac_n == 0) {
  167. frac_n = 0;
  168. frac_d = 1000;
  169. }
  170. // Partial assist for the calibration
  171. //Determine a VCO core and other parameters
  172. if (freq >= 7500e6 && freq <= 8600e6) {
  173. vco_core = 1;
  174. f_coremin = 7500e6;
  175. f_coremax = 8600e6;
  176. c_core_min = 164;
  177. c_core_max = 12;
  178. a_core_min = 299;
  179. a_core_max = 240;
  180. }
  181. else if (freq > 8600e6 && freq < 9800e6) {
  182. vco_core = 2;
  183. f_coremin = 8600e6;
  184. f_coremax = 9800e6;
  185. c_core_min = 165;
  186. c_core_max = 16;
  187. a_core_min = 356;
  188. a_core_max = 247;
  189. }
  190. else if (freq >= 9800e6 && freq <= 10800e6) {
  191. vco_core = 3;
  192. f_coremin = 9800e6;
  193. f_coremax = 10800e6;
  194. c_core_min = 158;
  195. c_core_max = 19;
  196. a_core_min = 324;
  197. a_core_max = 224;
  198. }
  199. else if (freq > 10800e6 && freq <= 12000e6) {
  200. vco_core = 4;
  201. f_coremin = 10800e6;
  202. f_coremax = 12000e6;
  203. c_core_min = 140;
  204. c_core_max = 0;
  205. a_core_min = 383;
  206. a_core_max = 244;
  207. }
  208. else if (freq > 12000e6 && freq <= 12900e6) {
  209. vco_core = 5;
  210. f_coremin = 12000e6;
  211. f_coremax = 12900e6;
  212. c_core_min = 183;
  213. c_core_max = 36;
  214. a_core_min = 205;
  215. a_core_max = 146;
  216. }
  217. else if (freq > 12900e6 && freq <= 13900e6) {
  218. vco_core = 6;
  219. f_coremin = 12900e6;
  220. f_coremax = 13900e6;
  221. c_core_min = 155;
  222. c_core_max = 6;
  223. a_core_min = 242;
  224. a_core_max = 163;
  225. }
  226. else if (freq > 13900e6 && freq <= 15000e6) {
  227. vco_core = 7;
  228. f_coremin = 13900e6;
  229. f_coremax = 15000e6;
  230. c_core_min = 175;
  231. c_core_max = 19;
  232. a_core_min = 323;
  233. a_core_max = 244;
  234. };
  235. if (freq >=11900e6 && freq <=12100e6) {
  236. vco_daciset_strt = 300;
  237. vco_core = 4;
  238. vco_cap_ctrl_strt = 1;
  239. }
  240. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  241. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  242. printf("VCO_CORE = %d\n", vco_core);
  243. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  244. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  245. // Calibration assist
  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. // Recommended sequnce for changin freq
  256. // 1. Change the N-div value
  257. // 2. Change the PLL numerator and denominator
  258. // 3. Program FCAL_EN bit
  259. // Clear the required parts of the register
  260. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  261. // Set the MASH_ORDER to 3
  262. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  263. // Set PF_DLY_SEL to 3
  264. if (freq <= 10e9) {
  265. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  266. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  267. printf("PFD_DLY_SEL = %d\n", 3);
  268. }
  269. else if (freq > 10e9) {
  270. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  271. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  272. printf("PFD_DLY_SEL = %d\n", 4);
  273. }
  274. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  275. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  276. //CLear the lower 16 bits of the register
  277. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  278. // Next 16 bits of the register
  279. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  280. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  281. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] & (~0xFFFF);
  282. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] | (frac_n >> 16);
  283. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  284. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] & (~0xFFFF);
  285. // Next 16 bits of the numerator
  286. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] | (frac_n & 0xFFFF);
  287. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  288. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] & (~0xFFFF);
  289. // most significant 16 bits of the denominator
  290. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] | (frac_d >> 16);
  291. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  292. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] & (~0xFFFF);
  293. // Next 16 bits of the denominator
  294. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] | (frac_d & 0xFFFF);
  295. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  296. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  297. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  298. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  299. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  300. // Program the FCAL_EN bit
  301. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  302. // Show the all the upper 16 bits of the register lmx2594regs[PLL_N_S]
  303. // Determine which regs are changed and send only those
  304. uint32_t lmx_change_freq_regs[] = {
  305. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  306. lmx2594regs[112 - VCO_SEL],
  307. lmx2594regs[112 - CAP_CTRL_START],
  308. lmx2594regs[112 - VCO_DACISET],
  309. lmx2594regs[112-MASH_ORDER],
  310. lmx2594regs[112-PFD_DLY_SEL],
  311. lmx2594regs[112-PLL_N_S],
  312. lmx2594regs[112-PLL_N_M],
  313. lmx2594regs[112-PLL_DEN_S],
  314. lmx2594regs[112-PLL_DEN_M],
  315. lmx2594regs[112-PLL_NUM_S],
  316. lmx2594regs[112-PLL_NUM_M],
  317. lmx2594regs[112 - CHDIV],
  318. lmx2594regs[112 - CHDIV_DIV2],
  319. lmx2594regs[112-OUTA_MUX],
  320. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  321. lmx2594regs[112-FCAL_ADDR]
  322. };
  323. // Create a header for the LMX2594 with the appropriate number of words
  324. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  325. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  326. *ptr = LMX_HEADER;
  327. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  328. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  329. *data_ptr = lmx_change_freq_regs[i];
  330. }
  331. char filename[100];
  332. sprintf(filename, "%f.txt", freq);
  333. FILE * f = fopen(filename, "w");
  334. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  335. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  336. }
  337. fclose(f);
  338. printf("File has been written\n");
  339. printf("N_div = %f\n", N_div);
  340. printf("f_vco = %f\n", freq);
  341. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  342. printf("N = %d\n", N);
  343. printf("frac_n = %u\n", frac_n);
  344. printf("frac_d = %u\n", frac_d);
  345. }
  346. int lmx_freq_set_main_band_int_mode(void *bar1, double freq, double f_pd) {
  347. uint32_t N_div;
  348. N_div = freq / f_pd;
  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. // Partial assist for the calibration
  359. //Determine a VCO core and other parameters
  360. if (freq >= 7500e6 && freq <= 8600e6) {
  361. vco_core = 1;
  362. f_coremin = 7500e6;
  363. f_coremax = 8600e6;
  364. c_core_min = 164;
  365. c_core_max = 12;
  366. a_core_min = 299;
  367. a_core_max = 240;
  368. }
  369. else if (freq > 8600e6 && freq < 9800e6) {
  370. vco_core = 2;
  371. f_coremin = 8600e6;
  372. f_coremax = 9800e6;
  373. c_core_min = 165;
  374. c_core_max = 16;
  375. a_core_min = 356;
  376. a_core_max = 247;
  377. }
  378. else if (freq >= 9800e6 && freq <= 10800e6) {
  379. vco_core = 3;
  380. f_coremin = 9800e6;
  381. f_coremax = 10800e6;
  382. c_core_min = 158;
  383. c_core_max = 19;
  384. a_core_min = 324;
  385. a_core_max = 224;
  386. }
  387. else if (freq > 10800e6 && freq <= 12000e6) {
  388. vco_core = 4;
  389. f_coremin = 10800e6;
  390. f_coremax = 12000e6;
  391. c_core_min = 140;
  392. c_core_max = 0;
  393. a_core_min = 383;
  394. a_core_max = 244;
  395. }
  396. else if (freq > 12000e6 && freq <= 12900e6) {
  397. vco_core = 5;
  398. f_coremin = 12000e6;
  399. f_coremax = 12900e6;
  400. c_core_min = 183;
  401. c_core_max = 36;
  402. a_core_min = 205;
  403. a_core_max = 146;
  404. }
  405. else if (freq > 12900e6 && freq <= 13900e6) {
  406. vco_core = 6;
  407. f_coremin = 12900e6;
  408. f_coremax = 13900e6;
  409. c_core_min = 155;
  410. c_core_max = 6;
  411. a_core_min = 242;
  412. a_core_max = 163;
  413. }
  414. else if (freq > 13900e6 && freq <= 15000e6) {
  415. vco_core = 7;
  416. f_coremin = 13900e6;
  417. f_coremax = 15000e6;
  418. c_core_min = 175;
  419. c_core_max = 19;
  420. a_core_min = 323;
  421. a_core_max = 244;
  422. };
  423. if (freq >=11900e6 && freq <=12100e6) {
  424. vco_daciset_strt = 300;
  425. vco_core = 4;
  426. vco_cap_ctrl_strt = 1;
  427. }
  428. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  429. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  430. printf("VCO_CORE = %d\n", vco_core);
  431. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  432. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  433. //Set the VCO_CORE
  434. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  435. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  436. // Set the VCO_CAP_CTRL
  437. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  438. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  439. // Set the VCO_DACISET
  440. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  441. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  442. // Set the PF_DLY_SEL
  443. if (freq <= 12500e6) {
  444. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  445. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  446. printf("PFD_DLY_SEL = %d\n", 1);
  447. }
  448. else if (freq > 12500e6) {
  449. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  450. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  451. printf("PFD_DLY_SEL = %d\n", 2);
  452. }
  453. // SET the N_DIV
  454. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  455. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N_div >> 16);
  456. //CLear the lower 16 bits of the register
  457. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  458. // Next 16 bits of the register
  459. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N_div & 0xFFFF);
  460. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  461. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  462. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  463. // Program the FCAL_EN bit
  464. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  465. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  466. uint32_t lmx_change_freq_regs[] = {
  467. lmx2594regs[112 - VCO_SEL],
  468. lmx2594regs[112 - CAP_CTRL_START],
  469. lmx2594regs[112 - VCO_DACISET],
  470. lmx2594regs[112-PFD_DLY_SEL],
  471. lmx2594regs[112-PLL_N_S],
  472. lmx2594regs[112-PLL_N_M],
  473. lmx2594regs[112 - OUTA_MUX],
  474. lmx2594regs[112-FCAL_ADDR]
  475. };
  476. // Create a header for the LMX2594 with the appropriate number of words
  477. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  478. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  479. *ptr = LMX_HEADER;
  480. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  481. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  482. *data_ptr = lmx_change_freq_regs[i];
  483. }
  484. char filename[100];
  485. sprintf(filename, "%f.txt", freq);
  486. FILE * f = fopen(filename, "w");
  487. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  488. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  489. }
  490. fclose(f);
  491. return 0;
  492. }
  493. int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
  494. if (freq >= 10e6 && freq <= 1000e6) {
  495. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  496. }
  497. else {
  498. lmx_freq = freq;
  499. }
  500. double f_vco = 2 * lmx_freq;
  501. int chan_div = 2;
  502. uint8_t ch_div_reg = 0; // 2
  503. double vco_div = 7.5e9 / lmx_freq;
  504. double N_div;
  505. int vco_core;
  506. double f_coremin;
  507. double f_coremax;
  508. int c_core_min;
  509. int c_core_max;
  510. int a_core_min;
  511. int a_core_max;
  512. uint16_t vco_cap_ctrl_strt;
  513. uint16_t vco_daciset_strt;
  514. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  515. if (f_vco < 7.5e9) {
  516. if (vco_div > 2 && vco_div <= 4)
  517. chan_div = 4; // 4
  518. f_vco = lmx_freq * chan_div;
  519. if (vco_div > 4 && vco_div <= 6) {
  520. chan_div = 6; // 6
  521. f_vco = lmx_freq * chan_div;
  522. }
  523. if (vco_div > 6 && vco_div <= 8) {
  524. chan_div = 8; // 8
  525. f_vco = lmx_freq * chan_div;
  526. }
  527. if (vco_div > 8 && vco_div <= 12) {
  528. chan_div = 12; // 12
  529. f_vco = lmx_freq * chan_div;
  530. }
  531. if (vco_div > 12 && vco_div <= 16) {
  532. chan_div = 16; // 16
  533. f_vco = lmx_freq * chan_div;
  534. }
  535. if (vco_div > 16 && vco_div <= 24) {
  536. chan_div = 24; // 24
  537. f_vco = lmx_freq * chan_div;
  538. }
  539. if (vco_div > 24 && vco_div <= 32) {
  540. chan_div = 32; // 32
  541. f_vco = lmx_freq * chan_div;
  542. }
  543. if (vco_div > 32 && vco_div <= 48) {
  544. chan_div = 48; // 48
  545. f_vco = lmx_freq * chan_div;
  546. }
  547. if (vco_div > 48 && vco_div <= 64) {
  548. chan_div = 64; // 64
  549. f_vco = lmx_freq * chan_div;
  550. }
  551. if (vco_div > 64 && vco_div <= 72) {
  552. chan_div = 72; // 72
  553. f_vco = lmx_freq * chan_div;
  554. }
  555. if (vco_div > 72 && vco_div <= 96) {
  556. chan_div = 96; // 96
  557. f_vco = lmx_freq * chan_div;
  558. }
  559. if (vco_div > 96 && vco_div <= 128) {
  560. chan_div = 128; // 128
  561. f_vco = lmx_freq * chan_div;
  562. }
  563. if (vco_div > 128 && vco_div <= 192) {
  564. chan_div = 192; // 192
  565. f_vco = lmx_freq * chan_div;
  566. }
  567. if (vco_div > 192 && vco_div <= 256) {
  568. chan_div = 256; // 256
  569. f_vco = lmx_freq * chan_div;
  570. }
  571. if (vco_div > 256 && vco_div <= 384) {
  572. chan_div = 384; // 384
  573. f_vco = lmx_freq * chan_div;
  574. }
  575. if (vco_div > 384 && vco_div <= 512) {
  576. chan_div = 512; // 512
  577. f_vco = lmx_freq * chan_div;
  578. }
  579. if (vco_div > 512 && vco_div <= 768) {
  580. chan_div = 768; // 768
  581. f_vco = lmx_freq * chan_div;
  582. }
  583. switch (chan_div) {
  584. case 2:
  585. ch_div_reg = 0;
  586. break;
  587. case 4:
  588. ch_div_reg = 1;
  589. break;
  590. case 6:
  591. ch_div_reg = 2;
  592. break;
  593. case 8:
  594. ch_div_reg = 3;
  595. break;
  596. case 12:
  597. ch_div_reg = 4;
  598. break;
  599. case 16:
  600. ch_div_reg = 5;
  601. break;
  602. case 24:
  603. ch_div_reg = 6;
  604. break;
  605. case 32:
  606. ch_div_reg = 7;
  607. break;
  608. case 48:
  609. ch_div_reg = 8;
  610. break;
  611. case 64:
  612. ch_div_reg = 9;
  613. break;
  614. case 72:
  615. ch_div_reg = 10;
  616. break;
  617. case 96:
  618. ch_div_reg = 11;
  619. break;
  620. case 128:
  621. ch_div_reg = 12;
  622. break;
  623. case 192:
  624. ch_div_reg = 13;
  625. break;
  626. case 256:
  627. ch_div_reg = 14;
  628. break;
  629. case 384:
  630. ch_div_reg = 15;
  631. break;
  632. case 512:
  633. ch_div_reg = 16;
  634. break;
  635. case 768:
  636. ch_div_reg = 17;
  637. break;
  638. }
  639. } else {
  640. ch_div_reg = 0;
  641. f_vco = lmx_freq * 2;
  642. }
  643. N_div = f_vco / f_pd;
  644. // divide whole part and fractional part
  645. uint32_t N = (uint32_t) N_div;
  646. uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
  647. uint32_t frac_d = 4294967295-1;
  648. // If frac part is 0 then the denominator is 1000 and numerator is 0
  649. if (frac_n == 0) {
  650. frac_n = 0;
  651. frac_d = 1000;
  652. }
  653. // Partial assist for the calibration
  654. //Determine a VCO core and other parameters
  655. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  656. vco_core = 1;
  657. f_coremin = 7500e6;
  658. f_coremax = 8600e6;
  659. c_core_min = 164;
  660. c_core_max = 12;
  661. a_core_min = 299;
  662. a_core_max = 240;
  663. }
  664. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  665. vco_core = 2;
  666. f_coremin = 8600e6;
  667. f_coremax = 9800e6;
  668. c_core_min = 165;
  669. c_core_max = 16;
  670. a_core_min = 356;
  671. a_core_max = 247;
  672. }
  673. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  674. vco_core = 3;
  675. f_coremin = 9800e6;
  676. f_coremax = 10800e6;
  677. c_core_min = 158;
  678. c_core_max = 19;
  679. a_core_min = 324;
  680. a_core_max = 224;
  681. }
  682. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  683. vco_core = 4;
  684. f_coremin = 10800e6;
  685. f_coremax = 12000e6;
  686. c_core_min = 140;
  687. c_core_max = 0;
  688. a_core_min = 383;
  689. a_core_max = 244;
  690. }
  691. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  692. vco_core = 5;
  693. f_coremin = 12000e6;
  694. f_coremax = 12900e6;
  695. c_core_min = 183;
  696. c_core_max = 36;
  697. a_core_min = 205;
  698. a_core_max = 146;
  699. }
  700. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  701. vco_core = 6;
  702. f_coremin = 12900e6;
  703. f_coremax = 13900e6;
  704. c_core_min = 155;
  705. c_core_max = 6;
  706. a_core_min = 242;
  707. a_core_max = 163;
  708. }
  709. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  710. vco_core = 7;
  711. f_coremin = 13900e6;
  712. f_coremax = 15000e6;
  713. c_core_min = 175;
  714. c_core_max = 19;
  715. a_core_min = 323;
  716. a_core_max = 244;
  717. };
  718. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  719. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  720. if (f_vco >=11900e6 && f_vco <=12100e6) {
  721. vco_daciset_strt = 300;
  722. vco_core = 4;
  723. vco_cap_ctrl_strt = 1;
  724. }
  725. printf("VCO_CORE = %d\n", vco_core);
  726. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  727. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  728. // Calibration assist
  729. //Set the VCO_CORE
  730. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  731. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  732. // Set the VCO_CAP_CTRL_START
  733. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  734. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  735. // Set the VCO_DACISET
  736. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  737. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  738. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  739. // Set the MASH_ORDER to 3
  740. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  741. // Set PF_DLY_SEL to appropriate value
  742. if (f_vco <=10e9){
  743. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  744. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  745. printf("PFD_DLY_SEL = %d\n", 3);
  746. }
  747. else if (f_vco > 10e9) {
  748. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  749. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  750. printf("PFD_DLY_SEL = %d\n", 4);
  751. }
  752. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  753. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  754. //CLear the lower 16 bits of the register
  755. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  756. // Next 16 bits of the register
  757. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  758. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  759. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  760. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  761. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  762. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  763. // Next 16 bits of the numerator
  764. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  765. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  766. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  767. // most significant 16 bits of the denominator
  768. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  769. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  770. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  771. // Next 16 bits of the denominator
  772. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  773. // Program the CHDIV value
  774. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  775. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  776. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  777. // If the ch_div > 2 then set the SEG1_EN bit
  778. if (chan_div > 2) {
  779. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  780. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  781. }
  782. else {
  783. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  784. }
  785. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  786. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  787. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  788. // Program the FCAL_EN bit
  789. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  790. uint32_t lmx_change_freq_regs[] = {
  791. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  792. lmx2594regs[112 - VCO_SEL],
  793. lmx2594regs[112 - CAP_CTRL_START],
  794. lmx2594regs[112 - VCO_DACISET],
  795. lmx2594regs[112-MASH_ORDER],
  796. lmx2594regs[112-PFD_DLY_SEL],
  797. lmx2594regs[112 - PLL_N_S],
  798. lmx2594regs[112 - PLL_N_M],
  799. lmx2594regs[112 - PLL_DEN_S],
  800. lmx2594regs[112 - PLL_DEN_M],
  801. lmx2594regs[112 - PLL_NUM_S],
  802. lmx2594regs[112 - PLL_NUM_M],
  803. lmx2594regs[112 - CHDIV],
  804. lmx2594regs[112 - CHDIV_DIV2],
  805. lmx2594regs[112 - OUTA_MUX],
  806. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  807. lmx2594regs[112 - FCAL_ADDR]
  808. };
  809. // Create a header for the LMX2594 with the appropriate number of words
  810. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  811. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  812. *ptr = LMX_HEADER;
  813. // Send the data
  814. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  815. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  816. *data_ptr = lmx_change_freq_regs[i];
  817. }
  818. char filename[100];
  819. sprintf(filename, "%f.txt", freq);
  820. FILE * f = fopen(filename, "w");
  821. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  822. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  823. }
  824. fclose(f);
  825. printf("N_div = %f\n", N_div);
  826. printf("f_vco = %f\n", f_vco);
  827. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  828. printf("N = %d\n", N);
  829. printf("frac_n = %u\n", frac_n);
  830. printf("frac_d = %u\n", frac_d);
  831. printf("chan_div = %d\n", chan_div);
  832. printf("chan_div_reg = %d\n", ch_div_reg);
  833. printf("LMX2594 Registers\n");
  834. return 0;
  835. }
  836. int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
  837. if (freq >= 10e6 && freq <= 1000e6) {
  838. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  839. }
  840. else {
  841. lmx_freq = freq;
  842. }
  843. double f_vco = 2 * lmx_freq;
  844. int chan_div = 2;
  845. uint8_t ch_div_reg = 0; // 2
  846. double vco_div = 7.5e9 / lmx_freq;
  847. int vco_core;
  848. double f_coremin;
  849. double f_coremax;
  850. int c_core_min;
  851. int c_core_max;
  852. int a_core_min;
  853. int a_core_max;
  854. uint16_t vco_cap_ctrl_strt;
  855. uint16_t vco_daciset_strt;
  856. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  857. if (f_vco < 7.5e9) {
  858. if (vco_div > 2 && vco_div <= 4)
  859. chan_div = 4; // 4
  860. f_vco = lmx_freq * chan_div;
  861. if (vco_div > 4 && vco_div <= 6) {
  862. chan_div = 6; // 6
  863. f_vco = lmx_freq * chan_div;
  864. }
  865. if (vco_div > 6 && vco_div <= 8) {
  866. chan_div = 8; // 8
  867. f_vco = lmx_freq * chan_div;
  868. }
  869. if (vco_div > 8 && vco_div <= 12) {
  870. chan_div = 12; // 12
  871. f_vco = lmx_freq * chan_div;
  872. }
  873. if (vco_div > 12 && vco_div <= 16) {
  874. chan_div = 16; // 16
  875. f_vco = lmx_freq * chan_div;
  876. }
  877. if (vco_div > 16 && vco_div <= 24) {
  878. chan_div = 24; // 24
  879. f_vco = lmx_freq * chan_div;
  880. }
  881. if (vco_div > 24 && vco_div <= 32) {
  882. chan_div = 32; // 32
  883. f_vco = lmx_freq * chan_div;
  884. }
  885. if (vco_div > 32 && vco_div <= 48) {
  886. chan_div = 48; // 48
  887. f_vco = lmx_freq * chan_div;
  888. }
  889. if (vco_div > 48 && vco_div <= 64) {
  890. chan_div = 64; // 64
  891. f_vco = lmx_freq * chan_div;
  892. }
  893. if (vco_div > 64 && vco_div <= 72) {
  894. chan_div = 72; // 72
  895. f_vco = lmx_freq * chan_div;
  896. }
  897. if (vco_div > 72 && vco_div <= 96) {
  898. chan_div = 96; // 96
  899. f_vco = lmx_freq * chan_div;
  900. }
  901. if (vco_div > 96 && vco_div <= 128) {
  902. chan_div = 128; // 128
  903. f_vco = lmx_freq * chan_div;
  904. }
  905. if (vco_div > 128 && vco_div <= 192) {
  906. chan_div = 192; // 192
  907. f_vco = lmx_freq * chan_div;
  908. }
  909. if (vco_div > 192 && vco_div <= 256) {
  910. chan_div = 256; // 256
  911. f_vco = lmx_freq * chan_div;
  912. }
  913. if (vco_div > 256 && vco_div <= 384) {
  914. chan_div = 384; // 384
  915. f_vco = lmx_freq * chan_div;
  916. }
  917. if (vco_div > 384 && vco_div <= 512) {
  918. chan_div = 512; // 512
  919. f_vco = lmx_freq * chan_div;
  920. }
  921. if (vco_div > 512 && vco_div <= 768) {
  922. chan_div = 768; // 768
  923. f_vco = lmx_freq * chan_div;
  924. }
  925. switch (chan_div) {
  926. case 2:
  927. ch_div_reg = 0;
  928. break;
  929. case 4:
  930. ch_div_reg = 1;
  931. break;
  932. case 6:
  933. ch_div_reg = 2;
  934. break;
  935. case 8:
  936. ch_div_reg = 3;
  937. break;
  938. case 12:
  939. ch_div_reg = 4;
  940. break;
  941. case 16:
  942. ch_div_reg = 5;
  943. break;
  944. case 24:
  945. ch_div_reg = 6;
  946. break;
  947. case 32:
  948. ch_div_reg = 7;
  949. break;
  950. case 48:
  951. ch_div_reg = 8;
  952. break;
  953. case 64:
  954. ch_div_reg = 9;
  955. break;
  956. case 72:
  957. ch_div_reg = 10;
  958. break;
  959. case 96:
  960. ch_div_reg = 11;
  961. break;
  962. case 128:
  963. ch_div_reg = 12;
  964. break;
  965. case 192:
  966. ch_div_reg = 13;
  967. break;
  968. case 256:
  969. ch_div_reg = 14;
  970. break;
  971. case 384:
  972. ch_div_reg = 15;
  973. break;
  974. case 512:
  975. ch_div_reg = 16;
  976. break;
  977. case 768:
  978. ch_div_reg = 17;
  979. break;
  980. }
  981. } else {
  982. ch_div_reg = 0;
  983. f_vco = lmx_freq * 2;
  984. }
  985. uint32_t N_div = f_vco / f_pd;
  986. // Partial assist for the calibration
  987. //Determine a VCO core and other parameters
  988. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  989. vco_core = 1;
  990. f_coremin = 7500e6;
  991. f_coremax = 8600e6;
  992. c_core_min = 164;
  993. c_core_max = 12;
  994. a_core_min = 299;
  995. a_core_max = 240;
  996. }
  997. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  998. vco_core = 2;
  999. f_coremin = 8600e6;
  1000. f_coremax = 9800e6;
  1001. c_core_min = 165;
  1002. c_core_max = 16;
  1003. a_core_min = 356;
  1004. a_core_max = 247;
  1005. }
  1006. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  1007. vco_core = 3;
  1008. f_coremin = 9800e6;
  1009. f_coremax = 10800e6;
  1010. c_core_min = 158;
  1011. c_core_max = 19;
  1012. a_core_min = 324;
  1013. a_core_max = 224;
  1014. }
  1015. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  1016. vco_core = 4;
  1017. f_coremin = 10800e6;
  1018. f_coremax = 12000e6;
  1019. c_core_min = 140;
  1020. c_core_max = 0;
  1021. a_core_min = 383;
  1022. a_core_max = 244;
  1023. }
  1024. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  1025. vco_core = 5;
  1026. f_coremin = 12000e6;
  1027. f_coremax = 12900e6;
  1028. c_core_min = 183;
  1029. c_core_max = 36;
  1030. a_core_min = 205;
  1031. a_core_max = 146;
  1032. }
  1033. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  1034. vco_core = 6;
  1035. f_coremin = 12900e6;
  1036. f_coremax = 13900e6;
  1037. c_core_min = 155;
  1038. c_core_max = 6;
  1039. a_core_min = 242;
  1040. a_core_max = 163;
  1041. }
  1042. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  1043. vco_core = 7;
  1044. f_coremin = 13900e6;
  1045. f_coremax = 15000e6;
  1046. c_core_min = 175;
  1047. c_core_max = 19;
  1048. a_core_min = 323;
  1049. a_core_max = 244;
  1050. };
  1051. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1052. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1053. if (f_vco >=11900e6 && f_vco <=12100e6) {
  1054. vco_daciset_strt = 300;
  1055. vco_core = 4;
  1056. vco_cap_ctrl_strt = 1;
  1057. }
  1058. printf("VCO_CORE = %d\n", vco_core);
  1059. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  1060. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  1061. // Calibration assist
  1062. //Set the VCO_CORE
  1063. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  1064. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  1065. // Set the VCO_CAP_CTRL_START
  1066. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  1067. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  1068. // Set the VCO_DACISET
  1069. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  1070. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  1071. // Set the PFD_DLY_SEL to appropriate value
  1072. if (freq <= 12500e6) {
  1073. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  1074. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1075. printf("PFD_DLY_SEL = %d\n", 1);
  1076. }
  1077. else if (freq > 12500e6) {
  1078. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  1079. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1080. printf("PFD_DLY_SEL = %d\n", 2);
  1081. }
  1082. // Set the N value
  1083. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  1084. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N_div >> 16);
  1085. // Clear the lower 16 bits of the register
  1086. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  1087. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N_div & 0xFFFF);
  1088. // Program the CHDIV value
  1089. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  1090. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  1091. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  1092. // If the ch_div > 2 then set the SEG1_EN bit
  1093. if (chan_div > 2) {
  1094. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1095. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  1096. }
  1097. else {
  1098. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1099. }
  1100. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  1101. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  1102. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  1103. // Program the FCAL_EN bit
  1104. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  1105. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  1106. uint32_t lmx_change_freq_regs []={
  1107. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  1108. lmx2594regs[112 - VCO_SEL],
  1109. lmx2594regs[112 - CAP_CTRL_START],
  1110. lmx2594regs[112 - VCO_DACISET],
  1111. lmx2594regs[112-PFD_DLY_SEL],
  1112. lmx2594regs[112 - PLL_N_S],
  1113. lmx2594regs[112 - PLL_N_M],
  1114. lmx2594regs[112 - CHDIV],
  1115. lmx2594regs[112 - CHDIV_DIV2],
  1116. lmx2594regs[112 - OUTA_MUX],
  1117. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  1118. lmx2594regs[112 - FCAL_ADDR]
  1119. };
  1120. // Create a header for the LMX2594 with the appropriate number of words
  1121. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  1122. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  1123. *ptr = LMX_HEADER;
  1124. // Send the data
  1125. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  1126. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  1127. *data_ptr = lmx_change_freq_regs[i];
  1128. }
  1129. char filename[100];
  1130. sprintf(filename, "%f.txt", freq);
  1131. FILE * f = fopen(filename, "w");
  1132. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  1133. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  1134. }
  1135. fclose(f);
  1136. return 0;
  1137. }
  1138. double lmx_lower_bond_set (double freq, double f_pd) {
  1139. double f_max2870 = 4e9;
  1140. double lmx_req_freq = f_max2870-freq; // 4 GHz - freq
  1141. return lmx_req_freq;
  1142. }
  1143. int lmx_freq_set(void *bar1, double freq,double f_pd) {
  1144. // double f_pd = 175e6;
  1145. double N_div = 0;
  1146. if (freq < 10e6 || freq > 15e9) {
  1147. printf("Frequency range is 10 MHz to 15 GHz\n");
  1148. return -1;
  1149. }
  1150. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  1151. if (freq >= 7.5e9 && freq <= 15e9) {
  1152. // lmx_freq_set_main_band(bar1, freq, f_pd);
  1153. lmx_freq_set_main_band_int_mode(bar1, freq, f_pd);
  1154. }
  1155. else if (freq < 7.5e9) {
  1156. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  1157. lmx_freq_set_out_of_band_int_mode(bar1, freq, f_pd);
  1158. }
  1159. // Switch the keys
  1160. key_switch(bar1, freq,lmx_freq);
  1161. return 0;
  1162. }
  1163. uint32_t lmx_ld_status(void *bar1) {
  1164. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  1165. uint32_t read_value = *read_ptr;
  1166. return read_value;
  1167. }