/*
 * HWISpi.c
 *
 *  Created on: 12 mars 2016
 *      Author: acali
 */


/*****************************************************************************/
/* INCLUDE FILES                                                             */
/*****************************************************************************/ 

/*********************** Definition of common types **************************/
#include "common.h"

/****************** Sources to be used within the module *********************/
#include "extern.h"
	/* TI-RTOS Header files */
	#include <ti/drivers/SPI.h>

	/* Example/Board Header files */
	#include "Board.h"

	#include "ERRorapi.h"

/***************** Headers implemented within the module *********************/
#include "alloc.h"
	#include "HWISpi.h"

/*****************************************************************************/
/* CONSTANTS, MACROS                                                         */
/*****************************************************************************/
#define HWISPI_ku8FIFO_LENGTH    ((UCHAR)8)

/*****************************************************************************/
/* TYPES                                                                     */
/*****************************************************************************/ 
typedef USHORT HWISpi_tDataType;

/*****************************************************************************/
/* PRIVATE FUNCTIONS PROTOTYPES                                              */
/*****************************************************************************/

/*****************************************************************************/
/* PRIVATE VARIABLES                                                         */
/*****************************************************************************/ 
SPI_Handle udtSpiHandle = (SPI_Handle)NULL;
BOOLEAN bIsInitialized = (BOOLEAN)FALSE;

// Polarity 0 = Idle LOW
// Phase 0 = rising edge
// Phase 1 = falling edge
// Polarity 1 = Idle HIGH
// Phase 0 = falling edge
// Phase 1 = rising edge

/*****************************************************************************/
/* PUBLIC FUNCTIONS                                                          */
/*****************************************************************************/ 
PUBLIC void HWISpi_vidInit(void)
{
	SPI_Params params;

    Board_initSPI();

    SPI_Params_init(&params);

    params.bitRate = (uint32_t)2000000; // 2MHz
    params.dataSize = (uint32_t)8; // 8-bits wide
    params.frameFormat = (SPI_FrameFormat)SPI_POL0_PHA0; // Idle Low Rising Edge
    params.mode = (SPI_Mode)SPI_MASTER;
    params.transferCallbackFxn = (SPI_CallbackFxn)0; // no callback
    params.transferMode = (SPI_TransferMode)SPI_MODE_BLOCKING;
    params.transferTimeout = (uint32_t)320; // 8 bits @ 2MHz = 4 us = 320 ticks @ MHz 80

    /* Initialize SPI handle as default master */
	udtSpiHandle = SPI_open(Board_SPI0, &params);

	if (udtSpiHandle == (SPI_Handle)NULL)
	{
		ERR_vidExceptionRaise(__FILE__,__LINE__);
	}
	else
	{
		bIsInitialized = (BOOLEAN)TRUE;
	}
}

/*****************************************************************************/
/* @Description:                                                             */
/*    Send characters by the SPI communication                              */
/*****************************************************************************/
PUBLIC BOOLEAN HWI_bSPIWrite
(
  /*@Comment: Data to send by the SPI                                        */
  const UCHAR                       aku8DataToWrite[],
  /*@Comment: Data read on the SPI                                           */
  UCHAR                       au8DataToRead[],
  /*@Comment: Size of the table of data to send                              */
  /*@Range: 1..255                                                           */
  /*@Unit: Byte                                                              */
  UCHAR                             u8NbData
)
{
	BOOLEAN bReturnValue;
	SPI_Transaction masterTransaction;
	HWISpi_tDataType audtTxData[HWISPI_ku8FIFO_LENGTH];
	HWISpi_tDataType audtRxData[HWISPI_ku8FIFO_LENGTH];
	UCHAR u8DataIndex;

	if(bIsInitialized == (BOOLEAN)TRUE)
	{
		if(u8NbData <= HWISPI_ku8FIFO_LENGTH)
		{
			for(u8DataIndex=(UCHAR)0;u8DataIndex<u8NbData;u8DataIndex++)
			{
				audtTxData[u8DataIndex] = (HWISpi_tDataType)aku8DataToWrite[u8DataIndex];
			}

			/* Initialize master SPI transaction structure */
			masterTransaction.count = u8NbData;
			masterTransaction.txBuf = audtTxData;
			masterTransaction.rxBuf = audtRxData;

			/* Initiate SPI transfer */
			bReturnValue = (BOOLEAN)SPI_transfer(udtSpiHandle, &masterTransaction);

			if(bReturnValue != (BOOLEAN)FALSE)
			{
				for(u8DataIndex=(UCHAR)0;u8DataIndex<u8NbData;u8DataIndex++)
				{
					au8DataToRead[u8DataIndex] = (UCHAR)audtRxData[u8DataIndex];
				}
			}
		}
		else
		{
			// Data to transmit is too wide
			bReturnValue = (BOOLEAN)FALSE;
		}
	}
	else
	{
		// SPI is not initialized
		bReturnValue = (BOOLEAN)FALSE;
	}

	return bReturnValue;
}

/*****************************************************************************/
/* PRIVATE FUNCTIONS                                                         */
/*****************************************************************************/
