mrfioc2  2.3.0
FracSynthControlWord.c
Go to the documentation of this file.
1 /**************************************************************************************************
2 |* FracSynthControlWord () -- Analyze a Micrel SY87739L Fractional Synthesizer Control Word
3 |*
4 |*--------------------------------------------------------------------------------------------------
5 |* Author: Eric Bjorklund (LANSCE)
6 |* Date: 13 September 2006
7 |*
8 |*--------------------------------------------------------------------------------------------------
9 |* MODIFICATION HISTORY:
10 |* 13 Sep 2006 E.Bjorklund Original Release
11 |*
12 |*--------------------------------------------------------------------------------------------------
13 |* REFERENCE:
14 |* Micrel SY87739L Product Description. Available at:
15 |* http://www.micrel.com
16 |*
17 |*--------------------------------------------------------------------------------------------------
18 |* MODULE DESCRIPTION:
19 |*
20 |* This function will take a the frequency (in MegaHertz) that you would like to produce
21 |* and constructs a Micrel SY87739L control word to produce that frequency. If the desired
22 |* frequency can not be produced exactly, the function will produce an output frequency
23 |* as close to the desired frequency as it can.
24 |*
25 |* The following information is displayed:
26 |*
27 |* Control Word: The Micrel SY87739L control word that will produce the desired freq.
28 |* Desired Frequency: Echo of the input parameter. The frequency you asked it to produce.
29 |* Effective Frequency: The output frequency actually produced by the control word
30 |* Error: The error ratio between the desired and effective frequencies
31 |* expressed in "parts-per-million". For the MRF Series-200
32 |* event system, the error should be less than 100 ppm.
33 |*
34 |*-------------------------------------------------------------------------------------------------
35 |* USAGE:
36 |* FracSynthControlWord <DesiredFreq>
37 |*
38 |*-------------------------------------------------------------------------------------------------
39 |* INPUT PARAMETERS:
40 |* DesiredFreq = (epicsFloat64) The desired output frequency (in MegaHertz) that you
41 |* would like the Micrel SY87739L chip to produce.
42 |*
43 |*-------------------------------------------------------------------------------------------------
44 |* RETURNS:
45 |* 0 (OK) if we were able to create a control word for the desired frequency.
46 |* -1 (ERROR) if we could not create a control word for the desired frequency.
47 |*
48 \**************************************************************************************************/
49 
50 /**************************************************************************************************
51 |* COPYRIGHT NOTIFICATION
52 |**************************************************************************************************
53 |*
54 |* THE FOLLOWING IS A NOTICE OF COPYRIGHT, AVAILABILITY OF THE CODE,
55 |* AND DISCLAIMER WHICH MUST BE INCLUDED IN THE PROLOGUE OF THE CODE
56 |* AND IN ALL SOURCE LISTINGS OF THE CODE.
57 |*
58 |**************************************************************************************************
59 |*
60 |* Copyright (c) 2006 Los Alamos National Security, LLC
61 |* as Operator of Los Alamos National Laboratory.
62 |*
63 |**************************************************************************************************
64 |*
65 |* This software is distributed under the EPICS Open License Agreement which
66 |* can be found in the file, LICENSE, included with this distribution.
67 |*
68 \*************************************************************************************************/
69 
70 /**************************************************************************************************/
71 /* Imported Header Files */
72 /**************************************************************************************************/
73 
74 #include <stdlib.h> /* Standard C utility routines and definitions */
75 #include <stdio.h> /* Standard C I/O library */
76 #include <errno.h> /* Standard C errno defintions */
77 
78 #include <epicsTypes.h> /* EPICS type definitions */
79 #ifdef _WIN32
80  #include <mrfFracSynth.h> /* MRF SY87739L control word creation & analysis prototypes */
81 #endif
82 #include <debugPrint.h> /* SLAC Debug print utility */
83 
84 #include <mrfCommon.h> /* MRF common definitions */
85 
86 #include <epicsExport.h> /* EPICS Symbol exporting macro definitions */
87 
88 /**************************************************************************************************/
89 /* Import the Fractional Synthesizer Utility Routines */
90 /**************************************************************************************************/
91 
92 #define HOST_BUILD
93 
94 #ifndef _WIN32
95 # include <mrfFracSynth.h> /* MRF SY87739L control word creation & analysis prototypes */
96 # include <mrfFracSynth.c> /* MRF SY87739L control word creation & analysis routines */
97 #endif
98 
99 /**************************************************************************************************/
100 /* Main Program */
101 /**************************************************************************************************/
102 
103 int main (int argc, char *argv[]) {
104 
105  /*---------------------
106  * Local Variables
107  */
108  int badArgs = 1; /* True if we could not parse the argument list */
109  epicsUInt32 controlWord = 0; /* Genearated SY87739L control word */
110  epicsFloat64 DesiredFreq; /* Frequency we wish to create a control word for */
111  epicsFloat64 EffectiveFreq; /* Freq. actually generated by the control word */
112  epicsFloat64 Error; /* Error between the desired and actual freqs. */
113  char *tailPtr; /* Pointer to tail of parsed control word string */
114 
115  /*---------------------
116  * Make sure we were passed only one argument.
117  * If so, see if we can parse it as a floating point value.
118  */
119  if (argc == 2) {
120  DesiredFreq = strtod (argv[1], &tailPtr);
121 
122  /*---------------------
123  * If we successfully parsed the desired frequency,
124  * try to compute a control word that will generate it.
125  */
126  if ((errno == OK) && (tailPtr != argv[1])) {
127  controlWord = FracSynthControlWord (DesiredFreq, MRF_FRAC_SYNTH_REF, DP_NONE, &Error);
128  badArgs = 0;
129 
130  /*---------------------
131  * Abort if we could not successfully create a control word for this frequency
132  */
133  if (controlWord == 0) {
134  printf ("Unable to create a control word for %f MHz.\n", DesiredFreq);
135  return ERROR;
136  }/*end if could not create control word*/
137 
138  /*---------------------
139  * Compute the effective frequency generated by the control word we created and
140  * check it for errors (we don't expect any errors, since we created it, but
141  * you never know....)
142  */
143  EffectiveFreq = FracSynthAnalyze (controlWord, MRF_FRAC_SYNTH_REF, DP_ERROR);
144 
145  /*---------------------
146  * Display the control word, the effective frequency, and the error.
147  */
148  printf ("Control Word = 0x%08X.\n", controlWord);
149  printf ("Desired Frequency = %f Mhz. Effective Frequency = %f MHz. ",
150  DesiredFreq, EffectiveFreq);
151  printf ("Error = %5.3f ppm.\n", Error);
152 
153  }/*end if control word parse was successful*/
154 
155  }/*end if we had the right number of arguments*/
156 
157  /*---------------------
158  * Print the "Usage" message if we could not parse the argument.
159  */
160  if (badArgs) {
161  printf ("Usage:\n");
162  printf ("FracSynthControlWord <DesiredFreq>\n");
163  printf (" Where <DesiredFreq> is the frequency (in MegaHertz)\n");
164  printf (" that you wish to generate an SY87739L control word for.\n");
165  }/*end if could not parse arguments*/
166 
167  /*---------------------
168  * Always return success.
169  */
170  return OK;
171 
172 }/*end main()*/
epicsShareExtern epicsUInt32 FracSynthControlWord(epicsFloat64 DesiredFreq, epicsFloat64 ReferenceFreq, epicsInt32 debugFlag, epicsFloat64 *Error)
Definition: mrfFracSynth.c:552
int main(int argc, char *argv[])
#define OK
Definition: mrfFracSynth.h:81
#define ERROR
Definition: mrfFracSynth.h:88
#define DP_ERROR
Definition: debugPrint.h:90
#define DP_NONE
Definition: debugPrint.h:88
#define MRF_FRAC_SYNTH_REF
Definition: mrfCommon.h:99
epicsShareExtern epicsFloat64 FracSynthAnalyze(epicsUInt32 ControlWord, epicsFloat64 ReferenceFreq, epicsInt32 PrintFlag)
Definition: mrfFracSynth.c:844
Support routines for the Micrel SY87739L Fractional-N Synthesizer.