lmx2594.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. #include "lmx2594.h"
  2. #include <math.h>
  3. const uint32_t lmx2594_rst[] = {
  4. 0x002516,
  5. 0x002514
  6. };
  7. uint32_t lmx2594regs[LMX_COUNT] = {
  8. 0x700000,
  9. 0x6F0000,
  10. 0x6E0000,
  11. 0x6D0000,
  12. 0x6C0000,
  13. 0x6B0000,
  14. 0x6A0000,
  15. 0x690021,
  16. 0x680000,
  17. 0x670000,
  18. 0x660000,
  19. 0x650011,
  20. 0x640000,
  21. 0x630000,
  22. 0x620000,
  23. 0x610888,
  24. 0x600000,
  25. 0x5F0000,
  26. 0x5E0000,
  27. 0x5D0000,
  28. 0x5C0000,
  29. 0x5B0000,
  30. 0x5A0000,
  31. 0x590000,
  32. 0x580000,
  33. 0x570000,
  34. 0x560000,
  35. 0x550000,
  36. 0x540000,
  37. 0x530000,
  38. 0x520000,
  39. 0x510000,
  40. 0x500000,
  41. 0x4F0000,
  42. 0x4E016F,
  43. 0x4D0000,
  44. 0x4C000C,
  45. 0x4B0800,
  46. 0x4A0000,
  47. 0x49003F,
  48. 0x480001,
  49. 0x470081,
  50. 0x46C350,
  51. 0x450000,
  52. 0x4403E8,
  53. 0x430000,
  54. 0x4201F4,
  55. 0x410000,
  56. 0x401388,
  57. 0x3F0000,
  58. 0x3E0322,
  59. 0x3D00A8,
  60. 0x3C03E8,
  61. 0x3B0001,
  62. 0x3A9001,
  63. 0x390020,
  64. 0x380000,
  65. 0x370000,
  66. 0x360000,
  67. 0x350000,
  68. 0x340820,
  69. 0x330080,
  70. 0x320000,
  71. 0x314180,
  72. 0x300300,
  73. 0x2F0300,
  74. 0x2E07FC,
  75. 0x2DC8DF,
  76. 0x2C1FA0,
  77. 0x2B0000,
  78. 0x2A0000,
  79. 0x290000,
  80. 0x280000,
  81. 0x2703E8,
  82. 0x260000,
  83. 0x250104,
  84. 0x240032,
  85. 0x230004,
  86. 0x220000,
  87. 0x211E21,
  88. 0x200393,
  89. 0x1F43EC,
  90. 0x1E318C,
  91. 0x1D318C,
  92. 0x1C0488,
  93. 0x1B0002,
  94. 0x1A0DB0,
  95. 0x190C2B,
  96. 0x18071A,
  97. 0x17007C,
  98. 0x160001,
  99. 0x150401,
  100. 0x14F848,
  101. 0x1327B7,
  102. 0x120064,
  103. 0x11012C,
  104. 0x100080,
  105. 0x0F064F,
  106. 0x0E1E40,
  107. 0x0D4000,
  108. 0x0C5001,
  109. 0x0B0018,
  110. 0x0A10D8,
  111. 0x090604,
  112. 0x082000,
  113. 0x0740B2,
  114. 0x06C802,
  115. 0x0500C8,
  116. 0x041243,
  117. 0x030642,
  118. 0x020500,
  119. 0x010808,
  120. 0x00251C
  121. };
  122. 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. double N_div;
  348. printf("f_pd before = %f\n",f_pd);
  349. N_div = freq / f_pd;
  350. uint32_t N = (uint32_t) N_div;
  351. if (freq <= 12500e6) {
  352. if (N < 28){
  353. N= 28;
  354. };
  355. }
  356. else if (freq > 12500e6) {
  357. if (N <32) {
  358. N = 32;
  359. }
  360. };
  361. int vco_core;
  362. double f_coremin;
  363. double f_coremax;
  364. int c_core_min;
  365. int c_core_max;
  366. int a_core_min;
  367. int a_core_max;
  368. uint16_t vco_cap_ctrl_strt;
  369. uint16_t vco_daciset_strt;
  370. // Partial assist for the calibration
  371. //Determine a VCO core and other parameters
  372. if (freq >= 7500e6 && freq <= 8600e6) {
  373. vco_core = 1;
  374. f_coremin = 7500e6;
  375. f_coremax = 8600e6;
  376. c_core_min = 164;
  377. c_core_max = 12;
  378. a_core_min = 299;
  379. a_core_max = 240;
  380. }
  381. else if (freq > 8600e6 && freq < 9800e6) {
  382. vco_core = 2;
  383. f_coremin = 8600e6;
  384. f_coremax = 9800e6;
  385. c_core_min = 165;
  386. c_core_max = 16;
  387. a_core_min = 356;
  388. a_core_max = 247;
  389. }
  390. else if (freq >= 9800e6 && freq <= 10800e6) {
  391. vco_core = 3;
  392. f_coremin = 9800e6;
  393. f_coremax = 10800e6;
  394. c_core_min = 158;
  395. c_core_max = 19;
  396. a_core_min = 324;
  397. a_core_max = 224;
  398. }
  399. else if (freq > 10800e6 && freq <= 12000e6) {
  400. vco_core = 4;
  401. f_coremin = 10800e6;
  402. f_coremax = 12000e6;
  403. c_core_min = 140;
  404. c_core_max = 0;
  405. a_core_min = 383;
  406. a_core_max = 244;
  407. }
  408. else if (freq > 12000e6 && freq <= 12900e6) {
  409. vco_core = 5;
  410. f_coremin = 12000e6;
  411. f_coremax = 12900e6;
  412. c_core_min = 183;
  413. c_core_max = 36;
  414. a_core_min = 205;
  415. a_core_max = 146;
  416. }
  417. else if (freq > 12900e6 && freq <= 13900e6) {
  418. vco_core = 6;
  419. f_coremin = 12900e6;
  420. f_coremax = 13900e6;
  421. c_core_min = 155;
  422. c_core_max = 6;
  423. a_core_min = 242;
  424. a_core_max = 163;
  425. }
  426. else if (freq > 13900e6 && freq <= 15000e6) {
  427. vco_core = 7;
  428. f_coremin = 13900e6;
  429. f_coremax = 15000e6;
  430. c_core_min = 175;
  431. c_core_max = 19;
  432. a_core_min = 323;
  433. a_core_max = 244;
  434. };
  435. if (freq >=11900e6 && freq <=12100e6) {
  436. vco_daciset_strt = 300;
  437. vco_core = 4;
  438. vco_cap_ctrl_strt = 1;
  439. }
  440. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  441. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (freq - f_coremin) / (f_coremax - f_coremin));
  442. printf("VCO_CORE = %d\n", vco_core);
  443. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  444. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  445. //Set the VCO_CORE
  446. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  447. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  448. // Set the VCO_CAP_CTRL
  449. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  450. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  451. // Set the VCO_DACISET
  452. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  453. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  454. // Set the PF_DLY_SEL
  455. if (freq <= 12500e6) {
  456. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  457. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  458. printf("PFD_DLY_SEL = %d\n", 1);
  459. }
  460. else if (freq > 12500e6) {
  461. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  462. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  463. printf("PFD_DLY_SEL = %d\n", 2);
  464. };
  465. int cal_clk_div;
  466. //SET the FCAL_HPFD_ADJ
  467. if (f_pd <= 100e6) {
  468. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  469. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  470. }
  471. else if (f_pd > 100e6 && f_pd <= 150e6) {
  472. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  473. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  474. }
  475. else if (f_pd > 150e6 && f_pd <= 200e6) {
  476. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  477. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  478. }
  479. else if (f_pd > 200e6) {
  480. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  481. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  482. };
  483. // SET the CAL_CLK_DIV value
  484. if (f_pd <= 200e6 ) {
  485. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  486. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  487. cal_clk_div = 0;
  488. }
  489. else if (f_pd > 200e6 && f_pd <= 400e6) {
  490. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  491. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  492. cal_clk_div = 1;
  493. }
  494. else if (f_pd > 400e6 && f_pd < 800e6) {
  495. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  496. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  497. cal_clk_div = 2;
  498. };
  499. //Calculate the ACAL_CMP_DLY
  500. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  501. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
  502. //Set the ACAL_CMP_DLY value
  503. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  504. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  505. // SET the N_DIV
  506. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  507. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  508. //CLear the lower 16 bits of the register
  509. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  510. // Next 16 bits of the register
  511. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  512. // Clear the SEG1_EN bit
  513. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  514. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  515. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  516. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  517. // Program the FCAL_EN bit
  518. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  519. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  520. uint32_t lmx_change_freq_regs[] = {
  521. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  522. lmx2594regs[112 - VCO_SEL],
  523. lmx2594regs[112 - CAP_CTRL_START],
  524. lmx2594regs[112 - VCO_DACISET],
  525. lmx2594regs[112-PFD_DLY_SEL],
  526. lmx2594regs[112-R4_ADDR],
  527. lmx2594regs[112-R1_ADDR],
  528. lmx2594regs[112-CHDIV_DIV2],
  529. lmx2594regs[112-PLL_N_S],
  530. lmx2594regs[112-PLL_N_M],
  531. lmx2594regs[112 - OUTA_MUX],
  532. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  533. lmx2594regs[112-FCAL_ADDR]
  534. };
  535. // Create a header for the LMX2594 with the appropriate number of words
  536. // uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  537. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  538. uint32_t LMX_HEADER = ((ENUM_SPIMODE_4MOSI << 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  539. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  540. *ptr = LMX_HEADER;
  541. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  542. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  543. *data_ptr = lmx_change_freq_regs[i];
  544. }
  545. char filename[100];
  546. sprintf(filename, "%f.txt", freq);
  547. FILE * f = fopen(filename, "w");
  548. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  549. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  550. }
  551. fclose(f);
  552. printf("N_div = %f\n", N_div);
  553. printf("N = %d\n", N);
  554. printf("f_vco = %f\n", freq);
  555. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  556. return 0;
  557. }
  558. int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
  559. if (freq >= 10e6 && freq <= 1000e6) {
  560. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  561. }
  562. else {
  563. lmx_freq = freq;
  564. }
  565. double f_vco = 2 * lmx_freq;
  566. int chan_div = 2;
  567. uint8_t ch_div_reg = 0; // 2
  568. double vco_div = 7.5e9 / lmx_freq;
  569. double N_div;
  570. int vco_core;
  571. double f_coremin;
  572. double f_coremax;
  573. int c_core_min;
  574. int c_core_max;
  575. int a_core_min;
  576. int a_core_max;
  577. uint16_t vco_cap_ctrl_strt;
  578. uint16_t vco_daciset_strt;
  579. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  580. if (f_vco < 7.5e9) {
  581. if (vco_div > 2 && vco_div <= 4)
  582. chan_div = 4; // 4
  583. f_vco = lmx_freq * chan_div;
  584. if (vco_div > 4 && vco_div <= 6) {
  585. chan_div = 6; // 6
  586. f_vco = lmx_freq * chan_div;
  587. }
  588. if (vco_div > 6 && vco_div <= 8) {
  589. chan_div = 8; // 8
  590. f_vco = lmx_freq * chan_div;
  591. }
  592. if (vco_div > 8 && vco_div <= 12) {
  593. chan_div = 12; // 12
  594. f_vco = lmx_freq * chan_div;
  595. }
  596. if (vco_div > 12 && vco_div <= 16) {
  597. chan_div = 16; // 16
  598. f_vco = lmx_freq * chan_div;
  599. }
  600. if (vco_div > 16 && vco_div <= 24) {
  601. chan_div = 24; // 24
  602. f_vco = lmx_freq * chan_div;
  603. }
  604. if (vco_div > 24 && vco_div <= 32) {
  605. chan_div = 32; // 32
  606. f_vco = lmx_freq * chan_div;
  607. }
  608. if (vco_div > 32 && vco_div <= 48) {
  609. chan_div = 48; // 48
  610. f_vco = lmx_freq * chan_div;
  611. }
  612. if (vco_div > 48 && vco_div <= 64) {
  613. chan_div = 64; // 64
  614. f_vco = lmx_freq * chan_div;
  615. }
  616. if (vco_div > 64 && vco_div <= 72) {
  617. chan_div = 72; // 72
  618. f_vco = lmx_freq * chan_div;
  619. }
  620. if (vco_div > 72 && vco_div <= 96) {
  621. chan_div = 96; // 96
  622. f_vco = lmx_freq * chan_div;
  623. }
  624. if (vco_div > 96 && vco_div <= 128) {
  625. chan_div = 128; // 128
  626. f_vco = lmx_freq * chan_div;
  627. }
  628. if (vco_div > 128 && vco_div <= 192) {
  629. chan_div = 192; // 192
  630. f_vco = lmx_freq * chan_div;
  631. }
  632. if (vco_div > 192 && vco_div <= 256) {
  633. chan_div = 256; // 256
  634. f_vco = lmx_freq * chan_div;
  635. }
  636. if (vco_div > 256 && vco_div <= 384) {
  637. chan_div = 384; // 384
  638. f_vco = lmx_freq * chan_div;
  639. }
  640. if (vco_div > 384 && vco_div <= 512) {
  641. chan_div = 512; // 512
  642. f_vco = lmx_freq * chan_div;
  643. }
  644. if (vco_div > 512 && vco_div <= 768) {
  645. chan_div = 768; // 768
  646. f_vco = lmx_freq * chan_div;
  647. }
  648. switch (chan_div) {
  649. case 2:
  650. ch_div_reg = 0;
  651. break;
  652. case 4:
  653. ch_div_reg = 1;
  654. break;
  655. case 6:
  656. ch_div_reg = 2;
  657. break;
  658. case 8:
  659. ch_div_reg = 3;
  660. break;
  661. case 12:
  662. ch_div_reg = 4;
  663. break;
  664. case 16:
  665. ch_div_reg = 5;
  666. break;
  667. case 24:
  668. ch_div_reg = 6;
  669. break;
  670. case 32:
  671. ch_div_reg = 7;
  672. break;
  673. case 48:
  674. ch_div_reg = 8;
  675. break;
  676. case 64:
  677. ch_div_reg = 9;
  678. break;
  679. case 72:
  680. ch_div_reg = 10;
  681. break;
  682. case 96:
  683. ch_div_reg = 11;
  684. break;
  685. case 128:
  686. ch_div_reg = 12;
  687. break;
  688. case 192:
  689. ch_div_reg = 13;
  690. break;
  691. case 256:
  692. ch_div_reg = 14;
  693. break;
  694. case 384:
  695. ch_div_reg = 15;
  696. break;
  697. case 512:
  698. ch_div_reg = 16;
  699. break;
  700. case 768:
  701. ch_div_reg = 17;
  702. break;
  703. }
  704. } else {
  705. ch_div_reg = 0;
  706. f_vco = lmx_freq * 2;
  707. }
  708. N_div = f_vco / f_pd;
  709. // divide whole part and fractional part
  710. uint32_t N = (uint32_t) N_div;
  711. uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
  712. uint32_t frac_d = 4294967295-1;
  713. // If frac part is 0 then the denominator is 1000 and numerator is 0
  714. if (frac_n == 0) {
  715. frac_n = 0;
  716. frac_d = 1000;
  717. }
  718. // Partial assist for the calibration
  719. //Determine a VCO core and other parameters
  720. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  721. vco_core = 1;
  722. f_coremin = 7500e6;
  723. f_coremax = 8600e6;
  724. c_core_min = 164;
  725. c_core_max = 12;
  726. a_core_min = 299;
  727. a_core_max = 240;
  728. }
  729. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  730. vco_core = 2;
  731. f_coremin = 8600e6;
  732. f_coremax = 9800e6;
  733. c_core_min = 165;
  734. c_core_max = 16;
  735. a_core_min = 356;
  736. a_core_max = 247;
  737. }
  738. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  739. vco_core = 3;
  740. f_coremin = 9800e6;
  741. f_coremax = 10800e6;
  742. c_core_min = 158;
  743. c_core_max = 19;
  744. a_core_min = 324;
  745. a_core_max = 224;
  746. }
  747. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  748. vco_core = 4;
  749. f_coremin = 10800e6;
  750. f_coremax = 12000e6;
  751. c_core_min = 140;
  752. c_core_max = 0;
  753. a_core_min = 383;
  754. a_core_max = 244;
  755. }
  756. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  757. vco_core = 5;
  758. f_coremin = 12000e6;
  759. f_coremax = 12900e6;
  760. c_core_min = 183;
  761. c_core_max = 36;
  762. a_core_min = 205;
  763. a_core_max = 146;
  764. }
  765. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  766. vco_core = 6;
  767. f_coremin = 12900e6;
  768. f_coremax = 13900e6;
  769. c_core_min = 155;
  770. c_core_max = 6;
  771. a_core_min = 242;
  772. a_core_max = 163;
  773. }
  774. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  775. vco_core = 7;
  776. f_coremin = 13900e6;
  777. f_coremax = 15000e6;
  778. c_core_min = 175;
  779. c_core_max = 19;
  780. a_core_min = 323;
  781. a_core_max = 244;
  782. };
  783. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  784. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  785. if (f_vco >=11900e6 && f_vco <=12100e6) {
  786. vco_daciset_strt = 300;
  787. vco_core = 4;
  788. vco_cap_ctrl_strt = 1;
  789. }
  790. printf("VCO_CORE = %d\n", vco_core);
  791. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  792. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  793. // Calibration assist
  794. //Set the VCO_CORE
  795. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  796. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  797. // Set the VCO_CAP_CTRL_START
  798. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  799. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  800. // Set the VCO_DACISET
  801. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  802. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  803. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  804. // Set the MASH_ORDER to 3
  805. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  806. // Set PF_DLY_SEL to appropriate value
  807. if (f_vco <=10e9){
  808. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  809. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  810. printf("PFD_DLY_SEL = %d\n", 3);
  811. }
  812. else if (f_vco > 10e9) {
  813. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  814. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  815. printf("PFD_DLY_SEL = %d\n", 4);
  816. }
  817. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  818. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  819. //CLear the lower 16 bits of the register
  820. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  821. // Next 16 bits of the register
  822. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  823. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  824. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  825. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  826. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  827. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  828. // Next 16 bits of the numerator
  829. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  830. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  831. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  832. // most significant 16 bits of the denominator
  833. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  834. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  835. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  836. // Next 16 bits of the denominator
  837. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  838. // Program the CHDIV value
  839. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  840. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  841. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  842. // If the ch_div > 2 then set the SEG1_EN bit
  843. if (chan_div > 2) {
  844. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  845. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  846. }
  847. else {
  848. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  849. }
  850. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  851. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  852. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  853. // Program the FCAL_EN bit
  854. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  855. uint32_t lmx_change_freq_regs[] = {
  856. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  857. lmx2594regs[112 - VCO_SEL],
  858. lmx2594regs[112 - CAP_CTRL_START],
  859. lmx2594regs[112 - VCO_DACISET],
  860. lmx2594regs[112-MASH_ORDER],
  861. lmx2594regs[112-PFD_DLY_SEL],
  862. lmx2594regs[112 - PLL_N_S],
  863. lmx2594regs[112 - PLL_N_M],
  864. lmx2594regs[112 - PLL_DEN_S],
  865. lmx2594regs[112 - PLL_DEN_M],
  866. lmx2594regs[112 - PLL_NUM_S],
  867. lmx2594regs[112 - PLL_NUM_M],
  868. lmx2594regs[112 - CHDIV],
  869. lmx2594regs[112 - CHDIV_DIV2],
  870. lmx2594regs[112 - OUTA_MUX],
  871. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  872. lmx2594regs[112 - FCAL_ADDR]
  873. };
  874. // Create a header for the LMX2594 with the appropriate number of words
  875. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  876. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  877. *ptr = LMX_HEADER;
  878. // Send the data
  879. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  880. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  881. *data_ptr = lmx_change_freq_regs[i];
  882. }
  883. char filename[100];
  884. sprintf(filename, "%f.txt", freq);
  885. FILE * f = fopen(filename, "w");
  886. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  887. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  888. }
  889. fclose(f);
  890. printf("N_div = %f\n", N_div);
  891. printf("f_vco = %f\n", f_vco);
  892. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  893. printf("N = %d\n", N);
  894. printf("frac_n = %u\n", frac_n);
  895. printf("frac_d = %u\n", frac_d);
  896. printf("chan_div = %d\n", chan_div);
  897. printf("chan_div_reg = %d\n", ch_div_reg);
  898. printf("LMX2594 Registers\n");
  899. return 0;
  900. }
  901. int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
  902. if (freq >= 10e6 && freq <= 1000e6) {
  903. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  904. }
  905. else {
  906. lmx_freq = freq;
  907. }
  908. double f_vco = 2 * lmx_freq;
  909. int chan_div = 2;
  910. uint8_t ch_div_reg = 0; // 2
  911. double vco_div = 7.5e9 / lmx_freq;
  912. int vco_core;
  913. double f_coremin;
  914. double f_coremax;
  915. int c_core_min;
  916. int c_core_max;
  917. int a_core_min;
  918. int a_core_max;
  919. uint16_t vco_cap_ctrl_strt;
  920. uint16_t vco_daciset_strt;
  921. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  922. if (f_vco < 7.5e9) {
  923. if (vco_div > 2 && vco_div <= 4)
  924. chan_div = 4; // 4
  925. f_vco = lmx_freq * chan_div;
  926. if (vco_div > 4 && vco_div <= 6) {
  927. chan_div = 6; // 6
  928. f_vco = lmx_freq * chan_div;
  929. }
  930. if (vco_div > 6 && vco_div <= 8) {
  931. chan_div = 8; // 8
  932. f_vco = lmx_freq * chan_div;
  933. }
  934. if (vco_div > 8 && vco_div <= 12) {
  935. chan_div = 12; // 12
  936. f_vco = lmx_freq * chan_div;
  937. }
  938. if (vco_div > 12 && vco_div <= 16) {
  939. chan_div = 16; // 16
  940. f_vco = lmx_freq * chan_div;
  941. }
  942. if (vco_div > 16 && vco_div <= 24) {
  943. chan_div = 24; // 24
  944. f_vco = lmx_freq * chan_div;
  945. }
  946. if (vco_div > 24 && vco_div <= 32) {
  947. chan_div = 32; // 32
  948. f_vco = lmx_freq * chan_div;
  949. }
  950. if (vco_div > 32 && vco_div <= 48) {
  951. chan_div = 48; // 48
  952. f_vco = lmx_freq * chan_div;
  953. }
  954. if (vco_div > 48 && vco_div <= 64) {
  955. chan_div = 64; // 64
  956. f_vco = lmx_freq * chan_div;
  957. }
  958. if (vco_div > 64 && vco_div <= 72) {
  959. chan_div = 72; // 72
  960. f_vco = lmx_freq * chan_div;
  961. }
  962. if (vco_div > 72 && vco_div <= 96) {
  963. chan_div = 96; // 96
  964. f_vco = lmx_freq * chan_div;
  965. }
  966. if (vco_div > 96 && vco_div <= 128) {
  967. chan_div = 128; // 128
  968. f_vco = lmx_freq * chan_div;
  969. }
  970. if (vco_div > 128 && vco_div <= 192) {
  971. chan_div = 192; // 192
  972. f_vco = lmx_freq * chan_div;
  973. }
  974. if (vco_div > 192 && vco_div <= 256) {
  975. chan_div = 256; // 256
  976. f_vco = lmx_freq * chan_div;
  977. }
  978. if (vco_div > 256 && vco_div <= 384) {
  979. chan_div = 384; // 384
  980. f_vco = lmx_freq * chan_div;
  981. }
  982. if (vco_div > 384 && vco_div <= 512) {
  983. chan_div = 512; // 512
  984. f_vco = lmx_freq * chan_div;
  985. }
  986. if (vco_div > 512 && vco_div <= 768) {
  987. chan_div = 768; // 768
  988. f_vco = lmx_freq * chan_div;
  989. }
  990. switch (chan_div) {
  991. case 2:
  992. ch_div_reg = 0;
  993. break;
  994. case 4:
  995. ch_div_reg = 1;
  996. break;
  997. case 6:
  998. ch_div_reg = 2;
  999. break;
  1000. case 8:
  1001. ch_div_reg = 3;
  1002. break;
  1003. case 12:
  1004. ch_div_reg = 4;
  1005. break;
  1006. case 16:
  1007. ch_div_reg = 5;
  1008. break;
  1009. case 24:
  1010. ch_div_reg = 6;
  1011. break;
  1012. case 32:
  1013. ch_div_reg = 7;
  1014. break;
  1015. case 48:
  1016. ch_div_reg = 8;
  1017. break;
  1018. case 64:
  1019. ch_div_reg = 9;
  1020. break;
  1021. case 72:
  1022. ch_div_reg = 10;
  1023. break;
  1024. case 96:
  1025. ch_div_reg = 11;
  1026. break;
  1027. case 128:
  1028. ch_div_reg = 12;
  1029. break;
  1030. case 192:
  1031. ch_div_reg = 13;
  1032. break;
  1033. case 256:
  1034. ch_div_reg = 14;
  1035. break;
  1036. case 384:
  1037. ch_div_reg = 15;
  1038. break;
  1039. case 512:
  1040. ch_div_reg = 16;
  1041. break;
  1042. case 768:
  1043. ch_div_reg = 17;
  1044. break;
  1045. }
  1046. } else {
  1047. ch_div_reg = 0;
  1048. f_vco = lmx_freq * 2;
  1049. }
  1050. double N_div = f_vco / f_pd;
  1051. uint32_t N = (uint32_t) N_div;
  1052. if (f_vco <= 12500e6) {
  1053. if (N < 28){
  1054. N= 28;
  1055. };
  1056. }
  1057. else if (f_vco > 12500e6) {
  1058. if (N <32) {
  1059. N = 32;
  1060. }
  1061. };
  1062. // Partial assist for the calibration
  1063. //Determine a VCO core and other parameters
  1064. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  1065. vco_core = 1;
  1066. f_coremin = 7500e6;
  1067. f_coremax = 8600e6;
  1068. c_core_min = 164;
  1069. c_core_max = 12;
  1070. a_core_min = 299;
  1071. a_core_max = 240;
  1072. }
  1073. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  1074. vco_core = 2;
  1075. f_coremin = 8600e6;
  1076. f_coremax = 9800e6;
  1077. c_core_min = 165;
  1078. c_core_max = 16;
  1079. a_core_min = 356;
  1080. a_core_max = 247;
  1081. }
  1082. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  1083. vco_core = 3;
  1084. f_coremin = 9800e6;
  1085. f_coremax = 10800e6;
  1086. c_core_min = 158;
  1087. c_core_max = 19;
  1088. a_core_min = 324;
  1089. a_core_max = 224;
  1090. }
  1091. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  1092. vco_core = 4;
  1093. f_coremin = 10800e6;
  1094. f_coremax = 12000e6;
  1095. c_core_min = 140;
  1096. c_core_max = 0;
  1097. a_core_min = 383;
  1098. a_core_max = 244;
  1099. }
  1100. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  1101. vco_core = 5;
  1102. f_coremin = 12000e6;
  1103. f_coremax = 12900e6;
  1104. c_core_min = 183;
  1105. c_core_max = 36;
  1106. a_core_min = 205;
  1107. a_core_max = 146;
  1108. }
  1109. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  1110. vco_core = 6;
  1111. f_coremin = 12900e6;
  1112. f_coremax = 13900e6;
  1113. c_core_min = 155;
  1114. c_core_max = 6;
  1115. a_core_min = 242;
  1116. a_core_max = 163;
  1117. }
  1118. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  1119. vco_core = 7;
  1120. f_coremin = 13900e6;
  1121. f_coremax = 15000e6;
  1122. c_core_min = 175;
  1123. c_core_max = 19;
  1124. a_core_min = 323;
  1125. a_core_max = 244;
  1126. };
  1127. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1128. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1129. if (f_vco >=11900e6 && f_vco <=12100e6) {
  1130. vco_daciset_strt = 300;
  1131. vco_core = 4;
  1132. vco_cap_ctrl_strt = 1;
  1133. }
  1134. printf("VCO_CORE = %d\n", vco_core);
  1135. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  1136. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  1137. // Calibration assist
  1138. //Set the VCO_CORE
  1139. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  1140. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  1141. // Set the VCO_CAP_CTRL_START
  1142. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  1143. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  1144. // Set the VCO_DACISET
  1145. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  1146. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  1147. // Set the PFD_DLY_SEL to appropriate value
  1148. if (freq <= 12500e6) {
  1149. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  1150. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1151. printf("PFD_DLY_SEL = %d\n", 1);
  1152. }
  1153. else if (freq > 12500e6) {
  1154. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  1155. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1156. printf("PFD_DLY_SEL = %d\n", 2);
  1157. }
  1158. if (f_pd <= 100e6) {
  1159. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  1160. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  1161. }
  1162. else if (f_pd > 100e6 && f_pd <= 150e6) {
  1163. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  1164. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  1165. }
  1166. else if (f_pd > 150e6 && f_pd <= 200e6) {
  1167. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  1168. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  1169. }
  1170. else if (f_pd > 200e6) {
  1171. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  1172. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  1173. };
  1174. // SET the CAL_CLK_DIV value
  1175. int cal_clk_div;
  1176. if (f_pd <= 200e6 ) {
  1177. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  1178. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  1179. cal_clk_div =0;
  1180. }
  1181. else if (f_pd > 200e6 && f_pd <= 400e6) {
  1182. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  1183. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  1184. cal_clk_div =1;
  1185. }
  1186. else if (f_pd > 400e6 && f_pd < 800e6) {
  1187. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  1188. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  1189. cal_clk_div = 2;
  1190. };
  1191. //Calculate the ACAL_CMP_DLY
  1192. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  1193. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));;
  1194. //Set the ACAL_CMP_DLY value
  1195. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  1196. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  1197. // Set the N value
  1198. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  1199. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  1200. //CLear the lower 16 bits of the register
  1201. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  1202. // Next 16 bits of the register
  1203. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  1204. // Program the CHDIV value
  1205. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  1206. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  1207. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  1208. // If the ch_div > 2 then set the SEG1_EN bit
  1209. if (chan_div > 2) {
  1210. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1211. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  1212. }
  1213. else {
  1214. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1215. }
  1216. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  1217. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  1218. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  1219. // Program the FCAL_EN bit
  1220. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  1221. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  1222. uint32_t lmx_change_freq_regs []={
  1223. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  1224. lmx2594regs[112 - VCO_SEL],
  1225. lmx2594regs[112 - CAP_CTRL_START],
  1226. lmx2594regs[112 - VCO_DACISET],
  1227. lmx2594regs[112-PFD_DLY_SEL],
  1228. lmx2594regs[112-R4_ADDR],
  1229. lmx2594regs[112-R1_ADDR],
  1230. lmx2594regs[112 - PLL_N_S],
  1231. lmx2594regs[112 - PLL_N_M],
  1232. lmx2594regs[112 - CHDIV],
  1233. lmx2594regs[112 - CHDIV_DIV2],
  1234. lmx2594regs[112 - OUTA_MUX],
  1235. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  1236. lmx2594regs[112 - FCAL_ADDR]
  1237. };
  1238. // Create a header for the LMX2594 with the appropriate number of words
  1239. // uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  1240. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  1241. uint32_t LMX_HEADER = ((ENUM_SPIMODE_4MOSI << 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  1242. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  1243. *ptr = LMX_HEADER;
  1244. // Send the data
  1245. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  1246. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  1247. *data_ptr = lmx_change_freq_regs[i];
  1248. }
  1249. char filename[100];
  1250. sprintf(filename, "%f.txt", freq);
  1251. FILE * f = fopen(filename, "w");
  1252. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  1253. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  1254. }
  1255. fclose(f);
  1256. printf("N_div = %f\n", N_div);
  1257. printf("f_vco = %f\n", f_vco);
  1258. printf("N = %d\n", N);
  1259. printf("chan_div = %d\n", chan_div);
  1260. printf("chan_div_reg = %d\n", ch_div_reg);
  1261. return 0;
  1262. }
  1263. double lmx_lower_bond_set (double freq, double f_pd) {
  1264. double f_max2870 = 4e9;
  1265. double lmx_req_freq = f_max2870-freq; // 4 GHz - freq
  1266. return lmx_req_freq;
  1267. }
  1268. int lmx_freq_set(void *bar1, double freq,double f_pd) {
  1269. // double f_pd = 175e6;
  1270. // Set the 4 Mosi mode
  1271. uint32_t *spi_mode = bar1 +RST_ADDR;
  1272. *spi_mode = SPI_MODE_4MOSI;
  1273. double N_div = 0;
  1274. if (freq < 10e6 || freq > 15e9) {
  1275. printf("Frequency range is 10 MHz to 15 GHz\n");
  1276. return -1;
  1277. }
  1278. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  1279. if (freq >= 7.5e9 && freq <= 15e9) {
  1280. // lmx_freq_set_main_band(bar1, freq, f_pd);
  1281. lmx_freq_set_main_band_int_mode(bar1, freq, f_pd);
  1282. }
  1283. else if (freq < 7.5e9) {
  1284. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  1285. lmx_freq_set_out_of_band_int_mode(bar1, freq, f_pd);
  1286. }
  1287. // Switch the keys
  1288. key_switch(bar1, freq,lmx_freq);
  1289. usleep(1);
  1290. // Return the 1 MOSI mode
  1291. *spi_mode = SPI_MODE_1MOSI;
  1292. return 0;
  1293. }
  1294. uint32_t lmx_ld_status(void *bar1) {
  1295. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  1296. uint32_t read_value = *read_ptr;
  1297. return read_value;
  1298. }