/*
 * MCP9801_Temp.c
 *
 *  Created on: Feb 7, 2017
 *      Author: User
 */

#include "MCP9801_Temp.h"

void MCP9801_GetDefaultConfig(MCP9801_temperature* config)
{
	config->i2cConfig.IIC_instance = 0x00;
	config->i2cConfig.i2c_device.baudRate_kbps = 300;
	config->i2cConfig.i2c_device.address = MPC9801_IIC_ADDRESS;
	config->i2cConfig.i2c_status = kStatus_I2C_Success;
}

i2c_status_t MCP9801_SetResolution(MCP9801_temperature* config, MCP9801_resolution resolution)
{
	uint8_t cmd = MPC9801_CONFIG_REGISTER;
	config->configData &= MCP9801_RESOLUTION_MASK;
	config->configData |= resolution;

	config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				&config->configData,
				MPC9801_CONFIG_DATA_LENGTH,
				I2C_TIMEOUT);

	return config->i2cConfig.i2c_status;
}

i2c_status_t MCP9801_GetTemperature(MCP9801_temperature* config)
{
	uint8_t cmd = MPC9801_TEMP_REGISTER;

	config->i2cConfig.i2c_status = I2C_DRV_MasterReceiveDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				config->buffer,
				MPC9801_TEMP_DATA_LENGTH,
				I2C_TIMEOUT);

	if(config->i2cConfig.i2c_status != kStatus_I2C_Success)
	{
		return kStatus_I2C_Fail;
	}

	config->hByteTemp = config->buffer[0];


	config->lByteTemp = ((config->buffer[1] & 0x80) ? 50 : 0) + ((config->buffer[1] & 0x40) ? 25 : 0);

	return kStatus_I2C_Success;

}
