/******************************************************************************
** Copyright 2015 MBARI - Monterey Bay Aquarium Research Institute
*******************************************************************************
*******************************************************************************
** Summary  : Routines for SPI I/O access to DS3234 RTC
** Filename : io_spi.c
** Author   : Luke Coletti
** Project  :
** Version  : 1.0
** Compiler : MetroWerks Code Warrior v8.3
** Created  : 12/15/14
** Archived :
*******************************************************************************
** Modification History:
** 01/02/15 : CF2 Port, LJC	(from HEBO supplied BEDS code)
*******************************************************************************/

#include        <cfxpico.h>                             // Persistor PicoDOS definitions
#include        <io_spi.h>



//	Module Local Data
static QPB      *rtcQPB;                // QPB for doing SPI I/O


/* DS3234 SPI Definition */
static QPBDev rtcQPBTemplate =
{
	RTCSLOT,                        // qslot - our qspi slot
	"DS3234",                       // Device name (char[16])
	1000000,                        // Max baud rate
	8,                                      // Bits Per Transfer
	iaHighSCK,                      // SPI Clock Polarity (iaLowSCK,iaHighSCK)
	captFall,                       // SPI Clock Phase (captLead/captFall)
	true,                                   // Continue CS assert between mult xfrs
	true,                                   // Auto adjust timing to clock flag
	0,                                      // Min. Delay Before SCK (picoSecs)
	0,                                      // Min. Delay After Transfer (ps)
	NULL,                                   // pointer to received data buffer
	// (filled in by QPBInitSlot)
	2                                       // Words transferred
};


/*************************************************************************
** Function    : spiInit
** Purpose     : Initialize SPI I/O
** Inputs      : None
** Outputs     : None
** Comment     : Configures SPI power on, so no need to call spiPower
*************************************************************************/
void spiInitRTC(void)
{
	rtcQPB = QPBInitSlot(&rtcQPBTemplate);

}


/*************************************************************************
** Function    : spiReadRTCReg
** Purpose     : Read one register from the DS3234 RTC chip
** Inputs      : Register address
** Outputs     : Register data or ERROR
*************************************************************************/
int spiReadRTCReg(ushort addr)
{
	int cmd[2];

	if (addr > 0x19)
		return(ERROR);

	cmd[0] = addr & 0x7f;
	cmd[1] = 0;

	QPBTransact(rtcQPB, 0, 2, cmd);

	return(rtcQPB->dev->rcvData[1] & 0xff);

}


/*************************************************************************
** Function    : spiWriteRTCReg
** Purpose     : Write one register to the DS3234 RTC chip
** Inputs      : Register address, data to write
** Outputs     : OK or ERROR
*************************************************************************/
int spiWriteRTCReg(ushort addr, ushort wdat)
{
	int cmd[2];

	if (addr > 0x19)
		return(ERROR);

	cmd[0] = addr | 0x80;
	cmd[1] = wdat & 0xff;

	QPBTransact(rtcQPB, 0, 2, cmd);

	return(OK);

}


/*************************************************************************
** Function    : spiReadMultRTCRegs
** Purpose     : Read multiple registers from the DS3234 RTC chip
** Inputs      : Starting address, number of registers, ptr for result data
** Outputs     : OK or ERROR
*************************************************************************/
int spiReadMultRTCRegs(ushort addr, ushort numRegs, ushort *result)
{
	int cmd[16];

	if ((addr + numRegs > 0x28) || (numRegs > 15))
		return(ERROR);

	memset(cmd, 0, sizeof(cmd));
	cmd[0] = addr & 0x7f;

	QPBTransact(rtcQPB, 0, numRegs+1, cmd);

//    LoopModeMemCopyWords(result, &rtcQPB->dev->rcvData[1], numRegs);
	memcpy(result, &rtcQPB->dev->rcvData[1], 2*numRegs);

	return(OK);

}


/*************************************************************************
** Function    : spiWriteMultRTCRegs
** Purpose     : Write multiple registers to the DS3234 RTC chip
** Inputs      : Starting address, number of registers, ptr to data
** Outputs     : OK or ERROR
*************************************************************************/
int spiWriteMultRTCRegs(ushort addr, ushort numRegs, ushort *wdat)
{
	int cmd[16];

	if ((addr + numRegs > 0x28) || (numRegs > 15))
		return(ERROR);

	cmd[0] = addr | 0x80;
	LoopModeMemCopyWords(&cmd[1], wdat, numRegs);
//    memcpy(&cmd[1], wdat,2* numRegs);

	QPBTransact(rtcQPB, 0, numRegs+1, cmd);

	return(OK);

}
