ad9912.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /*-------------------------AD9912 INIT FUNCTION-------------------------*/
  43. void ad9912_init(void *bar1) {
  44. uint32_t *ptr_rst = bar1 + AD9912_BASE_ADDR;
  45. *ptr_rst = GPIO_INIT_HEADER;
  46. //Rst on
  47. *ptr_rst = AD9912_RST_ON;
  48. // Rst off
  49. *ptr_rst = GPIO_REG;
  50. //Init Header
  51. uint32_t *ptr = bar1 + AD9912_BASE_ADDR;
  52. *ptr = InitDDSHeader;
  53. //Init Data
  54. for (int k = 0; k < AD9912_COUNT; k++) {
  55. uint32_t *ptr = bar1 + AD9912_BASE_ADDR;
  56. *ptr = ad9912regs[k];
  57. }
  58. }
  59. /*----------------------------------------------------------------------*/
  60. double ad9912_set_main_band(double lmx_freq,double f_pd) {
  61. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  62. uint32_t N = (uint32_t) (lmx_freq/f_pd);
  63. if (lmx_freq <= 12500e6) {
  64. if (N < 28){
  65. N= 28;
  66. };
  67. }
  68. else if (lmx_freq > 12500e6) {
  69. if (N <32) {
  70. N = 32;
  71. }
  72. };
  73. printf("N = %d\n", N);
  74. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  75. f_pd = lmx_freq/N; // Phase detector frequency
  76. return f_pd;
  77. }
  78. double ad9912_set_out_of_band(double lmx_freq,double f_pd) {
  79. double f_vco = 2*lmx_freq; // VCO frequency
  80. double vco_div = 7.5e9/lmx_freq;
  81. int chan_div = 2;
  82. if (f_vco < 7.5e9) {
  83. if (vco_div > 2 && vco_div <= 4) {
  84. chan_div = 4; // 4
  85. f_vco = lmx_freq * chan_div;
  86. }
  87. else if (vco_div > 4 && vco_div <= 6) {
  88. chan_div = 6; // 6
  89. f_vco = lmx_freq * chan_div;
  90. }
  91. else if (vco_div > 6 && vco_div <= 8) {
  92. chan_div = 8; // 8
  93. f_vco = lmx_freq * chan_div;
  94. }
  95. else if (vco_div > 8 && vco_div <= 12) {
  96. chan_div = 12; // 12
  97. f_vco = lmx_freq * chan_div;
  98. }
  99. else if (vco_div > 12 && vco_div <= 16) {
  100. chan_div = 16; // 16
  101. f_vco = lmx_freq * chan_div;
  102. }
  103. else if (vco_div > 16 && vco_div <= 24) {
  104. chan_div = 24; // 24
  105. f_vco = lmx_freq * chan_div;
  106. }
  107. else if (vco_div > 24 && vco_div <= 32) {
  108. chan_div = 32; // 32
  109. f_vco = lmx_freq * chan_div;
  110. }
  111. else if (vco_div > 32 && vco_div <= 48) {
  112. chan_div = 48; // 48
  113. f_vco = lmx_freq * chan_div;
  114. }
  115. else if (vco_div > 48 && vco_div <= 64) {
  116. chan_div = 64; // 64
  117. f_vco = lmx_freq * chan_div;
  118. }
  119. else if (vco_div > 64 && vco_div <= 72) {
  120. chan_div = 72; // 72
  121. f_vco = lmx_freq * chan_div;
  122. }
  123. else if (vco_div > 72 && vco_div <= 96) {
  124. chan_div = 96; // 96
  125. f_vco = lmx_freq * chan_div;
  126. }
  127. else if (vco_div > 96 && vco_div <= 128) {
  128. chan_div = 128; // 128
  129. f_vco = lmx_freq * chan_div;
  130. }
  131. else if (vco_div > 128 && vco_div <= 192) {
  132. chan_div = 192; // 192
  133. f_vco = lmx_freq * chan_div;
  134. }
  135. else if (vco_div > 192 && vco_div <= 256) {
  136. chan_div = 256; // 256
  137. f_vco = lmx_freq * chan_div;
  138. }
  139. else if (vco_div > 256 && vco_div <= 384) {
  140. chan_div = 384; // 384
  141. f_vco = lmx_freq * chan_div;
  142. }
  143. else if (vco_div > 384 && vco_div <= 512) {
  144. chan_div = 512; // 512
  145. f_vco = lmx_freq * chan_div;
  146. }
  147. else if (vco_div > 512 && vco_div <= 768) {
  148. chan_div = 768; // 768
  149. f_vco = lmx_freq * chan_div;
  150. }
  151. }
  152. else {
  153. f_vco = lmx_freq * 2;
  154. }
  155. // Divide the frequncy by the old value of the phase detector frequency and only left with the integer part
  156. uint32_t N = (uint32_t) (f_vco/f_pd);
  157. if (f_vco <= 12500e6) {
  158. if (N < 28){
  159. N= 28;
  160. };
  161. }
  162. else if (f_vco > 12500e6) {
  163. if (N <32) {
  164. N = 32;
  165. }
  166. };
  167. // Calculate the new phase detector frequency by dividing the frequency by the new value of N
  168. f_pd = f_vco/N; // Phase detector frequency
  169. return f_pd;
  170. }
  171. double ad9912_set(void *bar1, double freq, double f_pd) {
  172. double fs = 1e9;
  173. int lmx_n = 50;
  174. if (freq >= 7500e6 && freq <= 15000e6) {
  175. f_pd = ad9912_set_main_band(freq,f_pd);
  176. }
  177. else if (freq >= 10e6 && freq < 7500e6) {
  178. f_pd = ad9912_set_out_of_band(freq,f_pd);
  179. }
  180. else {
  181. return -1;
  182. printf("Frequency is out of range\n");
  183. }
  184. printf("f_pd = %f\n", f_pd);
  185. // FTW word
  186. double ftw_word = (f_pd*pow(2, 48))/fs;
  187. // Split the FTW word between 6 registers
  188. // First one is ftw_word[7:0]
  189. uint8_t ftw0_7_0 = (uint8_t) ftw_word;
  190. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0] & ~BITM_AD9912_FTW0_FREQ_WORD_7_0;
  191. 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);
  192. // Second one is ftw_word[15:8]
  193. uint8_t ftw0_15_8 = (uint8_t) ((uint64_t)ftw_word >> 8);
  194. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8] & ~BITM_AD9912_FTW0_FREQ_WORD_15_8;
  195. 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);
  196. // Third one is ftw_word[23:16]
  197. uint8_t ftw0_23_16 = (uint32_t) ((uint64_t)ftw_word >> 16);
  198. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16] & ~BITM_AD9912_FTW0_FREQ_WORD_23_16;
  199. 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);
  200. // Fourth one is ftw_word[31:24]
  201. uint8_t ftw0_31_24 = (uint64_t) ((uint64_t)ftw_word >> 24);
  202. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24] & ~BITM_AD9912_FTW0_FREQ_WORD_31_24;
  203. 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);
  204. // Fifth one is ftw_word[39:32]
  205. uint8_t ftw1_39_32 = (uint32_t) ((uint64_t)ftw_word >> 32);
  206. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24] & ~BITM_AD9912_FTW0_FREQ_WORD_39_24;
  207. 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);
  208. // Sixth one is ftw_word[47:40]
  209. uint8_t ftw1_47_40 = (uint32_t) ((uint64_t)ftw_word >> 40);
  210. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] = ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40] & ~BITM_AD9912_FTW0_FREQ_WORD_47_40;
  211. 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);
  212. // Set the frequency
  213. uint32_t ad9912_ftw_regs[] = {
  214. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_7_0],
  215. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_15_8],
  216. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_23_16],
  217. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_31_24],
  218. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_39_24],
  219. ad9912regs[REGP_AD9912_FTW0_FREQ_WORD_47_40]
  220. };
  221. // Create the appropriate header
  222. uint32_t *dds_header = bar1 + AD9912_BASE_ADDR;
  223. *dds_header = ((0 << 23) | (DeviceIdDDS << 18) | ((sizeof(ad9912_ftw_regs)/4) << 1) | 1);
  224. uint32_t *dds_data = bar1 + AD9912_BASE_ADDR;
  225. // Write the data
  226. for (int i = 0; i < sizeof(ad9912_ftw_regs)/4; i++) {
  227. *dds_data = ad9912_ftw_regs[i];
  228. }
  229. return f_pd;
  230. }