Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

EEPROM-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup other
00002  * @{
00003  * @defgroup EEPROM Generic I2C EEPROM
00004  * Generic I2C EEPROM
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: EEPROM-driver.h 49 2011-04-27 13:00:05Z xander $
00010  */
00011 
00012 /** \file EEPROM-driver.h
00013  * \brief RobotC EEPROM Driver
00014  *
00015  * EEPROM-driver.h provides an API for the AT24C512, 24AA512 and 24AA1025 EEPROMs.
00016  *
00017  * Changelog:
00018  * - 0.1: Initial release
00019  * - 0.2: Removed array structs, replaced with typedefs
00020  *
00021  * License: You may use this code as you wish, provided you give credit where its due.
00022  *
00023  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00024  * \author Xander Soldaat (mightor_at_gmail.com)
00025  * \date 20 February 2011
00026  * \version 0.2
00027  * \example EEPROM-test1.c
00028  * \example EEPROM-test2.c
00029  */
00030 
00031 #pragma systemFile
00032 
00033 #ifndef __COMMON_H__
00034 #include "common.h"
00035 #endif
00036 
00037 #define EEPROM_I2C_ADDR 0xA0      /*!< EEPROM I2C device address */
00038 
00039 /*! Page for the AT24C512 */
00040 #define EEPROM_PAGE_SIZE   128
00041 
00042 tByteArray EEPROM_I2CRequest;    /*!< Array to hold I2C command data */
00043 tByteArray EEPROM_I2CReply;      /*!< Array to hold I2C reply data */
00044 
00045 /*
00046 <function prototypes>
00047 */
00048 bool EEPROMreadBytes(tSensors link, long address, tByteArray &data, int numbytes);
00049 bool EEPROMwriteBytes(tSensors link, long address, tByteArray &data, int numbytes);
00050 bool _EEPROMwriteDummy(tSensors link, long address);
00051 
00052 
00053 /**
00054  * Read a number of bytes from address
00055  * @param link the EEPROM
00056  * @param address the address to read from
00057  * @param data the byte array to store read result in
00058  * @param numbytes the number of bytes to read (limited to 16 at a time)
00059  * @return true if no error occured, false if it did
00060  */
00061 bool EEPROMreadBytes(tSensors link, long address, tByteArray &data, int numbytes) {
00062   if (!_EEPROMwriteDummy(link, address))
00063     return false;
00064 
00065   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00066 
00067   EEPROM_I2CRequest[0] = 1;               // Message size
00068   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR; //I2C Address
00069 
00070   if (!writeI2C(link, EEPROM_I2CRequest, numbytes))
00071     return false;
00072 
00073   if (!readI2C(link, EEPROM_I2CReply, numbytes))
00074     return false;
00075 
00076   memcpy(data, EEPROM_I2CReply, sizeof(tByteArray));
00077   return true;
00078 }
00079 
00080 
00081 /**
00082  * Perform a dummy write to set address for next read
00083  *
00084  * Note: this is an internal function and should not be called directly.
00085  * @param link the EEPROM
00086  * @param address the address to set
00087  * @return true if no error occured, false if it did
00088  */
00089 bool _EEPROMwriteDummy(tSensors link, long address) {
00090   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00091 
00092   EEPROM_I2CRequest[0] = 3;                             // Message size
00093   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR;               // I2C Address
00094   EEPROM_I2CRequest[2] = (byte)((address >> 8) & 0xFF); // upper 8 bits of address word
00095   EEPROM_I2CRequest[3] = (byte)(address & 0xFF);        // lower 8 bits of address word
00096 
00097   return writeI2C(link, EEPROM_I2CRequest, 0);
00098 }
00099 
00100 
00101 /**
00102  * Write a single byte to address
00103  * @param link the EEPROM
00104  * @param address the address to write to
00105  * @param data the byte array to write
00106  * @param numbytes the number of bytes to write (limited to 13 at a time)
00107  * @return true if no error occured, false if it did
00108  */
00109 bool EEPROMwriteBytes(tSensors link, long address, tByteArray &data, int numbytes) {
00110   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00111 
00112   EEPROM_I2CRequest[0] = 3 + numbytes;                  // Message size
00113   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR;               // I2C Address
00114   EEPROM_I2CRequest[2] = (byte)((address >> 8) & 0xFF); // upper 8 bits of address word
00115   EEPROM_I2CRequest[3] = (byte)(address & 0xFF);        // lower 8 bits of address word
00116 
00117   if (numbytes > 13)
00118     numbytes = 13;
00119 
00120   if (numbytes < 0)
00121     numbytes = 0;
00122 
00123   memcpy(EEPROM_I2CRequest[4], data[0], numbytes);
00124   return writeI2C(link, EEPROM_I2CRequest, 0);
00125 }
00126 
00127 /*
00128  * $Id: EEPROM-driver.h 49 2011-04-27 13:00:05Z xander $
00129  */
00130 /* @} */
00131 /* @} */