Driver library for Microchip I2C EERAM (47x04 and 47x16) 4 kbit or 16 kbit EEPROM backed SRAM.

Dependents:   EERAM_example

Revision:
4:fd84e2f0d1de
Parent:
3:a869096d7a5d
Child:
5:9444f29f3429
diff -r a869096d7a5d -r fd84e2f0d1de EERAM.h
--- a/EERAM.h	Thu Apr 27 17:33:49 2017 +0000
+++ b/EERAM.h	Fri Apr 28 13:38:52 2017 +0000
@@ -2,7 +2,7 @@
 * @file    EERAM.h
 * @brief   mbed driver for Microchip I2C EERAM devices (47x04 and 47x16)
 * @author  Mark Peter Vargha, vmp@varghamarkpeter.hu
-* @version 1.2.0
+* @version 1.3.0
 *
 * Copyright (c) 2017
 *
@@ -68,17 +68,17 @@
     while (1)
     {
         char dataStore[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
-        eeram.write(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently.
+        eeram.writeBytes(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently.
         wait(2.0);
         char dataRead[16];
-        eeram.read(0x100, dataRead, 16);
+        eeram.readBytes(0x100, dataRead, 16);
         wait(2.0);
         float floatToWrite = -1.976f;
         const uint16_t address = 0x200;
-        eeram.writeValue(address, &floatToWrite);
+        eeram.write(address, &floatToWrite);
         wait(2.0);
         float floatToRead = 0;
-        eeram.readValue(address, &floatToWrite);
+        eeram.read(address, &floatToWrite);
         wait(2.0);
     }
 }
@@ -123,17 +123,17 @@
 
     /** Copies the bytes from the given memory area to the given buffer. Uses the size of T to determine write length.
     * @param buffer Buffer to put bytes into
-    * @param param Pointer to the memory location
+    * @param source Pointer to the memory location
     *
     * @return Number of written bytes.
     */
     template <typename T>
-    static int putValueIntoBuffer(char *buffer, const int bufferSize, T *param)
+    static int putIntoBuffer(char *buffer, const int bufferSize, T *source)
     {
         const int length = sizeof(T);
         if (length <= bufferSize)
         {
-            char *bytes = static_cast<char*>(static_cast<void*>(param));
+            char *bytes = static_cast<char*>(static_cast<void*>(source));
             memcpy(buffer, bytes, length);
             return length;
         }
@@ -145,17 +145,17 @@
 
     /** Copies the bytes from the given buffer to the given memory area. Uses the size of T to determine read length.
     * @param buffer Buffer to get bytes from
-    * @param param Pointer to the memory location
+    * @param destination Pointer to the memory location
     *
     * @return Number of read bytes.
     */
     template <typename T>
-    static int getValueFromBuffer(char *buffer, const int bufferSize, T *param)
+    static int getFromBuffer(char *buffer, const int bufferSize, T *destination)
     {
         const int length = sizeof(T);
         if (length <= bufferSize)
         {
-            char *bytes = static_cast<char*>(static_cast<void*>(param));
+            char *bytes = static_cast<char*>(static_cast<void*>(destination));
             memcpy(bytes, buffer, length);
             return length;
         }
@@ -165,81 +165,94 @@
         }
     }
 
-    /** Copies the bytes from the given memory area to the given address of the EERAM. Uses the size of T to determine write length.
-    * @param source Pointer to the memory location where copy from
-    *
-    * @return Number of written bytes.
-    */
-    template <typename T>
-    int writeValue(uint16_t address, T *source)
-    {
-        bool success = false;
-        const int bufferSize = sizeof(T) + 2;
-        char buffer[bufferSize];
-        putAddressIntoBuffer(address, buffer);
-        putValueIntoBuffer(buffer + 2, bufferSize - 2, source);
-        success = write(buffer, bufferSize);
-        return success ? bufferSize - 2 : 0;
-    }
-
-    /** Copies the bytes from the given memory area to the given address of the EERAM. Uses the size of T and the array length to determine write length.
-    * @param sourceArray Pointer to the array where copy from
-    * @param length Length of the array
+    /** Copies the bytes from the given memory area to the given EERAM address. Uses the size of T and the length to determine write length. Allowed types: Primitives, fixed arrays and structs without pointers.
+    * @param source Pointer to memory where copy from.
+    * @param length Length of the array. Pass 1 here if it is not an array.
     *
     * @return Number of written bytes.
     */
     template <typename T>
-    int writeArray(uint16_t address, T *sourceArray, const int length)
+    int write(uint16_t address, T *source, const int length = 1)
     {
-        uint16_t addressPtr = address;
-        for (int i = 0; i < length; i++)
+        bool success = false;
+        const int typeLength = sizeof(T);
+        const int writeLength = typeLength * length;
+        const int addressLength = sizeof(uint16_t);
+        char *sourceAsBytes = static_cast<char*>(static_cast<void*>(source));
+        success = checkAddressRange(address, writeLength);
+        if (success) //Address range check
         {
-            int writtenBytes = writeValue(addressPtr, &sourceArray[i]);
-            if (writtenBytes == 0) break;
-            addressPtr += writtenBytes;
+            uint16_t addressPtr = address;
+            int sourcePtr = 0;
+            for (int i = 0; i < length; i++)
+            {
+                const int bufferSize = typeLength + addressLength;
+                char buffer[bufferSize];
+                putAddressIntoBuffer(addressPtr, buffer);
+                memcpy(buffer + addressLength, sourceAsBytes + sourcePtr, typeLength);
+                success = writeBytes(buffer, bufferSize);
+                if (success) //Write to I2C
+                {
+                    addressPtr += typeLength;
+                    sourcePtr += typeLength;
+                }
+                else
+                {
+                    break;
+                }
+            }
         }
-        return addressPtr - address;
+        return success ? writeLength : 0;
     }
 
-    /** Copies the bytes from the given EERAM address to the given memory area. Uses the size of T to determine read length.
-    * @param destination Pointer to the memory location where copy to
+    /** Copies the bytes from the given EERAM address to the given memory area. Uses the size of T and the length to determine read length. Allowed types: Primitives, fixed arrays and structs without pointers. Destination shall be allocated.
+    * @param destination Pointer to memory where copy to.
+    * @param length Length of the array. Pass 1 here if it is not an array.
+    * @param direct Applicable only when length > 1. If true reads all items of the array in one I2C read, otherwise read them in blocks.
     *
     * @return Number of read bytes.
     */
     template <typename T>
-    int readValue(uint16_t address, T *destination)
+    int read(uint16_t address, T *destination, const int length = 1, bool direct = false)
     {
         bool success = false;
-        const int bufferSize = sizeof(T);
-        char buffer[bufferSize];
-        success = read(address, buffer, bufferSize);
+        const int typeLength = sizeof(T);
+        const int readLength = typeLength * length;
+        char *destinationAsBytes = static_cast<char*>(static_cast<void*>(destination));
+        success = checkAddressRange(address, readLength);
+        if (success) success = setMemoryPointer(address, true);
         if (success)
         {
-            getValueFromBuffer(buffer, bufferSize, destination);
+            if (direct)
+            {
+                success = _i2c.read(_sramAddressRead, destinationAsBytes, readLength) == 0;
+            }
+            else
+            {
+                uint16_t addressPtr = address;
+                int destinationPtr = 0;
+                for (int i = 0; i < length; i++)
+                {
+                    const int bufferSize = typeLength;
+                    char buffer[bufferSize];
+                    success = readBytes(addressPtr, buffer, bufferSize);
+                    if (success)
+                    {
+                        memcpy(destinationAsBytes + destinationPtr, buffer, bufferSize);
+                        addressPtr += typeLength;
+                        destinationPtr += typeLength;
+                    }
+                    else
+                    {
+                        break;
+                    }
+                }
+            }
         }
-        return success ? bufferSize : 0;
+        return success ? readLength : 0;
     }
 
-    /** Copies the bytes from the given EERAM address to the given memory area. Uses the size of T and the array length to determine read length.
-    * @param destinationArray Pointer to the array where copy to
-    * @param length Length of the array
-    *
-    * @return Number of read bytes.
-    */
-    template <typename T>
-    int readArray(uint16_t address, T *destinationArray, const int length)
-    {
-        uint16_t addressPtr = address;
-        for (int i = 0; i < length; i++)
-        {
-            int readBytes = readValue(addressPtr, &destinationArray[i]);
-            if (readBytes == 0) break;
-            addressPtr += readBytes;
-        }
-        return addressPtr - address;
-    }
-
-    /** Writes data to the specified address.
+    /** Writes bytes to the specified address.
     * @param address The 16 bit destination address
     * @param data Pointer to the data buffer to read from
     * @param length Data length
@@ -248,7 +261,7 @@
     *   true on success
     *   false on fail
     */
-    bool write(uint16_t address, char *data, int length);
+    bool writeBytes(uint16_t address, char *data, int length);
 
     /** Writes data to the SRAM. The first two bytes shall be the address.
     * @param data Pointer to the data buffer to read from
@@ -258,7 +271,7 @@
     *   true on success
     *   false on fail
     */
-    bool write(char *data, int length);
+    bool writeBytes(char *data, int length);
 
     /** Reads data from the specified address.
     * @param address The 16 bit source address
@@ -269,7 +282,7 @@
     *   true on success
     *   false on fail
     */
-    bool read(uint16_t address, char *data, int length);
+    bool readBytes(uint16_t address, char *data, int length);
 
     /** Reads data to the specified buffer. The first two bytes shall be the address. These bytes will be unchanged.
     * @param data Pointer to the data buffer to read to
@@ -279,7 +292,7 @@
     *   true on success
     *   false on fail
     */
-    bool read(char *data, int length);
+    bool readBytes(char *data, int length);
 
     /** Fills memory with the specified byte from the specified address
     * @param address The 16 bit destination address