lmx2594.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "lmx2594.h"
  2. const uint32_t lmx2594_rst[2] = {
  3. 0x00251e,
  4. 0x00251c
  5. };
  6. uint32_t lmx2594regs[LMX_COUNT] = {
  7. 0x700000,
  8. 0x6F0000,
  9. 0x6E0000,
  10. 0x6D0000,
  11. 0x6C0000,
  12. 0x6B0000,
  13. 0x6A0000,
  14. 0x690021,
  15. 0x680000,
  16. 0x670000,
  17. 0x660000,
  18. 0x650011,
  19. 0x640000,
  20. 0x630000,
  21. 0x620000,
  22. 0x610888,
  23. 0x600000,
  24. 0x5F0000,
  25. 0x5E0000,
  26. 0x5D0000,
  27. 0x5C0000,
  28. 0x5B0000,
  29. 0x5A0000,
  30. 0x590000,
  31. 0x580000,
  32. 0x570000,
  33. 0x560000,
  34. 0x550000,
  35. 0x540000,
  36. 0x530000,
  37. 0x520000,
  38. 0x510000,
  39. 0x500000,
  40. 0x4F0000,
  41. 0x4E0105,
  42. 0x4D0000,
  43. 0x4C000C,
  44. 0x4B0C40,
  45. 0x4A0000,
  46. 0x49003F,
  47. 0x480001,
  48. 0x470081,
  49. 0x46C350,
  50. 0x450000,
  51. 0x4403E8,
  52. 0x430000,
  53. 0x4201F4,
  54. 0x410000,
  55. 0x401388,
  56. 0x3F0000,
  57. 0x3E0322,
  58. 0x3D00A8,
  59. 0x3C03E8,
  60. 0x3B0001,
  61. 0x3A9001,
  62. 0x390020,
  63. 0x380000,
  64. 0x370000,
  65. 0x360000,
  66. 0x350000,
  67. 0x340820,
  68. 0x330080,
  69. 0x320000,
  70. 0x314180,
  71. 0x300300,
  72. 0x2F0300,
  73. 0x2E07FD,
  74. 0x2DC8DF,
  75. 0x2C1F20,
  76. 0x2B0000,
  77. 0x2A0000,
  78. 0x290000,
  79. 0x280000,
  80. 0x2703E8,
  81. 0x260000,
  82. 0x250104,
  83. 0x240032,
  84. 0x230004,
  85. 0x220000,
  86. 0x211E21,
  87. 0x200393,
  88. 0x1F43EC,
  89. 0x1E318C,
  90. 0x1D318C,
  91. 0x1C0488,
  92. 0x1B0002,
  93. 0x1A0DB0,
  94. 0x190C2B,
  95. 0x18071A,
  96. 0x17007C,
  97. 0x160001,
  98. 0x150401,
  99. 0x14D848,
  100. 0x1327B7,
  101. 0x120064,
  102. 0x110130,
  103. 0x100080,
  104. 0x0F064F,
  105. 0x0E1E40,
  106. 0x0D4000,
  107. 0x0C5001,
  108. 0x0B0018,
  109. 0x0A10D8,
  110. 0x090604,
  111. 0x082000,
  112. 0x0740B2,
  113. 0x06C802,
  114. 0x0500C8,
  115. 0x041443,
  116. 0x030642,
  117. 0x020500,
  118. 0x01080B,
  119. 0x00251C
  120. };
  121. void lmx2594_init(void *bar1) {
  122. // Header for LMX Reset
  123. uint32_t *ptr_rst = bar1 + LMX_BASE_ADDR;
  124. *ptr_rst = LMX2594_RST_HEADER;
  125. // Reset Data
  126. for (int m = 0; m < 2; m++) {
  127. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  128. *ptr = lmx2594_rst[m];
  129. }
  130. // Header for init data
  131. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  132. *ptr = InitLMX2594Header;
  133. // Init data
  134. for (int i = 0; i < LMX_COUNT; i++) {
  135. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  136. *ptr = lmx2594regs[i];
  137. }
  138. }
  139. /*-------------------------LMX2594 Frequency Set-------------------------*/
  140. int lmx_freq_set_main_band(void *bar1, uint64_t freq, double f_pd, double N_div) {
  141. N_div = freq / f_pd;
  142. // divide whole part and fractional part
  143. uint32_t N = (uint32_t) N_div;
  144. // In frac part there is separate denominator and numerator
  145. // If frac part is 0 then the denominator is 1000 and numerator is 0
  146. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  147. uint32_t frac_d = 524287;
  148. // If frac part is 0 then the denominator is 1000 and numerator is 0
  149. if (frac_n == 0) {
  150. frac_n = 0;
  151. frac_d = 524287;
  152. }
  153. // Recommended sequnce for changin freq
  154. // 1. Change the N-div value
  155. // 2. Change the PLL numerator and denominator
  156. // 3. Program FCAL_EN bit
  157. // Clear the required parts of the register
  158. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  159. // Set the MASH_ORDER to 3
  160. lmx2594regs[112-MASH_ORDER] = lmx2594regs[112-MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  161. // Set PF_DLY_SEL to 3
  162. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  163. lmx2594regs[112-PFD_DLY_SEL] = lmx2594regs[112-PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  164. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] &(~0xFFFF);
  165. lmx2594regs[112-PLL_N_S] = lmx2594regs[112-PLL_N_S] | (N >> 16);
  166. //CLear the lower 16 bits of the register
  167. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] & (~0xFFFF);
  168. // Next 16 bits of the register
  169. lmx2594regs[112-PLL_N_M] = lmx2594regs[112-PLL_N_M] | (N & 0xFFFF);
  170. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  171. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] & (~0xFFFF);
  172. lmx2594regs[112-PLL_NUM_S] = lmx2594regs[112-PLL_NUM_S] | (frac_n >> 16);
  173. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  174. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] & (~0xFFFF);
  175. // Next 16 bits of the numerator
  176. lmx2594regs[112-PLL_NUM_M] = lmx2594regs[112-PLL_NUM_M] | (frac_n & 0xFFFF);
  177. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  178. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] & (~0xFFFF);
  179. // most significant 16 bits of the denominator
  180. lmx2594regs[112-PLL_DEN_S] = lmx2594regs[112-PLL_DEN_S] | (frac_d >> 16);
  181. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  182. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] & (~0xFFFF);
  183. // Next 16 bits of the denominator
  184. lmx2594regs[112-PLL_DEN_M] = lmx2594regs[112-PLL_DEN_M] | (frac_d & 0xFFFF);
  185. // Program the FCAL_EN bit
  186. lmx2594regs[112-FCAL_ADDR] = lmx2594regs[112-FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  187. // Show the all the upper 16 bits of the register lmx2594regs[PLL_N_S]
  188. // Determine which regs are changed and send only those
  189. uint32_t lmx_change_freq_regs[] = {
  190. lmx2594regs[112-MASH_ORDER],
  191. lmx2594regs[112-PFD_DLY_SEL],
  192. lmx2594regs[112-PLL_N_S],
  193. lmx2594regs[112-PLL_N_M],
  194. lmx2594regs[112-PLL_DEN_S],
  195. lmx2594regs[112-PLL_DEN_M],
  196. lmx2594regs[112-PLL_NUM_S],
  197. lmx2594regs[112-PLL_NUM_M],
  198. lmx2594regs[112-FCAL_ADDR]
  199. };
  200. // Create a header for the LMX2594 with the appropriate number of words
  201. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs)/4) << 1) | 1);
  202. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  203. *ptr = LMX_HEADER;
  204. for (int i = 0; i < sizeof(lmx_change_freq_regs)/4; i++) {
  205. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  206. *data_ptr = lmx_change_freq_regs[i];
  207. }
  208. usleep(1);
  209. return 0;
  210. }
  211. int lmx_freq_set_out_of_band(void *bar1, uint64_t freq, double f_pd, double N_div) {
  212. double f_vco = 2 * freq;
  213. int chan_div = 2;
  214. uint8_t ch_div_reg = 0; // 2
  215. double vco_div = 7.5e9 / freq;
  216. // minimum N_div value is 28 and Vco frequency can't be less than 7.5 GHz
  217. if (f_vco < 7.5e9) {
  218. if (vco_div > 2 && vco_div <= 4)
  219. chan_div = 4; // 4
  220. f_vco = freq * chan_div;
  221. if (vco_div > 4 && vco_div <= 6) {
  222. chan_div = 6; // 6
  223. f_vco = freq * chan_div;
  224. }
  225. if (vco_div > 6 && vco_div <= 8) {
  226. chan_div = 8; // 8
  227. f_vco = freq * chan_div;
  228. }
  229. if (vco_div > 8 && vco_div <= 12) {
  230. chan_div = 12; // 12
  231. f_vco = freq * chan_div;
  232. }
  233. if (vco_div > 12 && vco_div <= 16) {
  234. chan_div = 16; // 16
  235. f_vco = freq * chan_div;
  236. }
  237. if (vco_div > 16 && vco_div <= 24) {
  238. chan_div = 24; // 24
  239. f_vco = freq * chan_div;
  240. }
  241. if (vco_div > 24 && vco_div <= 32) {
  242. chan_div = 32; // 32
  243. f_vco = freq * chan_div;
  244. }
  245. if (vco_div > 32 && vco_div <= 48) {
  246. chan_div = 48; // 48
  247. f_vco = freq * chan_div;
  248. }
  249. if (vco_div > 48 && vco_div <= 64) {
  250. chan_div = 64; // 64
  251. f_vco = freq * chan_div;
  252. }
  253. if (vco_div > 64 && vco_div <= 72) {
  254. chan_div = 72; // 72
  255. f_vco = freq * chan_div;
  256. }
  257. if (vco_div > 72 && vco_div <= 96) {
  258. chan_div = 96; // 96
  259. f_vco = freq * chan_div;
  260. }
  261. if (vco_div > 96 && vco_div <= 128) {
  262. chan_div = 128; // 128
  263. f_vco = freq * chan_div;
  264. }
  265. if (vco_div > 128 && vco_div <= 192) {
  266. chan_div = 192; // 192
  267. f_vco = freq * chan_div;
  268. }
  269. if (vco_div > 192 && vco_div <= 256) {
  270. chan_div = 256; // 256
  271. f_vco = freq * chan_div;
  272. }
  273. if (vco_div > 256 && vco_div <= 384) {
  274. chan_div = 384; // 384
  275. f_vco = freq * chan_div;
  276. }
  277. if (vco_div > 384 && vco_div <= 512) {
  278. chan_div = 512; // 512
  279. f_vco = freq * chan_div;
  280. }
  281. if (vco_div > 512 && vco_div <= 768) {
  282. chan_div = 768; // 768
  283. f_vco = freq * chan_div;
  284. }
  285. switch (chan_div) {
  286. case 2:
  287. ch_div_reg = 0;
  288. break;
  289. case 4:
  290. ch_div_reg = 1;
  291. break;
  292. case 6:
  293. ch_div_reg = 2;
  294. break;
  295. case 8:
  296. ch_div_reg = 3;
  297. break;
  298. case 12:
  299. ch_div_reg = 4;
  300. break;
  301. case 16:
  302. ch_div_reg = 5;
  303. break;
  304. case 24:
  305. ch_div_reg = 6;
  306. break;
  307. case 32:
  308. ch_div_reg = 7;
  309. break;
  310. case 48:
  311. ch_div_reg = 8;
  312. break;
  313. case 64:
  314. ch_div_reg = 9;
  315. break;
  316. case 72:
  317. ch_div_reg = 10;
  318. break;
  319. case 96:
  320. ch_div_reg = 11;
  321. break;
  322. case 128:
  323. ch_div_reg = 12;
  324. break;
  325. case 192:
  326. ch_div_reg = 13;
  327. break;
  328. case 256:
  329. ch_div_reg = 14;
  330. break;
  331. case 384:
  332. ch_div_reg = 15;
  333. break;
  334. case 512:
  335. ch_div_reg = 16;
  336. break;
  337. case 768:
  338. ch_div_reg = 17;
  339. break;
  340. }
  341. } else {
  342. ch_div_reg = 0;
  343. f_vco = freq * 2;
  344. }
  345. N_div = f_vco / f_pd;
  346. // divide whole part and fractional part
  347. uint32_t N = (uint32_t) N_div;
  348. uint32_t frac_n = (uint32_t) ((N_div - N) * 524287);
  349. uint32_t frac_d = 524287;
  350. // If frac part is 0 then the denominator is 1000 and numerator is 0
  351. if (frac_n == 0) {
  352. frac_n = 0;
  353. frac_d = 524287;
  354. }
  355. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] & (~BITM_LMX2594_R44_MASH_ORDER);
  356. // Set the MASH_ORDER to 3
  357. lmx2594regs[112 - MASH_ORDER] = lmx2594regs[112 - MASH_ORDER] | ENUM_LMX2594_R44_MASH_ORDER_3;
  358. // Set PF_DLY_SEL to 3
  359. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] & (~BITM_LMX2594_R37_PFD_DLY_SEL);
  360. lmx2594regs[112 - PFD_DLY_SEL] = lmx2594regs[112 - PFD_DLY_SEL] | (0x3 << BITP_LMX2594_R37_PFD_DLY_SEL);
  361. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] & (~0xFFFF);
  362. lmx2594regs[112 - PLL_N_S] = lmx2594regs[112 - PLL_N_S] | (N >> 16);
  363. //CLear the lower 16 bits of the register
  364. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] & (~0xFFFF);
  365. // Next 16 bits of the register
  366. lmx2594regs[112 - PLL_N_M] = lmx2594regs[112 - PLL_N_M] | (N & 0xFFFF);
  367. // Clear the upper 16 bits of the register lmx2594regs[PLL_NUM_S]
  368. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] & (~0xFFFF);
  369. lmx2594regs[112 - PLL_NUM_S] = lmx2594regs[112 - PLL_NUM_S] | (frac_n >> 16);
  370. // Clear the lower 16 bits of the register lmx2594regs[PLL_NUM_M]
  371. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] & (~0xFFFF);
  372. // Next 16 bits of the numerator
  373. lmx2594regs[112 - PLL_NUM_M] = lmx2594regs[112 - PLL_NUM_M] | (frac_n & 0xFFFF);
  374. // Clear the upper 16 bits of the register lmx2594regs[PLL_DEN_S]
  375. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] & (~0xFFFF);
  376. // most significant 16 bits of the denominator
  377. lmx2594regs[112 - PLL_DEN_S] = lmx2594regs[112 - PLL_DEN_S] | (frac_d >> 16);
  378. // Clear the lower 16 bits of the register lmx2594regs[PLL_DEN_M]
  379. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] & (~0xFFFF);
  380. // Next 16 bits of the denominator
  381. lmx2594regs[112 - PLL_DEN_M] = lmx2594regs[112 - PLL_DEN_M] | (frac_d & 0xFFFF);
  382. // Program the CHDIV value
  383. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] & (~BITM_LMX2594_R75_CHDIV);
  384. // Set the CHDIV value with the starting position BITP_LMX2594_R75_CHDIV
  385. lmx2594regs[112 - CHDIV] = lmx2594regs[112 - CHDIV] | (ch_div_reg << BITP_LMX2594_R75_CHDIV);
  386. // If the ch_div > 2 then set the SEG1_EN bit
  387. if (chan_div > 2) {
  388. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] & (~BITM_LMX2594_R31_CHDIV_DIV2);
  389. lmx2594regs[112 - CHDIV_DIV2] = lmx2594regs[112 - CHDIV_DIV2] | (ENUM_LMX2594_R31_CHDIV_DIV2_EN);
  390. }
  391. // Set the OUTA_MUX to channel divider R45[12:11]; 0 - Channel divider, 1 - VCO;
  392. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] & (~BITM_LMX2594_R45_OUTA_MUX);
  393. lmx2594regs[112 - OUTA_MUX] = lmx2594regs[112 - OUTA_MUX] | ENUM_LMX2594_R45_OUTA_MUX_CH_DIV;
  394. // Program the FCAL_EN bit
  395. lmx2594regs[112 - FCAL_ADDR] = lmx2594regs[112 - FCAL_ADDR] | (LMX2594_R0_FCAL_EN);
  396. uint32_t lmx_change_freq_regs[] = {
  397. lmx2594regs[112 - PLL_N_S],
  398. lmx2594regs[112 - PLL_N_M],
  399. lmx2594regs[112 - PLL_DEN_S],
  400. lmx2594regs[112 - PLL_DEN_M],
  401. lmx2594regs[112 - PLL_NUM_S],
  402. lmx2594regs[112 - PLL_NUM_M],
  403. lmx2594regs[112 - FCAL_ADDR],
  404. lmx2594regs[112 - CHDIV_DIV2],
  405. lmx2594regs[112 - CHDIV],
  406. lmx2594regs[112 - OUTA_MUX]
  407. };
  408. // Create a header for the LMX2594 with the appropriate number of words
  409. uint32_t LMX_HEADER = ((0 << 23) | (DeviceIdLmx2594 << 18) | ((sizeof(lmx_change_freq_regs) / 4) << 1) | 1);
  410. uint32_t *ptr = bar1 + LMX_BASE_ADDR;
  411. *ptr = LMX_HEADER;
  412. // Send the data
  413. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  414. uint32_t *data_ptr = bar1 + LMX_BASE_ADDR;
  415. *data_ptr = lmx_change_freq_regs[i];
  416. }
  417. usleep(1);
  418. return 0;
  419. }
  420. int lmx_freq_set(void *bar1, uint64_t freq) {
  421. double f_pd = 200e6;
  422. double N_div = 0;
  423. if (freq < 10e6 || freq > 15e9) {
  424. printf("Frequency range is 10 MHz to 15 GHz\n");
  425. return -1;
  426. }
  427. // if the frequency is in the main band - 7.5 GHz to 15 GHz
  428. if (freq >= 7.5e9 && freq <= 15e9) {
  429. lmx_freq_set_main_band(bar1, freq, f_pd, N_div);
  430. }
  431. else if (freq < 7.5e9) {
  432. lmx_freq_set_out_of_band(bar1, freq, f_pd, N_div);
  433. }
  434. return 0;
  435. }