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

Dependencies:   mbed

Fork of SI570 by Gerrit Polder

Committer:
DL3LD
Date:
Sun Mar 27 06:55:59 2016 +0000
Revision:
1:1556bcaaf759
STM32F746NG SI570 VFO Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DL3LD 1:1556bcaaf759 1 /**
DL3LD 1:1556bcaaf759 2 * @author Aaron Berk
DL3LD 1:1556bcaaf759 3 *
DL3LD 1:1556bcaaf759 4 * @section LICENSE
DL3LD 1:1556bcaaf759 5 *
DL3LD 1:1556bcaaf759 6 * Copyright (c) 2010 ARM Limited
DL3LD 1:1556bcaaf759 7 *
DL3LD 1:1556bcaaf759 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
DL3LD 1:1556bcaaf759 9 * of this software and associated documentation files (the "Software"), to deal
DL3LD 1:1556bcaaf759 10 * in the Software without restriction, including without limitation the rights
DL3LD 1:1556bcaaf759 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DL3LD 1:1556bcaaf759 12 * copies of the Software, and to permit persons to whom the Software is
DL3LD 1:1556bcaaf759 13 * furnished to do so, subject to the following conditions:
DL3LD 1:1556bcaaf759 14 *
DL3LD 1:1556bcaaf759 15 * The above copyright notice and this permission notice shall be included in
DL3LD 1:1556bcaaf759 16 * all copies or substantial portions of the Software.
DL3LD 1:1556bcaaf759 17 *
DL3LD 1:1556bcaaf759 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DL3LD 1:1556bcaaf759 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DL3LD 1:1556bcaaf759 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DL3LD 1:1556bcaaf759 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DL3LD 1:1556bcaaf759 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DL3LD 1:1556bcaaf759 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DL3LD 1:1556bcaaf759 24 * THE SOFTWARE.
DL3LD 1:1556bcaaf759 25 *
DL3LD 1:1556bcaaf759 26 * @section DESCRIPTION
DL3LD 1:1556bcaaf759 27 *
DL3LD 1:1556bcaaf759 28 * Quadrature Encoder Interface.
DL3LD 1:1556bcaaf759 29 *
DL3LD 1:1556bcaaf759 30 * A quadrature encoder consists of two code tracks on a disc which are 90
DL3LD 1:1556bcaaf759 31 * degrees out of phase. It can be used to determine how far a wheel has
DL3LD 1:1556bcaaf759 32 * rotated, relative to a known starting position.
DL3LD 1:1556bcaaf759 33 *
DL3LD 1:1556bcaaf759 34 * Only one code track changes at a time leading to a more robust system than
DL3LD 1:1556bcaaf759 35 * a single track, because any jitter around any edge won't cause a state
DL3LD 1:1556bcaaf759 36 * change as the other track will remain constant.
DL3LD 1:1556bcaaf759 37 *
DL3LD 1:1556bcaaf759 38 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
DL3LD 1:1556bcaaf759 39 * and paper code tracks consisting of alternating black and white sections;
DL3LD 1:1556bcaaf759 40 * alternatively, complete disk and PCB emitter/receiver encoder systems can
DL3LD 1:1556bcaaf759 41 * be bought, but the interface, regardless of implementation is the same.
DL3LD 1:1556bcaaf759 42 *
DL3LD 1:1556bcaaf759 43 * +-----+ +-----+ +-----+
DL3LD 1:1556bcaaf759 44 * Channel A | ^ | | | | |
DL3LD 1:1556bcaaf759 45 * ---+ ^ +-----+ +-----+ +-----
DL3LD 1:1556bcaaf759 46 * ^ ^
DL3LD 1:1556bcaaf759 47 * ^ +-----+ +-----+ +-----+
DL3LD 1:1556bcaaf759 48 * Channel B ^ | | | | | |
DL3LD 1:1556bcaaf759 49 * ------+ +-----+ +-----+ +-----
DL3LD 1:1556bcaaf759 50 * ^ ^
DL3LD 1:1556bcaaf759 51 * ^ ^
DL3LD 1:1556bcaaf759 52 * 90deg
DL3LD 1:1556bcaaf759 53 *
DL3LD 1:1556bcaaf759 54 * The interface uses X2 encoding by default which calculates the pulse count
DL3LD 1:1556bcaaf759 55 * based on reading the current state after each rising and falling edge of
DL3LD 1:1556bcaaf759 56 * channel A.
DL3LD 1:1556bcaaf759 57 *
DL3LD 1:1556bcaaf759 58 * +-----+ +-----+ +-----+
DL3LD 1:1556bcaaf759 59 * Channel A | | | | | |
DL3LD 1:1556bcaaf759 60 * ---+ +-----+ +-----+ +-----
DL3LD 1:1556bcaaf759 61 * ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 62 * ^ +-----+ ^ +-----+ ^ +-----+
DL3LD 1:1556bcaaf759 63 * Channel B ^ | ^ | ^ | ^ | ^ | |
DL3LD 1:1556bcaaf759 64 * ------+ ^ +-----+ ^ +-----+ +--
DL3LD 1:1556bcaaf759 65 * ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 66 * ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 67 * Pulse count 0 1 2 3 4 5 ...
DL3LD 1:1556bcaaf759 68 *
DL3LD 1:1556bcaaf759 69 * This interface can also use X4 encoding which calculates the pulse count
DL3LD 1:1556bcaaf759 70 * based on reading the current state after each rising and falling edge of
DL3LD 1:1556bcaaf759 71 * either channel.
DL3LD 1:1556bcaaf759 72 *
DL3LD 1:1556bcaaf759 73 * +-----+ +-----+ +-----+
DL3LD 1:1556bcaaf759 74 * Channel A | | | | | |
DL3LD 1:1556bcaaf759 75 * ---+ +-----+ +-----+ +-----
DL3LD 1:1556bcaaf759 76 * ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 77 * ^ +-----+ ^ +-----+ ^ +-----+
DL3LD 1:1556bcaaf759 78 * Channel B ^ | ^ | ^ | ^ | ^ | |
DL3LD 1:1556bcaaf759 79 * ------+ ^ +-----+ ^ +-----+ +--
DL3LD 1:1556bcaaf759 80 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 81 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 82 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
DL3LD 1:1556bcaaf759 83 *
DL3LD 1:1556bcaaf759 84 * It defaults
DL3LD 1:1556bcaaf759 85 *
DL3LD 1:1556bcaaf759 86 * An optional index channel can be used which determines when a full
DL3LD 1:1556bcaaf759 87 * revolution has occured.
DL3LD 1:1556bcaaf759 88 *
DL3LD 1:1556bcaaf759 89 * If a 4 pules per revolution encoder was used, with X4 encoding,
DL3LD 1:1556bcaaf759 90 * the following would be observed.
DL3LD 1:1556bcaaf759 91 *
DL3LD 1:1556bcaaf759 92 * +-----+ +-----+ +-----+
DL3LD 1:1556bcaaf759 93 * Channel A | | | | | |
DL3LD 1:1556bcaaf759 94 * ---+ +-----+ +-----+ +-----
DL3LD 1:1556bcaaf759 95 * ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 96 * ^ +-----+ ^ +-----+ ^ +-----+
DL3LD 1:1556bcaaf759 97 * Channel B ^ | ^ | ^ | ^ | ^ | |
DL3LD 1:1556bcaaf759 98 * ------+ ^ +-----+ ^ +-----+ +--
DL3LD 1:1556bcaaf759 99 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 100 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 101 * ^ ^ ^ +--+ ^ ^ +--+ ^
DL3LD 1:1556bcaaf759 102 * ^ ^ ^ | | ^ ^ | | ^
DL3LD 1:1556bcaaf759 103 * Index ------------+ +--------+ +-----------
DL3LD 1:1556bcaaf759 104 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
DL3LD 1:1556bcaaf759 105 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
DL3LD 1:1556bcaaf759 106 * Rev. count 0 1 2
DL3LD 1:1556bcaaf759 107 *
DL3LD 1:1556bcaaf759 108 * Rotational position in degrees can be calculated by:
DL3LD 1:1556bcaaf759 109 *
DL3LD 1:1556bcaaf759 110 * (pulse count / X * N) * 360
DL3LD 1:1556bcaaf759 111 *
DL3LD 1:1556bcaaf759 112 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
DL3LD 1:1556bcaaf759 113 * of pulses per revolution.
DL3LD 1:1556bcaaf759 114 *
DL3LD 1:1556bcaaf759 115 * Linear position can be calculated by:
DL3LD 1:1556bcaaf759 116 *
DL3LD 1:1556bcaaf759 117 * (pulse count / X * N) * (1 / PPI)
DL3LD 1:1556bcaaf759 118 *
DL3LD 1:1556bcaaf759 119 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
DL3LD 1:1556bcaaf759 120 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
DL3LD 1:1556bcaaf759 121 * any other unit of displacement. PPI can be calculated by taking the
DL3LD 1:1556bcaaf759 122 * circumference of the wheel or encoder disk and dividing it by the number
DL3LD 1:1556bcaaf759 123 * of pulses per revolution.
DL3LD 1:1556bcaaf759 124 */
DL3LD 1:1556bcaaf759 125
DL3LD 1:1556bcaaf759 126 #ifndef QEI_H
DL3LD 1:1556bcaaf759 127 #define QEI_H
DL3LD 1:1556bcaaf759 128
DL3LD 1:1556bcaaf759 129 /**
DL3LD 1:1556bcaaf759 130 * Includes
DL3LD 1:1556bcaaf759 131 */
DL3LD 1:1556bcaaf759 132 #include "mbed.h"
DL3LD 1:1556bcaaf759 133
DL3LD 1:1556bcaaf759 134 /**
DL3LD 1:1556bcaaf759 135 * Defines
DL3LD 1:1556bcaaf759 136 */
DL3LD 1:1556bcaaf759 137 #define PREV_MASK 0x1 //Mask for the previous state in determining direction
DL3LD 1:1556bcaaf759 138 //of rotation.
DL3LD 1:1556bcaaf759 139 #define CURR_MASK 0x2 //Mask for the current state in determining direction
DL3LD 1:1556bcaaf759 140 //of rotation.
DL3LD 1:1556bcaaf759 141 #define INVALID 0x3 //XORing two states where both bits have changed.
DL3LD 1:1556bcaaf759 142
DL3LD 1:1556bcaaf759 143 /**
DL3LD 1:1556bcaaf759 144 * Quadrature Encoder Interface.
DL3LD 1:1556bcaaf759 145 */
DL3LD 1:1556bcaaf759 146 class QEI {
DL3LD 1:1556bcaaf759 147
DL3LD 1:1556bcaaf759 148 public:
DL3LD 1:1556bcaaf759 149
DL3LD 1:1556bcaaf759 150 typedef enum Encoding {
DL3LD 1:1556bcaaf759 151
DL3LD 1:1556bcaaf759 152 X2_ENCODING,
DL3LD 1:1556bcaaf759 153 X4_ENCODING
DL3LD 1:1556bcaaf759 154
DL3LD 1:1556bcaaf759 155 } Encoding;
DL3LD 1:1556bcaaf759 156
DL3LD 1:1556bcaaf759 157 /**
DL3LD 1:1556bcaaf759 158 * Constructor.
DL3LD 1:1556bcaaf759 159 *
DL3LD 1:1556bcaaf759 160 * Reads the current values on channel A and channel B to determine the
DL3LD 1:1556bcaaf759 161 * initial state.
DL3LD 1:1556bcaaf759 162 *
DL3LD 1:1556bcaaf759 163 * Attaches the encode function to the rise/fall interrupt edges of
DL3LD 1:1556bcaaf759 164 * channels A and B to perform X4 encoding.
DL3LD 1:1556bcaaf759 165 *
DL3LD 1:1556bcaaf759 166 * Attaches the index function to the rise interrupt edge of channel index
DL3LD 1:1556bcaaf759 167 * (if it is used) to count revolutions.
DL3LD 1:1556bcaaf759 168 *
DL3LD 1:1556bcaaf759 169 * @param channelA mbed pin for channel A input.
DL3LD 1:1556bcaaf759 170 * @param channelB mbed pin for channel B input.
DL3LD 1:1556bcaaf759 171 * @param index mbed pin for optional index channel input,
DL3LD 1:1556bcaaf759 172 * (pass NC if not needed).
DL3LD 1:1556bcaaf759 173 * @param pulsesPerRev Number of pulses in one revolution.
DL3LD 1:1556bcaaf759 174 * @param encoding The encoding to use. Uses X2 encoding by default. X2
DL3LD 1:1556bcaaf759 175 * encoding uses interrupts on the rising and falling edges
DL3LD 1:1556bcaaf759 176 * of only channel A where as X4 uses them on both
DL3LD 1:1556bcaaf759 177 * channels.
DL3LD 1:1556bcaaf759 178 */
DL3LD 1:1556bcaaf759 179 QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING);
DL3LD 1:1556bcaaf759 180
DL3LD 1:1556bcaaf759 181 /**
DL3LD 1:1556bcaaf759 182 * Reset the encoder.
DL3LD 1:1556bcaaf759 183 *
DL3LD 1:1556bcaaf759 184 * Sets the pulses and revolutions count to zero.
DL3LD 1:1556bcaaf759 185 */
DL3LD 1:1556bcaaf759 186 void reset(void);
DL3LD 1:1556bcaaf759 187
DL3LD 1:1556bcaaf759 188 /**
DL3LD 1:1556bcaaf759 189 * Read the state of the encoder.
DL3LD 1:1556bcaaf759 190 *
DL3LD 1:1556bcaaf759 191 * @return The current state of the encoder as a 2-bit number, where:
DL3LD 1:1556bcaaf759 192 * bit 1 = The reading from channel B
DL3LD 1:1556bcaaf759 193 * bit 2 = The reading from channel A
DL3LD 1:1556bcaaf759 194 */
DL3LD 1:1556bcaaf759 195 int getCurrentState(void);
DL3LD 1:1556bcaaf759 196
DL3LD 1:1556bcaaf759 197 /**
DL3LD 1:1556bcaaf759 198 * Read the number of pulses recorded by the encoder.
DL3LD 1:1556bcaaf759 199 *
DL3LD 1:1556bcaaf759 200 * @return Number of pulses which have occured.
DL3LD 1:1556bcaaf759 201 */
DL3LD 1:1556bcaaf759 202 int getPulses(void);
DL3LD 1:1556bcaaf759 203
DL3LD 1:1556bcaaf759 204 /**
DL3LD 1:1556bcaaf759 205 * Read the number of revolutions recorded by the encoder on the index channel.
DL3LD 1:1556bcaaf759 206 *
DL3LD 1:1556bcaaf759 207 * @return Number of revolutions which have occured on the index channel.
DL3LD 1:1556bcaaf759 208 */
DL3LD 1:1556bcaaf759 209 int getRevolutions(void);
DL3LD 1:1556bcaaf759 210
DL3LD 1:1556bcaaf759 211 private:
DL3LD 1:1556bcaaf759 212
DL3LD 1:1556bcaaf759 213 /**
DL3LD 1:1556bcaaf759 214 * Update the pulse count.
DL3LD 1:1556bcaaf759 215 *
DL3LD 1:1556bcaaf759 216 * Called on every rising/falling edge of channels A/B.
DL3LD 1:1556bcaaf759 217 *
DL3LD 1:1556bcaaf759 218 * Reads the state of the channels and determines whether a pulse forward
DL3LD 1:1556bcaaf759 219 * or backward has occured, updating the count appropriately.
DL3LD 1:1556bcaaf759 220 */
DL3LD 1:1556bcaaf759 221 void encode(void);
DL3LD 1:1556bcaaf759 222
DL3LD 1:1556bcaaf759 223 /**
DL3LD 1:1556bcaaf759 224 * Called on every rising edge of channel index to update revolution
DL3LD 1:1556bcaaf759 225 * count by one.
DL3LD 1:1556bcaaf759 226 */
DL3LD 1:1556bcaaf759 227 void index(void);
DL3LD 1:1556bcaaf759 228
DL3LD 1:1556bcaaf759 229 Encoding encoding_;
DL3LD 1:1556bcaaf759 230
DL3LD 1:1556bcaaf759 231 InterruptIn channelA_;
DL3LD 1:1556bcaaf759 232 InterruptIn channelB_;
DL3LD 1:1556bcaaf759 233 InterruptIn index_;
DL3LD 1:1556bcaaf759 234
DL3LD 1:1556bcaaf759 235 int pulsesPerRev_;
DL3LD 1:1556bcaaf759 236 int prevState_;
DL3LD 1:1556bcaaf759 237 int currState_;
DL3LD 1:1556bcaaf759 238
DL3LD 1:1556bcaaf759 239 volatile int pulses_;
DL3LD 1:1556bcaaf759 240 volatile int revolutions_;
DL3LD 1:1556bcaaf759 241
DL3LD 1:1556bcaaf759 242 };
DL3LD 1:1556bcaaf759 243
DL3LD 1:1556bcaaf759 244 #endif /* QEI_H */
DL3LD 1:1556bcaaf759 245