#include "ms5192t.h"
#include "at32f403a_407.h"
#include "adc_transport.h"



/***************************************************************************//**
 * @brief Initializes the MS5192T and checks if the device is present.
 *
 * @return status - Result of the initialization procedure.
 *                  Example: 1 - if initialization was successful (ID is 0x0B).
 *                           0 - if initialization was unsuccessful.
*******************************************************************************/
unsigned char MS5192T_Init(void)
{ 
	unsigned char status = 0x1;
    
    if ((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
	{
		status = 0x0;
	}
    
	return status;
}


/***************************************************************************//**
 * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
 *
 * @return  None.    
*******************************************************************************/
void MS5192T_Reset(void)
{
	unsigned char dataToSend[5] = {0x03, 0xff, 0xff, 0xff, 0xff};
	
    MS5192T_CS_LOW;	    
	SPI_Write(dataToSend, 4);
	MS5192T_CS_HIGH;	
}


/***************************************************************************//**
 * @brief Reads the value of the selected register
 *
 * @param regAddress - The address of the register to read.
 * @param size - The size of the register to read.
 *
 * @return data - The value of the selected register register.
*******************************************************************************/
unsigned long MS5192T_GetRegisterValue(unsigned char regAddress, 
                                      unsigned char size,
                                      unsigned char modifyCS)
{
	unsigned char data[5]      = {0x00, 0x00, 0x00, 0x00, 0x00};
	unsigned long receivedData = 0x00;
    unsigned char i            = 0x00; 
    
	data[0] = 0x01 * modifyCS;
	data[1] = MS5192T_COMM_READ | MS5192T_COMM_ADDR(regAddress); 
    
	SPI_Read(data, (1 + size));
    
	for (i = 1; i < size + 1; i++) {
        receivedData = (receivedData << 8) + data[i];
    }
    
    return receivedData;
}


/***************************************************************************//**
 * @brief Writes the value to the register
 *
 * @param -  regAddress - The address of the register to write to.
 * @param -  regValue - The value to write to the register.
 * @param -  size - The size of the register to write.
 *
 * @return  None.    
*******************************************************************************/
void MS5192T_SetRegisterValue(unsigned char regAddress,
                             unsigned long regValue, 
                             unsigned char size,
                             unsigned char modifyCS)
{
	unsigned char data[5]      = {0x00, 0x00, 0x00, 0x00, 0x00};	
	unsigned char* dataPointer = (unsigned char*)&regValue;
    unsigned char bytesNr      = size + 1;
    
    data[0] = 0x01 * modifyCS;
    data[1] = MS5192T_COMM_WRITE |  MS5192T_COMM_ADDR(regAddress);
    while(bytesNr > 1)
    {
        data[bytesNr] = *dataPointer;
        dataPointer ++;
        bytesNr --;
    }	    
	SPI_Write(data,(1 + size));
}
/***************************************************************************//**
 * @brief  Waits for RDY pin to go low.
 *
 * @return None.
*******************************************************************************/
void MS5192T_WaitRdyGoLow(void)
{   
    while (adc_get_rdy() == SET) {}
}

/***************************************************************************//**
 * @brief Sets the operating mode of MS5192T.
 *
 * @param mode - Mode of operation.
 *
 * @return  None.    
*******************************************************************************/
void MS5192T_SetMode(unsigned long mode)
{
    unsigned long command;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    command &= ~MS5192T_MODE_SEL(0xFF);
    command |= MS5192T_MODE_SEL(mode);
    MS5192T_SetRegisterValue(
            MS5192T_REG_MODE,
            command,
            2, 
            1); // CS is modified by SPI read/write functions.
}

                              
void MS5192T_SetUpdateRate(unsigned long rate)
{
    unsigned long command;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    command &= ~MS5192T_MODE_RATE(0xFF);
    command |= MS5192T_MODE_RATE(rate);
    MS5192T_SetRegisterValue(
            MS5192T_REG_MODE,
            command,
            2, 
            1); // CS is modified by SPI read/write functions.
}
                              
/***************************************************************************//**
 * @brief Selects the channel of MS5192T.
 *
 * @param  channel - ADC channel selection.
 *
 * @return  None.    
*******************************************************************************/
void MS5192T_SetChannel(unsigned long channel)
{
    unsigned long command;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    command &= ~MS5192T_CONF_CHAN(0xFF);
    command |= MS5192T_CONF_CHAN(channel);
    MS5192T_SetRegisterValue(
            MS5192T_REG_CONF,
            command,
            2,
            1); // CS is modified by SPI read/write functions.
}

/***************************************************************************//**
 * @brief  Sets the gain of the In-Amp.
 *
 * @param  gain - Gain.
 *
 * @return  None.    
*******************************************************************************/
void MS5192T_SetGain(unsigned long gain)
{
    unsigned long command;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    
    command &= ~MS5192T_CONF_GAIN(0xFF);
    command |= MS5192T_CONF_GAIN(gain);
    
    MS5192T_SetRegisterValue(
            MS5192T_REG_CONF,
            command,
            2,
            1); // CS is modified by SPI read/write functions.
}


void MS5192T_SetPolar(unsigned long polar)
{
    unsigned long command;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    
    command &= ~(1 << 12);
    command |= polar;
    
    MS5192T_SetRegisterValue(
            MS5192T_REG_CONF,
            command,
            2,
            1); // CS is modified by SPI read/write functions.
}
 
/***************************************************************************//**
 * @brief Sets the reference source for the ADC.
 *
 * @param type - Type of the reference.
 *               Example: AD7793_REFSEL_EXT	- External Reference Selected
 *                        AD7793_REFSEL_INT	- Internal Reference Selected.
 *
 * @return None.    
*******************************************************************************/
void MS5192T_SetIntReference(unsigned char type)
{
    unsigned long command = 0;
    
    command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
                                      2,
                                      1); // CS is modified by SPI read/write functions.
    command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
    command |= MS5192T_CONF_REFSEL(type);
    MS5192T_SetRegisterValue(MS5192T_REG_CONF,
							command,
							2,
                            1); // CS is modified by SPI read/write functions.
}

/***************************************************************************//**
 * @brief Performs the given calibration to the specified channel.
 *
 * @param mode - Calibration type.
 * @param channel - Channel to be calibrated.
 *
 * @return none.
*******************************************************************************/
void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
{
    unsigned short oldRegValue = 0x0;
    unsigned short newRegValue = 0x0;
    
    MS5192T_SetChannel(channel);
    oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
    oldRegValue &= ~MS5192T_MODE_SEL(0x7);
    newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
    MS5192T_CS_LOW; 
    MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
    MS5192T_WaitRdyGoLow();
    MS5192T_CS_HIGH;
    
}

/***************************************************************************//**
 * @brief Returns the result of a single conversion.
 *
 * @return regData - Result of a single analog-to-digital conversion.
*******************************************************************************/
unsigned long MS5192T_SingleConversion(void)
{
    unsigned long command = 0x0;
    unsigned long regData = 0x0;
    
    command  = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
    MS5192T_CS_LOW;
    MS5192T_SetRegisterValue(MS5192T_REG_MODE, 
                            command,
                            2,
                            0);// CS is not modified by SPI read/write functions.
    MS5192T_WaitRdyGoLow();
    regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 2, 0); // CS is not modified by SPI read/write functions.
    MS5192T_CS_HIGH;

    return regData;
}

/***************************************************************************//**
 * @brief Returns the average of several conversion results.
 *
 * @return samplesAverage - The average of the conversion results.
*******************************************************************************/
unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
{
    unsigned long samplesAverage = 0x0;
    unsigned long command        = 0x0;
    unsigned char count          = 0x0;
    
    command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
    MS5192T_CS_LOW;
    MS5192T_SetRegisterValue(MS5192T_REG_MODE,
                            command, 
                            2,
                            0);// CS is not modified by SPI read/write functions.
    for(count = 0;count < sampleNumber;count ++)
    {
        MS5192T_WaitRdyGoLow();
        samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0);  // CS is not modified by SPI read/write functions.
    }
    MS5192T_CS_HIGH;
    samplesAverage = samplesAverage / sampleNumber;
    
    return(samplesAverage);
}