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

Dependents:   EERAM_example

Committer:
vargham
Date:
Fri Apr 28 13:38:52 2017 +0000
Revision:
4:fd84e2f0d1de
Parent:
3:a869096d7a5d
Child:
5:9444f29f3429
API cleanup.

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