pll_4113.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "includes.h"
  9. #include "zig_operate.h"
  10. #include "main.h"
  11. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2);
  12. uint32_t halSynSetFreq(uint32_t rf_Freq);
  13. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN);
  14. #define ADF4113_PRESCALE8 0
  15. #define ADF4113_PRESCALE16 1
  16. #define ADF4113_PRESCALE32 2
  17. #define ADF4113_PRESCALE64 3
  18. // ADF4113 Prescale value for minimum required division ratio
  19. #define ADF4113_PRE8_MIN_N 56
  20. #define ADF4113_PRE16_MIN_N 240
  21. #define ADF4113_PRE32_MIN_N 992
  22. #define ADF4113_PRE64_MIN_N 4032
  23. // Frequency Settings
  24. // Initally, the synthesizer will operate at 2450 MHz
  25. #define ADF4113_CH_STEP 50000
  26. #define HAL_SYN_INVALID_PRESCALE 0x04
  27. #define ADF4113_REF_FREQ_MHZ 13000000
  28. uint8_t PLL_1_8_DL_Error_Cnt = 0;
  29. uint8_t PLL_1_8_UL_Error_Cnt = 0;
  30. uint8_t PLL_2_1_DL_Error_Cnt = 0;
  31. uint8_t PLL_2_1_UL_Error_Cnt = 0;
  32. PLL_Setting_st ADF4113_1_8G_DL = {
  33. PLL_CLK_GPIO_Port,
  34. PLL_CLK_Pin,
  35. PLL_DATA_GPIO_Port,
  36. PLL_DATA_Pin,
  37. PLL_EN_1_8G_DL_GPIO_Port,
  38. PLL_EN_1_8G_DL_Pin,
  39. };
  40. PLL_Setting_st ADF4113_1_8G_UL = {
  41. PLL_CLK_GPIO_Port,
  42. PLL_CLK_Pin,
  43. PLL_DATA_GPIO_Port,
  44. PLL_DATA_Pin,
  45. PLL_EN_1_8G_UL_GPIO_Port,
  46. PLL_EN_1_8G_UL_Pin,
  47. };
  48. PLL_Setting_st ADF4113_2_1G_DL = {
  49. PLL_CLK_GPIO_Port,
  50. PLL_CLK_Pin,
  51. PLL_DATA_GPIO_Port,
  52. PLL_DATA_Pin,
  53. PLL_EN_2_1G_DL_GPIO_Port,
  54. PLL_EN_2_1G_DL_Pin,
  55. };
  56. PLL_Setting_st ADF4113_2_1G_UL = {
  57. PLL_CLK_GPIO_Port,
  58. PLL_CLK_Pin,
  59. PLL_DATA_GPIO_Port,
  60. PLL_DATA_Pin,
  61. PLL_EN_2_1G_UL_GPIO_Port,
  62. PLL_EN_2_1G_UL_Pin,
  63. };
  64. // Error Code
  65. typedef struct{
  66. uint16_t B;
  67. uint16_t P;
  68. uint16_t A;
  69. uint16_t N;
  70. }Adf4113_st;
  71. void ADF4113_Initialize(void){
  72. if(Flash_Save_data[INDEX_PLL_1_8G_DL_H] == 0 && Flash_Save_data[INDEX_PLL_1_8G_DL_L] == 0){
  73. Flash_Save_data[INDEX_PLL_1_8G_DL_H] = ((16000 & 0xFF00) >> 8);//0x47;
  74. Flash_Save_data[INDEX_PLL_1_8G_DL_L] = (16000& 0x00FF);
  75. }
  76. if(Flash_Save_data[INDEX_PLL_1_8G_UL_H] == 0 && Flash_Save_data[INDEX_PLL_1_8G_UL_L] == 0){
  77. Flash_Save_data[INDEX_PLL_1_8G_UL_H] = ((14550 & 0xFF00) >> 8);
  78. Flash_Save_data[INDEX_PLL_1_8G_UL_L] = (14550 & 0x00FF);
  79. }
  80. if(Flash_Save_data[INDEX_PLL_2_1G_DL_H] == 0 && Flash_Save_data[INDEX_PLL_2_1G_DL_L] == 0){
  81. Flash_Save_data[INDEX_PLL_2_1G_DL_H] = ((19950 & 0xFF00) >> 8);
  82. Flash_Save_data[INDEX_PLL_2_1G_DL_L] = (19950 & 0x00FF);
  83. }
  84. if(Flash_Save_data[INDEX_PLL_2_1G_UL_H] == 0 && Flash_Save_data[INDEX_PLL_2_1G_UL_L] == 0){
  85. Flash_Save_data[INDEX_PLL_2_1G_UL_H] = ((22950 & 0xFF00) >> 8);
  86. Flash_Save_data[INDEX_PLL_2_1G_UL_L] = (22950 & 0x00FF);
  87. }
  88. // ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,0x03E801,0x9F8092);
  89. // HAL_Delay(1);
  90. // ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,0x038D31,0x9f8092);
  91. // HAL_Delay(1);
  92. // ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x410,0x4DE71,0x9F8092);
  93. // HAL_Delay(1);
  94. // ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,0x59A31,0x9f8092);
  95. }
  96. void ADF4113_Check(void){
  97. uint16_t temp_val = 0;
  98. if(HAL_GPIO_ReadPin(PLL_LD_1_8G_DL_GPIO_Port, PLL_LD_1_8G_DL_Pin) == GPIO_PIN_RESET){
  99. temp_val = (Prev_data[INDEX_PLL_1_8G_DL_H] << 8) | (Prev_data[INDEX_PLL_1_8G_DL_L]);
  100. // ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  101. ADF4113_Module_Ctrl(ADF4113_1_8G_DL,0x000410,0x03E801,0x9F8092);
  102. if(PLL_1_8_DL_Error_Cnt == 3){
  103. Error_Message_Occur(DL_1_8);
  104. }
  105. if(PLL_1_8_DL_Error_Cnt < 4)
  106. PLL_1_8_DL_Error_Cnt++;
  107. HAL_Delay(1);
  108. }else{
  109. PLL_1_8_DL_Error_Cnt = 0;
  110. }
  111. if(HAL_GPIO_ReadPin(PLL_LD_1_8G_UL_GPIO_Port, PLL_LD_1_8G_UL_Pin) == GPIO_PIN_RESET){
  112. temp_val = (Prev_data[INDEX_PLL_1_8G_UL_H] << 8) | (Prev_data[INDEX_PLL_1_8G_UL_L]);
  113. ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  114. // ADF4113_Module_Ctrl(ADF4113_1_8G_UL,0x000410,0x038D31,0x9f8092);
  115. if(PLL_1_8_UL_Error_Cnt == 3){
  116. Error_Message_Occur(UL_1_8);
  117. }
  118. if(PLL_1_8_UL_Error_Cnt < 4)
  119. PLL_1_8_UL_Error_Cnt++;
  120. HAL_Delay(1);
  121. }else{
  122. PLL_1_8_UL_Error_Cnt = 0;
  123. }
  124. if(HAL_GPIO_ReadPin(PLL_LD_2_1G_DL_GPIO_Port, PLL_LD_2_1G_DL_Pin) == GPIO_PIN_RESET){
  125. temp_val = (Prev_data[INDEX_PLL_2_1G_DL_H] << 8) | (Prev_data[INDEX_PLL_2_1G_DL_L]);
  126. // ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  127. ADF4113_Module_Ctrl(ADF4113_2_1G_DL,0x410,0x4DE71,0x9F8092);
  128. if(PLL_2_1_DL_Error_Cnt == 3){
  129. Error_Message_Occur(DL_2_1);
  130. }
  131. if(PLL_2_1_DL_Error_Cnt < 4)
  132. PLL_2_1_DL_Error_Cnt++;
  133. HAL_Delay(1);
  134. }else{
  135. PLL_2_1_DL_Error_Cnt = 0;
  136. }
  137. if(HAL_GPIO_ReadPin(PLL_LD_2_1G_UL_GPIO_Port, PLL_LD_2_1G_UL_Pin) == GPIO_PIN_RESET){
  138. temp_val = (Prev_data[INDEX_PLL_2_1G_UL_H] << 8) | (Prev_data[INDEX_PLL_2_1G_UL_L]);
  139. // ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,halSynSetFreq((temp_val * 1000000) / 10 ),0x9F8092);
  140. ADF4113_Module_Ctrl(ADF4113_2_1G_UL,0x000410,0x59A31,0x9f8092);
  141. if(PLL_2_1_UL_Error_Cnt == 3){
  142. Error_Message_Occur(UL_2_1);
  143. }
  144. if(PLL_2_1_UL_Error_Cnt < 4)
  145. PLL_2_1_UL_Error_Cnt++;
  146. HAL_Delay(1);
  147. }else{
  148. PLL_2_1_UL_Error_Cnt = 0;
  149. }
  150. }
  151. uint32_t halSynSetFreq(uint32_t rf_Freq)
  152. {
  153. uint32_t B,A, P;
  154. uint32_t N_val = 0;
  155. N_val = (rf_Freq / ADF4113_CH_STEP);
  156. if( N_val < ADF4113_PRE8_MIN_N) {
  157. return HAL_SYN_INVALID_PRESCALE;
  158. } else if(( N_val> ADF4113_PRE8_MIN_N) && (N_val < ADF4113_PRE16_MIN_N)) {
  159. P = 8;
  160. //p_mode = ADF4113_PRESCALE8;
  161. } else if(( N_val > ADF4113_PRE16_MIN_N) && (N_val < ADF4113_PRE32_MIN_N)) {
  162. P = 16;
  163. // p_mode = ADF4113_PRESCALE16;
  164. } else if((N_val > ADF4113_PRE32_MIN_N) && ( N_val < ADF4113_PRE64_MIN_N)) {
  165. P = 32;
  166. // p_mode = ADF4113_PRESCALE32;
  167. } else if( N_val > ADF4113_PRE64_MIN_N) {
  168. P = 64;
  169. // p_mode = ADF4113_PRESCALE64;
  170. }
  171. P = 32;
  172. B = N_val / P;
  173. A = N_val -(B * P);
  174. // printf("FREQ:%f Mhz B : %d , A : %d N_VAL : %d \r\n",(float)(rf_Freq/1000000),B,A,N_val);
  175. // printf("YJ 4113 : %x \r\n",N_Counter_Latch_Create(A,B,0));
  176. return N_Counter_Latch_Create(A,B,0);
  177. }
  178. uint32_t N_Counter_Latch_Create(uint16_t _ACOUNTER,uint16_t _BCOUNTER,uint8_t _CPGAIN){
  179. uint32_t ret = 0;
  180. uint32_t shift_bit = 0x01;
  181. uint8_t control_bit = 1;
  182. uint8_t i = 0;
  183. uint8_t reserve = 0;
  184. // printf("_ACOUNTER : %d _BCOUNTER : %d \r\n",_ACOUNTER,_BCOUNTER);
  185. // printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  186. for(i = 0; i < 2; i++){
  187. if(control_bit & 0x01)
  188. ret += shift_bit << i;
  189. control_bit = control_bit >> 1;
  190. }
  191. #ifdef DEBUG_PRINT
  192. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  193. #endif /* DEBUG_PRINT */
  194. for(i = 2; i < 8; i++){
  195. if(_ACOUNTER & 0x01)
  196. ret += shift_bit << i;
  197. _ACOUNTER = _ACOUNTER >> 1;
  198. }
  199. #ifdef DEBUG_PRINT
  200. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  201. #endif /* DEBUG_PRINT */
  202. for(i = 8; i < 21; i++){
  203. if(_BCOUNTER & 0x01)
  204. ret += shift_bit << i;
  205. _BCOUNTER = _BCOUNTER >> 1;
  206. }
  207. #ifdef DEBUG_PRINT
  208. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  209. #endif /* DEBUG_PRINT */
  210. if(_CPGAIN & 0x01)
  211. ret += shift_bit << i++;
  212. for(i = 22; i < 24; i++){
  213. if(reserve & 0x01)
  214. ret += shift_bit << i;
  215. reserve = reserve >> 1;
  216. }
  217. #ifdef DEBUG_PRINT
  218. printf("\r\nLINE : %d ret : %x\r\n",__LINE__,ret);
  219. #endif /* DEBUG_PRINT */
  220. return ret;
  221. }
  222. void ADF4113_Module_Ctrl(PLL_Setting_st pll,uint32_t R0,uint32_t R1,uint32_t R2){
  223. R2 = R2 & 0xFFFFFF;
  224. R1 = R1 & 0xFFFFFF;
  225. R0 = R0 & 0xFFFFFF;
  226. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  227. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  228. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  229. /* R2 Ctrl */
  230. for(int i =0; i < 24; i++){
  231. if(R2 & 0x800000){
  232. #if 0 // PYJ.2019.08.11_BEGIN --
  233. printf("1");
  234. #endif // PYJ.2019.08.11_END --
  235. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  236. }
  237. else{
  238. #if 0 // PYJ.2019.08.11_BEGIN --
  239. printf("0");
  240. #endif // PYJ.2019.08.11_END --
  241. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  242. }
  243. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  244. Pol_Delay_us(10);
  245. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  246. R2 = ((R2 << 1) & 0xFFFFFF);
  247. }
  248. #if 0 // PYJ.2019.08.11_BEGIN --
  249. printf("\r\n");
  250. #endif // PYJ.2019.08.11_END --
  251. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  252. Pol_Delay_us(10);
  253. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  254. /* R0 Ctrl */
  255. for(int i =0; i < 24; i++){
  256. if(R0 & 0x800000){
  257. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  258. #if 0 // PYJ.2019.08.11_BEGIN --
  259. printf("1");
  260. #endif // PYJ.2019.08.11_END --
  261. }
  262. else{
  263. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  264. #if 0 // PYJ.2019.08.11_BEGIN --
  265. printf("0");
  266. #endif // PYJ.2019.08.11_END --
  267. }
  268. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  269. Pol_Delay_us(10);
  270. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  271. R0 = ((R0 << 1) & 0xFFFFFF);
  272. }
  273. #if 0 // PYJ.2019.08.11_BEGIN --
  274. printf("\r\n");
  275. #endif // PYJ.2019.08.11_END --
  276. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  277. Pol_Delay_us(10);
  278. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  279. /* R1 Ctrl */
  280. for(int i =0; i < 24; i++){
  281. if(R1 & 0x800000){
  282. #if 0 // PYJ.2019.08.11_BEGIN --
  283. printf("1");
  284. #endif // PYJ.2019.08.11_END --
  285. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_SET);
  286. }
  287. else{
  288. #if 0 // PYJ.2019.08.11_BEGIN --
  289. printf("0");
  290. #endif // PYJ.2019.08.11_END --
  291. HAL_GPIO_WritePin(pll.PLL_DATA_PORT, pll.PLL_DATA_PIN, GPIO_PIN_RESET);
  292. }
  293. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_SET);
  294. Pol_Delay_us(10);
  295. HAL_GPIO_WritePin(pll.PLL_CLK_PORT, pll.PLL_CLK_PIN, GPIO_PIN_RESET);
  296. R1 = ((R1 << 1) & 0xFFFFFF);
  297. }
  298. #if 0 // PYJ.2019.08.11_BEGIN --
  299. printf("\r\n");
  300. #endif // PYJ.2019.08.11_END --
  301. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_SET);
  302. Pol_Delay_us(10);
  303. HAL_GPIO_WritePin(pll.PLL_ENABLE_PORT, pll.PLL_ENABLE_PIN, GPIO_PIN_RESET);
  304. }