First version of the MAX2871 shield library. Includes demo program with terminal for setting frequency on channel A.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX2871.cpp Source File

MAX2871.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033  
00034  
00035 #include "MAX2871.h"
00036 #include <math.h>
00037 #include <stdio.h>
00038  
00039 //****************************************************************************
00040 MAX2871::MAX2871(SPI &spiBus, PinName le):
00041 m_spiBus(spiBus), m_le(le, 1)
00042 {
00043     f_reference = 50.0;
00044     
00045     reg0.all = 0x007d0000;
00046     reg1.all = 0x2000fff9;
00047     reg2.all = 0x00004042;
00048     reg3.all = 0x0000000b;
00049     reg4.all = 0x6180b23c;
00050     reg5.all = 0x00400005;
00051     reg6.all = 0x00000000;
00052     update();
00053 }
00054 
00055 //****************************************************************************
00056 MAX2871::~MAX2871()
00057 {
00058     //empty block
00059 }
00060 
00061 //****************************************************************************    
00062 void MAX2871::writeRegister(const uint32_t data)
00063 {
00064     m_le = 0;
00065     m_spiBus.write((0xFF000000 & data) >> 24);
00066     m_spiBus.write((0x00FF0000 & data) >> 16);
00067     m_spiBus.write((0x0000FF00 & data) >> 8);
00068     m_spiBus.write( 0x000000FF & data);
00069     m_le = 1;
00070 }
00071 
00072 //****************************************************************************    
00073 void MAX2871::readRegister6()
00074 {
00075     uint32_t raw, reg6read;
00076     
00077     reg5.bits.mux = 1;
00078     reg2.bits.mux = 0x4;
00079     writeRegister(reg5.all);
00080     writeRegister(reg2.all);
00081     
00082     writeRegister(0x00000006);
00083     
00084     m_spiBus.format(8,1);
00085     
00086     raw = m_spiBus.write(0x00);
00087     reg6read = (reg6read & 0x01FFFFF) + (raw << 25);
00088     raw = m_spiBus.write(0x00);
00089     reg6read = (reg6read & 0xFE01FFFF) + (raw << 17); 
00090     raw = m_spiBus.write(0x00);
00091     reg6read = (reg6read & 0xFFFE01FF) + (raw << 9);
00092     raw = m_spiBus.write(0x00);
00093     reg6read = (reg6read & 0xFFFFFE01) + (raw << 1);
00094     raw = m_spiBus.write(0x00);
00095     reg6read = (reg6read & 0xFFFFFFFE) + (raw >> 7);
00096     reg6.all = reg6read;
00097     
00098     m_spiBus.format(8,0);
00099 }
00100 
00101 //****************************************************************************    
00102 void MAX2871::update()
00103 {
00104     writeRegister(reg5.all);
00105     writeRegister(reg4.all);
00106     writeRegister(reg3.all);
00107     writeRegister(reg2.all);
00108     writeRegister(reg1.all);
00109     writeRegister(reg0.all);
00110 }
00111 
00112 //****************************************************************************    
00113 void MAX2871::frequency(const float freq)
00114 {
00115     uint32_t n,frac,m = 0;
00116     float pll_coefficient;
00117     
00118     float f_pfd = f_reference*(1+reg2.bits.dbr)/(reg2.bits.r*(1+reg2.bits.rdiv2));
00119     
00120     reg4.bits.diva = 0;
00121     while(freq*powf(2,reg4.bits.diva) < 3000.0)
00122     {
00123         reg4.bits.diva = reg4.bits.diva + 1;
00124     }
00125     pll_coefficient = freq*powf(2,reg4.bits.diva)/f_pfd;
00126     
00127     n = floor(pll_coefficient);
00128     pll_coefficient = pll_coefficient - n;
00129     
00130     m = 4000;
00131     frac = floor(m*pll_coefficient);
00132     
00133     reg0.bits.frac = frac;
00134     reg0.bits.n = n;
00135     reg1.bits.m = m;
00136     update();
00137 }