Mark Peter Vargha / EERAM

Dependents:   EERAM_example

Files at this revision

API Documentation at this revision

Comitter:
vargham
Date:
Fri Apr 28 13:38:52 2017 +0000
Parent:
3:a869096d7a5d
Child:
5:9444f29f3429
Commit message:
API cleanup.

Changed in this revision

EERAM.cpp Show annotated file Show diff for this revision Revisions of this file
EERAM.h Show annotated file Show diff for this revision Revisions of this file
--- a/EERAM.cpp	Thu Apr 27 17:33:49 2017 +0000
+++ b/EERAM.cpp	Fri Apr 28 13:38:52 2017 +0000
@@ -2,7 +2,7 @@
 * @file    EERAM.cpp
 * @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
 *
@@ -49,7 +49,7 @@
     return start < _memorySize && start + length <= _memorySize && length > 0;
 }
 
-bool EERAM::write(uint16_t address, char *data, int length)
+bool EERAM::writeBytes(uint16_t address, char *data, int length)
 {
     bool success = false;
     success = checkAddressRange(address, length);
@@ -64,14 +64,14 @@
     return success;
 }
 
-bool EERAM::write(char *data, int length)
+bool EERAM::writeBytes(char *data, int length)
 {
     bool success = false;
     success = _i2c.write(_sramAddressWrite, data, length) == 0;
     return success;
 }
 
-bool EERAM::read(uint16_t address, char *data, int length)
+bool EERAM::readBytes(uint16_t address, char *data, int length)
 {
     bool success = checkAddressRange(address, length);
     if (success) success = setMemoryPointer(address, true);
@@ -83,7 +83,7 @@
     return success;
 }
 
-bool EERAM::read(char *data, int length)
+bool EERAM::readBytes(char *data, int length)
 {
     bool success = setMemoryPointer((uint8_t)data[1], (uint8_t)data[0], true);
     if (success)
--- 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