/*********************************  dco.h  ***********************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/include/dco.h,v $
 *  Copyright (C) 2003 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: dco.h,v 1.4 2004/01/08 06:48:01 brent Exp $
 *
 * Primitives for manipulating the MSP430's
 * Digitally Controlled clock Oscillator
 *
 *****************************************************************************/

#include "types.h"        //type definitions
#include "msp430x16x.h"   //register and I/O definitions

extern 
 unsigned setDCO (unsigned target);
/*
  Set the MCLK to target frequency multiple of ACLK
  It uses timer A and its associated capture register #2
  Interrupts should be disabled
  returns the actual frequency attained (in MCLKS/ACLK)
*/
  

extern 
 int adjustDCO (unsigned target, unsigned actual);
/*
  adjust the DCO paramters toward specified target frequency
  given the actual measured frequency
  
  returns -2 if target too low
          -1 if target too high
          0 if adjustment should be repeated
          1 if target frequency has been reached
*/


static inline 
int DCOup (void)
/*
  increase DCO freq by one small step
  returns zero if successful
*/
{
  DCOCTL++;
  if (DCOCTL == 0x00) {
    if ((BCSCTL1 & 0x07) == 0x07) return 1;  //err if already at highest freq
    BCSCTL1++;                    // Did DCO role over? Sel higher RSEL
  }
  return 0;
}

static inline 
int DCOdown (void)
/*
  decrease DCO freq by one small step
  returns zero if successful
*/
{
  --DCOCTL; 
  if (DCOCTL == 0xFF) {
    if (!(BCSCTL1 & 0x07)) return 1; //err if already at lowest freq
    --BCSCTL1;                      // Did DCO role over? Sel lower RSEL
  }
  return 0;
}


