I2C EEPROM 24LCXXX Driver

Dependents:   LPC1114FN28_I2C_24LCXXX ILI9341_Clock_Nucleo StormXalike rtc ... more

Files at this revision

API Documentation at this revision

Comitter:
bant62
Date:
Fri Dec 06 09:34:38 2013 +0000
Commit message:
first commit

Changed in this revision

_24LCXXX.cpp Show annotated file Show diff for this revision Revisions of this file
_24LCXXX.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 859387a87312 _24LCXXX.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_24LCXXX.cpp	Fri Dec 06 09:34:38 2013 +0000
@@ -0,0 +1,195 @@
+/**
+ *****************************************************************************
+ * File Name    : _24LCXXX.cpp
+ *
+ * Title        : I2C EEPROM 24LCXXX Claass Source File
+ * Revision     : 0.1
+ * Notes        :
+ * Target Board : mbed NXP LPC1768, mbed LPC1114FN28  etc
+ * Tool Chain   : ????
+ *
+ * Revision History:
+ * When         Who         Description of change
+ * -----------  ----------- -----------------------
+ * 2012/12/06   Hiroshi M   init
+ *****************************************************************************
+ *
+ * Copyright (C) 2013 Hiroshi M, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **/
+
+/* Includes ----------------------------------------------------------------- */
+#include "_24LCXXX.h"
+#include "mbed.h"
+
+/* Private typedef ---------------------------------------------------------- */
+/* Private define ----------------------------------------------------------- */
+/* Private macro ------------------------------------------------------------ */
+/* Private variables -------------------------------------------------------- */
+
+/* member fanctions --------------------------------------------------------- */
+
+// Constractor
+_24LCXXX::_24LCXXX(I2C *i2c, const int address):
+    _i2c_address(address<<1), _i2c(i2c), _pc(NULL), _debug(false)
+{
+}
+
+_24LCXXX::_24LCXXX(I2C *i2c, Serial *pc, const int address):
+    _i2c_address(address<<1), _i2c(i2c), _pc(pc), _debug(true)
+{
+}
+
+int _24LCXXX::byte_write( int mem_addr, char data )
+{
+    int res;
+    char buf[3];
+
+    buf[0] = (0xff00 & mem_addr)>>8;        // Write Address High byte set
+    buf[1] = (0x00ff & mem_addr);           // Write Address Low  byte set
+    buf[2] = data;
+
+    res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
+    if(_debug)
+    {
+        if(res==0)
+        {
+            _pc->printf("Success! Byte Data Write. \n");
+        }
+        else
+        {
+            _pc->printf("Failed! Byte Data Write %d.\n", res);
+        }
+    }
+
+    wait_ms(5);         // 5mS
+
+    return res;
+}
+
+int _24LCXXX::nbyte_write( int mem_addr, void *data, int size )
+{
+    int  i;
+    int  res;
+    char buf[3];
+    char *p;
+
+    p = (char *)data;
+    res = -1;
+    for ( i = 0; i < size; i++ )
+    {
+        buf[0] = (0xff00 & mem_addr)>>8;        // Read Address High byte set
+        buf[1] = (0x00ff & mem_addr);           // Read Address Low  byte set
+        buf[2] = *p++;
+
+        res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
+        if(_debug)
+        {
+            if(res==0)
+            {
+                _pc->printf("Success! N-Byte Data Write. \n");
+            }
+            else
+            {
+                _pc->printf("Failed! N-Byte Data Write %d.\n", res);
+            }
+        }
+
+        if(res!=0)
+        {
+            return res;
+        }
+
+        wait_ms(5);         // 5mS
+
+        if( ++mem_addr >= MAXADR_24LCXXX )      // Address counter +1
+        {
+            return -1;                          // Address range over
+        }
+    }
+
+    return res;
+}
+
+int _24LCXXX::page_write( int mem_addr, char *data )
+{
+    int i;
+    int res;
+    char buf[PAGE_SIZE_24LCXXX+2];
+
+    buf[0] = (0xff00 & mem_addr)>>8;        // Write Address High byte set
+    buf[1] = (0x00ff & mem_addr);           // Write Address Low  byte set
+
+    for (i=0; i<PAGE_SIZE_24LCXXX; i++)
+    {
+        buf[i+2] = data[i];
+    }
+    res = _i2c->write(_i2c_address, buf, sizeof(buf), false);
+    if(_debug)
+    {
+        if(res==0)
+        {
+            _pc->printf("Success! Page Data Write. \n");
+        }
+        else
+        {
+            _pc->printf("Failed! Page Data Write %d.\n", res);
+        }
+    }
+
+    return res;
+}
+
+
+int _24LCXXX::nbyte_read( int mem_addr, void *data, int size )
+{
+    int res;
+    char buf[2];
+
+    buf[0] = (0xff00 & mem_addr)>>8;        // Read Address High byte set
+    buf[1] = (0x00ff & mem_addr);           // Read Address Low  byte set
+
+    res = _i2c->write(_i2c_address, buf, sizeof(buf), true);
+    if(_debug)
+    {
+        if(res==0)
+        {
+            _pc->printf("Success! nbyte read address send. \n");
+        }
+        else
+        {
+            _pc->printf("Failed! nbyte read address send %d.\n", res);
+        }
+    }
+
+    //
+    res = _i2c->read(_i2c_address, (char *)data, size, false);
+    if(_debug)
+    {
+        if(res==0)
+        {
+            _pc->printf("Success! nbyte read address send. \n");
+        }
+        else
+        {
+            _pc->printf("Failed! nbyte read address send %d.\n", res);
+        }
+    }
+
+    return res;
+}
diff -r 000000000000 -r 859387a87312 _24LCXXX.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_24LCXXX.h	Fri Dec 06 09:34:38 2013 +0000
@@ -0,0 +1,84 @@
+/**
+ *****************************************************************************
+ * File Name    : _24LCXXX.h
+ *
+ * Title        : I2C EEPROM 24LCXXX Claass Header File
+ * Revision     : 0.1
+ * Notes        :
+ * Target Board : mbed NXP LPC1768, mbed LPC1114FN28  etc
+ * Tool Chain   : ????
+ *
+ * Revision History:
+ * When         Who         Description of change
+ * -----------  ----------- -----------------------
+ * 2012/12/06   Hiroshi M   init
+ *****************************************************************************
+ *
+ * Copyright (C) 2013 Hiroshi M, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **/
+
+#ifndef __24LCXXX_H_
+#define __24LCXXX_H_
+
+/* Includes ----------------------------------------------------------------- */
+#include "mbed.h"
+
+/* typedef ------------------------------------------------------------------ */
+
+/* define ------------------------------------------------------------------- */
+#define     I2C_ADDR_24LCXXX        0x50
+#define     __24LC256__
+
+#if defined(__24LC64__)
+#define     MAXADR_24LCXXX          8192
+#define     PAGE_SIZE_24LCXXX       32
+#endif
+
+#if defined(__24LC128__)
+#define     MAXADR_24LCXXX          16384
+#define     PAGE_SIZE_24LCXXX       64
+#endif
+
+#if defined(__24LC256__)
+#define     MAXADR_24LCXXX          32768
+#define     PAGE_SIZE_24LCXXX       64
+#endif
+
+/* macro -------------------------------------------------------------------- */
+/* variables ---------------------------------------------------------------- */
+/* class -------------------------------------------------------------------- */
+
+class _24LCXXX
+{
+private:
+    int _i2c_address;
+    I2C *_i2c;
+    Serial *_pc;
+    bool _debug;
+
+public:
+    _24LCXXX(I2C *i2c, const int address=I2C_ADDR_24LCXXX );
+    _24LCXXX(I2C *i2c, Serial *pc, const int address=I2C_ADDR_24LCXXX );
+    int byte_write( int mem_addr, char data );
+    int nbyte_write( int mem_addr, void *data, int size );
+    int page_write( int mem_addr, char *data );
+    int nbyte_read( int mem_addr, void *data, int size );
+};
+
+#endif /* __24LCXXX_H_ */
\ No newline at end of file