Library to control Silicon Labs SI570 10 MHZ TO 1.4 GHZ I2C PROGRAMMABLE XO/VCXO.

Dependents:   t2d Thing2Do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SI570.h Source File

SI570.h

00001 /* mbed SI570 Library, for driving the SI570 programable VCXO
00002  * Copyright (c) 2010, Gerrit Polder, PA3BYA
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef MBED_SI570_H
00024 #define MBED_SI570_H
00025 
00026 #include "mbed.h"
00027 
00028 //these must be floating point number especially 2^28 so that
00029 //there is enough memory to use them in the calculation
00030 #define POW_2_16              65536.0
00031 #define POW_2_24           16777216.0
00032 #define POW_2_28          268435456.0
00033 #define FOUT_START_UP            56.320     //MHz 
00034 #define PPM                    3500         // +/- max ppm from center frequency
00035 #define FDCO_MAX                5670.0         //MHz
00036 #define FDCO_MIN                4850.0         //MHz
00037 
00038 
00039 /** Interface to the popular SI570 VCXO  */
00040 class SI570 {
00041 public:
00042     /** Create an instance of the SI570 connected to specified I2C pins, with the specified address.
00043      *
00044      * Example:
00045      * @code
00046      * #include "mbed.h"
00047      * #include "TextLCD.h"
00048      * #include "SI570.h"
00049      * #include "QEI.h"
00050      * 
00051      * TextLCD lcd(p11, p12, p15, p16, p29, p30); // rs, e, d0-d3
00052      * SI570 si570(p9, p10, 0xAA);
00053      * QEI wheel (p5, p6, NC, 360);
00054      *
00055      * int main() {
00056      *     int wp,swp=0;
00057      *     float startfreq=7.0;
00058      *     float freq;
00059      *
00060      *     while (1) {
00061      *         wp =  wheel.getPulses();
00062      *         freq=startfreq+wp*0.00001;
00063      *         if (swp != wp) {
00064      *             si570.set_frequency(freq);
00065      *             swp = wp;
00066      *         }                
00067      *         lcd.locate(0,0);
00068      *         lcd.printf("%f MHz", si570.get_frequency());
00069      *     }
00070      * }
00071      * @endcode
00072      *
00073      * @param sda The I2C data pin
00074      * @param scl The I2C clock pin
00075      * @param address The I2C address for this SI570
00076      */
00077     SI570(PinName sda, PinName scl, int address);
00078 
00079     /** Resets the SI570.
00080      *
00081      * Resets and reads the startup configuration from the Si570
00082      */
00083     void si570reset(void);
00084     
00085    /** Read the current frequency.
00086      *
00087      * get the SI570 registers an calculate the current frequency
00088      *
00089      * @returns the current frequency   
00090      */
00091     double get_frequency(void);
00092 
00093    /** Read the current rfreq value.
00094      *
00095      * get the SI570 registers an calculate the current rfreq
00096      *
00097      * @returns the current rfreq   
00098      */    
00099     double get_rfreq(void);
00100     
00101     /** Read the current n1.
00102      *
00103      * get the SI570 registers an calculate the current n1
00104      *
00105      * @returns the current n1   
00106      */
00107     int get_n1(void);
00108     
00109    /** Read the current hsdiv.
00110      *
00111      * get the SI570 registers an calculate the current hsdiv
00112      *
00113      * @returns the current hsdiv   
00114      */
00115     int get_hsdiv(void); 
00116     
00117    /** Set a new frequency.
00118      *
00119      * Set the SI570 registers to output frequency
00120      * When the new frequency is within the 3500 PPM range of the center frequency, hsdiv and n1 needs not to be reprogrammed, resulting in a glitch free transition.
00121      * For changes larger than 3500 PPM, the process of freezing and unfreezing the DCO will cause the output clock to momentarily stop and start at any arbitrary point during a clock cycle. This process can take up to 10 ms.
00122      *
00123      * @param frequency the frequency to set.
00124      * @returns the current frequency   
00125      */   
00126     int set_frequency(double frequency);
00127 
00128 
00129 private:
00130     I2C _i2c;
00131     int _address;
00132     char cmd[2];
00133     char buf[6];
00134     unsigned char n1;
00135     unsigned char hsdiv;
00136     unsigned long frac_bits;
00137     double rfreq;
00138     double fxtal_device;
00139     double currentFreq;
00140     double currentRfreq;
00141 
00142     void get_registers(void);
00143     int set_frequency_small_change(double currentFrequency);
00144     int set_frequency_large_change(double currentFrequency);
00145 
00146     unsigned char SetBits(unsigned char original, unsigned char reset_mask, unsigned char new_val);
00147 };
00148 
00149 
00150 #endif