/**
 * \file   osdrv_mcspi.c
 *
 * \brief  McPI Driver source file
 *
 * 
*/

/*
 * Copyright (c) 2013 Texas Instruments Incorporated ALL RIGHTS RESERVED
 */

/* ========================================================================== */
/*                             Include Files                                  */
/* ========================================================================== */
#include "osdrv_mcspi.h"

/* ========================================================================== */
/*                           Macros & Typedefs                                */
/* ========================================================================== */
#define SPI_INSTANCE_COUNT	2
/* ========================================================================== */
/*                         Structures and Enums                               */
/* ========================================================================== */


/* ========================================================================== */
/*                 Internal Function Declarations                             */
/* ========================================================================== */

/* ========================================================================== */
/*                            Global Variables                                */
/* ========================================================================== */
volatile static unsigned int spiInitCount[SPI_INSTANCE_COUNT] = {0};

/* ========================================================================== */
/*                          Function Definitions                              */
/* ========================================================================== */

unsigned int McSPIInit(int spiId, unsigned char spiClockDiv, 
        unsigned char spiDir,unsigned int chMode,unsigned int chNum)
{

    int baseAdd = McSPIGetBaseAddress(spiId);

    if(!baseAdd)
    	return 0;

    
    spiInitCount[spiId]++;
        
    if(spiInitCount[spiId] == 1)    
    {
        /* Doing initialization for this SPI instance for the first time */
    
        McSPIModuleClkConfig(spiId);
            
        /* McSPI Reset */
        McSPIReset(baseAdd);
        
        /* Enable chip select pin.*/
        McSPICSEnable(baseAdd);    

        /* Configue McSPI SYS_CONFIG Register */
        HWREG(baseAdd + MCSPI_SYSCONFIG) = 
            ((MCSPI_SYSCONFIG_AUTOIDLE_ENABLE <<  MCSPI_SYSCONFIG_AUTOIDLE_SHIFT) | 
             (MCSPI_SYSCONFIG_CLOCKACTIVITY_BOTH << MCSPI_SYSCONFIG_CLOCKACTIVITY_SHIFT) |
             (MCSPI_SYSCONFIG_SIDLEMODE_WAKEUP << MCSPI_SYSCONFIG_SIDLEMODE_SHIFT));

        McSPIMasterModeEnable(baseAdd);

    }
    else
    {
        /* Already intialized. */

    }
    
    McSPIWordLengthSet(baseAdd,MCSPI_WORD_LENGTH(8),chNum);
    /* Set chip select polarity to "LOW"*/
    McSPICSPolarityConfig(baseAdd,MCSPI_CS_POL_LOW,chNum);        

    /* Configure the McSPI bus clock depending on clock mode. */
    McSPIClkConfig(baseAdd, MCSPI_IN_CLK, MCSPI_IN_CLK/spiClockDiv , chNum, MCSPI_CLK_MODE_3);        
    if(spiDir == 0)
    {
        McSPIMasterModeConfig(baseAdd,chMode,MCSPI_TX_RX_MODE,MCSPI_DATA_LINE_COMM_MODE_1,chNum);
    }
    else    
    {
        McSPIMasterModeConfig(baseAdd,chMode,MCSPI_TX_RX_MODE,MCSPI_DATA_LINE_COMM_MODE_6,chNum);
    }
        
    return baseAdd;
}

unsigned int McSPIDeInit(int spiId)
{
    if(spiId >= SPI_INSTANCE_COUNT)
        return 1;
        
    spiInitCount[spiId]--; 
		return 0;	
}

void McSPIRead( int spiId, unsigned char *buf, 
                unsigned short len, unsigned short spiCs )
{
    McSPICycle( spiId, buf, len, spiCs );
}


void McSPIWrite( int spiId, unsigned char *buf,
                 unsigned short len, unsigned short spiCs )
{
    McSPICycle( spiId, buf, len, spiCs );
}

/* -------------------------------------------------------------------------- */
/*                 Internal Function Definitions                              */
/* -------------------------------------------------------------------------- */
