/********************************************************************************

					REva Library
			Copyright (c), RAISONANCE

+-------------------------------------------------------------------------------+
 The present source code which is for guidance only aims at providing
 customers with information regarding their products in order for them to
 save time. As a result, RAISONANCE shall not be held liable for any
 direct indirect or consequential damages with respect to any claims arising
 from the content of such a source code and/or the use made by customers of 
 the information contained herein in connection with their products.
+-------------------------------------------------------------------------------+*/
   
//this file implements the functions for easy handling of the REva board
// it is as more an example than a library and therefore is not optimized much



//WARNING: all these functions assume that the associated peripherals are already initialized
// You can use RBuilder for that.

#include "ST7lib_config.h"

#include "REvaLib.h"


//------------------------------------------------------------------
// Analog functions
//------------------------------------------------------------------

//that's the maximum value returned by the ADC.
#define MAX_ADC_VAL (0x03FF)

//this function reads an analogic value on one of the ADC inputs
// it is not exported out of this file,
// but is called from the other analog functions below
static unsigned short AnaRead(char channel)
{
	unsigned short TempRead;

	//select channel.
	ADC_Select_Channel (channel);
	
	//launch conversion
	ADC_Enable();

	//wait for the conversion to be complete
	while(!ADC_Test_Conversn_Complete ());
	
	//read the value	
	TempRead = ADC_Conversn_Read ();
	
	//end conversion
	ADC_Disable();
	
	//return value
	return TempRead;
}

//this function reads the value from the temperature sensor (in tenth of Celcius degrees)
signed short ReadTemp(unsigned short VDDmV)
{
	unsigned long TempRead;

	//read the value from the Temp sensor	
	TempRead=AnaRead(2);
	
	//convert it in mV
	TempRead *= VDDmV;
	TempRead /= MAX_ADC_VAL; //max value is 0x3ff
	
	//convert it into tenth of Celcius degrees
	TempRead -= 1;
	
	//return the value
	return TempRead;
	
}

//this function reads the value from the potentiometer (as a percentage)
unsigned char ReadPot()
{
	unsigned long TempRead;
	
	//read value on channel 3.
	TempRead = AnaRead(3);
	
	//convert in percentage
	TempRead *= 100;
	TempRead /= MAX_ADC_VAL; //max value is 0x3FF
	
	return (unsigned char)(TempRead);
}

//this function reads the value from the analog input IN1 (in mV)
unsigned short ReadI1(unsigned short VDDmV)
{
	unsigned long TempRead;
	//read value on channel 5. no conversion needed
	TempRead = AnaRead(5);
	
	//convert it in mV
	TempRead *= VDDmV;
	TempRead /= MAX_ADC_VAL; //max value is 0x3FF
	
	return (unsigned short)(TempRead);
}

//this function reads the value from the analog input IN2 (in mV)
unsigned short ReadI2(unsigned short VDDmV)
{
	unsigned long TempRead;
	//read value on channel 4. no conversion needed
	TempRead= AnaRead(4);
	
	//convert it in mV
	TempRead *= VDDmV;
	TempRead /= MAX_ADC_VAL; //max value is 0x3FF
	
	return (unsigned short)(TempRead);
}

//that's the maximum value for the PWM register.
#define MAX_PWM_VAL (0x0FF)

//this function sets Vout at NewVal (in mV)
void SetOUT(unsigned short NewVal, unsigned short VDDmV)
{
	unsigned long TempWrite=NewVal;
	
	//convert it from mV
	TempWrite *= MAX_PWM_VAL; //max value is 0xFF
	TempWrite /= VDDmV;
	
	//set the PWM with the new duty cycle
	PWMART_PWM_Mode(0, PWMART_POLARITY_0, TempWrite);
}





//------------------------------------------------------------------
// EEPROM access functions through I2C
//------------------------------------------------------------------

//that's the MSB address of the EEPROM
#define EEPROM_ADDRESS 0xA0

//this function writes data from the ST7 memory to the EEPROM
void WriteEEPROM(unsigned char addr, unsigned char cnt, unsigned char *buf)
{
	//we can only write in a 16-bytes aligned page at a time...
	while( (addr&0xf0) != ((addr+cnt-1)&0xf0) )
	{
		unsigned char WriteThisTime=((addr&0xf0)+0x10)-addr;
		if(WriteThisTime>cnt)
			WriteThisTime=cnt;
		WriteEEPROM(addr, WriteThisTime, buf);
		addr+=WriteThisTime;
		buf+=WriteThisTime;
		cnt-=WriteThisTime;
	}

	//Generate Start
	I2C_Generate_Start();
	while(I2C_IsTransmitCompleted() != I2C_START_OK);
	
	//address the EEPROM
	I2C_Load_Address(EEPROM_ADDRESS, I2C_TX_MODE);
	while(I2C_IsTransmitCompleted() != I2C_ADD_TX_OK);
	
	//Transmit the address
	I2C_PutByte(addr);
	while(I2C_IsTransmitCompleted() != I2C_DATA_TX_OK);
	
	//Transmit the data
	I2C_PutBuffer(buf, cnt);
	while(I2C_IsTransmitCompleted() != I2C_DATA_TX_OK);
	
	//Generate Stop
	I2C_Generate_Stop();
	while(!I2C_IsStopGen());
	
}

//this function reads data from the EEPROM to the ST7 memory
void ReadEEPROM(unsigned char addr, unsigned char cnt, unsigned char *buf)
{
	//Generate Start
	I2C_Generate_Start();
	while(I2C_IsTransmitCompleted() != I2C_START_OK);
	
	//address the EEPROM
	I2C_Load_Address(EEPROM_ADDRESS, I2C_TX_MODE);
	while(I2C_IsTransmitCompleted() != I2C_ADD_TX_OK);
	
	//Transmit the address
	I2C_PutByte(addr);
	while(I2C_IsTransmitCompleted() != I2C_DATA_TX_OK);
	
	//Generate Start
	I2C_Generate_Start();
	while(I2C_IsTransmitCompleted() != I2C_START_OK);
	
	//address the EEPROM
	I2C_Load_Address(EEPROM_ADDRESS, I2C_RX_MODE);
	while(I2C_IsTransmitCompleted() != I2C_ADD_TX_OK);
	
	//Receive the data
	I2C_GetBuffer(buf, cnt);
	
	//Stop condition is managed in GetBuff function
}





//------------------------------------------------------------------
// utility functions
//------------------------------------------------------------------

//this function converts a hex number from ascii to binary
unsigned char readhex(char car)
{
	if( (car>='a') && (car<='f') )
		return car-'a'+10;

	if( (car>='A') && (car<='F') )
		return car-'A'+10;

	if( (car>='0') && (car<='9') )
		return car-'0';

	return 0xff;
}




