lmx2594.c 48 KB

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