/*
  ssp_dac.c - SPI Digital to Analog functions for the Micromint Lincoln 60 
              (Cortex-M3)
          by Micromint Support <support@micromint.com>    June-2011

  Copyright (c) 2008-20011 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:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. 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.
  3. Neither the name Micromint 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 OWNER 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.
*/
#include "ssp_dac.h"

/* -------------------- DAC functions -------------------- */
/*******************************************************************************
 
 Configure the interface to the DAC

*******************************************************************************/
void Init_SSP_Dac(void)
{
  PINSEL_CFG_Type PinCfg;
  /* SSP Configuration structure variable */
  SSP_CFG_Type SSP_ConfigStruct;
  /* Initialize SSP pin connect
     P2.8 - DAC SYNC Pin
     P0.15 - SSP0 Clock
     P0.18 - SSP0 Tx (MOSI0)    */    
  PinCfg.OpenDrain = 0;
  PinCfg.Pinmode = 0;
  /* Configure DAC Sync pin */
  PinCfg.Funcnum = SSP_DAC_SYNC_PIN_FUNCTION;
  PinCfg.Portnum = SSP_DAC_SYNC_PIN_PORT;    
  PinCfg.Pinnum = SSP_DAC_SYNC_PIN;
  PINSEL_ConfigPin(&PinCfg);
  SET_SSP_DAC_SYNC_DIR;
  SSP_DAC_DISABLE;
  /* Configure SSP0 pins */
  PinCfg.Funcnum = SSP_DAC_PIN_FUNCTION;
  PinCfg.Portnum = SSP_DAC_GPIO_PORT;    
  PinCfg.Pinnum = SSP_DAC_CLK_PIN ;
  PINSEL_ConfigPin(&PinCfg);
  PinCfg.Pinnum = SSP_DAC_TX_PIN;
  PINSEL_ConfigPin(&PinCfg);
  /* Initializing Master SSP device section */
  SSP_ConfigStructInit(&SSP_ConfigStruct);
  /* Configure SSP Data length */
  SSP_ConfigStruct.Databit = SSP_DAC_DATABIT;
  /* Configure SSP Clock Phase */
  SSP_ConfigStruct.CPHA = SSP_DAC_CPHA;
  /* Configure SSP Clock Polarity */
  SSP_ConfigStruct.CPOL = SSP_DAC_CPOL;  
  /* Configure SSP to Master mode */
  SSP_ConfigStruct.Mode = SSP_DAC_MODE;
  /* Configure SSP frame format */
  SSP_ConfigStruct.FrameFormat = SSP_DAC_FRAME;
  /* Configure SSP Clock Rate */
  SSP_ConfigStruct.ClockRate = SSP_DAC_CLOCK_RATE;    
  /* Initialize SSP peripheral with parameter given in structure above */
  SSP_Init(SSP_DAC_PORT, &SSP_ConfigStruct);
  /* Enable SSP peripheral */
  SSP_Cmd(SSP_DAC_PORT, ENABLE);
}
/*******************************************************************************
 
 Write to the DAC

*******************************************************************************/
unsigned long SSP_Dac_Write(unsigned long channel, unsigned long conv_count)
{ 
  /* Enable the DAC */ 
  SSP_DAC_ENABLE;     
  /* Write the data to the SSP. */
  #ifdef DAC_10BIT
    SSP_SendData(SSP_DAC_PORT,(channel | WR_REG_UPDATE | (conv_count << 2)));
  #else
    /* 12-bit DAC */
    SSP_SendData(SSP_DAC_PORT,(channel | WR_REG_UPDATE | conv_count));
  #endif      
  /* Wait for SPI transmit to finish */
  while(SSP_GetStatus(SSP_DAC_PORT, SSP_STAT_BUSY));
  /* Disable the DAC */
  SSP_DAC_DISABLE;
  return 0;
}
