lmx2594.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. void auto_cal(void *bar1) {
  123. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  124. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | LMX2594_R0_FCAL_EN;
  125. uint32_t *ptr_header = bar1+LMX_BASE_ADDR;
  126. *ptr_header = ((0 << 23) | (DeviceIdLmx2594 << 18) | (0x1 << 1) | 1);
  127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  128. *ptr = lmx2594regs[112-FCAL_ADDR];
  129. }
  130. void lmx2594_init(void *bar1) {
  131. // Header for LMX Reset
  132. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  133. *ptr = LMX2594_RST_HEADER;
  134. // Reset Data
  135. for (int m = 0; m < (sizeof(lmx2594_rst))/4; m++) {
  136. *ptr = lmx2594_rst[m];
  137. }
  138. // Header for init data
  139. *ptr = InitLMX2594Header;
  140. // Init data
  141. for (int i = 0; i < LMX_COUNT; i++) {
  142. *ptr = lmx2594regs[i];
  143. }
  144. usleep(10);
  145. auto_cal(bar1);
  146. }
  147. /*-------------------------LMX2594 Frequency Set-------------------------*/
  148. int lmx_freq_set_main_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  149. double N_div;
  150. printf("f_pd before = %f\n",f_pd);
  151. N_div = lmx_freq / f_pd;
  152. uint32_t N = (uint32_t) N_div;
  153. if (lmx_freq <= 12500e6) {
  154. if (N < 28){
  155. N= 28;
  156. }
  157. }
  158. else if (lmx_freq > 12500e6) {
  159. if (N <32) {
  160. N = 32;
  161. }
  162. };
  163. int vco_core;
  164. double f_coremin;
  165. double f_coremax;
  166. int c_core_min;
  167. int c_core_max;
  168. int a_core_min;
  169. int a_core_max;
  170. uint16_t vco_cap_ctrl_strt;
  171. uint16_t vco_daciset_strt;
  172. // Partial assist for the calibration
  173. //Determine a VCO core and other parameters
  174. if (lmx_freq >= 7500e6 && lmx_freq <= 8600e6) {
  175. vco_core = 1;
  176. f_coremin = 7500e6;
  177. f_coremax = 8600e6;
  178. c_core_min = 164;
  179. c_core_max = 12;
  180. a_core_min = 299;
  181. a_core_max = 240;
  182. }
  183. else if (lmx_freq > 8600e6 && lmx_freq < 9800e6) {
  184. vco_core = 2;
  185. f_coremin = 8600e6;
  186. f_coremax = 9800e6;
  187. c_core_min = 165;
  188. c_core_max = 16;
  189. a_core_min = 356;
  190. a_core_max = 247;
  191. }
  192. else if (lmx_freq >= 9800e6 && lmx_freq <= 10800e6) {
  193. vco_core = 3;
  194. f_coremin = 9800e6;
  195. f_coremax = 10800e6;
  196. c_core_min = 158;
  197. c_core_max = 19;
  198. a_core_min = 324;
  199. a_core_max = 224;
  200. }
  201. else if (lmx_freq > 10800e6 && lmx_freq <= 12000e6) {
  202. vco_core = 4;
  203. f_coremin = 10800e6;
  204. f_coremax = 12000e6;
  205. c_core_min = 140;
  206. c_core_max = 0;
  207. a_core_min = 383;
  208. a_core_max = 244;
  209. }
  210. else if (lmx_freq > 12000e6 && lmx_freq <= 12900e6) {
  211. vco_core = 5;
  212. f_coremin = 12000e6;
  213. f_coremax = 12900e6;
  214. c_core_min = 183;
  215. c_core_max = 36;
  216. a_core_min = 205;
  217. a_core_max = 146;
  218. }
  219. else if (lmx_freq > 12900e6 && lmx_freq <= 13900e6) {
  220. vco_core = 6;
  221. f_coremin = 12900e6;
  222. f_coremax = 13900e6;
  223. c_core_min = 155;
  224. c_core_max = 6;
  225. a_core_min = 242;
  226. a_core_max = 163;
  227. }
  228. else if (lmx_freq > 13900e6 && lmx_freq <= 15000e6) {
  229. vco_core = 7;
  230. f_coremin = 13900e6;
  231. f_coremax = 15000e6;
  232. c_core_min = 175;
  233. c_core_max = 19;
  234. a_core_min = 323;
  235. a_core_max = 244;
  236. };
  237. if (lmx_freq >=11900e6 && lmx_freq <=12100e6) {
  238. vco_daciset_strt = 300;
  239. vco_core = 4;
  240. vco_cap_ctrl_strt = 1;
  241. }
  242. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (lmx_freq - f_coremin) / (f_coremax - f_coremin));
  243. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (lmx_freq - f_coremin) / (f_coremax - f_coremin));
  244. //Set the VCO_CORE
  245. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  246. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  247. // Set the VCO_CAP_CTRL
  248. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  249. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  250. // Set the VCO_DACISET
  251. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  252. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  253. // Set the PF_DLY_SEL
  254. if (lmx_freq <= 12500e6) {
  255. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  256. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  257. }
  258. else if (lmx_freq > 12500e6) {
  259. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  260. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  261. };
  262. int cal_clk_div;
  263. //SET the FCAL_HPFD_ADJ
  264. if (f_pd <= 100e6) {
  265. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  266. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  267. }
  268. else if (f_pd > 100e6 && f_pd <= 150e6) {
  269. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  270. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  271. }
  272. else if (f_pd > 150e6 && f_pd <= 200e6) {
  273. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  274. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  275. }
  276. else if (f_pd > 200e6) {
  277. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  278. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  279. }
  280. // SET the CAL_CLK_DIV value
  281. if (f_pd <= 200e6 ) {
  282. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  283. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  284. cal_clk_div = 0;
  285. }
  286. else if (f_pd > 200e6 && f_pd <= 400e6) {
  287. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  288. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  289. cal_clk_div = 1;
  290. }
  291. else if (f_pd > 400e6 && f_pd < 800e6) {
  292. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  293. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  294. cal_clk_div = 2;
  295. };
  296. //Calculate the ACAL_CMP_DLY
  297. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  298. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));
  299. //Set the ACAL_CMP_DLY value
  300. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  301. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  302. // SET the N_DIV
  303. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  304. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  305. //CLear the lower 16 bits of the register
  306. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  307. // Next 16 bits of the register
  308. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  309. // Clear the SEG1_EN bit
  310. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  311. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  312. // Removed unnecessary commented-out LMX_HEADER definition
  313. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_VCO;
  314. // Program the FCAL_EN bit
  315. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_R0_FCAL);
  316. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  317. uint32_t lmx_change_freq_regs[] = {
  318. lmx2594regs[112 - VCO_SEL],
  319. lmx2594regs[112 - CAP_CTRL_START],
  320. lmx2594regs[112 - VCO_DACISET],
  321. lmx2594regs[112-PFD_DLY_SEL],
  322. lmx2594regs[112-R4_ADDR],
  323. lmx2594regs[112-R1_ADDR],
  324. lmx2594regs[112-CHDIV_DIV2],
  325. lmx2594regs[112-PLL_N_S],
  326. lmx2594regs[112-PLL_N_M],
  327. lmx2594regs[112 - OUTA_MUX],
  328. lmx2594regs[112-FCAL_ADDR]
  329. };
  330. // Create a header for the LMX2594 with the appropriate number of words
  331. // uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  332. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  333. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  334. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  335. *ptr = LMX_HEADER;
  336. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  337. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  338. *data_ptr = lmx_change_freq_regs[i];
  339. }
  340. return 0;
  341. }
  342. int lmx_freq_set_out_of_band_int_mode(void *bar1, double lmx_freq, double f_pd) {
  343. double f_vco = 2 * lmx_freq;
  344. int chan_div = 2;
  345. uint8_t ch_div_reg = 0; // 2
  346. double vco_div = 7.5e9 / lmx_freq;
  347. int vco_core;
  348. double f_coremin;
  349. double f_coremax;
  350. int c_core_min;
  351. int c_core_max;
  352. int a_core_min;
  353. int a_core_max;
  354. uint16_t vco_cap_ctrl_strt;
  355. uint16_t vco_daciset_strt;
  356. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  357. if (f_vco < 7.5e9) {
  358. if (vco_div > 2 && vco_div <= 4) {
  359. chan_div = 4; // 4
  360. f_vco = lmx_freq * chan_div;
  361. }
  362. else if (vco_div > 4 && vco_div <= 6) {
  363. chan_div = 6; // 6
  364. f_vco = lmx_freq * chan_div;
  365. }
  366. else if (vco_div > 6 && vco_div <= 8) {
  367. chan_div = 8; // 8
  368. f_vco = lmx_freq * chan_div;
  369. }
  370. else if (vco_div > 8 && vco_div <= 12) {
  371. chan_div = 12; // 12
  372. f_vco = lmx_freq * chan_div;
  373. }
  374. else if (vco_div > 12 && vco_div <= 16) {
  375. chan_div = 16; // 16
  376. f_vco = lmx_freq * chan_div;
  377. }
  378. else if (vco_div > 16 && vco_div <= 24) {
  379. chan_div = 24; // 24
  380. f_vco = lmx_freq * chan_div;
  381. }
  382. else if (vco_div > 24 && vco_div <= 32) {
  383. chan_div = 32; // 32
  384. f_vco = lmx_freq * chan_div;
  385. }
  386. else if (vco_div > 32 && vco_div <= 48) {
  387. chan_div = 48; // 48
  388. f_vco = lmx_freq * chan_div;
  389. }
  390. else if (vco_div > 48 && vco_div <= 64) {
  391. chan_div = 64; // 64
  392. f_vco = lmx_freq * chan_div;
  393. }
  394. else if (vco_div > 64 && vco_div <= 72) {
  395. chan_div = 72; // 72
  396. f_vco = lmx_freq * chan_div;
  397. }
  398. else if (vco_div > 72 && vco_div <= 96) {
  399. chan_div = 96; // 96
  400. f_vco = lmx_freq * chan_div;
  401. }
  402. else if (vco_div > 96 && vco_div <= 128) {
  403. chan_div = 128; // 128
  404. f_vco = lmx_freq * chan_div;
  405. }
  406. else if (vco_div > 128 && vco_div <= 192) {
  407. chan_div = 192; // 192
  408. f_vco = lmx_freq * chan_div;
  409. }
  410. else if (vco_div > 192 && vco_div <= 256) {
  411. chan_div = 256; // 256
  412. f_vco = lmx_freq * chan_div;
  413. }
  414. else if (vco_div > 256 && vco_div <= 384) {
  415. chan_div = 384; // 384
  416. f_vco = lmx_freq * chan_div;
  417. }
  418. else if (vco_div > 384 && vco_div <= 512) {
  419. chan_div = 512; // 512
  420. f_vco = lmx_freq * chan_div;
  421. }
  422. else if (vco_div > 512 && vco_div <= 768) {
  423. chan_div = 768; // 768
  424. f_vco = lmx_freq * chan_div;
  425. }
  426. switch (chan_div) {
  427. case 2:
  428. ch_div_reg = 0;
  429. break;
  430. case 4:
  431. ch_div_reg = 1;
  432. break;
  433. case 6:
  434. ch_div_reg = 2;
  435. break;
  436. case 8:
  437. ch_div_reg = 3;
  438. break;
  439. case 12:
  440. ch_div_reg = 4;
  441. break;
  442. case 16:
  443. ch_div_reg = 5;
  444. break;
  445. case 24:
  446. ch_div_reg = 6;
  447. break;
  448. case 32:
  449. ch_div_reg = 7;
  450. break;
  451. case 48:
  452. ch_div_reg = 8;
  453. break;
  454. case 64:
  455. ch_div_reg = 9;
  456. break;
  457. case 72:
  458. ch_div_reg = 10;
  459. break;
  460. case 96:
  461. ch_div_reg = 11;
  462. break;
  463. case 128:
  464. ch_div_reg = 12;
  465. break;
  466. case 192:
  467. ch_div_reg = 13;
  468. break;
  469. case 256:
  470. ch_div_reg = 14;
  471. break;
  472. case 384:
  473. ch_div_reg = 15;
  474. break;
  475. case 512:
  476. ch_div_reg = 16;
  477. break;
  478. case 768:
  479. ch_div_reg = 17;
  480. break;
  481. }
  482. }
  483. else {
  484. ch_div_reg = 0;
  485. f_vco = lmx_freq * 2;
  486. }
  487. double N_div = f_vco / f_pd;
  488. uint32_t N = (uint32_t) N_div;
  489. if (f_vco <= 12500e6) {
  490. if (N < 28){
  491. N= 28;
  492. };
  493. }
  494. else if (f_vco > 12500e6) {
  495. if (N <32) {
  496. N = 32;
  497. }
  498. };
  499. // Partial assist for the calibration
  500. //Determine a VCO core and other parameters
  501. if (f_vco >= 7500e6 && f_vco <= 8600e6) {
  502. vco_core = 1;
  503. f_coremin = 7500e6;
  504. f_coremax = 8600e6;
  505. c_core_min = 164;
  506. c_core_max = 12;
  507. a_core_min = 299;
  508. a_core_max = 240;
  509. }
  510. else if (f_vco > 8600e6 && f_vco < 9800e6) {
  511. vco_core = 2;
  512. f_coremin = 8600e6;
  513. f_coremax = 9800e6;
  514. c_core_min = 165;
  515. c_core_max = 16;
  516. a_core_min = 356;
  517. a_core_max = 247;
  518. }
  519. else if (f_vco >= 9800e6 && f_vco <= 10800e6) {
  520. vco_core = 3;
  521. f_coremin = 9800e6;
  522. f_coremax = 10800e6;
  523. c_core_min = 158;
  524. c_core_max = 19;
  525. a_core_min = 324;
  526. a_core_max = 224;
  527. }
  528. else if (f_vco > 10800e6 && f_vco <= 12000e6) {
  529. vco_core = 4;
  530. f_coremin = 10800e6;
  531. f_coremax = 12000e6;
  532. c_core_min = 140;
  533. c_core_max = 0;
  534. a_core_min = 383;
  535. a_core_max = 244;
  536. }
  537. else if (f_vco > 12000e6 && f_vco <= 12900e6) {
  538. vco_core = 5;
  539. f_coremin = 12000e6;
  540. f_coremax = 12900e6;
  541. c_core_min = 183;
  542. c_core_max = 36;
  543. a_core_min = 205;
  544. a_core_max = 146;
  545. }
  546. else if (f_vco > 12900e6 && f_vco <= 13900e6) {
  547. vco_core = 6;
  548. f_coremin = 12900e6;
  549. f_coremax = 13900e6;
  550. c_core_min = 155;
  551. c_core_max = 6;
  552. a_core_min = 242;
  553. a_core_max = 163;
  554. }
  555. else if (f_vco > 13900e6 && f_vco <= 15000e6) {
  556. vco_core = 7;
  557. f_coremin = 13900e6;
  558. f_coremax = 15000e6;
  559. c_core_min = 175;
  560. c_core_max = 19;
  561. a_core_min = 323;
  562. a_core_max = 244;
  563. };
  564. vco_cap_ctrl_strt = round(c_core_min - (c_core_min - c_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  565. vco_daciset_strt = round(a_core_min + (a_core_min - a_core_max) * (f_vco - f_coremin) / (f_coremax - f_coremin));
  566. if (f_vco >=11900e6 && f_vco <=12100e6) {
  567. vco_daciset_strt = 300;
  568. vco_core = 4;
  569. vco_cap_ctrl_strt = 1;
  570. }
  571. // Calibration assist
  572. //Set the VCO_CORE
  573. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] & (~BITM_LMX2594_R20_VCO_SEL);
  574. lmx2594regs[112 - VCO_SEL] = lmx2594regs[112 - VCO_SEL] | (vco_core << BITP_LMX2594_R20_VCO_SEL);
  575. // Set the VCO_CAP_CTRL_START
  576. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] & (~BITM_LMX2594_R78_VCO_CAP_CTRL_START);
  577. lmx2594regs[112 - CAP_CTRL_START] = lmx2594regs[112 - CAP_CTRL_START] | (vco_cap_ctrl_strt << BITP_LMX2594_R78_VCO_CAP_CTRL_START);
  578. // Set the VCO_DACISET
  579. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] & (~BITM_LMX2594_R17_VCO_DACISET);
  580. lmx2594regs[112 - VCO_DACISET] = lmx2594regs[112 - VCO_DACISET] | (vco_daciset_strt << BITP_LMX2594_R17_VCO_DACISET);
  581. // Set the PFD_DLY_SEL to appropriate value
  582. if (f_vco <= 12500e6) {
  583. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  584. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x1 << BITP_LMX2594_R37_PFD_DLY_SEL);
  585. // printf("PFD_DLY_SEL = %d\n", 1);
  586. }
  587. else if (f_vco > 12500e6) {
  588. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  589. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x2 << BITP_LMX2594_R37_PFD_DLY_SEL);
  590. // printf("PFD_DLY_SEL = %d\n", 2);
  591. }
  592. if (f_pd <= 100e6) {
  593. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  594. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_LESS100MHZ;
  595. }
  596. else if (f_pd > 100e6 && f_pd <= 150e6) {
  597. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  598. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_100_150MHZ;
  599. }
  600. else if (f_pd > 150e6 && f_pd <= 200e6) {
  601. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  602. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_150_200MHZ;
  603. }
  604. else if (f_pd > 200e6) {
  605. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] & (~BITM_LMX2594_RO_FCAL_HPFD_ADJ);
  606. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | ENUM_LMX2594_R0_FCAL_HPFD_ADJ_MORE200MHZ;
  607. };
  608. // SET the CAL_CLK_DIV value
  609. int cal_clk_div;
  610. if (f_pd <= 200e6 ) {
  611. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  612. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV1;
  613. cal_clk_div =0;
  614. }
  615. else if (f_pd > 200e6 && f_pd <= 400e6) {
  616. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  617. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV2;
  618. cal_clk_div =1;
  619. }
  620. else if (f_pd > 400e6 && f_pd < 800e6) {
  621. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] & (~BITM_LMX2594_R1_CAL_CLK_DIV);
  622. lmx2594regs[112-R1_ADDR] = lmx2594regs[112-R1_ADDR] | ENUM_LMX2594_R1_CAL_CLK_DIV4;
  623. cal_clk_div = 2;
  624. };
  625. //Calculate the ACAL_CMP_DLY
  626. double Fsmclk = f_pd/(pow(2,cal_clk_div));
  627. uint8_t acal_cmp_dly = (uint8_t) ((uint64_t)round((Fsmclk)/10e6));;
  628. //Set the ACAL_CMP_DLY value
  629. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] &(~BITM_LMX2594_R4_ACAL_CMP_DLY);
  630. lmx2594regs[112-R4_ADDR] = lmx2594regs[112-R4_ADDR] | (acal_cmp_dly << BITP_LMX2594_R4_ACAL_CMP_DLY);
  631. // Set the N value
  632. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  633. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  634. //CLear the lower 16 bits of the register
  635. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  636. // Next 16 bits of the register
  637. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  638. // Program the CHDIV value
  639. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  640. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  641. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  642. // If the ch_div > 2 then set the SEG1_EN bit
  643. if (chan_div > 2) {
  644. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  645. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  646. }
  647. else {
  648. lmx2594regs[112-CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  649. }
  650. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  651. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  652. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  653. // Program the FCAL_EN bit
  654. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] & (~LMX2594_R0_FCAL_EN);
  655. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  656. uint32_t lmx_change_freq_regs []={
  657. lmx2594regs[112 - VCO_SEL],
  658. lmx2594regs[112 - CAP_CTRL_START],
  659. lmx2594regs[112 - VCO_DACISET],
  660. lmx2594regs[112-PFD_DLY_SEL],
  661. lmx2594regs[112-R4_ADDR],
  662. lmx2594regs[112-R1_ADDR],
  663. lmx2594regs[112 - PLL_N_S],
  664. lmx2594regs[112 - PLL_N_M],
  665. lmx2594regs[112 - CHDIV],
  666. lmx2594regs[112 - CHDIV_DIV2],
  667. lmx2594regs[112 - OUTA_MUX],
  668. lmx2594regs[112 - FCAL_ADDR]
  669. };
  670. // Create a header for the LMX2594 with the appropriate number of words MOSI 4
  671. uint32_t LMX_HEADER = ((0x1<< 23) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | 1);
  672. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  673. *ptr = LMX_HEADER;
  674. // Send the data
  675. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  676. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  677. *data_ptr = lmx_change_freq_regs[i];
  678. }
  679. // char filename[100];
  680. // sprintf(filename, "%f.txt", lmx_freq);
  681. // FILE * f = fopen(filename, "w");
  682. // for (int i = 0; i < sizeof(lmx2594regs) / 4; i++) {
  683. // fprintf(f, "0x%08X\n", lmx2594regs[i]);
  684. // }
  685. // fclose(f);
  686. // printf("N_div = %f\n", N_div);
  687. // printf("f_vco = %f\n", f_vco);
  688. // printf("N = %d\n", N);
  689. // printf("chan_div = %d\n", chan_div);
  690. // printf("chan_div_reg = %d\n", ch_div_reg);
  691. return 0;
  692. }
  693. double lmx_get_freq(double freq) {
  694. if (freq < 100e3 || freq> 45e9) {
  695. printf("Frequency range is 100 kHz to 45 GHz\n");
  696. return -1;
  697. }
  698. if (freq >= 100e3 && freq <= 1000e6) {
  699. double f_max2870 = 4e9;
  700. double lmx_freq = f_max2870-freq; // 4 GHz - freq
  701. return lmx_freq;
  702. }
  703. else if (freq > 1000e6 && freq <= 15e9) {
  704. return freq;
  705. }
  706. else if (freq > 15e9 && freq <=27e9) {
  707. return freq/2;
  708. }
  709. else if (freq > 27e9 && freq <= 45e9) {
  710. return freq/4;
  711. }
  712. }
  713. int lmx_freq_set(void *bar1, double lmx_freq,double f_pd) {
  714. // Set the 4 Mosi mode
  715. usleep(1);
  716. uint32_t cfg_reg = get_cfg_reg();
  717. SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_MODE_BITM, CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
  718. uint32_t *spi_mode = bar1 +CFG_REG_ADDR;
  719. *spi_mode = cfg_reg;
  720. usleep(1);
  721. SET_REGISTER_PARAM(cfg_reg, CFG_REG_RST_FOR_FPGA_BITM, CFG_REG_RST_FOR_FPGA_BITP, CFG_REG_RST_FOR_FPGA_ON);
  722. *spi_mode = cfg_reg;
  723. usleep(1);
  724. SET_REGISTER_PARAM(cfg_reg, CFG_REG_RST_FOR_FPGA_BITM, CFG_REG_RST_FOR_FPGA_BITP, CFG_REG_RST_FOR_FPGA_OFF);
  725. *spi_mode = cfg_reg;
  726. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  727. if (lmx_freq >= 7.5e9 && lmx_freq <= 15e9) {
  728. // lmx_freq_set_main_band(bar1, freq, f_pd);
  729. lmx_freq_set_main_band_int_mode(bar1, lmx_freq, f_pd);
  730. }
  731. else if (lmx_freq < 7.5e9) {
  732. // lmx_freq_set_out_of_band(bar1, freq, f_pd);
  733. lmx_freq_set_out_of_band_int_mode(bar1, lmx_freq, f_pd);
  734. }
  735. // Return the 1 MOSI mode
  736. usleep(1);
  737. SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_1MOSI);
  738. *spi_mode = cfg_reg;
  739. usleep(1);
  740. set_cfg_reg(cfg_reg);
  741. return 0;
  742. }
  743. uint32_t lmx_ld_status(void *bar1) {
  744. uint32_t *read_ptr = (uint32_t *)(bar1 + LMX_LD_STATUS_ADDR);
  745. uint32_t read_value = *read_ptr;
  746. return read_value;
  747. }