Simplified access to Ramtron (Cypress) FM24Vxx F-RAM devices

Dependents:   FM24Vxx_I2CApp

Files at this revision

API Documentation at this revision

Comitter:
Yann
Date:
Sat Apr 13 14:23:41 2013 +0000
Parent:
1:6a16bddd7222
Commit message:
Added new Read method from Reed Kimble request

Changed in this revision

FM24Vxx_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
FM24Vxx_I2C.h Show annotated file Show diff for this revision Revisions of this file
diff -r 6a16bddd7222 -r bf7d1264d3ff FM24Vxx_I2C.cpp
--- a/FM24Vxx_I2C.cpp	Wed Apr 03 12:51:20 2013 +0000
+++ b/FM24Vxx_I2C.cpp	Sat Apr 13 14:23:41 2013 +0000
@@ -106,12 +106,12 @@
         DEBUG("CFM24VXX_I2C::GetDevideIDs: pI2CBuffer[0]: 0x%02x", i2cBuffer[0])
         // 2. Send I2C start + 0xF8 + I2C ReStart
        if (_i2cInstance->write(0xf8, i2cBuffer, 1, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::GetDevideIDs: Write F8 done")
             // 3. read data + I2C stop
             unsigned char buffer[3];
             int result = _i2cInstance->read(0xf9, (char *)buffer, 3);
-            wait(0.02);
+            //wait(0.02);
             if (result == 0) {
                 // 4. Setup the device IDs
                 _deviceID = new CFM24VXX_IDs(buffer[0], buffer[1], buffer[2]);
@@ -135,12 +135,12 @@
         DEBUG("CFM24VXX_I2C::GetSerialNumber: pI2CBuffer[0]: 0x%02x", i2cBuffer[0])
         // 2. Send I2C start + 0xF8 + I2C ReStart
        if (_i2cInstance->write(0xf8, i2cBuffer, 1, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::GetSerialNumber: Write F8 done")
             // 3. read data + I2C stop
             unsigned char buffer[8]; // See FM24V10_ds.pdf Page 10/16 Figure 15. 8-Byte Serial Number (read-only)
             int result = _i2cInstance->read(0xcd, (char *)buffer, 8);
-            wait(0.02);
+            //wait(0.02);
             if (result == 0) {
                 // 4. Check if it is supported
                 if (buffer[7] != 0x00) { // SN supported
@@ -190,7 +190,7 @@
     
         // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
         int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3);
-        wait(0.02);
+        //wait(0.02);
     
         DEBUG_LEAVE("CFM24VXX_I2C::Write (byte) %x", (bool)(result == 0))
         return (bool)(result == 0);
@@ -219,7 +219,7 @@
     
         // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
         int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4);
-        wait(0.02);
+        //wait(0.02);
     
         DEBUG_LEAVE("CFM24VXX_I2C::Write (short) %x", (bool)(result == 0))
         return (bool)(result == 0);
@@ -252,7 +252,7 @@
     
         // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
         int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6);
-        wait(0.02);
+        //wait(0.02);
     
         DEBUG_LEAVE("CFM24VXX_I2C::Write (int) %x", (bool)(result == 0))
         return (bool)(result == 0);
@@ -270,18 +270,18 @@
         unsigned char array[length];
         std::copy(p_datas.begin(), p_datas.end(), array);
         bool result = Write(p_address, array, p_storeLength, length);
-        wait(0.02);
+        //wait(0.02);
     
         DEBUG_LEAVE("CFM24VXX_I2C::Write (std::vector): %d", result)
         return result;
     }
     
-    bool CFM24VXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) {
+    bool CFM24VXX_I2C::Write(const short p_address, const char *p_string, const bool p_storeLength, const int p_length2write) {
         DEBUG_ENTER("CFM24VXX_I2C::Write (char *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write)
     
         DEBUG("CFM24VXX_I2C::Write (char *): Slave address: %02x", _slaveAddress)
         // 1.Prepare buffer
-        int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write;
+        int length = (p_length2write == -1) ? strlen(p_string) : p_length2write;
         if (p_storeLength) {
             length += 4; // Add four bytes for the length as integer
         }
@@ -302,17 +302,17 @@
             i2cBuffer[4] = (unsigned char)(length >> 8);
             i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff);
             for (int i = 0; i < length - 4; i++) {
-                i2cBuffer[6 + i] = *(p_datas + i);
+                i2cBuffer[6 + i] = *(p_string + i);
             }
         } else { // The length was not stored
             for (int i = 0; i < length; i++) {
-                i2cBuffer[2 + i] = *(p_datas + i);
+                i2cBuffer[2 + i] = *(p_string + i);
             }
         }
         
         // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop
         int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length);
-        wait(0.02);
+        //wait(0.02);
     
         DEBUG_LEAVE("CFM24VXX_I2C::Write (char *) %x", (bool)(result == 0))
         return (bool)(result == 0);
@@ -336,11 +336,11 @@
     
         // 2. Send I2C start + memory address
         if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (byte): Write memory done")
             // 2. Read data + I2C stop
             int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1);
-            wait(0.02);
+            //wait(0.02);
     
             DEBUG_LEAVE("CFM24VXX_I2C::Read (byte): %x", (bool)(result == 0))
             return (bool)(result == 0);
@@ -363,7 +363,7 @@
     
         // 2. Send I2C start + memory address
         if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (short): Write memory done")
             // 2. Read data + I2C stop
             int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2);
@@ -397,13 +397,13 @@
     
         // 2. Send I2C start + memory address
         if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (int): Write memory done")
             // 2. Read data + I2C stop
             int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4);
             if (result == 0) {
                 DEBUG("CFM24VXX_I2C::Read (int): value: 0x%02x - 0x%02x - 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1], i2cBuffer[2], i2cBuffer[3])
-                wait(0.02);
+                //wait(0.02);
                 if (p_mode ==  BigEndian) {
                     *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]);
                 } else {
@@ -457,12 +457,12 @@
     
         // 3. Send I2C start + memory address
         if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (vector): Write memory done")
             // 4. read data + I2C stop
             unsigned char buffer[length];
             int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length);
-            wait(0.02);
+            //wait(0.02);
             if (result == 0) {
                 p_datas.assign(buffer, buffer + length);
     
@@ -477,17 +477,6 @@
     
     bool CFM24VXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) {
         DEBUG_ENTER("CFM24VXX_I2C::Read (string): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write)
-
-/*        std::vector<unsigned char> datas;
-        if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) {
-            p_string.assign((char *)datas.begin(), datas.size());
-            
-            return true;
-        }
-        
-        DEBUG_LEAVE("CFM24VXX_I2C::Read (string) (false)")
-        return false;
-*/        
     
         // 1.Prepare buffer
         short address = p_address;
@@ -497,7 +486,7 @@
                 DEBUG_ERROR("CFM24VXX_I2C::Read (string): Failed to read length")
                 return false;
             }
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (string): length=%d", length)
             if (length == 0) {
                 DEBUG_ERROR("CFM24VXX_I2C::Read (string): empty")
@@ -524,7 +513,7 @@
     
         // 3. Send I2C start + memory address with repeat start
         if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
-            wait(0.02);
+            //wait(0.02);
             DEBUG("CFM24VXX_I2C::Read (string): Write memory done")
             // 4. Read data + I2C stop
             char buffer[length];
@@ -540,6 +529,59 @@
         return false;
     }
     
+    bool CFM24VXX_I2C::Read(const short p_address, char *p_string, const bool p_storeLength, const int p_length2write) {
+        DEBUG_ENTER("CFM24VXX_I2C::Read (char *): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_storeLength, p_length2write)
+    
+        // 1.Prepare buffer
+        short address = p_address;
+        int length = -1;
+        if (p_storeLength) { // The string was stored with its length
+            if (!Read(address, &length)) { // Read the length as integer in big endian mode
+                DEBUG_ERROR("CFM24VXX_I2C::Read (char *): Failed to read length")
+                return false;
+            }
+            //wait(0.02);
+            DEBUG("CFM24VXX_I2C::Read (char *): length=%d", length)
+            if (length == 0) {
+                DEBUG_ERROR("CFM24VXX_I2C::Read (char *): empty")
+                return true;
+            }
+            address += 4; // Skip the length value size 
+            length -= 4; // length is the size of (string length + string)
+        } else { // The string length is provided by p_length2write parameter
+            if (p_length2write == -1) {
+                DEBUG_ERROR("CFM24VXX_I2C::Read (char *): undefined length")
+                return false;
+            } else {
+                length = p_length2write;
+            }
+        }
+        DEBUG("CFM24VXX_I2C::Read (char *): Address=0x%02x - Length=%d", address, length)
+    
+        // 2. Memory address
+        char i2cBuffer[2];
+        i2cBuffer[0] = (unsigned char)(address >> 8);
+        DEBUG("CFM24VXX_I2C::Read (char *): pI2CBuffer[0]: 0x%02x", i2cBuffer[0])
+        i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff);
+        DEBUG("CFM24VXX_I2C::Read (char *): pI2CBuffer[1]: 0x%02x", i2cBuffer[1])
+    
+        // 3. Send I2C start + memory address with repeat start
+        if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) {
+            //wait(0.02);
+            DEBUG("CFM24VXX_I2C::Read (char *): Write memory done")
+            // 4. Read data + I2C stop
+            char buffer[length];
+            int result = _i2cInstance->read(_slaveAddress, buffer, length);
+            memcpy((void *)p_string, (const void *)buffer, length);
+
+            DEBUG_LEAVE("CFM24VXX_I2C::Read (char *): %x", (bool)(result == 0))
+            return (bool)(result == 0);
+        }
+                
+        DEBUG_LEAVE("CFM24VXX_I2C::Read (char *) (false)")
+        return false;
+    }
+    
     unsigned char CFM24VXX_I2C::ChecksumSN(const unsigned char *pdatas, const unsigned int length) {
         DEBUG_ENTER("CFM24VXX_I2C::ChecksumSN")
    
diff -r 6a16bddd7222 -r bf7d1264d3ff FM24Vxx_I2C.h
--- a/FM24Vxx_I2C.h	Wed Apr 03 12:51:20 2013 +0000
+++ b/FM24Vxx_I2C.h	Sat Apr 13 14:23:41 2013 +0000
@@ -221,16 +221,17 @@
          */
         bool Write(const short p_address, const std::string & p_string, const bool p_storeLength = true, const int p_length2write = -1);
     
-        /** Write a buffer of characters at the specified memory address (from 0 to N - 1, N is the number of cells of the memory)
+        /** Write a buffer of characters at the specified memory address (from 0 to N - 1, N is the number of cells of the memory).
+         *  The NULL character is not written.
          *
          * Note that the length of the buffer is not saved and the string is saved in Big Endian mode
          * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory)
-         * @param p_datas The string to save
+         * @param p_string The string to save
          * @param p_storeLength If true, store also the length of the string in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true.
          * @param length2write The number of character to write, -1 for all characters
          * @return true on success, false otherwise
          */
-        bool Write(const short p_address, const char *p_datas, const bool p_storeLength = true, const int p_length2write = -1);
+        bool Write(const short p_address, const char *p_string, const bool p_storeLength = true, const int p_length2write = -1);
     
         /** Read a byte from the specified memory address
          *
@@ -312,7 +313,17 @@
          * @endcode
          */
         bool Read(const short p_address, std::string & p_string, bool p_readLengthFirst = true, int p_length2write = -1);
-    
+        
+        /** Read a buffer of characters at the specified memory address. The NULL character is not read.
+         *
+         * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory)
+         * @param p_string The string to read
+         * @param p_storeLength If true, store also the length of the buffer in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true.
+         * @param p_length2write The number of characters to write, -1 for all bytes. Default value: -1
+         * @return true on success, false otherwise
+         */
+        bool Read(const short p_address, char *p_string, const bool p_storeLength = true, const int p_length2write = -1);
+        
         /** Activate or deactivate write protect (pin 7)
          *
          * @param p_writeProtect: Set to true to activate write protection, false otherwise