/*
 * 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_MAX31855: simple test application to drive SPI commands
 * to MAX31855 PMOD placed on MicroZed connector top row
 *
 */

#include "MAX31855_Utilities.h"

int main()
{
	int i;
	// TxBuffer is not used to communicate with the MAX31855 but it is still necessary
	//      for the XSPI utilities to function
	u32 TxBuffer[6] = {0,0,0,0,0,0};
	u32 RxBuffer[6];
	int nTemporaryValue=0;
	int nTemporaryValue2=0;
	float MAX31855_internal_temp=0.0f;


	//Set up the ARM PS
    init_platform();

	print("     - 	 MicroZed PS_PMOD\n\r");
	print("     -    PS PMOD MAX31855 Thermocouple\n\r");

	// Set up the PS SPI Controller to operate with the MAX31855
	XSpiPS_MAX31855_INIT(SPI_BASEADDR);

	print("     - MAX31855 Init Done\n\r");

	while(1)
	{
		fflush(stdin);

		// Initialize RxBuffer with all 1's
		for(i = 0; i < 5; i++) RxBuffer[i] = 0xFFFFFFFF; // init RxBuffer with 0xFFs
		// Execute 5-byte read transaction.
		XSpiPS_LowLevelExecute(SPI_BASEADDR, SPI_CHANNEL_SEL_0, TxBuffer, RxBuffer, 4);
		for(i=0;i<4;i++)
		{
			xil_printf("Byte %d = 0x%x\n\r",i,RxBuffer[i]);
		}

		{
			// Internal Temp
			nTemporaryValue = RxBuffer[2];  			// bits 11..4
			nTemporaryValue = nTemporaryValue << 4;		// shift left to make room for bits 3..0
			nTemporaryValue2 = RxBuffer[3];				// bits 3..0 in the most significant spots
			nTemporaryValue2 = nTemporaryValue2 >> 4;	// shift right to get rid of extra bits and position
			nTemporaryValue |= nTemporaryValue2;		// Combine to get bits 11..0
			if((RxBuffer[2] & 0x80)==0x80)				// Check the sign bit and sign-extend if need be
				nTemporaryValue |= 0xFFFFF800;
			MAX31855_internal_temp = (float)nTemporaryValue / 16.0f;



			printf("MAX31855 Internal Temp     = %.1f deg C\n\r",MAX31855_internal_temp);

		}
		sleep(3);
	}

	cleanup_platform();
    return 0;
}