// --------------------------------------------------------------------------------------------
// Driver for external FRAM FR24C65
// designed for the Smart-Its Core
// adapted for FRAM FR24C65
// based on code for the EEPROM MicroChip 24C65 distributed by CCS 

// --------------------------------------------------------------------------------------------
// last modified: 09.03.2003 

// --------------------------------------------------------------------------------------------
// 2002, 2003 by Albrecht Schmidt - http://www.comp.lancs.ac.uk/~albrecht/
// Lancaster University, http://www.comp.lancs.ac.uk/
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// --------------------------------------------------------------------------------------------
// developed/improved for various prototypes 
// in the Smart-Its Project (http://www.smart-its.org) and
// in the Equator IRC (http://www.equator.ac.uk)

// --------------------------------------------------------------------------------------------
// compiled with
// CCS PCM C Compiler, Version 3.080
// CCS PCH C Compiler

// --------------------------------------------------------------------------------------------
// *********************************************************************************************
// you must define the I2C Bus pins in your program
// typically...
// #define i2c_sda PIN_C5
// #define i2c_scl PIN_C4
// #use i2c(master, sda=i2c_sda, scl=i2c_scl,fast,RESTART_WDT)
// *********************************************************************************************

// --------------------------------------------------------------------------------------------
// *********************************************************************************************
// init_ext_fram() - setup i2c bus
// write_ext_fram(address, data) - write a data byte to address
// data = read_ext_fram(address) - read the data byte from the address
// *********************************************************************************************

#define hi(x)  (*(&x+1)) 

#define FRAM_ADDRESS long int 
#define FRAM_SIZE    8192 


// init_fram() - setup i2c bus
void init_ext_fram() {
   output_float(i2c_scl);
   output_float(i2c_sda);
}

// write_fram(address, data) - write a data byte to address
void write_ext_fram(long int address, byte data) {
   i2c_start();
   //i2c_write(0xa0);
   // change to address wired on smart-its core 
   i2c_write(0xa2);
   i2c_write(hi(address));
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   delay_ms(11);
}

// data = read_fram(address) - read the data byte from the address
byte read_ext_fram(long int address) {
   byte data;

   i2c_start();
   //i2c_write(0xa0);
   // change to address wired on smart-its core 
   i2c_write(0xa2);
   i2c_write(hi(address));
   i2c_write(address);
   i2c_start();
   //i2c_write(0xa1);
    i2c_write(0xa3);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}