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

eeprom.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.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 /** \file eeprom.h
00013  * \brief RobotC EEPROM Driver
00014  *
00015  * eeprom.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 3.59 AND HIGHER. 
00024 
00025  * \author Xander Soldaat (xander_at_botbench.com)
00026  * \date 20 February 2011
00027  * \version 0.2
00028  * \example eeprom-test1.c
00029  * \example eeprom-test2.c
00030  */
00031 
00032 #pragma systemFile
00033 
00034 #ifndef __COMMON_H__
00035 #include "common.h"
00036 #endif
00037 
00038 #define EEPROM_I2C_ADDR 0xA0      /*!< EEPROM I2C device address */
00039 
00040 /*! Page for the AT24C512 */
00041 #define EEPROM_PAGE_SIZE   128
00042 
00043 tByteArray EEPROM_I2CRequest;    /*!< Array to hold I2C command data */
00044 tByteArray EEPROM_I2CReply;      /*!< Array to hold I2C reply data */
00045 
00046 /*
00047 <function prototypes>
00048 */
00049 bool EEPROMreadBytes(tSensors link, long address, tByteArray &data, int numbytes);
00050 bool EEPROMwriteBytes(tSensors link, long address, tByteArray &data, int numbytes);
00051 bool _EEPROMwriteDummy(tSensors link, long address);
00052 
00053 
00054 /**
00055  * Read a number of bytes from address
00056  * @param link the EEPROM
00057  * @param address the address to read from
00058  * @param data the byte array to store read result in
00059  * @param numbytes the number of bytes to read (limited to 16 at a time)
00060  * @return true if no error occured, false if it did
00061  */
00062 bool EEPROMreadBytes(tSensors link, long address, tByteArray &data, int numbytes) {
00063   if (!_EEPROMwriteDummy(link, address))
00064     return false;
00065 
00066   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00067 
00068   EEPROM_I2CRequest[0] = 1;               // Message size
00069   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR; //I2C Address
00070 
00071   if (!writeI2C(link, EEPROM_I2CRequest, EEPROM_I2CReply, numbytes))
00072     return false;
00073 
00074   memcpy(data, EEPROM_I2CReply, sizeof(tByteArray));
00075   return true;
00076 }
00077 
00078 
00079 /**
00080  * Perform a dummy write to set address for next read
00081  *
00082  * Note: this is an internal function and should not be called directly.
00083  * @param link the EEPROM
00084  * @param address the address to set
00085  * @return true if no error occured, false if it did
00086  */
00087 bool _EEPROMwriteDummy(tSensors link, long address) {
00088   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00089 
00090   EEPROM_I2CRequest[0] = 3;                             // Message size
00091   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR;               // I2C Address
00092   EEPROM_I2CRequest[2] = (byte)((address >> 8) & 0xFF); // upper 8 bits of address word
00093   EEPROM_I2CRequest[3] = (byte)(address & 0xFF);        // lower 8 bits of address word
00094 
00095   return writeI2C(link, EEPROM_I2CRequest);
00096 }
00097 
00098 
00099 /**
00100  * Write a single byte to address
00101  * @param link the EEPROM
00102  * @param address the address to write to
00103  * @param data the byte array to write
00104  * @param numbytes the number of bytes to write (limited to 13 at a time)
00105  * @return true if no error occured, false if it did
00106  */
00107 bool EEPROMwriteBytes(tSensors link, long address, tByteArray &data, int numbytes) {
00108   memset(EEPROM_I2CRequest, 0, sizeof(tByteArray));
00109 
00110   EEPROM_I2CRequest[0] = 3 + numbytes;                  // Message size
00111   EEPROM_I2CRequest[1] = EEPROM_I2C_ADDR;               // I2C Address
00112   EEPROM_I2CRequest[2] = (byte)((address >> 8) & 0xFF); // upper 8 bits of address word
00113   EEPROM_I2CRequest[3] = (byte)(address & 0xFF);        // lower 8 bits of address word
00114 
00115   if (numbytes > 13)
00116     numbytes = 13;
00117 
00118   if (numbytes < 0)
00119     numbytes = 0;
00120 
00121   memcpy(&EEPROM_I2CRequest[4], &data[0], numbytes);
00122   return writeI2C(link, EEPROM_I2CRequest);
00123 }
00124 
00125 /*
00126  * $Id: eeprom.h 133 2013-03-10 15:15:38Z xander $
00127  */
00128 /* @} */
00129 /* @} */