I2C EEPROM 24LCXXX Driver, custom to work with tortuga

Dependents:   TORTUGA_BLE

Fork of _24LCXXX by Hiroshi M

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     for (uint64_t i = 0 ; i<0xFFFF ; i++);
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         for (uint64_t i = 0 ; i<0xFFFF ; i++);
00120 
00121         if( ++mem_addr >= MAXADR_24LCXXX )      // Address counter +1
00122         {
00123             return -1;                          // Address range over
00124         }
00125     }
00126 
00127     return res;
00128 }
00129 
00130 int _24LCXXX::page_write( int mem_addr, char *data )
00131 {
00132     int i;
00133     int res;
00134     char buf[PAGE_SIZE_24LCXXX+2];
00135 
00136     buf[0] = (0xff00 & mem_addr)>>8;        // Write Address High byte set
00137     buf[1] = (0x00ff & mem_addr);           // Write Address Low  byte set
00138 
00139     for (i=0; i<PAGE_SIZE_24LCXXX; i++)
00140     {
00141         buf[i+2] = data[i];
00142     }
00143     res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
00144     if(_debug)
00145     {
00146         if(res==0)
00147         {
00148             _pc->printf("Success! Page Data Write. \n");
00149         }
00150         else
00151         {
00152             _pc->printf("Failed! Page Data Write %d.\n", res);
00153         }
00154     }
00155 
00156     return res;
00157 }
00158 
00159 
00160 int _24LCXXX::nbyte_read( int mem_addr, void *data, int size )
00161 {
00162     int res;
00163     char buf[2];
00164 
00165     buf[0] = (0xff00 & mem_addr)>>8;        // Read Address High byte set
00166     buf[1] = (0x00ff & mem_addr);           // Read Address Low  byte set
00167 
00168     res = _i2c->write(_i2c_address, buf, sizeof(buf), true);
00169     if(_debug)
00170     {
00171         if(res==0)
00172         {
00173             _pc->printf("Success! nbyte read address send. \n");
00174         }
00175         else
00176         {
00177             _pc->printf("Failed! nbyte read address send %d.\n", res);
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 }