/*
  sadc.c - ADC124S085 SPI ADC functions

  Copyright (c) 2008-2010 Micromint USA LLC
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.
  * Neither the name of Micromint USA LLC nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  DAMAGE.

  SPI ADCs

  Header ADC
  ------ ---
  J7-1   SADC0
  J7-3   SADC1
  J7-5   SADC2
  J7-7   SADC3
  J7-2   SADC4
  J7-4   SADC5
  J7-6   SADC6
  J7-8   SADC7
*/

#include "board.h"
#include "sadc.h"

/* PIO pins to configure */
static const Pin pinsSADC[] = {PINS_SADC};

/* Initialize the SPI ADC */
void SADC_Initialize(void)
{
  /* Enable the SPI pins used by the application */
  PIO_Configure(pinsSADC, PIO_LISTSIZE(pinsSADC));

  /* Configure SPI channel */
  SPI_Configure(SADC_BASE, 
          SADC_ID,
          /* SPI_MR - SPI Mode Register configuration */
          (1 << 0)  | /* MSTR: Master/Slave Mode (1=master) */
          (1 << 1)  | /* PS: Peripheral Select (1=variable) */
          (0 << 2)  | /* PCSDEC: Chip Select Decode (0=direct) */
          (1 << 4)  | /* MODFDIS: Mode Fault Detection (1=disabled) */
          (0 << 7)  | /* LLB: Local Loopback Enable (0=disabled) */
          (0 << 16) | /* PCS: Peripheral Chip Select (n/a,variable PS) */
          (0 << 24)); /* DLYBCS: Delay Between Chip Selects (default=6 MCK) */

  /* Configure SPI chip select */
  SPI_ConfigureNPCS(SADC_BASE, 
          SADC_CS,    /* NPCS: Chip Select (0..3) */
          /* SPI_CSR0...SPI_CSR3 SPI Chip Select Register configuration */
          (1 << 0)  | /* CPOL: Clock Polarity */
          (0 << 1)  | /* NCPHA: Clock Phase */
          (0 << 3)  | /* CSAAT: Chip Select Active After Transfer (0=no) */
          (8 << 4)  | /* BITS: Bits Per Transfer (0=8bits..8=16 bits) */
          (17 << 8) | /* SCBR: Serial Clock Baud Rate
                           SPCK=MCK/SCBR  MCK=132.096 MHz
                           ADC128S052 clock frequency: 3.2 - 8.0 MHz
                             SCBR=17  SPCK=7.770 MHz
                                  22       6.004 MHz
                                  33       4.003 MHz
                                  41       3.222 MHz
                      */
          (0 << 16) | /* DLYBS: Delay Before SPCK (default=1/2 SPCK) */
          (0 << 24)); /* DLYBCT: Delay Between Consecutive Transfers */

  /* Enable SPI */
  SPI_Enable(SADC_BASE);
}

/* Read the ADC */
unsigned int SADC_Read(unsigned int channel)
{
  /* Request channel to read */
  SPI_Write(SADC_BASE, SADC_CS, channel);
  SPI_Read(SADC_BASE);

  /* Read channel */
  SPI_Write(SADC_BASE, SADC_CS, channel);
  return SPI_Read(SADC_BASE) & SADC_MASK;
}
