/*
 * Copyright (c) 2016 Avnet, Inc.  All rights reserved.
 *
 * Avnet, Inc.
 * Avnet IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, Avnet IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * Avnet EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

/*
 * SPI_TempSensor_MAX31723: simple test application to drive SPI comands
 * to MAX31723 PMOD placed on MicroZed FMC Carrier Card connector J3 top row
 *
  */

#include "PmodUtilities.h"

int main()
{

	int i;
	int nTemp;
	float fTemp;
	u32 TxBuffer[5];
	u32 RxBuffer[5];

	//Set up the ARM PS
	init_platform();

	// Set up the AXI SPI Controller to operate with the MAX31723
	XSpi_MAX31723_INIT(SPI_BASEADDR);

	print("     - MicroZed FMC Carrier Card\n\r");
	print("     -    PL PMOD MAX31723 Temp Sensor\n\r");

	// Set up the TxBuffer to continuously perform temperature conversions with 12-bit resolution
	TxBuffer[0] = MAX31723_CSR_WRITE;
	TxBuffer[1] = MAX31723_12BIT_MODE;
	// Execute 2-byte write transaction.
	// Data returned in RxBuffer is expected to be garbage since we were only writing data
	// By design, the SPI protocol will return data anyway
	XSpi_LowLevelExecute(SPI_BASEADDR, SPI_CHANNEL_SEL_0, TxBuffer, RxBuffer, 2);
	sleep(1);
	// Set up the TxBuffer to read a 2-byte LSB/MSB temperature
	// Data in TxBuffer[1:2] should be ignored but setting to 0x0 anyway
	TxBuffer[0] = MAX31723_TEMP_READ;
	TxBuffer[1] = 0x00;
	TxBuffer[2] = 0x00;

	while(1)
	{
		fflush(stdin);


		// Initialize RxBuffer with all 1's
		for(i = 0; i < 5; i++) RxBuffer[i] = 0xFFFFFFFF; // init RxBuffer with 0xFFs

		// Execute 3-byte read transaction.
		// Data in RxBuffer[0] is garbage since it is populated while the Read Command in TxBuffer[0] is being sent
		// RxBuffer[1] = LSB
		// RxBuffer[2] = MSB
		XSpi_LowLevelExecute(SPI_BASEADDR, SPI_CHANNEL_SEL_0, TxBuffer, RxBuffer, 3);

		// Convert to approximate floating point
		// The 31723 temperature readings are signed 12 bits long, and each bit represents 1/16th of a degree C
		nTemp = 0;
		// Extract the MSB
		nTemp = (int)RxBuffer[2];
		// Shift left by 4 to make room for the LS nibble
		nTemp = nTemp << 4;
		// Combine the MSB with the LS nibble. Lower 4 bits of the LSB are always 0.
		nTemp |= (int)((RxBuffer[1] & 0xF0)>>4);
		// Is the 12bit bit set?  If so, set the other bits for the 2s complement negative value
		if((nTemp & 0x00000800)==0x00000800)//pos-neg
			nTemp |= 0xFFFFF000;
		// Convert to floating point
		fTemp = (float)nTemp;
		// Each count of nTemp = 1/16th of a degree C
		fTemp = fTemp / 16.0f;
		printf("%.1f deg C\n\r",fTemp);
		sleep(1);
	}
    return 0;
}
