adf4153(5862).c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /******************************************************************************
  2. * @file ADF4153.c
  3. * @brief Implementation of ADF4153 Driver for Microblaze processor.
  4. * @author Istvan Csomortani (istvan.csomortani@analog.com)
  5. *
  6. *******************************************************************************
  7. * Copyright 2013(c) Analog Devices, Inc.
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * - Neither the name of Analog Devices, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. * - The use of this software may or may not infringe the patent rights
  23. * of one or more patent holders. This license does not release you
  24. * from the requirement that you obtain separate licenses from these
  25. * patent holders to use this software.
  26. * - Use of the software either in source or binary form, must be run
  27. * on or directly connected to an Analog Devices Inc. component.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
  31. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  32. * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  36. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************/
  41. /*****************************************************************************/
  42. /****************************** Include Files ********************************/
  43. /*****************************************************************************/
  44. #include "adf4153.h"
  45. typedef struct _adf4153_st{
  46. unsigned long long PFD_Value;
  47. uint16_t MOD_Value;
  48. uint32_t FRAC_Value;
  49. uint16_t INT_Value;
  50. double N_Value;
  51. }adf4153_st;
  52. uint32_t pow2(uint32_t val,int32_t val2){
  53. for(uint8_t i = 0; i < val2 - 1; i++){
  54. val = val * val;
  55. }
  56. return val;
  57. }
  58. double round_up( double value, int pos )
  59. {
  60. double temp;
  61. temp = value * pow2( 10, pos ); // �썝�븯�뒗 �냼�닔�젏 �옄由ъ닔留뚰겮 10�쓽 �늻�듅�쓣 �븿
  62. temp = (int)(temp + 0.5); // 0.5瑜� �뜑�븳�썑 踰꾨┝�븯硫� 諛섏삱由쇱씠 �맖
  63. temp *= pow2( 10, -pos ); // �떎�떆 �썝�옒 �냼�닔�젏 �옄由ъ닔濡�
  64. return temp;
  65. }
  66. double N_Reg_Value_Calc(double val){
  67. return val / 1000;
  68. }
  69. uint32_t N_Divider_Reg_Create(uint16_t _FRAC,uint16_t _INT,uint8_t _FASTLOCK){
  70. uint32_t ret = 0;
  71. uint32_t shift_bit = 0x01;
  72. uint8_t control_bit = 0;
  73. uint8_t i = 0;
  74. printf("FRAC : %d INT : %d \r\n",_FRAC,_INT);
  75. for(i = 0; i < 2; i++){
  76. if(control_bit & 0x01)
  77. ret += shift_bit << i;
  78. control_bit = control_bit >> 1;
  79. }
  80. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  81. for(i = 2; i < 14; i++){
  82. if(_FRAC & 0x01)
  83. ret += shift_bit << i;
  84. _FRAC = _FRAC >> 1;
  85. }
  86. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  87. for(i = 14; i < 22; i++){
  88. if(_INT & 0x01)
  89. ret += shift_bit << i;
  90. _INT = _INT >> 1;
  91. }
  92. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  93. if(_FASTLOCK & 0x01)
  94. ret += shift_bit << i;
  95. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  96. return ret;
  97. }
  98. uint32_t R_Divider_Reg_Create(uint16_t _MOD,uint8_t _RCOUNTER,uint8_t _PRESCALER,uint8_t _RESERVED,uint8_t _MUXOUT,uint8_t LOAD_CONTROL){
  99. uint32_t ret = 0;
  100. uint32_t shift_bit = 0x01;
  101. uint8_t control_bit = 1;
  102. uint8_t i = 0;
  103. printf("_MOD : %d INT : %d \r\n",_MOD,_RCOUNTER);
  104. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  105. for(i = 0; i < 2; i++){
  106. if(control_bit & 0x01)
  107. ret += shift_bit << i;
  108. control_bit = control_bit >> 1;
  109. }
  110. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  111. for(i = 2; i < 14; i++){
  112. if(_MOD & 0x01)
  113. ret += shift_bit << i;
  114. _MOD = _MOD >> 1;
  115. }
  116. for(i = 14; i < 18; i++){
  117. if(_RCOUNTER & 0x01)
  118. ret += shift_bit << i;
  119. _RCOUNTER = _RCOUNTER >> 1;
  120. }
  121. if(_PRESCALER & 0x01)
  122. ret += shift_bit << i++;
  123. if(_RESERVED & 0x01)
  124. ret += shift_bit << i++;
  125. for(i = 19; i < 22; i++){
  126. if(_MUXOUT & 0x01)
  127. ret += shift_bit << i;
  128. _MUXOUT = _MUXOUT >> 1;
  129. }
  130. if(LOAD_CONTROL & 0x01)
  131. ret += shift_bit << i++;
  132. return ret;
  133. }
  134. void ADF4153_Freq_Calc(unsigned long long Freq,unsigned long long REFin,uint8_t R_Counter,uint32_t chspacing){
  135. adf4153_st temp_adf4153;
  136. double temp = 0;
  137. temp_adf4153.PFD_Value = REFin / (R_Counter * 1000);
  138. temp_adf4153.MOD_Value = (temp_adf4153.PFD_Value / chspacing) * 1000;
  139. temp_adf4153.N_Value = N_Reg_Value_Calc(((double)(Freq / 1000) / (double)(temp_adf4153.PFD_Value / 1000)));
  140. temp_adf4153.INT_Value = temp_adf4153.N_Value ;
  141. // printf("\r\ntemp_adf4153.N_Value : %f temp_adf4153.INT_Value : %f temp_adf4153.MOD_Value : %f \r\n",temp_adf4153.N_Value,(double)temp_adf4153.INT_Value,(double)temp_adf4153.MOD_Value);
  142. temp = temp_adf4153.N_Value - (double)temp_adf4153.INT_Value;
  143. // printf("\r\n temp_adf4153.N_Value - (double)temp_adf4153.INT_Value) : %f temp * (double)temp_adf4153.MOD_Value : %f \r\n",temp,temp * (double)temp_adf4153.MOD_Value);
  144. temp_adf4153.FRAC_Value = (float)temp * temp_adf4153.MOD_Value;
  145. printf("\r\ntemp_adf4153.N_Value : %x : %f ",temp_adf4153.N_Value,((double)(Freq / 1000) / (double)(temp_adf4153.PFD_Value / 1000)) / 1000);
  146. printf("temp_adf4153.MOD_Value : %x : %d \r\n",temp_adf4153.MOD_Value,temp_adf4153.MOD_Value);
  147. uint16_t tempmod = temp_adf4153.FRAC_Value;
  148. for(uint8_t i = 0; i < 12; i++){
  149. if(temp_adf4153.MOD_Value & 0x800){
  150. printf("1");
  151. }else{
  152. printf("0");
  153. }
  154. tempmod = tempmod << 1;
  155. }
  156. printf("\r\n");
  157. printf("temp_adf4153.FRAC_Value : %x : %d\r\n",temp_adf4153.FRAC_Value,temp_adf4153.FRAC_Value);
  158. uint16_t tempfrac = temp_adf4153.FRAC_Value;
  159. for(uint8_t i = 0; i < 12; i++){
  160. if(tempfrac & 0x800){
  161. printf("1");
  162. }else{
  163. printf("0");
  164. }
  165. tempfrac = tempfrac << 1;
  166. }
  167. printf("\r\n");
  168. printf("temp_adf4153.INT_Value : %x : %d\r\n",temp_adf4153.INT_Value,temp_adf4153.INT_Value);
  169. uint16_t tempint = temp_adf4153.INT_Value;
  170. for(uint8_t i = 0; i < 9; i++){
  171. if(tempint & 0x100){
  172. printf("1");
  173. }else{
  174. printf("0");
  175. }
  176. tempint = tempint << 1;
  177. }
  178. printf("\r\n");
  179. printf("R0: %x R1: %x \r\n",N_Divider_Reg_Create(temp_adf4153.FRAC_Value,temp_adf4153.INT_Value,0),R_Divider_Reg_Create(temp_adf4153.MOD_Value,R_Counter,1,0,2,0));
  180. // R_Divider_Reg_Create(temp_adf4153.MOD_Value,R_Counter,1,0,1,0); //prescaler 1 : 8/9 0: 4/5
  181. }
  182. void ADF_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2,uint32_t R3){
  183. R3 = R3 & 0x0007FF;
  184. R2 = R2 & 0x00FFFF;
  185. R1 = R1 & 0xFFFFFF;
  186. R0 = R0 & 0xFFFFFF;
  187. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  188. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  189. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  190. printf("YJ : R3 : %x ",R3);
  191. printf("\r\n");
  192. /* R3 Ctrl */
  193. for(int i =0; i < 11; i++){
  194. if(R3 & 0x000400){
  195. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  196. printf("1");
  197. }
  198. else{
  199. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  200. printf("0");
  201. }
  202. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  203. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  204. R3 = (R3 << 1);
  205. }
  206. printf("\r\n");
  207. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  208. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  209. /* R2 Ctrl */
  210. for(int i =0; i < 16; i++){
  211. if(R2 & 0x008000){
  212. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  213. printf("1");
  214. }
  215. else{
  216. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  217. printf("0");
  218. }
  219. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  220. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  221. R2 = ((R2 << 1) & 0x00FFFF);
  222. }
  223. printf("\r\n");
  224. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  225. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  226. /* R1 Ctrl */
  227. for(int i =0; i < 24; i++){
  228. if(R1 & 0x800000){
  229. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  230. printf("1");
  231. }
  232. else{
  233. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  234. printf("0");
  235. }
  236. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  237. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  238. R1 = ((R1 << 1) & 0xFFFFFF);
  239. }
  240. printf("\r\n");
  241. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  242. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  243. /* R0 Ctrl */
  244. for(int i =0; i < 24; i++){
  245. if(R0 & 0x800000){
  246. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  247. printf("1");
  248. }
  249. else{
  250. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  251. printf("0");
  252. }
  253. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  254. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  255. R0 = ((R0 << 1) & 0xFFFFFF);
  256. }
  257. printf("\r\n");
  258. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  259. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  260. }