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

					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"


//------------------------------------------------------------------
// 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;
}




