EEprom i2c 24AA08 & 24LC08 (no Address pin) library control

Dependents:   accuBlast_display

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Wed Sep 10 08:57:38 2014 +0000
Commit message:
first release

Changed in this revision

Ee24xx08.cpp Show annotated file Show diff for this revision Revisions of this file
Ee24xx08.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ee24xx08.cpp	Wed Sep 10 08:57:38 2014 +0000
@@ -0,0 +1,49 @@
+#include "Ee24xx08.h"
+
+
+//***********************************/************************************
+//                         Constructors                                 //
+//***********************************/************************************
+Ee24xx08::Ee24xx08(I2C *i2c):_i2c(i2c)
+{ 
+}
+
+//***********************************/************************************
+//                             Public Methods                           //
+//***********************************/************************************      
+bool Ee24xx08::write(const short  addr, const char byte)
+{
+    char i2cBuffer[2];
+    i2cBuffer[0] = (unsigned char)addr; //8lsb of address word
+    i2cBuffer[1] = byte;                //byte to write
+    return _i2c->write(EE24xx08ADDR|((addr>>7)&0X0E),i2cBuffer,2);  //i2c adress + page adress & data send
+}
+
+bool Ee24xx08::write(const short  addr, const char* buffer,const int len)
+{
+    char i2cBuffer[len+1];
+    i2cBuffer[0] = (unsigned char)addr;
+    memcpy(i2cBuffer+1,buffer,len);
+
+    return _i2c->write(EE24xx08ADDR|((addr>>7)&0X0E),i2cBuffer,len);
+}
+
+bool Ee24xx08::read(const short  addr, char *byte)
+{
+
+    if(_i2c->write(EE24xx08ADDR|((addr>>7)&0X0E),(char*)&addr,1,true))
+    {
+        return true;   
+    }
+    return _i2c->read(EE24xx08ADDR|((addr>>7)&0X0E),byte,1);
+}
+
+bool Ee24xx08::read(const short  addr, char *buffer, const int len)
+{
+
+    if(_i2c->write(EE24xx08ADDR|((addr>>7)&0X0E),(char*)&addr,1,true))
+    {
+        return true;   
+    }
+    return _i2c->read(EE24xx08ADDR|((addr>>7)&0X0E),buffer,len);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ee24xx08.h	Wed Sep 10 08:57:38 2014 +0000
@@ -0,0 +1,77 @@
+#ifndef EE24XX08_H
+#define EE24XX08_H
+
+#include "mbed.h"
+
+#define EE24xx08ADDR  0xA0
+
+/** Ee24xx08 class.
+ *  I2C eeprom (without functional adress pins) driver
+ *  for 24xx00,24xx01,24xx02,24xx04,24xx08,24xx016 device
+ 
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Ee24xx08.h"
+ *
+ * Serial ser(USBTX,USBRX);
+ * I2C iic(p28,p27);Ee24xx08 eeprom(&iic, &ser);
+ *
+ * int main() 
+ * {
+ *    char str[12] = "hello wolrd";
+ *
+ *     eeprom.write(54,str,12);
+ *     wait(1);
+ *     eeprom.read(54,str,12);
+ *     ser.printf("%s\r\n",str);
+ *     
+ *     while(1) 
+ *     {
+ *     }
+ * }
+ * @endcode
+ */  
+class Ee24xx08
+{
+    public:
+    /** Create Ee24xx08 instance
+    * @param I2C bus connected to eeprom
+    */
+    Ee24xx08(I2C *i2c);
+    
+    /**Write byte on eeprom
+    * @param addr Byte address in memory
+    * @param byte Byte to write in memory
+    * @returns 0 if succes
+    */   
+    bool write(const short  addr, const char byte);
+    
+    /**Write buffer on eeprom
+    * @param addr Buffer start address in memory
+    * @param buffer buffer to write in memory
+    * @param len number of byte to write in memory
+    * @returns 0 if succes
+    */     
+    bool write(const short  addr, const char* buffer,const int len);
+    
+    /**Read byte on eeprom
+    * @param addr Byte address in memory
+    * @param byte Byte to read in memory
+    * @returns 0 if succes
+    */ 
+    bool read(const short  addr, char *byte);
+    
+    /**Read buffer on eeprom
+    * @param addr Buffer start address in memory
+    * @param buffer buffer to read in memory
+    * @param len number of byte to read in memory
+    * @returns 0 if succes
+    */ 
+    bool read(const short  addr, char* buffer,const int len);
+        
+    protected:
+        I2C *_i2c;
+};
+
+#endif
\ No newline at end of file