lmx2594.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. 0x4E0105,
  43. 0x4D0000,
  44. 0x4C000C,
  45. 0x4B0C40,
  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. 0x2E07FD,
  75. 0x2DC8DF,
  76. 0x2C1F20,
  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. 0x14D848,
  101. 0x1327B7,
  102. 0x120064,
  103. 0x110130,
  104. 0x100080,
  105. 0x0F064F,
  106. 0x0E1E40,
  107. 0x0D4000,
  108. 0x0C5001,
  109. 0x0B0018,
  110. 0x0A10D8,
  111. 0x090604,
  112. 0x082000,
  113. 0x0740B2,
  114. 0x06C802,
  115. 0x0500C8,
  116. 0x041443,
  117. 0x030642,
  118. 0x020500,
  119. 0x01080B,
  120. 0x00251C
  121. };
  122. struct vco_params calculate_vco_params (double lmx_freq, double f_pd) {
  123. struct vco_params params;
  124. if (lmx_freq >= 7500e6 && lmx_freq <= 8600e6) {
  125. params.vco_core = 1;
  126. params.f_coremin = 7500e6;
  127. params.f_coremax = 8600e6;
  128. params.c_core_min = 164;
  129. params.c_core_max = 12;
  130. params.a_core_min = 299;
  131. params.a_core_max = 240;
  132. }
  133. else if (lmx_freq > 8600e6 && lmx_freq < 9800e6) {
  134. params.vco_core = 2;
  135. params.f_coremin = 8600e6;
  136. params.f_coremax = 9800e6;
  137. params.c_core_min = 165;
  138. params.c_core_max = 16;
  139. params.a_core_min = 356;
  140. params.a_core_max = 247;
  141. }
  142. else if (lmx_freq >= 9800e6 && lmx_freq <= 10800e6) {
  143. params.vco_core = 3;
  144. params.f_coremin = 9800e6;
  145. params.f_coremax = 10800e6;
  146. params.c_core_min = 158;
  147. params.c_core_max = 19;
  148. params.a_core_min = 324;
  149. params.a_core_max = 224;
  150. }
  151. else if (lmx_freq > 10800e6 && lmx_freq <= 12000e6) {
  152. params.vco_core = 4;
  153. params.f_coremin = 10800e6;
  154. params.f_coremax = 12000e6;
  155. params.c_core_min = 140;
  156. params.c_core_max = 0;
  157. params.a_core_min = 383;
  158. params.a_core_max = 244;
  159. }
  160. else if (lmx_freq > 12000e6 && lmx_freq <= 12900e6) {
  161. params.vco_core = 5;
  162. params.f_coremin = 12000e6;
  163. params.f_coremax = 12900e6;
  164. params.c_core_min = 183;
  165. params.c_core_max = 36;
  166. params.a_core_min = 205;
  167. params.a_core_max = 146;
  168. }
  169. else if (lmx_freq > 12900e6 && lmx_freq <= 13900e6) {
  170. params.vco_core = 6;
  171. params.f_coremin = 12900e6;
  172. params.f_coremax = 13900e6;
  173. params.c_core_min = 155;
  174. params.c_core_max = 6;
  175. params.a_core_min = 242;
  176. params.a_core_max = 163;
  177. }
  178. else if (lmx_freq > 13900e6 && lmx_freq <= 15000e6) {
  179. params.vco_core = 7;
  180. params.f_coremin = 13900e6;
  181. params.f_coremax = 15000e6;
  182. params.c_core_min = 175;
  183. params.c_core_max = 19;
  184. params.a_core_min = 323;
  185. params.a_core_max = 244;
  186. }
  187. if (lmx_freq >=11900e6 && lmx_freq <=12100e6) {
  188. params.vco_daciset_strt = 300;
  189. params.vco_core = 4;
  190. params.vco_cap_ctrl_strt = 1;
  191. }
  192. params.vco_cap_ctrl_strt = round(params.c_core_min - (params.c_core_min - params.c_core_max) * (lmx_freq - params.f_coremin) / (params.f_coremax - params.f_coremin));
  193. params.vco_daciset_strt = round(params.a_core_min + (params.a_core_min - params.a_core_max) * (lmx_freq - params.f_coremin) / (params.f_coremax - params.f_coremin));
  194. if (lmx_freq <= 12500e6) {
  195. params.pfd_dly_sel = 1;
  196. }
  197. else if (lmx_freq > 12500e6) {
  198. params.pfd_dly_sel = 2;
  199. }
  200. if (f_pd <= 100e6) {
  201. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  202. }
  203. else if (f_pd > 100e6 && f_pd <= 150e6) {
  204. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  205. }
  206. else if (f_pd > 150e6 && f_pd <= 200e6) {
  207. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  208. }
  209. else if (f_pd > 200e6) {
  210. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  211. }
  212. // SET the CAL_CLK_DIV value
  213. if (f_pd <= 200e6 ) {
  214. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV1;
  215. }
  216. else if (f_pd > 200e6 && f_pd <= 400e6) {
  217. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV2;
  218. }
  219. else if (f_pd > 400e6 && f_pd < 800e6) {
  220. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV4;
  221. }
  222. // Calculate the ACAL_CMP_DLY
  223. double Fsmclk = f_pd/(pow(2,params.cal_clk_div));
  224. params.acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
  225. return params;
  226. }
  227. void set_vco_params (struct vco_params *params) {
  228. // Set the VCO_CORE
  229. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  230. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (params->vco_core << BITP_LMX2594_R20_VCO_SEL);
  231. // Set the VCO_CAP_CTRL_START
  232. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  233. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (params->vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  234. // Set the VCO_DACISET
  235. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  236. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (params->vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  237. // Set PFD_DLY_SEL
  238. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  239. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (params->pfd_dly_sel << BITP_LMX2594_R37_PFD_DLY_SEL);
  240. // Set the FCAL_HPFD_ADJ
  241. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  242. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | params->fcal_hpfd_adj;
  243. // SET the CAL_CLK_DIV value
  244. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  245. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | params->cal_clk_div;
  246. // Set the ACAL_CMP_DLY value
  247. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] & (~BITM_LMX2594_R4_ACAL_CMP_DLY);
  248. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (params->acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  249. }
  250. void auto_cal(void *bar1) {
  251. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  252. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | LMX2594_R0_FCAL_EN;
  253. uint32_t *ptr_header = bar1+LMX_BASE_ADDR;
  254. *ptr_header = ((0 << 23) | (DeviceIdLmx2594 << 18) | (0x1 << 1) | 1);
  255. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  256. *ptr = lmx2594regs[112-FCAL_ADDR];
  257. }
  258. void lmx2594_init(void *bar1) {
  259. // Header for LMX Reset
  260. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  261. *ptr = LMX2594_RST_HEADER;
  262. // Reset Data
  263. for (int m = 0; m < (sizeof(lmx2594_rst))/4; m++) {
  264. *ptr = lmx2594_rst[m];
  265. }
  266. // Header for init data
  267. *ptr = InitLMX2594Header;
  268. // Init data
  269. for (int i = 0; i < LMX_COUNT; i++) {
  270. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  271. *data_ptr = lmx2594regs[i];
  272. }
  273. }
  274. /*-------------------------LMX2594 Frequency Set-------------------------*/
  275. int lmx_freq_set_main_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  276. double N_div;
  277. printf("f_pd before = %f\n",f_pd);
  278. N_div = lmx_freq / f_pd;
  279. uint32_t N = (uint32_t) N_div;
  280. if (lmx_freq <= 12500e6) {
  281. if (N < 28){
  282. N= 28;
  283. }
  284. }
  285. else if (lmx_freq > 12500e6) {
  286. if (N <32) {
  287. N = 32;
  288. }
  289. };
  290. // Partial assist for the calibration
  291. struct vco_params params = calculate_vco_params(lmx_freq, f_pd);
  292. // Set the vco params
  293. set_vco_params(&params);
  294. // SET the N_DIV
  295. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  296. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  297. //CLear the lower 16 bits of the register
  298. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  299. // Next 16 bits of the register
  300. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  301. // Clear the SEG1_EN bit
  302. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  303. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  304. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  305. // Program the FCAL_EN bit
  306. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  307. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  308. uint32_t lmx_change_freq_regs[] = {
  309. lmx2594regs[112 - VCO_SEL],
  310. lmx2594regs[112 - CAP_CTRL_START],
  311. lmx2594regs[112 - VCO_DACISET],
  312. lmx2594regs[112-PFD_DLY_SEL],
  313. lmx2594regs[112-R4_ADDR],
  314. lmx2594regs[112-R1_ADDR],
  315. lmx2594regs[112-CHDIV_DIV2],
  316. lmx2594regs[112-PLL_N_S],
  317. lmx2594regs[112-PLL_N_M],
  318. lmx2594regs[112 - OUTA_MUX],
  319. lmx2594regs[112-FCAL_ADDR]
  320. };
  321. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  322. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  323. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  324. *ptr = LMX_HEADER;
  325. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  326. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  327. *data_ptr = lmx_change_freq_regs[i];
  328. }
  329. return 0;
  330. }
  331. int lmx_freq_set_out_of_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  332. double f_vco = 2 * lmx_freq;
  333. int chan_div = 2;
  334. uint8_t ch_div_reg = 0; // 2
  335. double vco_div = 7.5e9 / lmx_freq;
  336. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  337. if (f_vco < 7.5e9) {
  338. if (vco_div > 2 && vco_div <= 4) {
  339. chan_div = 4; // 4
  340. f_vco = lmx_freq * chan_div;
  341. }
  342. else if (vco_div > 4 && vco_div <= 6) {
  343. chan_div = 6; // 6
  344. f_vco = lmx_freq * chan_div;
  345. }
  346. else if (vco_div > 6 && vco_div <= 8) {
  347. chan_div = 8; // 8
  348. f_vco = lmx_freq * chan_div;
  349. }
  350. else if (vco_div > 8 && vco_div <= 12) {
  351. chan_div = 12; // 12
  352. f_vco = lmx_freq * chan_div;
  353. }
  354. else if (vco_div > 12 && vco_div <= 16) {
  355. chan_div = 16; // 16
  356. f_vco = lmx_freq * chan_div;
  357. }
  358. else if (vco_div > 16 && vco_div <= 24) {
  359. chan_div = 24; // 24
  360. f_vco = lmx_freq * chan_div;
  361. }
  362. else if (vco_div > 24 && vco_div <= 32) {
  363. chan_div = 32; // 32
  364. f_vco = lmx_freq * chan_div;
  365. }
  366. else if (vco_div > 32 && vco_div <= 48) {
  367. chan_div = 48; // 48
  368. f_vco = lmx_freq * chan_div;
  369. }
  370. else if (vco_div > 48 && vco_div <= 64) {
  371. chan_div = 64; // 64
  372. f_vco = lmx_freq * chan_div;
  373. }
  374. else if (vco_div > 64 && vco_div <= 72) {
  375. chan_div = 72; // 72
  376. f_vco = lmx_freq * chan_div;
  377. }
  378. else if (vco_div > 72 && vco_div <= 96) {
  379. chan_div = 96; // 96
  380. f_vco = lmx_freq * chan_div;
  381. }
  382. else if (vco_div > 96 && vco_div <= 128) {
  383. chan_div = 128; // 128
  384. f_vco = lmx_freq * chan_div;
  385. }
  386. else if (vco_div > 128 && vco_div <= 192) {
  387. chan_div = 192; // 192
  388. f_vco = lmx_freq * chan_div;
  389. }
  390. else if (vco_div > 192 && vco_div <= 256) {
  391. chan_div = 256; // 256
  392. f_vco = lmx_freq * chan_div;
  393. }
  394. else if (vco_div > 256 && vco_div <= 384) {
  395. chan_div = 384; // 384
  396. f_vco = lmx_freq * chan_div;
  397. }
  398. else if (vco_div > 384 && vco_div <= 512) {
  399. chan_div = 512; // 512
  400. f_vco = lmx_freq * chan_div;
  401. }
  402. else if (vco_div > 512 && vco_div <= 768) {
  403. chan_div = 768; // 768
  404. f_vco = lmx_freq * chan_div;
  405. }
  406. switch (chan_div) {
  407. case 2:
  408. ch_div_reg = 0;
  409. break;
  410. case 4:
  411. ch_div_reg = 1;
  412. break;
  413. case 6:
  414. ch_div_reg = 2;
  415. break;
  416. case 8:
  417. ch_div_reg = 3;
  418. break;
  419. case 12:
  420. ch_div_reg = 4;
  421. break;
  422. case 16:
  423. ch_div_reg = 5;
  424. break;
  425. case 24:
  426. ch_div_reg = 6;
  427. break;
  428. case 32:
  429. ch_div_reg = 7;
  430. break;
  431. case 48:
  432. ch_div_reg = 8;
  433. break;
  434. case 64:
  435. ch_div_reg = 9;
  436. break;
  437. case 72:
  438. ch_div_reg = 10;
  439. break;
  440. case 96:
  441. ch_div_reg = 11;
  442. break;
  443. case 128:
  444. ch_div_reg = 12;
  445. break;
  446. case 192:
  447. ch_div_reg = 13;
  448. break;
  449. case 256:
  450. ch_div_reg = 14;
  451. break;
  452. case 384:
  453. ch_div_reg = 15;
  454. break;
  455. case 512:
  456. ch_div_reg = 16;
  457. break;
  458. case 768:
  459. ch_div_reg = 17;
  460. break;
  461. }
  462. }
  463. else {
  464. ch_div_reg = 0;
  465. f_vco = lmx_freq * 2;
  466. }
  467. double N_div = f_vco / f_pd;
  468. uint32_t N = (uint32_t) N_div;
  469. if (f_vco <= 12500e6) {
  470. if (N < 28){
  471. N= 28;
  472. };
  473. }
  474. else if (f_vco > 12500e6) {
  475. if (N <32) {
  476. N = 32;
  477. }
  478. };
  479. // Partial assist for the calibration
  480. struct vco_params params = calculate_vco_params(f_vco, f_pd);
  481. // Set the vco params
  482. set_vco_params(&params);
  483. // Set the N value
  484. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  485. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  486. //CLear the lower 16 bits of the register
  487. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  488. // Next 16 bits of the register
  489. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  490. // Program the CHDIV value
  491. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  492. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  493. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  494. // If the ch_div > 2 then set the SEG1_EN bit
  495. if (chan_div > 2) {
  496. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  497. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  498. }
  499. else {
  500. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  501. }
  502. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  503. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  504. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  505. // Program the FCAL_EN bit
  506. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  507. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  508. uint32_t lmx_change_freq_regs []={
  509. lmx2594regs[112 - VCO_SEL],
  510. lmx2594regs[112 - CAP_CTRL_START],
  511. lmx2594regs[112 - VCO_DACISET],
  512. lmx2594regs[112-PFD_DLY_SEL],
  513. lmx2594regs[112-R4_ADDR],
  514. lmx2594regs[112-R1_ADDR],
  515. lmx2594regs[112 - PLL_N_S],
  516. lmx2594regs[112 - PLL_N_M],
  517. lmx2594regs[112 - CHDIV],
  518. lmx2594regs[112 - CHDIV_DIV2],
  519. lmx2594regs[112 - OUTA_MUX],
  520. lmx2594regs[112 - FCAL_ADDR]
  521. };
  522. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  523. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  524. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  525. *ptr = LMX_HEADER;
  526. // Send the data
  527. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  528. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  529. *data_ptr = lmx_change_freq_regs[i];
  530. }
  531. // char filename[100];
  532. // sprintf(filename, "%f.txt", lmx_freq);
  533. // FILE * f = fopen(filename, "w");
  534. // for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  535. // fprintf(f, "0x%08X\n", lmx2594regs[i]);
  536. // }
  537. // fclose(f);
  538. // printf("N_div = %f\n", N_div);
  539. // printf("f_vco = %f\n", f_vco);
  540. // printf("N = %d\n", N);
  541. // printf("chan_div = %d\n", chan_div);
  542. // printf("chan_div_reg = %d\n", ch_div_reg);
  543. return 0;
  544. }
  545. double lmx_get_freq(double freq) {
  546. if (freq < 100e3 || freq> 45e9) {
  547. printf("Frequency range is 100 kHz to 45 GHz\n");
  548. return -1;
  549. }
  550. if (freq >= 100e3 && freq <= 1000e6) {
  551. double f_max2870 = 4e9;
  552. double lmx_freq = f_max2870-freq; // 4 GHz - freq
  553. return lmx_freq;
  554. }
  555. else if (freq > 1000e6 && freq <= 15e9) {
  556. return freq;
  557. }
  558. else if (freq > 15e9 && freq <=27e9) {
  559. return freq/2;
  560. }
  561. else if (freq > 27e9 && freq <= 45e9) {
  562. return freq/4;
  563. }
  564. return 0;
  565. }
  566. int lmx_freq_set(void *bar1, double lmx_freq,double f_pd) {
  567. // Set the 4 Mosi mode
  568. usleep(1);
  569. uint32_t cfg_reg = get_cfg_reg();
  570. SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_MODE_BITM, CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
  571. uint32_t *spi_mode = bar1 +CFG_REG_ADDR;
  572. *spi_mode = cfg_reg;
  573. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  574. if (lmx_freq >= 7.5e9 && lmx_freq <= 15e9) {
  575. // lmx_freq_set_main_band(bar1, freq, f_pd);
  576. lmx_freq_set_main_band_int_mode(bar1, lmx_freq, f_pd);
  577. }
  578. else if (lmx_freq < 7.5e9) {
  579. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  580. lmx_freq_set_out_of_band_int_mode(bar1, lmx_freq, f_pd);
  581. }
  582. // Return the 1 MOSI mode
  583. usleep(1);
  584. SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_1MOSI);
  585. *spi_mode = cfg_reg;
  586. set_cfg_reg(cfg_reg);
  587. return 0;
  588. }
  589. uint32_t lmx_ld_status(void *bar1) {
  590. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  591. uint32_t read_value = *read_ptr;
  592. return read_value;
  593. }