Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 3:a869096d7a5d, committed 2017-04-27
- Comitter:
- vargham
- Date:
- Thu Apr 27 17:33:49 2017 +0000
- Parent:
- 2:bdbf9de0e985
- Child:
- 4:fd84e2f0d1de
- Commit message:
- Added array handling.
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 13:25:23 2017 +0000 +++ b/EERAM.cpp Thu Apr 27 17:33:49 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.1.0 +* @version 1.2.0 * * Copyright (c) 2017 *
--- a/EERAM.h Thu Apr 27 13:25:23 2017 +0000
+++ b/EERAM.h Thu Apr 27 17:33:49 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.1.0
+* @version 1.2.0
*
* Copyright (c) 2017
*
@@ -117,74 +117,126 @@
initialize(A1, A2);
};
- /** Puts the given 16 bit SRAM address into the first two bytes of the given buffer.
+ /** Puts the given 16 bit SRAM address into the first two bytes of the given buffer. The buffer size shall be minimum two bytes. No length check.
*/
static void putAddressIntoBuffer(uint16_t address, char *data);
- /** Copies the bytes from the given memory area to the given buffer. Warning, does not check buffer size. Uses the size of T to determine write length.
+ /** 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
*
* @return Number of written bytes.
*/
template <typename T>
- static int putValueIntoBuffer(char *buffer, T *param)
+ static int putValueIntoBuffer(char *buffer, const int bufferSize, T *param)
{
const int length = sizeof(T);
- char *bytes = static_cast<char*>(static_cast<void*>(param));
- memcpy(buffer, bytes, length);
- return length;
+ if (length <= bufferSize)
+ {
+ char *bytes = static_cast<char*>(static_cast<void*>(param));
+ memcpy(buffer, bytes, length);
+ return length;
+ }
+ else
+ {
+ return 0;
+ }
}
- /** Copies the bytes from the given buffer to the given memory area. Warning, does not check buffer size. Uses the size of T to determine read length.
+ /** 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
*
* @return Number of read bytes.
*/
template <typename T>
- static int getValueFromBuffer(char *buffer, T *param)
+ static int getValueFromBuffer(char *buffer, const int bufferSize, T *param)
{
const int length = sizeof(T);
- char *bytes = static_cast<char*>(static_cast<void*>(param));
- memcpy(bytes, buffer, length);
- return length;
+ if (length <= bufferSize)
+ {
+ char *bytes = static_cast<char*>(static_cast<void*>(param));
+ memcpy(bytes, buffer, length);
+ return length;
+ }
+ else
+ {
+ return 0;
+ }
}
/** 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 param Pointer to the memory location where copy from
+ * @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
*
* @return Number of written bytes.
*/
template <typename T>
- int writeValue(uint16_t address, T *param)
+ int writeArray(uint16_t address, T *sourceArray, const int length)
{
- bool success = false;
- const int length = sizeof(T) + 2;
- char data[length];
- putAddressIntoBuffer(address, data);
- putValueIntoBuffer(data + 2, param);
- success = write(data, length);
- return success ? length - 2 : 0;
+ uint16_t addressPtr = address;
+ for (int i = 0; i < length; i++)
+ {
+ int writtenBytes = writeValue(addressPtr, &sourceArray[i]);
+ if (writtenBytes == 0) break;
+ addressPtr += writtenBytes;
+ }
+ return addressPtr - address;
}
/** Copies the bytes from the given EERAM address to the given memory area. Uses the size of T to determine read length.
- * @param param Pointer to the memory location where copy to
+ * @param destination Pointer to the memory location where copy to
*
* @return Number of read bytes.
*/
template <typename T>
- int readValue(uint16_t address, T *param)
+ int readValue(uint16_t address, T *destination)
{
bool success = false;
- const int length = sizeof(T);
- char data[length];
- success = read(address, data, length);
+ const int bufferSize = sizeof(T);
+ char buffer[bufferSize];
+ success = read(address, buffer, bufferSize);
if (success)
{
- getValueFromBuffer(data, param);
+ getValueFromBuffer(buffer, bufferSize, destination);
}
- return success ? length : 0;
+ return success ? bufferSize : 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.