lmx2594.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. 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. // Clear the SEG1_EN bit
  461. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  462. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  463. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  464. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  465. // Program the FCAL_EN bit
  466. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  467. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  468. uint32_t lmx_change_freq_regs[] = {
  469. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  470. lmx2594regs[112 - VCO_SEL],
  471. lmx2594regs[112 - CAP_CTRL_START],
  472. lmx2594regs[112 - VCO_DACISET],
  473. lmx2594regs[112-PFD_DLY_SEL],
  474. lmx2594regs[112-CHDIV_DIV2],
  475. lmx2594regs[112-PLL_N_S],
  476. lmx2594regs[112-PLL_N_M],
  477. lmx2594regs[112 - OUTA_MUX],
  478. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  479. lmx2594regs[112-FCAL_ADDR]
  480. };
  481. // Create a header for the LMX2594 with the appropriate number of words
  482. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  483. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  484. *ptr = LMX_HEADER;
  485. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  486. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  487. *data_ptr = lmx_change_freq_regs[i];
  488. }
  489. char filename[100];
  490. sprintf(filename, "%f.txt", freq);
  491. FILE * f = fopen(filename, "w");
  492. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  493. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  494. }
  495. fclose(f);
  496. return 0;
  497. }
  498. int lmx_freq_set_out_of_band(void *bar1, double freq, double f_pd) {
  499. if (freq >= 10e6 && freq <= 1000e6) {
  500. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  501. }
  502. else {
  503. lmx_freq = freq;
  504. }
  505. double f_vco = 2 * lmx_freq;
  506. int chan_div = 2;
  507. uint8_t ch_div_reg = 0; // 2
  508. double vco_div = 7.5e9 / lmx_freq;
  509. double N_div;
  510. int vco_core;
  511. double f_coremin;
  512. double f_coremax;
  513. int c_core_min;
  514. int c_core_max;
  515. int a_core_min;
  516. int a_core_max;
  517. uint16_t vco_cap_ctrl_strt;
  518. uint16_t vco_daciset_strt;
  519. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  520. if (f_vco < 7.5e9) {
  521. if (vco_div > 2 && vco_div <= 4)
  522. chan_div = 4; // 4
  523. f_vco = lmx_freq * chan_div;
  524. if (vco_div > 4 && vco_div <= 6) {
  525. chan_div = 6; // 6
  526. f_vco = lmx_freq * chan_div;
  527. }
  528. if (vco_div > 6 && vco_div <= 8) {
  529. chan_div = 8; // 8
  530. f_vco = lmx_freq * chan_div;
  531. }
  532. if (vco_div > 8 && vco_div <= 12) {
  533. chan_div = 12; // 12
  534. f_vco = lmx_freq * chan_div;
  535. }
  536. if (vco_div > 12 && vco_div <= 16) {
  537. chan_div = 16; // 16
  538. f_vco = lmx_freq * chan_div;
  539. }
  540. if (vco_div > 16 && vco_div <= 24) {
  541. chan_div = 24; // 24
  542. f_vco = lmx_freq * chan_div;
  543. }
  544. if (vco_div > 24 && vco_div <= 32) {
  545. chan_div = 32; // 32
  546. f_vco = lmx_freq * chan_div;
  547. }
  548. if (vco_div > 32 && vco_div <= 48) {
  549. chan_div = 48; // 48
  550. f_vco = lmx_freq * chan_div;
  551. }
  552. if (vco_div > 48 && vco_div <= 64) {
  553. chan_div = 64; // 64
  554. f_vco = lmx_freq * chan_div;
  555. }
  556. if (vco_div > 64 && vco_div <= 72) {
  557. chan_div = 72; // 72
  558. f_vco = lmx_freq * chan_div;
  559. }
  560. if (vco_div > 72 && vco_div <= 96) {
  561. chan_div = 96; // 96
  562. f_vco = lmx_freq * chan_div;
  563. }
  564. if (vco_div > 96 && vco_div <= 128) {
  565. chan_div = 128; // 128
  566. f_vco = lmx_freq * chan_div;
  567. }
  568. if (vco_div > 128 && vco_div <= 192) {
  569. chan_div = 192; // 192
  570. f_vco = lmx_freq * chan_div;
  571. }
  572. if (vco_div > 192 && vco_div <= 256) {
  573. chan_div = 256; // 256
  574. f_vco = lmx_freq * chan_div;
  575. }
  576. if (vco_div > 256 && vco_div <= 384) {
  577. chan_div = 384; // 384
  578. f_vco = lmx_freq * chan_div;
  579. }
  580. if (vco_div > 384 && vco_div <= 512) {
  581. chan_div = 512; // 512
  582. f_vco = lmx_freq * chan_div;
  583. }
  584. if (vco_div > 512 && vco_div <= 768) {
  585. chan_div = 768; // 768
  586. f_vco = lmx_freq * chan_div;
  587. }
  588. switch (chan_div) {
  589. case 2:
  590. ch_div_reg = 0;
  591. break;
  592. case 4:
  593. ch_div_reg = 1;
  594. break;
  595. case 6:
  596. ch_div_reg = 2;
  597. break;
  598. case 8:
  599. ch_div_reg = 3;
  600. break;
  601. case 12:
  602. ch_div_reg = 4;
  603. break;
  604. case 16:
  605. ch_div_reg = 5;
  606. break;
  607. case 24:
  608. ch_div_reg = 6;
  609. break;
  610. case 32:
  611. ch_div_reg = 7;
  612. break;
  613. case 48:
  614. ch_div_reg = 8;
  615. break;
  616. case 64:
  617. ch_div_reg = 9;
  618. break;
  619. case 72:
  620. ch_div_reg = 10;
  621. break;
  622. case 96:
  623. ch_div_reg = 11;
  624. break;
  625. case 128:
  626. ch_div_reg = 12;
  627. break;
  628. case 192:
  629. ch_div_reg = 13;
  630. break;
  631. case 256:
  632. ch_div_reg = 14;
  633. break;
  634. case 384:
  635. ch_div_reg = 15;
  636. break;
  637. case 512:
  638. ch_div_reg = 16;
  639. break;
  640. case 768:
  641. ch_div_reg = 17;
  642. break;
  643. }
  644. } else {
  645. ch_div_reg = 0;
  646. f_vco = lmx_freq * 2;
  647. }
  648. N_div = f_vco / f_pd;
  649. // divide whole part and fractional part
  650. uint32_t N = (uint32_t) N_div;
  651. uint32_t frac_n = (uint32_t) ((N_div - N) * (4294967295-1));
  652. uint32_t frac_d = 4294967295-1;
  653. // If frac part is 0 then the denominator is 1000 and numerator is 0
  654. if (frac_n == 0) {
  655. frac_n = 0;
  656. frac_d = 1000;
  657. }
  658. // Partial assist for the calibration
  659. //Determine a VCO core and other parameters
  660. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  661. vco_core = 1;
  662. f_coremin = 7500e6;
  663. f_coremax = 8600e6;
  664. c_core_min = 164;
  665. c_core_max = 12;
  666. a_core_min = 299;
  667. a_core_max = 240;
  668. }
  669. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  670. vco_core = 2;
  671. f_coremin = 8600e6;
  672. f_coremax = 9800e6;
  673. c_core_min = 165;
  674. c_core_max = 16;
  675. a_core_min = 356;
  676. a_core_max = 247;
  677. }
  678. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  679. vco_core = 3;
  680. f_coremin = 9800e6;
  681. f_coremax = 10800e6;
  682. c_core_min = 158;
  683. c_core_max = 19;
  684. a_core_min = 324;
  685. a_core_max = 224;
  686. }
  687. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  688. vco_core = 4;
  689. f_coremin = 10800e6;
  690. f_coremax = 12000e6;
  691. c_core_min = 140;
  692. c_core_max = 0;
  693. a_core_min = 383;
  694. a_core_max = 244;
  695. }
  696. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  697. vco_core = 5;
  698. f_coremin = 12000e6;
  699. f_coremax = 12900e6;
  700. c_core_min = 183;
  701. c_core_max = 36;
  702. a_core_min = 205;
  703. a_core_max = 146;
  704. }
  705. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  706. vco_core = 6;
  707. f_coremin = 12900e6;
  708. f_coremax = 13900e6;
  709. c_core_min = 155;
  710. c_core_max = 6;
  711. a_core_min = 242;
  712. a_core_max = 163;
  713. }
  714. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  715. vco_core = 7;
  716. f_coremin = 13900e6;
  717. f_coremax = 15000e6;
  718. c_core_min = 175;
  719. c_core_max = 19;
  720. a_core_min = 323;
  721. a_core_max = 244;
  722. };
  723. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  724. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  725. if (f_vco >=11900e6 && f_vco <=12100e6) {
  726. vco_daciset_strt = 300;
  727. vco_core = 4;
  728. vco_cap_ctrl_strt = 1;
  729. }
  730. printf("VCO_CORE = %d\n", vco_core);
  731. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  732. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  733. // Calibration assist
  734. //Set the VCO_CORE
  735. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  736. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  737. // Set the VCO_CAP_CTRL_START
  738. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  739. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  740. // Set the VCO_DACISET
  741. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  742. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  743. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  744. // Set the MASH_ORDER to 3
  745. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  746. // Set PF_DLY_SEL to appropriate value
  747. 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] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  750. printf("PFD_DLY_SEL = %d\n", 3);
  751. }
  752. else if (f_vco > 10e9) {
  753. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  754. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x4 << BITP_LMX2594_R37_PFD_DLY_SEL);
  755. printf("PFD_DLY_SEL = %d\n", 4);
  756. }
  757. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  758. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  759. //CLear the lower 16 bits of the register
  760. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  761. // Next 16 bits of the register
  762. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  763. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  764. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  765. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  766. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  767. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  768. // Next 16 bits of the numerator
  769. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  770. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  771. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  772. // most significant 16 bits of the denominator
  773. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  774. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  775. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  776. // Next 16 bits of the denominator
  777. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  778. // Program the CHDIV value
  779. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  780. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  781. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  782. // If the ch_div > 2 then set the SEG1_EN bit
  783. if (chan_div > 2) {
  784. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  785. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  786. }
  787. else {
  788. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  789. }
  790. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  791. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  792. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  793. // Program the FCAL_EN bit
  794. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  795. uint32_t lmx_change_freq_regs[] = {
  796. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  797. lmx2594regs[112 - VCO_SEL],
  798. lmx2594regs[112 - CAP_CTRL_START],
  799. lmx2594regs[112 - VCO_DACISET],
  800. lmx2594regs[112-MASH_ORDER],
  801. lmx2594regs[112-PFD_DLY_SEL],
  802. lmx2594regs[112 - PLL_N_S],
  803. lmx2594regs[112 - PLL_N_M],
  804. lmx2594regs[112 - PLL_DEN_S],
  805. lmx2594regs[112 - PLL_DEN_M],
  806. lmx2594regs[112 - PLL_NUM_S],
  807. lmx2594regs[112 - PLL_NUM_M],
  808. lmx2594regs[112 - CHDIV],
  809. lmx2594regs[112 - CHDIV_DIV2],
  810. lmx2594regs[112 - OUTA_MUX],
  811. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  812. lmx2594regs[112 - FCAL_ADDR]
  813. };
  814. // Create a header for the LMX2594 with the appropriate number of words
  815. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  816. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  817. *ptr = LMX_HEADER;
  818. // Send the data
  819. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  820. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  821. *data_ptr = lmx_change_freq_regs[i];
  822. }
  823. char filename[100];
  824. sprintf(filename, "%f.txt", freq);
  825. FILE * f = fopen(filename, "w");
  826. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  827. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  828. }
  829. fclose(f);
  830. printf("N_div = %f\n", N_div);
  831. printf("f_vco = %f\n", f_vco);
  832. printf("SEG1_EN %08X\n",lmx2594regs[112 - CHDIV_DIV2]);
  833. printf("N = %d\n", N);
  834. printf("frac_n = %u\n", frac_n);
  835. printf("frac_d = %u\n", frac_d);
  836. printf("chan_div = %d\n", chan_div);
  837. printf("chan_div_reg = %d\n", ch_div_reg);
  838. printf("LMX2594 Registers\n");
  839. return 0;
  840. }
  841. int lmx_freq_set_out_of_band_int_mode(void *bar1, double freq, double f_pd) {
  842. if (freq >= 10e6 && freq <= 1000e6) {
  843. lmx_freq = lmx_lower_bond_set(freq, f_pd);
  844. }
  845. else {
  846. lmx_freq = freq;
  847. }
  848. double f_vco = 2 * lmx_freq;
  849. int chan_div = 2;
  850. uint8_t ch_div_reg = 0; // 2
  851. double vco_div = 7.5e9 / lmx_freq;
  852. int vco_core;
  853. double f_coremin;
  854. double f_coremax;
  855. int c_core_min;
  856. int c_core_max;
  857. int a_core_min;
  858. int a_core_max;
  859. uint16_t vco_cap_ctrl_strt;
  860. uint16_t vco_daciset_strt;
  861. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  862. if (f_vco < 7.5e9) {
  863. if (vco_div > 2 && vco_div <= 4)
  864. chan_div = 4; // 4
  865. f_vco = lmx_freq * chan_div;
  866. if (vco_div > 4 && vco_div <= 6) {
  867. chan_div = 6; // 6
  868. f_vco = lmx_freq * chan_div;
  869. }
  870. if (vco_div > 6 && vco_div <= 8) {
  871. chan_div = 8; // 8
  872. f_vco = lmx_freq * chan_div;
  873. }
  874. if (vco_div > 8 && vco_div <= 12) {
  875. chan_div = 12; // 12
  876. f_vco = lmx_freq * chan_div;
  877. }
  878. if (vco_div > 12 && vco_div <= 16) {
  879. chan_div = 16; // 16
  880. f_vco = lmx_freq * chan_div;
  881. }
  882. if (vco_div > 16 && vco_div <= 24) {
  883. chan_div = 24; // 24
  884. f_vco = lmx_freq * chan_div;
  885. }
  886. if (vco_div > 24 && vco_div <= 32) {
  887. chan_div = 32; // 32
  888. f_vco = lmx_freq * chan_div;
  889. }
  890. if (vco_div > 32 && vco_div <= 48) {
  891. chan_div = 48; // 48
  892. f_vco = lmx_freq * chan_div;
  893. }
  894. if (vco_div > 48 && vco_div <= 64) {
  895. chan_div = 64; // 64
  896. f_vco = lmx_freq * chan_div;
  897. }
  898. if (vco_div > 64 && vco_div <= 72) {
  899. chan_div = 72; // 72
  900. f_vco = lmx_freq * chan_div;
  901. }
  902. if (vco_div > 72 && vco_div <= 96) {
  903. chan_div = 96; // 96
  904. f_vco = lmx_freq * chan_div;
  905. }
  906. if (vco_div > 96 && vco_div <= 128) {
  907. chan_div = 128; // 128
  908. f_vco = lmx_freq * chan_div;
  909. }
  910. if (vco_div > 128 && vco_div <= 192) {
  911. chan_div = 192; // 192
  912. f_vco = lmx_freq * chan_div;
  913. }
  914. if (vco_div > 192 && vco_div <= 256) {
  915. chan_div = 256; // 256
  916. f_vco = lmx_freq * chan_div;
  917. }
  918. if (vco_div > 256 && vco_div <= 384) {
  919. chan_div = 384; // 384
  920. f_vco = lmx_freq * chan_div;
  921. }
  922. if (vco_div > 384 && vco_div <= 512) {
  923. chan_div = 512; // 512
  924. f_vco = lmx_freq * chan_div;
  925. }
  926. if (vco_div > 512 && vco_div <= 768) {
  927. chan_div = 768; // 768
  928. f_vco = lmx_freq * chan_div;
  929. }
  930. switch (chan_div) {
  931. case 2:
  932. ch_div_reg = 0;
  933. break;
  934. case 4:
  935. ch_div_reg = 1;
  936. break;
  937. case 6:
  938. ch_div_reg = 2;
  939. break;
  940. case 8:
  941. ch_div_reg = 3;
  942. break;
  943. case 12:
  944. ch_div_reg = 4;
  945. break;
  946. case 16:
  947. ch_div_reg = 5;
  948. break;
  949. case 24:
  950. ch_div_reg = 6;
  951. break;
  952. case 32:
  953. ch_div_reg = 7;
  954. break;
  955. case 48:
  956. ch_div_reg = 8;
  957. break;
  958. case 64:
  959. ch_div_reg = 9;
  960. break;
  961. case 72:
  962. ch_div_reg = 10;
  963. break;
  964. case 96:
  965. ch_div_reg = 11;
  966. break;
  967. case 128:
  968. ch_div_reg = 12;
  969. break;
  970. case 192:
  971. ch_div_reg = 13;
  972. break;
  973. case 256:
  974. ch_div_reg = 14;
  975. break;
  976. case 384:
  977. ch_div_reg = 15;
  978. break;
  979. case 512:
  980. ch_div_reg = 16;
  981. break;
  982. case 768:
  983. ch_div_reg = 17;
  984. break;
  985. }
  986. } else {
  987. ch_div_reg = 0;
  988. f_vco = lmx_freq * 2;
  989. }
  990. uint32_t N_div = f_vco / f_pd;
  991. // Partial assist for the calibration
  992. //Determine a VCO core and other parameters
  993. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  994. vco_core = 1;
  995. f_coremin = 7500e6;
  996. f_coremax = 8600e6;
  997. c_core_min = 164;
  998. c_core_max = 12;
  999. a_core_min = 299;
  1000. a_core_max = 240;
  1001. }
  1002. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  1003. vco_core = 2;
  1004. f_coremin = 8600e6;
  1005. f_coremax = 9800e6;
  1006. c_core_min = 165;
  1007. c_core_max = 16;
  1008. a_core_min = 356;
  1009. a_core_max = 247;
  1010. }
  1011. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  1012. vco_core = 3;
  1013. f_coremin = 9800e6;
  1014. f_coremax = 10800e6;
  1015. c_core_min = 158;
  1016. c_core_max = 19;
  1017. a_core_min = 324;
  1018. a_core_max = 224;
  1019. }
  1020. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  1021. vco_core = 4;
  1022. f_coremin = 10800e6;
  1023. f_coremax = 12000e6;
  1024. c_core_min = 140;
  1025. c_core_max = 0;
  1026. a_core_min = 383;
  1027. a_core_max = 244;
  1028. }
  1029. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  1030. vco_core = 5;
  1031. f_coremin = 12000e6;
  1032. f_coremax = 12900e6;
  1033. c_core_min = 183;
  1034. c_core_max = 36;
  1035. a_core_min = 205;
  1036. a_core_max = 146;
  1037. }
  1038. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  1039. vco_core = 6;
  1040. f_coremin = 12900e6;
  1041. f_coremax = 13900e6;
  1042. c_core_min = 155;
  1043. c_core_max = 6;
  1044. a_core_min = 242;
  1045. a_core_max = 163;
  1046. }
  1047. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  1048. vco_core = 7;
  1049. f_coremin = 13900e6;
  1050. f_coremax = 15000e6;
  1051. c_core_min = 175;
  1052. c_core_max = 19;
  1053. a_core_min = 323;
  1054. a_core_max = 244;
  1055. };
  1056. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1057. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  1058. if (f_vco >=11900e6 && f_vco <=12100e6) {
  1059. vco_daciset_strt = 300;
  1060. vco_core = 4;
  1061. vco_cap_ctrl_strt = 1;
  1062. }
  1063. printf("VCO_CORE = %d\n", vco_core);
  1064. printf("VCO_CAP_CTRL_STR = %d\n", vco_cap_ctrl_strt);
  1065. printf("VCO_DACISET_STR = %d\n", vco_daciset_strt);
  1066. // Calibration assist
  1067. //Set the VCO_CORE
  1068. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  1069. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  1070. // Set the VCO_CAP_CTRL_START
  1071. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  1072. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  1073. // Set the VCO_DACISET
  1074. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  1075. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  1076. // Set the PFD_DLY_SEL to appropriate value
  1077. 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] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1080. printf("PFD_DLY_SEL = %d\n", 1);
  1081. }
  1082. else if (freq > 12500e6) {
  1083. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  1084. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  1085. printf("PFD_DLY_SEL = %d\n", 2);
  1086. }
  1087. // Set the N value
  1088. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  1089. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N_div >> 16);
  1090. // Clear the lower 16 bits of the register
  1091. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  1092. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N_div & 0xFFFF);
  1093. // Program the CHDIV value
  1094. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  1095. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  1096. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  1097. // If the ch_div > 2 then set the SEG1_EN bit
  1098. if (chan_div > 2) {
  1099. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1100. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  1101. }
  1102. else {
  1103. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  1104. }
  1105. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  1106. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  1107. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  1108. // Program the FCAL_EN bit
  1109. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  1110. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  1111. uint32_t lmx_change_freq_regs []={
  1112. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_TRISTATE,
  1113. lmx2594regs[112 - VCO_SEL],
  1114. lmx2594regs[112 - CAP_CTRL_START],
  1115. lmx2594regs[112 - VCO_DACISET],
  1116. lmx2594regs[112-PFD_DLY_SEL],
  1117. lmx2594regs[112 - PLL_N_S],
  1118. lmx2594regs[112 - PLL_N_M],
  1119. lmx2594regs[112 - CHDIV],
  1120. lmx2594regs[112 - CHDIV_DIV2],
  1121. lmx2594regs[112 - OUTA_MUX],
  1122. lmx2594regs[112-CPG_REG] = (lmx2594regs[112-CPG_REG] & (~BITM_LMX2594_R14_CPG)) | ENUM_LMX2594_R14_CPG_15ma,
  1123. lmx2594regs[112 - FCAL_ADDR]
  1124. };
  1125. // Create a header for the LMX2594 with the appropriate number of words
  1126. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  1127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  1128. *ptr = LMX_HEADER;
  1129. // Send the data
  1130. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  1131. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  1132. *data_ptr = lmx_change_freq_regs[i];
  1133. }
  1134. char filename[100];
  1135. sprintf(filename, "%f.txt", freq);
  1136. FILE * f = fopen(filename, "w");
  1137. for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  1138. fprintf(f, "0x%08X\n", lmx2594regs[i]);
  1139. }
  1140. fclose(f);
  1141. return 0;
  1142. }
  1143. double lmx_lower_bond_set (double freq, double f_pd) {
  1144. double f_max2870 = 4e9;
  1145. double lmx_req_freq = f_max2870-freq; // 4 GHz - freq
  1146. return lmx_req_freq;
  1147. }
  1148. int lmx_freq_set(void *bar1, double freq,double f_pd) {
  1149. // double f_pd = 175e6;
  1150. double N_div = 0;
  1151. if (freq < 10e6 || freq > 15e9) {
  1152. printf("Frequency range is 10 MHz to 15 GHz\n");
  1153. return -1;
  1154. }
  1155. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  1156. if (freq >= 7.5e9 && freq <= 15e9) {
  1157. // lmx_freq_set_main_band(bar1, freq, f_pd);
  1158. lmx_freq_set_main_band_int_mode(bar1, freq, f_pd);
  1159. }
  1160. else if (freq < 7.5e9) {
  1161. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  1162. lmx_freq_set_out_of_band_int_mode(bar1, freq, f_pd);
  1163. }
  1164. // Switch the keys
  1165. key_switch(bar1, freq,lmx_freq);
  1166. return 0;
  1167. }
  1168. uint32_t lmx_ld_status(void *bar1) {
  1169. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  1170. uint32_t read_value = *read_ptr;
  1171. return read_value;
  1172. }