ad9912.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "ad9912.h"
  2. #include <math.h>
  3. uint32_t ad9912regs[AD9912_COUNT] = {
  4. 0x000018,
  5. 0x000100,
  6. 0x000202,
  7. 0x000309,
  8. 0x000400,
  9. 0x000500,
  10. 0x001010,
  11. 0x001200,
  12. 0x001300,
  13. 0x002012,
  14. 0x002204,
  15. 0x010400,
  16. 0x010500,
  17. 0x010600,
  18. 0x01A633,
  19. 0x01A733,
  20. 0x01A833,
  21. 0x01A933,
  22. 0x01AA33,
  23. 0x01AB33,
  24. 0x01AC00,
  25. 0x01AD00,
  26. 0x020005,
  27. 0x020100,
  28. 0x040BFF,
  29. 0x040C01,
  30. 0x040E10,
  31. 0x050000,
  32. 0x050100,
  33. 0x050200,
  34. 0x050300,
  35. 0x050400,
  36. 0x050500,
  37. 0x050600,
  38. 0x050700,
  39. 0x050800,
  40. 0x050900
  41. };
  42. uint32_t ad9912_ftw_regs_qspi[4];
  43. /*-------------------------AD9912 INIT FUNCTION-------------------------*/
  44. void ad9912_init(reg_addr_pci* pci_bar_1) {
  45. pci_bar_1->sbtmsg_addr = INIT_GPIO1_HEADER;
  46. //Rst on
  47. pci_bar_1->sbtmsg_addr = AD9912_RST_ON;
  48. // Rst off
  49. pci_bar_1->sbtmsg_addr = GPIO_REG;
  50. //Init Header
  51. pci_bar_1->sbtmsg_addr = INIT_DDS_HEADER;
  52. //Init Data
  53. for (int k = 0; k < AD9912_COUNT; k++) {
  54. pci_bar_1->sbtmsg_addr = ad9912regs[k];
  55. }
  56. }
  57. /*----------------------------------------------------------------------*/
  58. double ad9912_set_main_band(double lmx_freq,double f_pd) {
  59. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  60. uint32_t N = (uint32_t) (lmx_freq/f_pd);
  61. if (lmx_freq <= 12500e6) {
  62. if (N < 28){
  63. N = 28;
  64. }
  65. }
  66. else if (lmx_freq > 12500e6) {
  67. if (N < 32) {
  68. N = 32;
  69. }
  70. }
  71. printf("N = %d\n", N);
  72. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  73. f_pd = lmx_freq/N; // Phase detector frequency
  74. return f_pd;
  75. }
  76. double ad9912_set_out_of_band(double lmx_freq,double f_pd) {
  77. double f_vco = 2*lmx_freq; // VCO frequency
  78. double vco_div = 7.5e9/lmx_freq;
  79. int chan_div = 2;
  80. if (f_vco < 7.5e9) {
  81. if (vco_div > 2 && vco_div <= 4) {
  82. chan_div = 4; // 4
  83. f_vco = lmx_freq * chan_div;
  84. }
  85. else if (vco_div > 4 && vco_div <= 6) {
  86. chan_div = 6; // 6
  87. f_vco = lmx_freq * chan_div;
  88. }
  89. else if (vco_div > 6 && vco_div <= 8) {
  90. chan_div = 8; // 8
  91. f_vco = lmx_freq * chan_div;
  92. }
  93. else if (vco_div > 8 && vco_div <= 12) {
  94. chan_div = 12; // 12
  95. f_vco = lmx_freq * chan_div;
  96. }
  97. else if (vco_div > 12 && vco_div <= 16) {
  98. chan_div = 16; // 16
  99. f_vco = lmx_freq * chan_div;
  100. }
  101. else if (vco_div > 16 && vco_div <= 24) {
  102. chan_div = 24; // 24
  103. f_vco = lmx_freq * chan_div;
  104. }
  105. else if (vco_div > 24 && vco_div <= 32) {
  106. chan_div = 32; // 32
  107. f_vco = lmx_freq * chan_div;
  108. }
  109. else if (vco_div > 32 && vco_div <= 48) {
  110. chan_div = 48; // 48
  111. f_vco = lmx_freq * chan_div;
  112. }
  113. else if (vco_div > 48 && vco_div <= 64) {
  114. chan_div = 64; // 64
  115. f_vco = lmx_freq * chan_div;
  116. }
  117. else if (vco_div > 64 && vco_div <= 72) {
  118. chan_div = 72; // 72
  119. f_vco = lmx_freq * chan_div;
  120. }
  121. else if (vco_div > 72 && vco_div <= 96) {
  122. chan_div = 96; // 96
  123. f_vco = lmx_freq * chan_div;
  124. }
  125. else if (vco_div > 96 && vco_div <= 128) {
  126. chan_div = 128; // 128
  127. f_vco = lmx_freq * chan_div;
  128. }
  129. else if (vco_div > 128 && vco_div <= 192) {
  130. chan_div = 192; // 192
  131. f_vco = lmx_freq * chan_div;
  132. }
  133. else if (vco_div > 192 && vco_div <= 256) {
  134. chan_div = 256; // 256
  135. f_vco = lmx_freq * chan_div;
  136. }
  137. else if (vco_div > 256 && vco_div <= 384) {
  138. chan_div = 384; // 384
  139. f_vco = lmx_freq * chan_div;
  140. }
  141. else if (vco_div > 384 && vco_div <= 512) {
  142. chan_div = 512; // 512
  143. f_vco = lmx_freq * chan_div;
  144. }
  145. else if (vco_div > 512 && vco_div <= 768) {
  146. chan_div = 768; // 768
  147. f_vco = lmx_freq * chan_div;
  148. }
  149. }
  150. else {
  151. f_vco = lmx_freq * 2;
  152. }
  153. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  154. uint32_t N = (uint32_t) (f_vco/f_pd);
  155. if (f_vco <= 12500e6) {
  156. if (N < 28){
  157. N = 28;
  158. }
  159. }
  160. else if (f_vco > 12500e6) {
  161. if (N < 32) {
  162. N = 32;
  163. }
  164. }
  165. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  166. f_pd = f_vco/N; // Phase detector frequency
  167. return f_pd;
  168. }
  169. double ad9912_set(reg_addr_pci* pci_bar_1, double freq, double f_pd) {
  170. double fs = 1e9;
  171. if (freq >= 7500e6 && freq <= 15000e6) {
  172. f_pd = ad9912_set_main_band(freq, f_pd);
  173. }
  174. else if (freq >= 10e6 && freq < 7500e6) {
  175. f_pd = ad9912_set_out_of_band(freq, f_pd);
  176. }
  177. else {
  178. return -1;
  179. }
  180. printf("f_pd = %f\n", f_pd);
  181. // FTW word
  182. double ftw_word = (f_pd*pow(2, 48))/fs;
  183. // Split the FTW word between 6 registers
  184. // First one is ftw_word[7:0]
  185. uint8_t ftw0_7_0 = (uint8_t) ftw_word;
  186. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] & ~BITM_AD9912_FTW0_FREQ_WORD_7_0;
  187. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] | (ftw0_7_0 << BITP_AD9912_FTW0_FREQ_WORD_7_0);
  188. // Second one is ftw_word[15:8]
  189. uint8_t ftw0_15_8 = (uint8_t) ((uint64_t)ftw_word >> 8);
  190. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] & ~BITM_AD9912_FTW0_FREQ_WORD_15_8;
  191. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] | (ftw0_15_8 << BITP_AD9912_FTW0_FREQ_WORD_15_8);
  192. // Third one is ftw_word[23:16]
  193. uint8_t ftw0_23_16 = (uint32_t) ((uint64_t)ftw_word >> 16);
  194. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] & ~BITM_AD9912_FTW0_FREQ_WORD_23_16;
  195. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] | (ftw0_23_16 << BITP_AD9912_FTW0_FREQ_WORD_23_16);
  196. // Fourth one is ftw_word[31:24]
  197. uint8_t ftw0_31_24 = (uint64_t) ((uint64_t)ftw_word >> 24);
  198. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] & ~BITM_AD9912_FTW0_FREQ_WORD_31_24;
  199. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] | (ftw0_31_24 << BITP_AD9912_FTW0_FREQ_WORD_31_24);
  200. // Fifth one is ftw_word[39:32]
  201. uint8_t ftw1_39_32 = (uint32_t) ((uint64_t)ftw_word >> 32);
  202. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] & ~BITM_AD9912_FTW0_FREQ_WORD_39_24;
  203. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] | (ftw1_39_32<< BITP_AD9912_FTW0_FREQ_WORD_39_24);
  204. // Sixth one is ftw_word[47:40]
  205. uint8_t ftw1_47_40 = (uint32_t) ((uint64_t)ftw_word >> 40);
  206. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] & ~BITM_AD9912_FTW0_FREQ_WORD_47_40;
  207. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] | (ftw1_47_40 << BITP_AD9912_FTW0_FREQ_WORD_47_40);
  208. // Set the frequency
  209. // uint32_t ad9912_ftw_regs[] = {
  210. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0],
  211. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8],
  212. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16],
  213. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24],
  214. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24],
  215. // ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40]
  216. // };
  217. // First 16 bits is the instruction word
  218. ad9912_ftw_regs_qspi[0] = (ENUM_AD9912_INSTRUCTION_WORD_WRITE | ENUM_AD9912_INSTRUCTION_WORD_STREAM | ENUM_ADD9912_INSTRUCTION_WORD_INIT_ADDR_9_12);
  219. ad9912_ftw_regs_qspi[1] = (ENUM_ADD9912_INSTRUCTION_WORD_INIT_ADDR_0_8 | (0x00 << BITP_AD9912_QSPI_PHASE_13_8) | (0x00 << BITP_AD9912_QSPI_PHASE_7_0));
  220. ad9912_ftw_regs_qspi[2] = ((ftw1_47_40 << BITP_AD9912_QSPI_47_40) | (ftw1_39_32 << BITP_AD9912_QSPI_39_32) | (ftw0_31_24 << BITP_AD9912_QSPI_31_24));
  221. ad9912_ftw_regs_qspi[3] = ((ftw0_23_16 << BITP_AD9912_QSPI_23_16) | (ftw0_15_8 << BITP_AD9912_QSPI_15_8) | (ftw0_7_0 << BITP_AD9912_QSPI_7_0));
  222. // // Create the appropriate header
  223. // uint32_t *dds_header = bar1 + TMSG_BASE_ADDR;
  224. // *dds_header = ((0 << 23) | (DeviceIdDDS << 18) | ((sizeof(ad9912_ftw_regs)/4) << 1) | 1);
  225. // uint32_t *dds_data = bar1 + TMSG_BASE_ADDR;
  226. // // Write the data
  227. // for (int i = 0; i < sizeof(ad9912_ftw_regs)/4; i++) {
  228. // *dds_data = ad9912_ftw_regs[i];
  229. // }
  230. return f_pd;
  231. }