/********************************  dwarfrotary.h  ****************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/include/dwarfrotary.h,v $
 *  Copyright (C) 2003 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: dwarfrotary.h,v 1.2 2004/03/25 01:11:29 brent Exp $
 *
 * This header documents the hardware interface between the
 * MSP430F1xx microcontroller and the Dwarf's rotory valve 
 * daughter board.
 *
 * Theory of operation:
 *
 * There are 4 channels of absolute rotary servo control.
 *  Each channel consists of a 2 8-bit registers to represent position
 *  and a PWM H-bridge to amplify the PWM control waveforms from the
 *  core board.
 *
 *  All four position inputs are latched in parallel by writing to
 *  a common control register and polling a status register until
 *  it indicates that the data are stable.
 *
 *  As with all dwarf daughter boards, the register addresses here are
 *  offsets from the base address of the selected board.
 *
 *****************************************************************************/

#ifndef dwarfrotary_h
#define dwarfrotary_h

#include "dwarfcore.h"  //register and I/O definitions for the mother board


// Common Control/Status register
// Write Power enable is active high
// Read Power fault status is active low
//Bit 0 = channel 0 ... Bit 3 = channel 3, upper nibble is undefined
#define DROTPWR   0     //encoder power enable/fault status

#define DROTSTAT  1     //read status, write anything to start conversion
#define DROTDAV   0x1   //mask set when position Data is AVailable to read

// Data Registers (4 pair)
#define DROT0LOW  2     //low byte of channel 0
#define DROT0HIGH 3     //high byte of channel 0
#define DROT0LOW  4     //low byte of channel 1
#define DROT0HIGH 5     //high byte of channel 1
#define DROT0LOW  6     //low byte of channel 2
#define DROT0HIGH 7     //high byte of channel 2
#define DROT0LOW  8     //low byte of channel 3
#define DROT0HIGH 9     //high byte of channel 3

#if false
/*
  Usage example:  Read position for all absolute rotary encoder channels
*/
{
  unsigned position[4];  //raw encoder positions end up here
  unsigned lowByte;
  writeDWRF (base+DROTPWR, 0xf);  //power up encoders, may need to delay also.
  if (readDWRF(base+DROTSTAT) & DROTDAV) { //start a new conversion
    driveDWRFbus();
    writeDWRFlast(0);  //write dummy value to DROTSTAT to start read cycle
    floatDWRFbus();
  }
  while (!(readDWRFlast() & DROTDAV)) ;  //poll until conversion is complete
  lowByte = readDWRFfirst(base+DROT0LOW);
  position[0] = (readDWRFnext()<<8) | lowByte;
  lowByte = readDWRFnext();
  position[1] = (readDWRFnext()<<8) | lowByte;
  lowByte = readDWRFnext();
  position[2] = (readDWRFnext()<<8) | lowByte;
  lowByte = readDWRFnext();
  position[3] = (readDWRFlast()<<8) | lowByte;
}
#endif

#endif
