lmx2594.c 26 KB

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