/*********************************************************************
 *
 *                  SPI API implementation file
 *
 *********************************************************************
 * FileName:        spi_chn_chg_mode_lib.c
 * Dependencies:	Generic.h
 * 					Spi.h
 *
 * Processor:       PIC32
 *
 * Complier:        MPLAB Cxx
 *                  MPLAB IDE
 * Company:         Microchip Technology Inc..
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the Company) for its PIC32 Microcontroller is intended
 * and supplied to you, the Companys customer, for use solely and
 * exclusively on Microchip PIC32 Microcontroller products.
 * The software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * $Id$
 * $Name$
 *
 ********************************************************************/
#include <peripheral/generic.h>


#include <peripheral/spi.h>
#include <peripheral/ports.h>

#include "_spi_io_tbl.h"
#include "_spi_map_tbl.h"


/********************
 * Channel functions.
 * Defined only if there's at least one valid SPI channel
 * *********************************/
#if defined(_SPI1) || defined(_SPI2)

/*********************************************************************
 * Function:        void SpiChnChgMode(int chn, int isMaster, int isFrmMaster)
 *
 * PreCondition:    None
 *
 * Input:			chn			- the channel to set
 *					isMaster	- master/slave mode
 * 					isFrmMaster	- frame master/slave mode
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function changes the SPI channel mode on the fly.
 *
 * Note:            - When changing mode, the function blocks until the
 * 					current transfer, if any, is completed.
 * 					- isFrmMaster is relevant only if the SPI channel is operating in frame mode
 * 					- The SCK is properly configured as an digital I/O pin
 * 					- The SS is configured as a digital pin only if in frame mode or if the SSEN configuration bit is set.
 *
 * Example:			SpiChnChgMode(1, TRUE, TRUE, CFG_SPI_SCK|CFG_SPI_SS);
 ********************************************************************/
PUBLIC void SpiChnChgMode(int chn, int isMaster, int isFrmMaster)
{
	unsigned int		rdBuff;
	int					wasOn;
	fConfPorts	fPorts;
	int		spiChn=chn-1;
	register volatile	SpiRegMap*	pReg=SpiMapTbl[spiChn];
	const SpiIoDcpt*	pDcpt=SpiIoTbl+spiChn;

	if(pReg->con.ON)
	{
		while(pReg->stat.SPIBUSY);	// wait current txfer to finish
		pReg->conClr=_SPI2CON_ON_MASK;	// turn off
		rdBuff=pReg->buf;
		wasOn=1;
	}




	fPorts=isMaster?PORTSetPinsDigitalOut:PORTSetPinsDigitalIn;
	(*fPorts)(pDcpt->portSck, pDcpt->sckMask);		// configure the SCK

	if(pReg->con.FRMEN || pReg->con.SSEN)
	{
		if(pReg->con.FRMEN)
		{
			fPorts=isFrmMaster?PORTSetPinsDigitalOut:PORTSetPinsDigitalIn;
		}
		// else fPorts properly set by isMaster

		(*fPorts)(pDcpt->portSS, pDcpt->ssMask);		// configure the SS
	}


	// reconfigure the SPI channel
	if(isMaster)
	{
		pReg->conSet=_SPI2CON_MSTEN_MASK;
	}
	else
	{
		pReg->conClr=_SPI2CON_MSTEN_MASK;
	}

	if(isFrmMaster)
	{
		pReg->conClr=_SPI2CON_FRMSYNC_MASK;
	}
	else
	{
		pReg->conSet=_SPI2CON_FRMSYNC_MASK;
	}

	if(wasOn)
	{
		pReg->conSet=_SPI2CON_ON_MASK;		// turn on
	}

}

#endif	//_SPI1 || _SPI2


