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

Dependents:   EERAM_example

Committer:
vargham
Date:
Sun Oct 29 18:31:47 2017 +0000
Revision:
7:038c52e2268b
Parent:
6:b3f9b29b07ba
Changed I2C pointer to reference.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vargham 0:19f9af07424a 1 /**
vargham 0:19f9af07424a 2 * @file EERAM.h
vargham 0:19f9af07424a 3 * @brief mbed driver for Microchip I2C EERAM devices (47x04 and 47x16)
vargham 0:19f9af07424a 4 * @author Mark Peter Vargha, vmp@varghamarkpeter.hu
vargham 5:9444f29f3429 5 * @version 1.4.0
vargham 0:19f9af07424a 6 *
vargham 0:19f9af07424a 7 * Copyright (c) 2017
vargham 0:19f9af07424a 8 *
vargham 0:19f9af07424a 9 * Licensed under the Apache License, Version 2.0 (the "License");
vargham 0:19f9af07424a 10 * you may not use this file except in compliance with the License.
vargham 0:19f9af07424a 11 * You may obtain a copy of the License at
vargham 0:19f9af07424a 12 *
vargham 0:19f9af07424a 13 * http://www.apache.org/licenses/LICENSE-2.0
vargham 0:19f9af07424a 14 *
vargham 0:19f9af07424a 15 * Unless required by applicable law or agreed to in writing, software
vargham 0:19f9af07424a 16 * distributed under the License is distributed on an "AS IS" BASIS,
vargham 0:19f9af07424a 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vargham 0:19f9af07424a 18 * See the License for the specific language governing permissions and
vargham 0:19f9af07424a 19 * limitations under the License.
vargham 0:19f9af07424a 20 */
vargham 0:19f9af07424a 21
vargham 0:19f9af07424a 22 #ifndef EERAM_h
vargham 0:19f9af07424a 23 #define EERAM_h
vargham 0:19f9af07424a 24
vargham 0:19f9af07424a 25 #include "mbed.h"
vargham 0:19f9af07424a 26
vargham 0:19f9af07424a 27 //#define DEBUG_EERAM
vargham 0:19f9af07424a 28
vargham 0:19f9af07424a 29 #ifdef DEBUG_EERAM
vargham 0:19f9af07424a 30 extern Serial serial;
vargham 0:19f9af07424a 31 #endif
vargham 0:19f9af07424a 32
vargham 0:19f9af07424a 33 enum ProtectedMemoryArea
vargham 0:19f9af07424a 34 {
vargham 0:19f9af07424a 35 NONE = 0, U64, U32, U16, U8, U4, U2, ALL
vargham 0:19f9af07424a 36 };
vargham 0:19f9af07424a 37
vargham 0:19f9af07424a 38 /** An I2C EERAM interface to communicate with Microchip 47x04 and 47x16 devices
vargham 0:19f9af07424a 39 * 4 kbit (512 byte) or 16 kbit (2048 byte) EEPROM backed I2C SRAM
vargham 0:19f9af07424a 40 * The device could detect power down and stores SRAM contents in EEPROM. The SRAM is recalled from EEPROM on power up.
vargham 0:19f9af07424a 41 *
vargham 0:19f9af07424a 42 * <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/20005371C.pdf">47x04 and 47x16 datasheet</a>
vargham 0:19f9af07424a 43 * <a href="http://ww1.microchip.com/downloads/cn/AppNotes/cn588417.pdf">Recommended Usage of Microchip I2C EERAM Devices</a>
vargham 0:19f9af07424a 44 * <a href="http://ww1.microchip.com/downloads/en/AppNotes/00002257A.pdf">Choosing the Right EERAM VCAP Capacitor</a>
vargham 0:19f9af07424a 45 *
vargham 0:19f9af07424a 46 * Example:
vargham 0:19f9af07424a 47 * @code
vargham 0:19f9af07424a 48 #include "mbed.h"
vargham 0:19f9af07424a 49 #include "EERAM.h"
vargham 0:19f9af07424a 50
vargham 2:bdbf9de0e985 51 EERAM eeram(PC_9, PA_8, 2048); //SDA, SCL
vargham 0:19f9af07424a 52
vargham 0:19f9af07424a 53 int main()
vargham 0:19f9af07424a 54 {
vargham 0:19f9af07424a 55 if (!eeram.isReady(100)) //Checks device with 100 ms timeout
vargham 0:19f9af07424a 56 {
vargham 0:19f9af07424a 57 printf("Device is not present.");
vargham 0:19f9af07424a 58 while (1);
vargham 0:19f9af07424a 59 }
vargham 0:19f9af07424a 60 eeram.readStatus(); //Reads status register
vargham 0:19f9af07424a 61 eeram.setAutoStoreEnabled(true, true); //Set auto store on power down to true and stores if not stored before
vargham 0:19f9af07424a 62 while (1)
vargham 0:19f9af07424a 63 {
vargham 0:19f9af07424a 64 char dataStore[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
vargham 4:fd84e2f0d1de 65 eeram.writeBytes(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently.
vargham 0:19f9af07424a 66 wait(2.0);
vargham 0:19f9af07424a 67 char dataRead[16];
vargham 4:fd84e2f0d1de 68 eeram.readBytes(0x100, dataRead, 16);
vargham 0:19f9af07424a 69 wait(2.0);
vargham 2:bdbf9de0e985 70 float floatToWrite = -1.976f;
vargham 2:bdbf9de0e985 71 const uint16_t address = 0x200;
vargham 4:fd84e2f0d1de 72 eeram.write(address, &floatToWrite);
vargham 2:bdbf9de0e985 73 wait(2.0);
vargham 2:bdbf9de0e985 74 float floatToRead = 0;
vargham 4:fd84e2f0d1de 75 eeram.read(address, &floatToWrite);
vargham 2:bdbf9de0e985 76 wait(2.0);
vargham 0:19f9af07424a 77 }
vargham 0:19f9af07424a 78 }
vargham 0:19f9af07424a 79
vargham 0:19f9af07424a 80 * @endcode
vargham 0:19f9af07424a 81 */
vargham 0:19f9af07424a 82 class EERAM
vargham 0:19f9af07424a 83 {
vargham 0:19f9af07424a 84
vargham 0:19f9af07424a 85 public:
vargham 0:19f9af07424a 86
vargham 0:19f9af07424a 87 /** Create an I2C EERAM interface, connected to the specified pins, with specified size and with the specified address pins
vargham 0:19f9af07424a 88 * @param SDA I2C data line pin
vargham 0:19f9af07424a 89 * @param SCL I2C clock line pin
vargham 0:19f9af07424a 90 * @param memorySize Size of EERAM, 512 for 47x04 and 2048 for 47x16
vargham 0:19f9af07424a 91 * @param A1 EERAM A1 pin state (true = high, false = low)
vargham 0:19f9af07424a 92 * @param A2 EERAM A2 pin state (true = high, false = low)
vargham 0:19f9af07424a 93 */
vargham 6:b3f9b29b07ba 94 // EERAM(PinName SDA, PinName SCL, uint16_t memorySize, bool A1 = false, bool A2 = false) :
vargham 6:b3f9b29b07ba 95 // _i2c(SDA, SCL),
vargham 6:b3f9b29b07ba 96 // _memorySize(memorySize)
vargham 6:b3f9b29b07ba 97 // {
vargham 6:b3f9b29b07ba 98 // initialize(A1, A2);
vargham 6:b3f9b29b07ba 99 // };
vargham 0:19f9af07424a 100
vargham 2:bdbf9de0e985 101 /** Create an I2C EERAM interface, connected to the I2C, with specified size and with the specified address pins.
vargham 0:19f9af07424a 102 * @param 12c I2C
vargham 0:19f9af07424a 103 * @param memorySize Size of EERAM, 512 for 47x04 and 2048 for 47x16
vargham 0:19f9af07424a 104 * @param A1 EERAM A1 pin state (true = high, false = low)
vargham 0:19f9af07424a 105 * @param A2 EERAM A2 pin state (true = high, false = low)
vargham 0:19f9af07424a 106 */
vargham 7:038c52e2268b 107 EERAM(I2C &i2c, uint16_t memorySize, bool A1 = false, bool A2 = false) :
vargham 0:19f9af07424a 108 _i2c(i2c),
vargham 0:19f9af07424a 109 _memorySize(memorySize)
vargham 0:19f9af07424a 110 {
vargham 0:19f9af07424a 111 initialize(A1, A2);
vargham 0:19f9af07424a 112 };
vargham 0:19f9af07424a 113
vargham 3:a869096d7a5d 114 /** 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.
vargham 2:bdbf9de0e985 115 */
vargham 2:bdbf9de0e985 116 static void putAddressIntoBuffer(uint16_t address, char *data);
vargham 2:bdbf9de0e985 117
vargham 3:a869096d7a5d 118 /** Copies the bytes from the given memory area to the given buffer. Uses the size of T to determine write length.
vargham 2:bdbf9de0e985 119 * @param buffer Buffer to put bytes into
vargham 4:fd84e2f0d1de 120 * @param source Pointer to the memory location
vargham 2:bdbf9de0e985 121 *
vargham 2:bdbf9de0e985 122 * @return Number of written bytes.
vargham 2:bdbf9de0e985 123 */
vargham 2:bdbf9de0e985 124 template <typename T>
vargham 4:fd84e2f0d1de 125 static int putIntoBuffer(char *buffer, const int bufferSize, T *source)
vargham 2:bdbf9de0e985 126 {
vargham 2:bdbf9de0e985 127 const int length = sizeof(T);
vargham 3:a869096d7a5d 128 if (length <= bufferSize)
vargham 3:a869096d7a5d 129 {
vargham 4:fd84e2f0d1de 130 char *bytes = static_cast<char*>(static_cast<void*>(source));
vargham 3:a869096d7a5d 131 memcpy(buffer, bytes, length);
vargham 3:a869096d7a5d 132 return length;
vargham 3:a869096d7a5d 133 }
vargham 3:a869096d7a5d 134 else
vargham 3:a869096d7a5d 135 {
vargham 3:a869096d7a5d 136 return 0;
vargham 3:a869096d7a5d 137 }
vargham 2:bdbf9de0e985 138 }
vargham 2:bdbf9de0e985 139
vargham 3:a869096d7a5d 140 /** Copies the bytes from the given buffer to the given memory area. Uses the size of T to determine read length.
vargham 2:bdbf9de0e985 141 * @param buffer Buffer to get bytes from
vargham 4:fd84e2f0d1de 142 * @param destination Pointer to the memory location
vargham 2:bdbf9de0e985 143 *
vargham 2:bdbf9de0e985 144 * @return Number of read bytes.
vargham 2:bdbf9de0e985 145 */
vargham 2:bdbf9de0e985 146 template <typename T>
vargham 4:fd84e2f0d1de 147 static int getFromBuffer(char *buffer, const int bufferSize, T *destination)
vargham 2:bdbf9de0e985 148 {
vargham 2:bdbf9de0e985 149 const int length = sizeof(T);
vargham 3:a869096d7a5d 150 if (length <= bufferSize)
vargham 3:a869096d7a5d 151 {
vargham 4:fd84e2f0d1de 152 char *bytes = static_cast<char*>(static_cast<void*>(destination));
vargham 3:a869096d7a5d 153 memcpy(bytes, buffer, length);
vargham 3:a869096d7a5d 154 return length;
vargham 3:a869096d7a5d 155 }
vargham 3:a869096d7a5d 156 else
vargham 3:a869096d7a5d 157 {
vargham 3:a869096d7a5d 158 return 0;
vargham 3:a869096d7a5d 159 }
vargham 2:bdbf9de0e985 160 }
vargham 2:bdbf9de0e985 161
vargham 4:fd84e2f0d1de 162 /** 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.
vargham 4:fd84e2f0d1de 163 * @param source Pointer to memory where copy from.
vargham 4:fd84e2f0d1de 164 * @param length Length of the array. Pass 1 here if it is not an array.
vargham 2:bdbf9de0e985 165 *
vargham 2:bdbf9de0e985 166 * @return Number of written bytes.
vargham 2:bdbf9de0e985 167 */
vargham 2:bdbf9de0e985 168 template <typename T>
vargham 4:fd84e2f0d1de 169 int write(uint16_t address, T *source, const int length = 1)
vargham 2:bdbf9de0e985 170 {
vargham 4:fd84e2f0d1de 171 bool success = false;
vargham 4:fd84e2f0d1de 172 const int typeLength = sizeof(T);
vargham 4:fd84e2f0d1de 173 const int writeLength = typeLength * length;
vargham 4:fd84e2f0d1de 174 const int addressLength = sizeof(uint16_t);
vargham 4:fd84e2f0d1de 175 char *sourceAsBytes = static_cast<char*>(static_cast<void*>(source));
vargham 4:fd84e2f0d1de 176 success = checkAddressRange(address, writeLength);
vargham 4:fd84e2f0d1de 177 if (success) //Address range check
vargham 3:a869096d7a5d 178 {
vargham 4:fd84e2f0d1de 179 uint16_t addressPtr = address;
vargham 4:fd84e2f0d1de 180 int sourcePtr = 0;
vargham 4:fd84e2f0d1de 181 for (int i = 0; i < length; i++)
vargham 4:fd84e2f0d1de 182 {
vargham 4:fd84e2f0d1de 183 const int bufferSize = typeLength + addressLength;
vargham 4:fd84e2f0d1de 184 char buffer[bufferSize];
vargham 4:fd84e2f0d1de 185 putAddressIntoBuffer(addressPtr, buffer);
vargham 4:fd84e2f0d1de 186 memcpy(buffer + addressLength, sourceAsBytes + sourcePtr, typeLength);
vargham 4:fd84e2f0d1de 187 success = writeBytes(buffer, bufferSize);
vargham 4:fd84e2f0d1de 188 if (success) //Write to I2C
vargham 4:fd84e2f0d1de 189 {
vargham 4:fd84e2f0d1de 190 addressPtr += typeLength;
vargham 4:fd84e2f0d1de 191 sourcePtr += typeLength;
vargham 4:fd84e2f0d1de 192 }
vargham 4:fd84e2f0d1de 193 else
vargham 4:fd84e2f0d1de 194 {
vargham 4:fd84e2f0d1de 195 break;
vargham 4:fd84e2f0d1de 196 }
vargham 4:fd84e2f0d1de 197 }
vargham 3:a869096d7a5d 198 }
vargham 4:fd84e2f0d1de 199 return success ? writeLength : 0;
vargham 2:bdbf9de0e985 200 }
vargham 2:bdbf9de0e985 201
vargham 4:fd84e2f0d1de 202 /** 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.
vargham 4:fd84e2f0d1de 203 * @param destination Pointer to memory where copy to.
vargham 4:fd84e2f0d1de 204 * @param length Length of the array. Pass 1 here if it is not an array.
vargham 4:fd84e2f0d1de 205 * @param direct Applicable only when length > 1. If true reads all items of the array in one I2C read, otherwise read them in blocks.
vargham 2:bdbf9de0e985 206 *
vargham 2:bdbf9de0e985 207 * @return Number of read bytes.
vargham 2:bdbf9de0e985 208 */
vargham 2:bdbf9de0e985 209 template <typename T>
vargham 4:fd84e2f0d1de 210 int read(uint16_t address, T *destination, const int length = 1, bool direct = false)
vargham 2:bdbf9de0e985 211 {
vargham 2:bdbf9de0e985 212 bool success = false;
vargham 4:fd84e2f0d1de 213 const int typeLength = sizeof(T);
vargham 4:fd84e2f0d1de 214 const int readLength = typeLength * length;
vargham 4:fd84e2f0d1de 215 char *destinationAsBytes = static_cast<char*>(static_cast<void*>(destination));
vargham 4:fd84e2f0d1de 216 success = checkAddressRange(address, readLength);
vargham 4:fd84e2f0d1de 217 if (success) success = setMemoryPointer(address, true);
vargham 2:bdbf9de0e985 218 if (success)
vargham 2:bdbf9de0e985 219 {
vargham 4:fd84e2f0d1de 220 if (direct)
vargham 4:fd84e2f0d1de 221 {
vargham 7:038c52e2268b 222 success = _i2c.read(_sramAddressRead, destinationAsBytes, readLength) == 0;
vargham 4:fd84e2f0d1de 223 }
vargham 4:fd84e2f0d1de 224 else
vargham 4:fd84e2f0d1de 225 {
vargham 4:fd84e2f0d1de 226 uint16_t addressPtr = address;
vargham 4:fd84e2f0d1de 227 int destinationPtr = 0;
vargham 4:fd84e2f0d1de 228 for (int i = 0; i < length; i++)
vargham 4:fd84e2f0d1de 229 {
vargham 4:fd84e2f0d1de 230 const int bufferSize = typeLength;
vargham 4:fd84e2f0d1de 231 char buffer[bufferSize];
vargham 4:fd84e2f0d1de 232 success = readBytes(addressPtr, buffer, bufferSize);
vargham 4:fd84e2f0d1de 233 if (success)
vargham 4:fd84e2f0d1de 234 {
vargham 4:fd84e2f0d1de 235 memcpy(destinationAsBytes + destinationPtr, buffer, bufferSize);
vargham 4:fd84e2f0d1de 236 addressPtr += typeLength;
vargham 4:fd84e2f0d1de 237 destinationPtr += typeLength;
vargham 4:fd84e2f0d1de 238 }
vargham 4:fd84e2f0d1de 239 else
vargham 4:fd84e2f0d1de 240 {
vargham 4:fd84e2f0d1de 241 break;
vargham 4:fd84e2f0d1de 242 }
vargham 4:fd84e2f0d1de 243 }
vargham 4:fd84e2f0d1de 244 }
vargham 2:bdbf9de0e985 245 }
vargham 4:fd84e2f0d1de 246 return success ? readLength : 0;
vargham 3:a869096d7a5d 247 }
vargham 3:a869096d7a5d 248
vargham 4:fd84e2f0d1de 249 /** Writes bytes to the specified address.
vargham 0:19f9af07424a 250 * @param address The 16 bit destination address
vargham 0:19f9af07424a 251 * @param data Pointer to the data buffer to read from
vargham 0:19f9af07424a 252 * @param length Data length
vargham 0:19f9af07424a 253 *
vargham 0:19f9af07424a 254 * @return
vargham 0:19f9af07424a 255 * true on success
vargham 0:19f9af07424a 256 * false on fail
vargham 0:19f9af07424a 257 */
vargham 4:fd84e2f0d1de 258 bool writeBytes(uint16_t address, char *data, int length);
vargham 0:19f9af07424a 259
vargham 2:bdbf9de0e985 260 /** Writes data to the SRAM. The first two bytes shall be the address.
vargham 2:bdbf9de0e985 261 * @param data Pointer to the data buffer to read from
vargham 2:bdbf9de0e985 262 * @param length Data length
vargham 2:bdbf9de0e985 263 *
vargham 2:bdbf9de0e985 264 * @return
vargham 2:bdbf9de0e985 265 * true on success
vargham 2:bdbf9de0e985 266 * false on fail
vargham 2:bdbf9de0e985 267 */
vargham 4:fd84e2f0d1de 268 bool writeBytes(char *data, int length);
vargham 2:bdbf9de0e985 269
vargham 2:bdbf9de0e985 270 /** Reads data from the specified address.
vargham 0:19f9af07424a 271 * @param address The 16 bit source address
vargham 0:19f9af07424a 272 * @param data Pointer to the data buffer to read to
vargham 0:19f9af07424a 273 * @param length Data length
vargham 0:19f9af07424a 274 *
vargham 0:19f9af07424a 275 * @return
vargham 0:19f9af07424a 276 * true on success
vargham 0:19f9af07424a 277 * false on fail
vargham 0:19f9af07424a 278 */
vargham 4:fd84e2f0d1de 279 bool readBytes(uint16_t address, char *data, int length);
vargham 0:19f9af07424a 280
vargham 2:bdbf9de0e985 281 /** Reads data to the specified buffer. The first two bytes shall be the address. These bytes will be unchanged.
vargham 2:bdbf9de0e985 282 * @param data Pointer to the data buffer to read to
vargham 2:bdbf9de0e985 283 * @param length Data length
vargham 2:bdbf9de0e985 284 *
vargham 2:bdbf9de0e985 285 * @return
vargham 2:bdbf9de0e985 286 * true on success
vargham 2:bdbf9de0e985 287 * false on fail
vargham 2:bdbf9de0e985 288 */
vargham 4:fd84e2f0d1de 289 bool readBytes(char *data, int length);
vargham 2:bdbf9de0e985 290
vargham 0:19f9af07424a 291 /** Fills memory with the specified byte from the specified address
vargham 0:19f9af07424a 292 * @param address The 16 bit destination address
vargham 0:19f9af07424a 293 * @param data The byte to write
vargham 0:19f9af07424a 294 * @param length Memory are to fill with the data byte
vargham 0:19f9af07424a 295 *
vargham 0:19f9af07424a 296 * @return
vargham 0:19f9af07424a 297 * true on success
vargham 0:19f9af07424a 298 * false on fail
vargham 0:19f9af07424a 299 */
vargham 0:19f9af07424a 300 bool fillMemory(uint16_t address, char data, int length);
vargham 0:19f9af07424a 301
vargham 0:19f9af07424a 302 /** Fills the whole memory with the specified byte
vargham 0:19f9af07424a 303 * @param data The byte to write
vargham 0:19f9af07424a 304 *
vargham 0:19f9af07424a 305 * @return
vargham 0:19f9af07424a 306 * true on success
vargham 0:19f9af07424a 307 * false on fail
vargham 0:19f9af07424a 308 */
vargham 0:19f9af07424a 309 bool fillMemory(char data);
vargham 0:19f9af07424a 310
vargham 0:19f9af07424a 311 /** Continuously checks I2C EERAM device till ready or reaches timeout
vargham 0:19f9af07424a 312 * @param timeout_ms Timeout for busy-wait I2C device polling
vargham 0:19f9af07424a 313 *
vargham 0:19f9af07424a 314 * @return
vargham 0:19f9af07424a 315 * true on ready
vargham 0:19f9af07424a 316 * false on timeout
vargham 0:19f9af07424a 317 */
vargham 0:19f9af07424a 318 bool isReady(int timeout_ms);
vargham 0:19f9af07424a 319
vargham 0:19f9af07424a 320 /** Checks I2C EERAM device one time
vargham 0:19f9af07424a 321 *
vargham 0:19f9af07424a 322 * @return
vargham 0:19f9af07424a 323 * true on ready
vargham 0:19f9af07424a 324 * false on not ready
vargham 0:19f9af07424a 325 */
vargham 0:19f9af07424a 326 bool isReady();
vargham 0:19f9af07424a 327
vargham 0:19f9af07424a 328 /** Store SRAM in EEPROM
vargham 0:19f9af07424a 329 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 330 *
vargham 0:19f9af07424a 331 * @return
vargham 0:19f9af07424a 332 * true on success
vargham 0:19f9af07424a 333 * false on fail
vargham 0:19f9af07424a 334 */
vargham 0:19f9af07424a 335 bool store(bool block);
vargham 0:19f9af07424a 336
vargham 0:19f9af07424a 337 /** Recall SRAM from EEPROM
vargham 0:19f9af07424a 338 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 339 *
vargham 0:19f9af07424a 340 * @return
vargham 0:19f9af07424a 341 * true on success
vargham 0:19f9af07424a 342 * false on fail
vargham 0:19f9af07424a 343 */
vargham 0:19f9af07424a 344 bool recall(bool block);
vargham 0:19f9af07424a 345
vargham 0:19f9af07424a 346 /** Reads status register
vargham 0:19f9af07424a 347 *
vargham 0:19f9af07424a 348 * @return
vargham 0:19f9af07424a 349 * true on success
vargham 0:19f9af07424a 350 * false on fail
vargham 0:19f9af07424a 351 */
vargham 0:19f9af07424a 352 bool readStatus();
vargham 0:19f9af07424a 353
vargham 0:19f9af07424a 354 /** Gets status register
vargham 0:19f9af07424a 355 *
vargham 0:19f9af07424a 356 * @return The content of the 8 bit status register
vargham 0:19f9af07424a 357 */
vargham 0:19f9af07424a 358 inline uint8_t getStatus() const
vargham 0:19f9af07424a 359 {
vargham 0:19f9af07424a 360 return _status;
vargham 0:19f9af07424a 361 }
vargham 0:19f9af07424a 362
vargham 0:19f9af07424a 363 /** Writes the 8 bit status register to the I2C device
vargham 0:19f9af07424a 364 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 365 *
vargham 0:19f9af07424a 366 * @return
vargham 0:19f9af07424a 367 * true on success
vargham 0:19f9af07424a 368 * false on fail
vargham 0:19f9af07424a 369 */
vargham 0:19f9af07424a 370 bool writeStatus(bool block);
vargham 0:19f9af07424a 371
vargham 0:19f9af07424a 372 /** Writes the 8 bit status register to the I2C device if changed
vargham 0:19f9af07424a 373 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 374 */
vargham 0:19f9af07424a 375 void writeStatusIfChanged(bool block);
vargham 0:19f9af07424a 376
vargham 0:19f9af07424a 377 /** Sets protected memory area. The selected area will be write protected.
vargham 0:19f9af07424a 378 * @param protectedMemoryArea The enum represents the area from NONE through upper 64, 32, 16, 8, 4, 2 to ALL
vargham 0:19f9af07424a 379 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 380 */
vargham 0:19f9af07424a 381 void setProtectedMemoryArea(ProtectedMemoryArea protectedMemoryArea, bool store = false);
vargham 0:19f9af07424a 382
vargham 0:19f9af07424a 383 /** Gets protected memory area
vargham 0:19f9af07424a 384 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 385 */
vargham 0:19f9af07424a 386 ProtectedMemoryArea getProtectedMemoryArea();
vargham 0:19f9af07424a 387
vargham 0:19f9af07424a 388 /** Gets Memory Modified
vargham 0:19f9af07424a 389 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 390 * @return
vargham 0:19f9af07424a 391 * true The SRAM memory have been modified since the last store or recall operation
vargham 0:19f9af07424a 392 * false The SRAM memory have not been modified since the last store or recall operation
vargham 0:19f9af07424a 393 */
vargham 0:19f9af07424a 394 bool isMemoryModified();
vargham 0:19f9af07424a 395
vargham 0:19f9af07424a 396 /** Sets Auto Store Enabled
vargham 0:19f9af07424a 397 * @param enabled Auto store SRAM to EEPROM on power down enabled
vargham 0:19f9af07424a 398 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 399 */
vargham 0:19f9af07424a 400 void setAutoStoreEnabled(bool enabled, bool store = false);
vargham 0:19f9af07424a 401
vargham 0:19f9af07424a 402 /** Gets Auto Store Enabled
vargham 0:19f9af07424a 403 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 404 * @return
vargham 0:19f9af07424a 405 * true Auto Store is Enabled
vargham 0:19f9af07424a 406 * false Auto Store is not Enabled
vargham 0:19f9af07424a 407 */
vargham 0:19f9af07424a 408 bool isAutoStoreEnabled();
vargham 0:19f9af07424a 409
vargham 0:19f9af07424a 410 /** Sets event detected
vargham 0:19f9af07424a 411 * @param detected The value of the detected bit
vargham 0:19f9af07424a 412 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 413 */
vargham 0:19f9af07424a 414 void setEventDetected(bool detected, bool store);
vargham 0:19f9af07424a 415
vargham 0:19f9af07424a 416 /** Gets event detected
vargham 0:19f9af07424a 417 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 418 * @return
vargham 0:19f9af07424a 419 * true External store event detected (The HS pin pulled up)
vargham 0:19f9af07424a 420 * false External event not detected
vargham 0:19f9af07424a 421 */
vargham 0:19f9af07424a 422 bool isEventDetected();
vargham 0:19f9af07424a 423
vargham 0:19f9af07424a 424 /** Prints the SRAM content from the given address to the given serial port in human readable format in 1-32 byte long lines
vargham 0:19f9af07424a 425 * @param serial The port to print to
vargham 0:19f9af07424a 426 */
vargham 0:19f9af07424a 427 void dump(Serial &serial, uint16_t start, uint16_t length, int lineSize = 16);
vargham 0:19f9af07424a 428
vargham 0:19f9af07424a 429 /** Prints the whole SRAM content to the given serial port in human readable format in 16 byte long lines
vargham 0:19f9af07424a 430 * @param serial The port to print to
vargham 0:19f9af07424a 431 */
vargham 0:19f9af07424a 432 void dump(Serial &serial);
vargham 0:19f9af07424a 433
vargham 0:19f9af07424a 434 /** Prints the 8 bit status register's contents to the given serial port in human readable format
vargham 0:19f9af07424a 435 * @param serial The port to print to
vargham 0:19f9af07424a 436 */
vargham 0:19f9af07424a 437 void dumpRegisters(Serial &serial);
vargham 0:19f9af07424a 438
vargham 0:19f9af07424a 439 private:
vargham 0:19f9af07424a 440 static const uint8_t RW_BIT = 0;
vargham 0:19f9af07424a 441 static const uint8_t A1_BIT = 2;
vargham 0:19f9af07424a 442 static const uint8_t A2_BIT = 3;
vargham 0:19f9af07424a 443 static const uint8_t STATUS_AM_BIT = 7;
vargham 0:19f9af07424a 444 static const uint8_t STATUS_ASE_BIT = 1;
vargham 0:19f9af07424a 445 static const uint8_t STATUS_EVENT_BIT = 0;
vargham 0:19f9af07424a 446 static const uint8_t OPCODE_SRAM = 0b10100000;
vargham 0:19f9af07424a 447 static const uint8_t OPCODE_CONTROL = 0b00110000;
vargham 0:19f9af07424a 448 static const uint8_t REGISTER_STATUS = 0x0;
vargham 0:19f9af07424a 449 static const uint8_t REGISTER_COMMAND = 0x55;
vargham 0:19f9af07424a 450 static const uint8_t COMMAND_STORE = 0b00110011;
vargham 0:19f9af07424a 451 static const uint8_t COMMAND_RECALL = 0b11011101;
vargham 0:19f9af07424a 452 static const int TIME_RECALL_16_MS = 5;
vargham 0:19f9af07424a 453 static const int TIME_RECALL_04_MS = 2;
vargham 0:19f9af07424a 454 static const int TIME_STORE_16_MS = 25;
vargham 0:19f9af07424a 455 static const int TIME_STORE_04_MS = 8;
vargham 0:19f9af07424a 456 static const int TIME_STORE_STATUS_MS = 1;
vargham 7:038c52e2268b 457 I2C &_i2c;
vargham 0:19f9af07424a 458 uint16_t _memorySize;
vargham 0:19f9af07424a 459 uint8_t _sramAddressWrite;
vargham 0:19f9af07424a 460 uint8_t _sramAddressRead;
vargham 0:19f9af07424a 461 uint8_t _controlAddressWrite;
vargham 0:19f9af07424a 462 uint8_t _controlAddressRead;
vargham 0:19f9af07424a 463 uint8_t _status;
vargham 0:19f9af07424a 464 uint8_t _statusToWrite;
vargham 0:19f9af07424a 465 void initialize(bool A1 = false, bool A2 = false);
vargham 0:19f9af07424a 466 bool checkAddressRange(uint16_t start, uint16_t length);
vargham 0:19f9af07424a 467 bool writeRegister(uint8_t registerAddress, uint8_t data);
vargham 0:19f9af07424a 468 bool setMemoryPointer(uint16_t address, bool stop = true);
vargham 2:bdbf9de0e985 469 bool setMemoryPointer(uint8_t address_0, uint8_t address_1, bool stop = true);
vargham 0:19f9af07424a 470 };
vargham 0:19f9af07424a 471
vargham 0:19f9af07424a 472 #endif //EERAM_h