pll_4113.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**************************************************************************************************
  2. Filename: hal_adf4113.c
  3. Revised: $Date: 2013-11-17 $
  4. Revision: $Revision: $
  5. Description: This file contains the interface to the ADF4113 frequency synthesizer.
  6. **************************************************************************************************/
  7. #include "pll_4113.h"
  8. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2);
  9. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN);
  10. #define ADF4113_PRESCALE8 0
  11. #define ADF4113_PRESCALE16 1
  12. #define ADF4113_PRESCALE32 2
  13. #define ADF4113_PRESCALE64 3
  14. // ADF4113 Prescale value for minimum required division ratio
  15. #define ADF4113_PRE8_MIN_N 56
  16. #define ADF4113_PRE16_MIN_N 240
  17. #define ADF4113_PRE32_MIN_N 992
  18. #define ADF4113_PRE64_MIN_N 4032
  19. // Frequency Settings
  20. // Initally, the synthesizer will operate at 2450 MHz
  21. #define ADF4113_CH_STEP 50000
  22. #define HAL_SYN_INVALID_PRESCALE 0x04
  23. #define ADF4113_REF_FREQ_MHZ 13000000
  24. PLL_Setting_st ADF4113_1_8G_DL = {
  25. PLL_CLK_GPIO_Port,
  26. PLL_CLK_Pin,
  27. PLL_DATA_GPIO_Port,
  28. PLL_DATA_Pin,
  29. PLL_EN_1_8G_DL_GPIO_Port,
  30. PLL_EN_1_8G_DL_Pin,
  31. };
  32. PLL_Setting_st ADF4113_1_8G_UL = {
  33. PLL_CLK_GPIO_Port,
  34. PLL_CLK_Pin,
  35. PLL_DATA_GPIO_Port,
  36. PLL_DATA_Pin,
  37. PLL_EN_1_8G_UL_GPIO_Port,
  38. PLL_EN_1_8G_UL_Pin,
  39. };
  40. PLL_Setting_st ADF4113_2_1G_DL = {
  41. PLL_CLK_GPIO_Port,
  42. PLL_CLK_Pin,
  43. PLL_DATA_GPIO_Port,
  44. PLL_DATA_Pin,
  45. PLL_EN_2_1G_DL_GPIO_Port,
  46. PLL_EN_2_1G_DL_Pin,
  47. };
  48. PLL_Setting_st ADF4113_2_1G_UL = {
  49. PLL_CLK_GPIO_Port,
  50. PLL_CLK_Pin,
  51. PLL_DATA_GPIO_Port,
  52. PLL_DATA_Pin,
  53. PLL_EN_2_1G_UL_GPIO_Port,
  54. PLL_EN_2_1G_UL_Pin,
  55. };
  56. // Error Code
  57. typedef struct{
  58. uint16_t B;
  59. uint16_t P;
  60. uint16_t A;
  61. uint16_t N;
  62. }Adf4113_st;
  63. uint8_t halSynSetFreq(uint32_t rf_Freq)
  64. {
  65. uint32_t R, B;
  66. uint32_t A, P, p_mode;
  67. uint32_t N_val = 0;
  68. N_val = (rf_Freq / ADF4113_CH_STEP);
  69. if( N_val < ADF4113_PRE8_MIN_N) {
  70. return HAL_SYN_INVALID_PRESCALE;
  71. } else if(( N_val> ADF4113_PRE8_MIN_N) && (N_val < ADF4113_PRE16_MIN_N)) {
  72. P = 8;
  73. p_mode = ADF4113_PRESCALE8;
  74. } else if(( N_val > ADF4113_PRE16_MIN_N) && (N_val < ADF4113_PRE32_MIN_N)) {
  75. P = 16;
  76. p_mode = ADF4113_PRESCALE16;
  77. } else if((N_val > ADF4113_PRE32_MIN_N) && ( N_val < ADF4113_PRE64_MIN_N)) {
  78. P = 32;
  79. p_mode = ADF4113_PRESCALE32;
  80. } else if( N_val > ADF4113_PRE64_MIN_N) {
  81. P = 64;
  82. p_mode = ADF4113_PRESCALE64;
  83. }
  84. P = 32;
  85. B = N_val / P;
  86. A = N_val -(B * P);
  87. #ifdef DEBUG_PRINT
  88. printf("FREQ:%f Mhz B : %d , A : %d N_VAL : %d \r\n",(float)(rf_Freq/1000000),B,A,N_val);
  89. printf("YJ 4113 : %x \r\n",N_Counter_Latch_Create(A,B,0));
  90. #endif /* DEBUG_PRINT */
  91. }
  92. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN){
  93. uint32_t ret = 0;
  94. uint32_t shift_bit = 0x01;
  95. uint8_t control_bit = 1;
  96. uint8_t i = 0;
  97. uint8_t reserve = 0;
  98. #ifdef DEBUG_PRINT
  99. printf("_ACOUNTER : %d _BCOUNTER : %d \r\n",_ACOUNTER,_BCOUNTER);
  100. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  101. #endif /* DEBUG_PRINT */
  102. for(i = 0; i < 2; i++){
  103. if(control_bit & 0x01)
  104. ret += shift_bit << i;
  105. control_bit = control_bit >> 1;
  106. }
  107. #ifdef DEBUG_PRINT
  108. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  109. #endif /* DEBUG_PRINT */
  110. for(i = 2; i < 8; i++){
  111. if(_ACOUNTER & 0x01)
  112. ret += shift_bit << i;
  113. _ACOUNTER = _ACOUNTER >> 1;
  114. }
  115. #ifdef DEBUG_PRINT
  116. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  117. #endif /* DEBUG_PRINT */
  118. for(i = 8; i < 21; i++){
  119. if(_BCOUNTER & 0x01)
  120. ret += shift_bit << i;
  121. _BCOUNTER = _BCOUNTER >> 1;
  122. }
  123. #ifdef DEBUG_PRINT
  124. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  125. #endif /* DEBUG_PRINT */
  126. if(_CPGAIN & 0x01)
  127. ret += shift_bit << i++;
  128. for(i = 22; i < 24; i++){
  129. if(reserve & 0x01)
  130. ret += shift_bit << i;
  131. reserve = reserve >> 1;
  132. }
  133. #ifdef DEBUG_PRINT
  134. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  135. #endif /* DEBUG_PRINT */
  136. return ret;
  137. }
  138. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2){
  139. R2 = R2 & 0xFFFFFF;
  140. R1 = R1 & 0xFFFFFF;
  141. R0 = R0 & 0xFFFFFF;
  142. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  143. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  144. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  145. /* R2 Ctrl */
  146. for(int i =0; i < 24; i++){
  147. if(R2 & 0x800000){
  148. #ifdef DEBUG_PRINT
  149. printf("1");
  150. #endif /* DEBUG_PRINT */
  151. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  152. }
  153. else{
  154. #ifdef DEBUG_PRINT
  155. printf("0");
  156. #endif /* DEBUG_PRINT */
  157. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  158. }
  159. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  160. Pol_Delay_us(10);
  161. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  162. R2 = ((R2 << 1) & 0xFFFFFF);
  163. }
  164. #ifdef DEBUG_PRINT
  165. printf("\r\n");
  166. #endif /* DEBUG_PRINT */
  167. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  168. Pol_Delay_us(10);
  169. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  170. /* R0 Ctrl */
  171. for(int i =0; i < 24; i++){
  172. if(R0 & 0x800000){
  173. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  174. #ifdef DEBUG_PRINT
  175. printf("1");
  176. #endif /* DEBUG_PRINT */
  177. }
  178. else{
  179. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  180. #ifdef DEBUG_PRINT
  181. printf("0");
  182. #endif /* DEBUG_PRINT */
  183. }
  184. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  185. Pol_Delay_us(10);
  186. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  187. R0 = ((R0 << 1) & 0xFFFFFF);
  188. }
  189. #ifdef DEBUG_PRINT
  190. printf("\r\n");
  191. #endif /* DEBUG_PRINT */
  192. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  193. Pol_Delay_us(10);
  194. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  195. /* R1 Ctrl */
  196. for(int i =0; i < 24; i++){
  197. if(R1 & 0x800000){
  198. #ifdef DEBUG_PRINT
  199. printf("1");
  200. #endif /* DEBUG_PRINT */
  201. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  202. }
  203. else{
  204. #ifdef DEBUG_PRINT
  205. printf("0");
  206. #endif /* DEBUG_PRINT */
  207. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  208. }
  209. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  210. Pol_Delay_us(10);
  211. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  212. R1 = ((R1 << 1) & 0xFFFFFF);
  213. }
  214. #ifdef DEBUG_PRINT
  215. printf("\r\n");
  216. #endif /* DEBUG_PRINT */
  217. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  218. Pol_Delay_us(10);
  219. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  220. }