/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2004, Jonathan Bagg
	All rights reserved.

	 Redistribution and use in source and binary forms, with or without modification, are permitted 
	 provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
	  conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of 
	  conditions and the following disclaimer in the documentation and/or other materials provided 
	  with the distribution.
    * Neither the name of Jonathan Bagg nor the names of its contributors may be used to 
	  endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega64, ATMega128
   File name    : eeprom.c
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2004/06/17 	Bagg
   - Cleaned up for release
   
   Revision 1.1  2004/12/15 	Bagg
   - Small optimizations made in checking EECR
---------------------------------------------------------------------------------------------------
   Created      : 24 March 2004     	           Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   EEprom poled read / write Driver with BlockWrites Interrupt based
---------------------------------------------------------------------------------------------------
**************************************************************************************************/

#include <avr/io.h> 
#include <avr/signal.h>
#include <avr/interrupt.h>
#include "eeprom.h"

/**************************************************************************************************
*   INTERNAL GLOBAL VARIABLES
**************************************************************************************************/
static unsigned int eaddress, elength;
static unsigned char *epnt;
static unsigned char block;

/**************************************************************************************************
*   INTERNAL Function declaration - See 'eeprom.h' Header file for Descriptions
**************************************************************************************************/
static unsigned char ee_busy(void);

/**************************************************************************************************
*   ewrite(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
void ewrite(int address, char data) 
{
 while (ee_busy());
 EEAR = address;
 EEDR = data;
 cli();
 EECR|= _BV(EEMWE);
 EECR|= _BV(EEWE);
 sei();
}

/**************************************************************************************************
*   ewrite16(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
void ewrite16(int address, int data)
{
 ewrite(address,(data >> 8));
 ewrite(address + 1,(data & 0xFF));
}

/**************************************************************************************************
*   eread(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
unsigned char eread(int address) 
{
 while (ee_busy());
 EEAR = address;
 EECR|= _BV(EERE);
 return EEDR;
}

/**************************************************************************************************
*   eread16(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
unsigned int eread16(int address)
{
 return (eread(address) << 8) | eread(address + 1);
}

/**************************************************************************************************
*   ewriteblock(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
void ewriteblock(void *input, int ee_address, int size)
{
 eaddress = ee_address + 1;
 elength = size + ee_address;
 epnt = input;
 ewrite(ee_address, *epnt);
 block = 1;
 EECR|= _BV(EERIE);	//Enable EEPROM INT
}

/**************************************************************************************************
*   ereadblock(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
void ereadblock(void *output, int ee_address, int size)
{
 unsigned int i;
 unsigned char *p;
 p = output;
 for (i=0;i<size;i++) {
	*p = eread(ee_address + i);
	p++;
 }
}

/**************************************************************************************************
*   ee_busy(); - See 'eeprom.h' Header file for Description
**************************************************************************************************/
static unsigned char ee_busy(void)
{
 if ((EECR & _BV(EEWE)) || block)
	return 1;
 return 0;
}

/**************************************************************************************************
*   EEPROM READY / WRITE DONE ISR - See 'eeprom.h' Header file for Description
**************************************************************************************************/
SIGNAL(SIG_EEPROM_READY)
{
 epnt++;
 EEAR = eaddress;
 EEDR = *epnt;
 EECR|= _BV(EEMWE);
 EECR|= _BV(EEWE);
 eaddress++;
 if (eaddress >= elength) {
	EECR&= ~_BV(EERIE);			//Disable EEPROM Interrupt
	block = 0;
 }
}

