I2C EEPROM 24LCXXX Driver

Dependents:   LPC1114FN28_I2C_24LCXXX ILI9341_Clock_Nucleo StormXalike rtc ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers _24LCXXX.cpp Source File

_24LCXXX.cpp

00001 /**
00002  *****************************************************************************
00003  * File Name    : _24LCXXX.cpp
00004  *
00005  * Title        : I2C EEPROM 24LCXXX Claass Source File
00006  * Revision     : 0.1
00007  * Notes        :
00008  * Target Board : mbed NXP LPC1768, mbed LPC1114FN28  etc
00009  * Tool Chain   : ????
00010  *
00011  * Revision History:
00012  * When         Who         Description of change
00013  * -----------  ----------- -----------------------
00014  * 2012/12/06   Hiroshi M   init
00015  *****************************************************************************
00016  *
00017  * Copyright (C) 2013 Hiroshi M, MIT License
00018  *
00019  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00020  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00021  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00022  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00023  * furnished to do so, subject to the following conditions:
00024  *
00025  * The above copyright notice and this permission notice shall be included in all copies or
00026  * substantial portions of the Software.
00027  *
00028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00029  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00030  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00031  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00032  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  **/
00035 
00036 /* Includes ----------------------------------------------------------------- */
00037 #include "_24LCXXX.h"
00038 #include "mbed.h"
00039 
00040 /* Private typedef ---------------------------------------------------------- */
00041 /* Private define ----------------------------------------------------------- */
00042 /* Private macro ------------------------------------------------------------ */
00043 /* Private variables -------------------------------------------------------- */
00044 
00045 /* member fanctions --------------------------------------------------------- */
00046 
00047 // Constractor
00048 _24LCXXX::_24LCXXX(I2C *i2c, const int address):
00049     _i2c_address(address<<1), _i2c(i2c), _pc(NULL), _debug(false)
00050 {
00051 }
00052 
00053 _24LCXXX::_24LCXXX(I2C *i2c, Serial *pc, const int address):
00054     _i2c_address(address<<1), _i2c(i2c), _pc(pc), _debug(true)
00055 {
00056 }
00057 
00058 int _24LCXXX::byte_write( int mem_addr, char data )
00059 {
00060     int res;
00061     char buf[3];
00062 
00063     buf[0] = (0xff00 & mem_addr)>>8;        // Write Address High byte set
00064     buf[1] = (0x00ff & mem_addr);           // Write Address Low  byte set
00065     buf[2] = data;
00066 
00067     res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
00068     if(_debug)
00069     {
00070         if(res==0)
00071         {
00072             _pc->printf("Success! Byte Data Write. \n");
00073         }
00074         else
00075         {
00076             _pc->printf("Failed! Byte Data Write %d.\n", res);
00077         }
00078     }
00079 
00080     wait_ms(5);         // 5mS
00081 
00082     return res;
00083 }
00084 
00085 int _24LCXXX::nbyte_write( int mem_addr, void *data, int size )
00086 {
00087     int  i;
00088     int  res;
00089     char buf[3];
00090     char *p;
00091 
00092     p = (char *)data;
00093     res = -1;
00094     for ( i = 0; i < size; i++ )
00095     {
00096         buf[0] = (0xff00 & mem_addr)>>8;        // Read Address High byte set
00097         buf[1] = (0x00ff & mem_addr);           // Read Address Low  byte set
00098         buf[2] = *p++;
00099 
00100         res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
00101         if(_debug)
00102         {
00103             if(res==0)
00104             {
00105                 _pc->printf("Success! N-Byte Data Write. \n");
00106             }
00107             else
00108             {
00109                 _pc->printf("Failed! N-Byte Data Write %d.\n", res);
00110             }
00111         }
00112 
00113         if(res!=0)
00114         {
00115             return res;
00116         }
00117 
00118         wait_ms(5);         // 5mS
00119 
00120         if( ++mem_addr >= MAXADR_24LCXXX )      // Address counter +1
00121         {
00122             return -1;                          // Address range over
00123         }
00124     }
00125 
00126     return res;
00127 }
00128 
00129 int _24LCXXX::page_write( int mem_addr, char *data )
00130 {
00131     int i;
00132     int res;
00133     char buf[PAGE_SIZE_24LCXXX+2];
00134 
00135     buf[0] = (0xff00 & mem_addr)>>8;        // Write Address High byte set
00136     buf[1] = (0x00ff & mem_addr);           // Write Address Low  byte set
00137 
00138     for (i=0; i<PAGE_SIZE_24LCXXX; i++)
00139     {
00140         buf[i+2] = data[i];
00141     }
00142     res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
00143     if(_debug)
00144     {
00145         if(res==0)
00146         {
00147             _pc->printf("Success! Page Data Write. \n");
00148         }
00149         else
00150         {
00151             _pc->printf("Failed! Page Data Write %d.\n", res);
00152         }
00153     }
00154 
00155     return res;
00156 }
00157 
00158 
00159 int _24LCXXX::nbyte_read( int mem_addr, void *data, int size )
00160 {
00161     int res;
00162     char buf[2];
00163 
00164     buf[0] = (0xff00 & mem_addr)>>8;        // Read Address High byte set
00165     buf[1] = (0x00ff & mem_addr);           // Read Address Low  byte set
00166 
00167     res = _i2c->write(_i2c_address, buf, sizeof(buf), true);
00168     if(_debug)
00169     {
00170         if(res==0)
00171         {
00172             _pc->printf("Success! nbyte read address send. \n");
00173         }
00174         else
00175         {
00176             _pc->printf("Failed! nbyte read address send %d.\n", res);
00177         }
00178     }
00179 
00180     //
00181     res = _i2c->read(_i2c_address, (char *)data, size, false);
00182     if(_debug)
00183     {
00184         if(res==0)
00185         {
00186             _pc->printf("Success! nbyte read address send. \n");
00187         }
00188         else
00189         {
00190             _pc->printf("Failed! nbyte read address send %d.\n", res);
00191         }
00192     }
00193 
00194     return res;
00195 }