lmx2594.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #include "lmx2594.h"
  2. #include <math.h>
  3. struct vco_params calculate_vco_params (double lmx_freq, double f_pd) {
  4. struct vco_params params;
  5. if (lmx_freq >= 7500e6 && lmx_freq <= 8600e6) {
  6. params.vco_core = 1;
  7. params.f_coremin = 7500e6;
  8. params.f_coremax = 8600e6;
  9. params.c_core_min = 164;
  10. params.c_core_max = 12;
  11. params.a_core_min = 299;
  12. params.a_core_max = 240;
  13. }
  14. else if (lmx_freq > 8600e6 && lmx_freq < 9800e6) {
  15. params.vco_core = 2;
  16. params.f_coremin = 8600e6;
  17. params.f_coremax = 9800e6;
  18. params.c_core_min = 165;
  19. params.c_core_max = 16;
  20. params.a_core_min = 356;
  21. params.a_core_max = 247;
  22. }
  23. else if (lmx_freq >= 9800e6 && lmx_freq <= 10800e6) {
  24. params.vco_core = 3;
  25. params.f_coremin = 9800e6;
  26. params.f_coremax = 10800e6;
  27. params.c_core_min = 158;
  28. params.c_core_max = 19;
  29. params.a_core_min = 324;
  30. params.a_core_max = 224;
  31. }
  32. else if (lmx_freq > 10800e6 && lmx_freq <= 12000e6) {
  33. params.vco_core = 4;
  34. params.f_coremin = 10800e6;
  35. params.f_coremax = 12000e6;
  36. params.c_core_min = 140;
  37. params.c_core_max = 0;
  38. params.a_core_min = 383;
  39. params.a_core_max = 244;
  40. }
  41. else if (lmx_freq > 12000e6 && lmx_freq <= 12900e6) {
  42. params.vco_core = 5;
  43. params.f_coremin = 12000e6;
  44. params.f_coremax = 12900e6;
  45. params.c_core_min = 183;
  46. params.c_core_max = 36;
  47. params.a_core_min = 205;
  48. params.a_core_max = 146;
  49. }
  50. else if (lmx_freq > 12900e6 && lmx_freq <= 13900e6) {
  51. params.vco_core = 6;
  52. params.f_coremin = 12900e6;
  53. params.f_coremax = 13900e6;
  54. params.c_core_min = 155;
  55. params.c_core_max = 6;
  56. params.a_core_min = 242;
  57. params.a_core_max = 163;
  58. }
  59. else if (lmx_freq > 13900e6 && lmx_freq <= 15000e6) {
  60. params.vco_core = 7;
  61. params.f_coremin = 13900e6;
  62. params.f_coremax = 15000e6;
  63. params.c_core_min = 175;
  64. params.c_core_max = 19;
  65. params.a_core_min = 323;
  66. params.a_core_max = 244;
  67. }
  68. if (lmx_freq >=11900e6 && lmx_freq <=12100e6) {
  69. params.vco_daciset_strt = 300;
  70. params.vco_core = 4;
  71. params.vco_cap_ctrl_strt = 1;
  72. }
  73. 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));
  74. 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));
  75. if (lmx_freq <= 12500e6) {
  76. params.pfd_dly_sel = 1;
  77. }
  78. else if (lmx_freq > 12500e6) {
  79. params.pfd_dly_sel = 2;
  80. }
  81. if (f_pd <= 100e6) {
  82. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  83. }
  84. else if (f_pd > 100e6 && f_pd <= 150e6) {
  85. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  86. }
  87. else if (f_pd > 150e6 && f_pd <= 200e6) {
  88. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  89. }
  90. else if (f_pd > 200e6) {
  91. params.fcal_hpfd_adj = ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  92. }
  93. // SET the CAL_CLK_DIV value
  94. if (f_pd <= 200e6 ) {
  95. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV1;
  96. }
  97. else if (f_pd > 200e6 && f_pd <= 400e6) {
  98. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV2;
  99. }
  100. else if (f_pd > 400e6 && f_pd < 800e6) {
  101. params.cal_clk_div = ENUM_LMX2594_R1_CAL_CLK_DIV4;
  102. }
  103. // Calculate the ACAL_CMP_DLY
  104. double Fsmclk = f_pd/(pow(2,params.cal_clk_div));
  105. params.acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
  106. return params;
  107. }
  108. void set_vco_params (struct vco_params *params) {
  109. // Set the VCO_CORE
  110. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  111. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (params->vco_core << BITP_LMX2594_R20_VCO_SEL);
  112. // Set the VCO_CAP_CTRL_START
  113. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  114. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (params->vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  115. // Set the VCO_DACISET
  116. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  117. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (params->vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  118. // Set PFD_DLY_SEL
  119. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  120. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (params->pfd_dly_sel << BITP_LMX2594_R37_PFD_DLY_SEL);
  121. // Set the FCAL_HPFD_ADJ
  122. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  123. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | params->fcal_hpfd_adj;
  124. // SET the CAL_CLK_DIV value
  125. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  126. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | params->cal_clk_div;
  127. // Set the ACAL_CMP_DLY value
  128. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] & (~BITM_LMX2594_R4_ACAL_CMP_DLY);
  129. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (params->acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  130. }
  131. const uint32_t lmx2594_rst[] = {
  132. 0x002516,
  133. 0x002514
  134. };
  135. uint32_t lmx2594regs[LMX_COUNT] = {
  136. 0x700000,
  137. 0x6F0000,
  138. 0x6E0000,
  139. 0x6D0000,
  140. 0x6C0000,
  141. 0x6B0000,
  142. 0x6A0000,
  143. 0x690021,
  144. 0x680000,
  145. 0x670000,
  146. 0x660000,
  147. 0x650011,
  148. 0x640000,
  149. 0x630000,
  150. 0x620000,
  151. 0x610888,
  152. 0x600000,
  153. 0x5F0000,
  154. 0x5E0000,
  155. 0x5D0000,
  156. 0x5C0000,
  157. 0x5B0000,
  158. 0x5A0000,
  159. 0x590000,
  160. 0x580000,
  161. 0x570000,
  162. 0x560000,
  163. 0x550000,
  164. 0x540000,
  165. 0x530000,
  166. 0x520000,
  167. 0x510000,
  168. 0x500000,
  169. 0x4F0000,
  170. 0x4E0105,
  171. 0x4D0000,
  172. 0x4C000C,
  173. 0x4B0C40,
  174. 0x4A0000,
  175. 0x49003F,
  176. 0x480001,
  177. 0x470081,
  178. 0x46C350,
  179. 0x450000,
  180. 0x4403E8,
  181. 0x430000,
  182. 0x4201F4,
  183. 0x410000,
  184. 0x401388,
  185. 0x3F0000,
  186. 0x3E0322,
  187. 0x3D00A8,
  188. 0x3C03E8,
  189. 0x3B0001,
  190. 0x3A9001,
  191. 0x390020,
  192. 0x380000,
  193. 0x370000,
  194. 0x360000,
  195. 0x350000,
  196. 0x340820,
  197. 0x330080,
  198. 0x320000,
  199. 0x314180,
  200. 0x300300,
  201. 0x2F0300,
  202. 0x2E07FD,
  203. 0x2DC8DF,
  204. 0x2C1F20,
  205. 0x2B0000,
  206. 0x2A0000,
  207. 0x290000,
  208. 0x280000,
  209. 0x2703E8,
  210. 0x260000,
  211. 0x250104,
  212. 0x240032,
  213. 0x230004,
  214. 0x220000,
  215. 0x211E21,
  216. 0x200393,
  217. 0x1F43EC,
  218. 0x1E318C,
  219. 0x1D318C,
  220. 0x1C0488,
  221. 0x1B0002,
  222. 0x1A0DB0,
  223. 0x190C2B,
  224. 0x18071A,
  225. 0x17007C,
  226. 0x160001,
  227. 0x150401,
  228. 0x14D848,
  229. 0x1327B7,
  230. 0x120064,
  231. 0x110130,
  232. 0x100080,
  233. 0x0F064F,
  234. 0x0E1E40,
  235. 0x0D4000,
  236. 0x0C5001,
  237. 0x0B0018,
  238. 0x0A10D8,
  239. 0x090604,
  240. 0x082000,
  241. 0x0740B2,
  242. 0x06C802,
  243. 0x0500C8,
  244. 0x041443,
  245. 0x030642,
  246. 0x020500,
  247. 0x01080B,
  248. 0x00251C
  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. }