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 /**
DL3LD 1:1556bcaaf759 127 * Includes
DL3LD 1:1556bcaaf759 128 */
DL3LD 1:1556bcaaf759 129 #include "QEI.h"
DL3LD 1:1556bcaaf759 130
DL3LD 1:1556bcaaf759 131 QEI::QEI(PinName channelA,
DL3LD 1:1556bcaaf759 132 PinName channelB,
DL3LD 1:1556bcaaf759 133 PinName index,
DL3LD 1:1556bcaaf759 134 int pulsesPerRev,
DL3LD 1:1556bcaaf759 135 Encoding encoding) : channelA_(channelA), channelB_(channelB),
DL3LD 1:1556bcaaf759 136 index_(index) {
DL3LD 1:1556bcaaf759 137
DL3LD 1:1556bcaaf759 138 pulses_ = 0;
DL3LD 1:1556bcaaf759 139 revolutions_ = 0;
DL3LD 1:1556bcaaf759 140 pulsesPerRev_ = pulsesPerRev;
DL3LD 1:1556bcaaf759 141 encoding_ = encoding;
DL3LD 1:1556bcaaf759 142
DL3LD 1:1556bcaaf759 143 //Workout what the current state is.
DL3LD 1:1556bcaaf759 144 int chanA = channelA_.read();
DL3LD 1:1556bcaaf759 145 int chanB = channelB_.read();
DL3LD 1:1556bcaaf759 146
DL3LD 1:1556bcaaf759 147 //2-bit state.
DL3LD 1:1556bcaaf759 148 currState_ = (chanA << 1) | (chanB);
DL3LD 1:1556bcaaf759 149 prevState_ = currState_;
DL3LD 1:1556bcaaf759 150
DL3LD 1:1556bcaaf759 151 //X2 encoding uses interrupts on only channel A.
DL3LD 1:1556bcaaf759 152 //X4 encoding uses interrupts on channel A,
DL3LD 1:1556bcaaf759 153 //and on channel B.
DL3LD 1:1556bcaaf759 154 channelA_.rise(this, &QEI::encode);
DL3LD 1:1556bcaaf759 155 channelA_.fall(this, &QEI::encode);
DL3LD 1:1556bcaaf759 156
DL3LD 1:1556bcaaf759 157 //If we're using X4 encoding, then attach interrupts to channel B too.
DL3LD 1:1556bcaaf759 158 if (encoding == X4_ENCODING) {
DL3LD 1:1556bcaaf759 159 channelB_.rise(this, &QEI::encode);
DL3LD 1:1556bcaaf759 160 channelB_.fall(this, &QEI::encode);
DL3LD 1:1556bcaaf759 161 }
DL3LD 1:1556bcaaf759 162 //Index is optional.
DL3LD 1:1556bcaaf759 163 if (index != NC) {
DL3LD 1:1556bcaaf759 164 index_.rise(this, &QEI::index);
DL3LD 1:1556bcaaf759 165 }
DL3LD 1:1556bcaaf759 166
DL3LD 1:1556bcaaf759 167 }
DL3LD 1:1556bcaaf759 168
DL3LD 1:1556bcaaf759 169 void QEI::reset(void) {
DL3LD 1:1556bcaaf759 170
DL3LD 1:1556bcaaf759 171 pulses_ = 0;
DL3LD 1:1556bcaaf759 172 revolutions_ = 0;
DL3LD 1:1556bcaaf759 173
DL3LD 1:1556bcaaf759 174 }
DL3LD 1:1556bcaaf759 175
DL3LD 1:1556bcaaf759 176 int QEI::getCurrentState(void) {
DL3LD 1:1556bcaaf759 177
DL3LD 1:1556bcaaf759 178 return currState_;
DL3LD 1:1556bcaaf759 179
DL3LD 1:1556bcaaf759 180 }
DL3LD 1:1556bcaaf759 181
DL3LD 1:1556bcaaf759 182 int QEI::getPulses(void) {
DL3LD 1:1556bcaaf759 183
DL3LD 1:1556bcaaf759 184 return pulses_;
DL3LD 1:1556bcaaf759 185
DL3LD 1:1556bcaaf759 186 }
DL3LD 1:1556bcaaf759 187
DL3LD 1:1556bcaaf759 188 int QEI::getRevolutions(void) {
DL3LD 1:1556bcaaf759 189
DL3LD 1:1556bcaaf759 190 return revolutions_;
DL3LD 1:1556bcaaf759 191
DL3LD 1:1556bcaaf759 192 }
DL3LD 1:1556bcaaf759 193
DL3LD 1:1556bcaaf759 194 // +-------------+
DL3LD 1:1556bcaaf759 195 // | X2 Encoding |
DL3LD 1:1556bcaaf759 196 // +-------------+
DL3LD 1:1556bcaaf759 197 //
DL3LD 1:1556bcaaf759 198 // When observing states two patterns will appear:
DL3LD 1:1556bcaaf759 199 //
DL3LD 1:1556bcaaf759 200 // Counter clockwise rotation:
DL3LD 1:1556bcaaf759 201 //
DL3LD 1:1556bcaaf759 202 // 10 -> 01 -> 10 -> 01 -> ...
DL3LD 1:1556bcaaf759 203 //
DL3LD 1:1556bcaaf759 204 // Clockwise rotation:
DL3LD 1:1556bcaaf759 205 //
DL3LD 1:1556bcaaf759 206 // 11 -> 00 -> 11 -> 00 -> ...
DL3LD 1:1556bcaaf759 207 //
DL3LD 1:1556bcaaf759 208 // We consider counter clockwise rotation to be "forward" and
DL3LD 1:1556bcaaf759 209 // counter clockwise to be "backward". Therefore pulse count will increase
DL3LD 1:1556bcaaf759 210 // during counter clockwise rotation and decrease during clockwise rotation.
DL3LD 1:1556bcaaf759 211 //
DL3LD 1:1556bcaaf759 212 // +-------------+
DL3LD 1:1556bcaaf759 213 // | X4 Encoding |
DL3LD 1:1556bcaaf759 214 // +-------------+
DL3LD 1:1556bcaaf759 215 //
DL3LD 1:1556bcaaf759 216 // There are four possible states for a quadrature encoder which correspond to
DL3LD 1:1556bcaaf759 217 // 2-bit gray code.
DL3LD 1:1556bcaaf759 218 //
DL3LD 1:1556bcaaf759 219 // A state change is only valid if of only one bit has changed.
DL3LD 1:1556bcaaf759 220 // A state change is invalid if both bits have changed.
DL3LD 1:1556bcaaf759 221 //
DL3LD 1:1556bcaaf759 222 // Clockwise Rotation ->
DL3LD 1:1556bcaaf759 223 //
DL3LD 1:1556bcaaf759 224 // 00 01 11 10 00
DL3LD 1:1556bcaaf759 225 //
DL3LD 1:1556bcaaf759 226 // <- Counter Clockwise Rotation
DL3LD 1:1556bcaaf759 227 //
DL3LD 1:1556bcaaf759 228 // If we observe any valid state changes going from left to right, we have
DL3LD 1:1556bcaaf759 229 // moved one pulse clockwise [we will consider this "backward" or "negative"].
DL3LD 1:1556bcaaf759 230 //
DL3LD 1:1556bcaaf759 231 // If we observe any valid state changes going from right to left we have
DL3LD 1:1556bcaaf759 232 // moved one pulse counter clockwise [we will consider this "forward" or
DL3LD 1:1556bcaaf759 233 // "positive"].
DL3LD 1:1556bcaaf759 234 //
DL3LD 1:1556bcaaf759 235 // We might enter an invalid state for a number of reasons which are hard to
DL3LD 1:1556bcaaf759 236 // predict - if this is the case, it is generally safe to ignore it, update
DL3LD 1:1556bcaaf759 237 // the state and carry on, with the error correcting itself shortly after.
DL3LD 1:1556bcaaf759 238 void QEI::encode(void) {
DL3LD 1:1556bcaaf759 239
DL3LD 1:1556bcaaf759 240 int change = 0;
DL3LD 1:1556bcaaf759 241 int chanA = channelA_.read();
DL3LD 1:1556bcaaf759 242 int chanB = channelB_.read();
DL3LD 1:1556bcaaf759 243
DL3LD 1:1556bcaaf759 244 //2-bit state.
DL3LD 1:1556bcaaf759 245 currState_ = (chanA << 1) | (chanB);
DL3LD 1:1556bcaaf759 246
DL3LD 1:1556bcaaf759 247 if (encoding_ == X2_ENCODING) {
DL3LD 1:1556bcaaf759 248
DL3LD 1:1556bcaaf759 249 //11->00->11->00 is counter clockwise rotation or "forward".
DL3LD 1:1556bcaaf759 250 if ((prevState_ == 0x3 && currState_ == 0x0) ||
DL3LD 1:1556bcaaf759 251 (prevState_ == 0x0 && currState_ == 0x3)) {
DL3LD 1:1556bcaaf759 252
DL3LD 1:1556bcaaf759 253 pulses_++;
DL3LD 1:1556bcaaf759 254
DL3LD 1:1556bcaaf759 255 }
DL3LD 1:1556bcaaf759 256 //10->01->10->01 is clockwise rotation or "backward".
DL3LD 1:1556bcaaf759 257 else if ((prevState_ == 0x2 && currState_ == 0x1) ||
DL3LD 1:1556bcaaf759 258 (prevState_ == 0x1 && currState_ == 0x2)) {
DL3LD 1:1556bcaaf759 259
DL3LD 1:1556bcaaf759 260 pulses_--;
DL3LD 1:1556bcaaf759 261
DL3LD 1:1556bcaaf759 262 }
DL3LD 1:1556bcaaf759 263
DL3LD 1:1556bcaaf759 264 } else if (encoding_ == X4_ENCODING) {
DL3LD 1:1556bcaaf759 265
DL3LD 1:1556bcaaf759 266 //Entered a new valid state.
DL3LD 1:1556bcaaf759 267 if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) {
DL3LD 1:1556bcaaf759 268 //2 bit state. Right hand bit of prev XOR left hand bit of current
DL3LD 1:1556bcaaf759 269 //gives 0 if clockwise rotation and 1 if counter clockwise rotation.
DL3LD 1:1556bcaaf759 270 change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1);
DL3LD 1:1556bcaaf759 271
DL3LD 1:1556bcaaf759 272 if (change == 0) {
DL3LD 1:1556bcaaf759 273 change = -1;
DL3LD 1:1556bcaaf759 274 }
DL3LD 1:1556bcaaf759 275
DL3LD 1:1556bcaaf759 276 pulses_ -= change;
DL3LD 1:1556bcaaf759 277 }
DL3LD 1:1556bcaaf759 278
DL3LD 1:1556bcaaf759 279 }
DL3LD 1:1556bcaaf759 280
DL3LD 1:1556bcaaf759 281 prevState_ = currState_;
DL3LD 1:1556bcaaf759 282
DL3LD 1:1556bcaaf759 283 }
DL3LD 1:1556bcaaf759 284
DL3LD 1:1556bcaaf759 285 void QEI::index(void) {
DL3LD 1:1556bcaaf759 286
DL3LD 1:1556bcaaf759 287 revolutions_++;
DL3LD 1:1556bcaaf759 288
DL3LD 1:1556bcaaf759 289 }
DL3LD 1:1556bcaaf759 290