Driver library for Microchip I2C EERAM (47x04 and 47x16) 4 kbit or 16 kbit EEPROM backed SRAM.
Diff: EERAM.h
- Revision:
- 2:bdbf9de0e985
- Parent:
- 0:19f9af07424a
- Child:
- 3:a869096d7a5d
diff -r 6028bc22d82f -r bdbf9de0e985 EERAM.h --- a/EERAM.h Tue Apr 25 16:22:53 2017 +0000 +++ b/EERAM.h Thu Apr 27 13:25:23 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.0.0 +* @version 1.1.0 * * Copyright (c) 2017 * @@ -54,7 +54,7 @@ #include "mbed.h" #include "EERAM.h" -EERAM eeram(I2C_SDA, I2C_SCL, 2048); +EERAM eeram(PC_9, PA_8, 2048); //SDA, SCL int main() { @@ -73,6 +73,13 @@ char dataRead[16]; eeram.read(0x100, dataRead, 16); wait(2.0); + float floatToWrite = -1.976f; + const uint16_t address = 0x200; + eeram.writeValue(address, &floatToWrite); + wait(2.0); + float floatToRead = 0; + eeram.readValue(address, &floatToWrite); + wait(2.0); } } @@ -97,7 +104,7 @@ initialize(A1, A2); }; - /** Create an I2C EERAM interface, connected to the I2C, with specified size and with the specified address pins + /** Create an I2C EERAM interface, connected to the I2C, with specified size and with the specified address pins. * @param 12c I2C * @param memorySize Size of EERAM, 512 for 47x04 and 2048 for 47x16 * @param A1 EERAM A1 pin state (true = high, false = low) @@ -110,7 +117,77 @@ initialize(A1, A2); }; - /** Writes data to the specified address + /** Puts the given 16 bit SRAM address into the first two bytes of the given buffer. + */ + 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. + * @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) + { + const int length = sizeof(T); + char *bytes = static_cast<char*>(static_cast<void*>(param)); + memcpy(buffer, bytes, length); + return length; + } + + /** 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. + * @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) + { + const int length = sizeof(T); + char *bytes = static_cast<char*>(static_cast<void*>(param)); + memcpy(bytes, buffer, length); + return length; + } + + /** 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 + * + * @return Number of written bytes. + */ + template <typename T> + int writeValue(uint16_t address, T *param) + { + 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; + } + + /** 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 + * + * @return Number of read bytes. + */ + template <typename T> + int readValue(uint16_t address, T *param) + { + bool success = false; + const int length = sizeof(T); + char data[length]; + success = read(address, data, length); + if (success) + { + getValueFromBuffer(data, param); + } + return success ? length : 0; + } + + /** Writes data 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 @@ -121,7 +198,17 @@ */ bool write(uint16_t address, char *data, int length); - /** Reads data from the specified address + /** Writes data to the SRAM. The first two bytes shall be the address. + * @param data Pointer to the data buffer to read from + * @param length Data length + * + * @return + * true on success + * false on fail + */ + bool write(char *data, int length); + + /** Reads data from the specified address. * @param address The 16 bit source address * @param data Pointer to the data buffer to read to * @param length Data length @@ -132,6 +219,16 @@ */ bool read(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 + * @param length Data length + * + * @return + * true on success + * false on fail + */ + bool read(char *data, int length); + /** Fills memory with the specified byte from the specified address * @param address The 16 bit destination address * @param data The byte to write @@ -310,6 +407,7 @@ bool checkAddressRange(uint16_t start, uint16_t length); bool writeRegister(uint8_t registerAddress, uint8_t data); bool setMemoryPointer(uint16_t address, bool stop = true); + bool setMemoryPointer(uint8_t address_0, uint8_t address_1, bool stop = true); }; #endif //EERAM_h