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

Dependents:   EERAM_example

Committer:
vargham
Date:
Thu Apr 27 13:25:23 2017 +0000
Revision:
2:bdbf9de0e985
Parent:
0:19f9af07424a
Child:
3:a869096d7a5d
Added read and write with type template.

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 2:bdbf9de0e985 5 * @version 1.1.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 0:19f9af07424a 71 eeram.write(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 0:19f9af07424a 74 eeram.read(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 2:bdbf9de0e985 78 eeram.writeValue(address, &floatToWrite);
vargham 2:bdbf9de0e985 79 wait(2.0);
vargham 2:bdbf9de0e985 80 float floatToRead = 0;
vargham 2:bdbf9de0e985 81 eeram.readValue(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 2:bdbf9de0e985 120 /** Puts the given 16 bit SRAM address into the first two bytes of the given buffer.
vargham 2:bdbf9de0e985 121 */
vargham 2:bdbf9de0e985 122 static void putAddressIntoBuffer(uint16_t address, char *data);
vargham 2:bdbf9de0e985 123
vargham 2:bdbf9de0e985 124 /** 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.
vargham 2:bdbf9de0e985 125 * @param buffer Buffer to put bytes into
vargham 2:bdbf9de0e985 126 * @param param 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 2:bdbf9de0e985 131 static int putValueIntoBuffer(char *buffer, T *param)
vargham 2:bdbf9de0e985 132 {
vargham 2:bdbf9de0e985 133 const int length = sizeof(T);
vargham 2:bdbf9de0e985 134 char *bytes = static_cast<char*>(static_cast<void*>(param));
vargham 2:bdbf9de0e985 135 memcpy(buffer, bytes, length);
vargham 2:bdbf9de0e985 136 return length;
vargham 2:bdbf9de0e985 137 }
vargham 2:bdbf9de0e985 138
vargham 2:bdbf9de0e985 139 /** 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.
vargham 2:bdbf9de0e985 140 * @param buffer Buffer to get bytes from
vargham 2:bdbf9de0e985 141 * @param param Pointer to the memory location
vargham 2:bdbf9de0e985 142 *
vargham 2:bdbf9de0e985 143 * @return Number of read bytes.
vargham 2:bdbf9de0e985 144 */
vargham 2:bdbf9de0e985 145 template <typename T>
vargham 2:bdbf9de0e985 146 static int getValueFromBuffer(char *buffer, T *param)
vargham 2:bdbf9de0e985 147 {
vargham 2:bdbf9de0e985 148 const int length = sizeof(T);
vargham 2:bdbf9de0e985 149 char *bytes = static_cast<char*>(static_cast<void*>(param));
vargham 2:bdbf9de0e985 150 memcpy(bytes, buffer, length);
vargham 2:bdbf9de0e985 151 return length;
vargham 2:bdbf9de0e985 152 }
vargham 2:bdbf9de0e985 153
vargham 2:bdbf9de0e985 154 /** Copies the bytes from the given memory area to the given address of the EERAM. Uses the size of T to determine write length.
vargham 2:bdbf9de0e985 155 * @param param Pointer to the memory location where copy from
vargham 2:bdbf9de0e985 156 *
vargham 2:bdbf9de0e985 157 * @return Number of written bytes.
vargham 2:bdbf9de0e985 158 */
vargham 2:bdbf9de0e985 159 template <typename T>
vargham 2:bdbf9de0e985 160 int writeValue(uint16_t address, T *param)
vargham 2:bdbf9de0e985 161 {
vargham 2:bdbf9de0e985 162 bool success = false;
vargham 2:bdbf9de0e985 163 const int length = sizeof(T) + 2;
vargham 2:bdbf9de0e985 164 char data[length];
vargham 2:bdbf9de0e985 165 putAddressIntoBuffer(address, data);
vargham 2:bdbf9de0e985 166 putValueIntoBuffer(data + 2, param);
vargham 2:bdbf9de0e985 167 success = write(data, length);
vargham 2:bdbf9de0e985 168 return success ? length - 2 : 0;
vargham 2:bdbf9de0e985 169 }
vargham 2:bdbf9de0e985 170
vargham 2:bdbf9de0e985 171 /** Copies the bytes from the given EERAM address to the given memory area. Uses the size of T to determine read length.
vargham 2:bdbf9de0e985 172 * @param param Pointer to the memory location where copy to
vargham 2:bdbf9de0e985 173 *
vargham 2:bdbf9de0e985 174 * @return Number of read bytes.
vargham 2:bdbf9de0e985 175 */
vargham 2:bdbf9de0e985 176 template <typename T>
vargham 2:bdbf9de0e985 177 int readValue(uint16_t address, T *param)
vargham 2:bdbf9de0e985 178 {
vargham 2:bdbf9de0e985 179 bool success = false;
vargham 2:bdbf9de0e985 180 const int length = sizeof(T);
vargham 2:bdbf9de0e985 181 char data[length];
vargham 2:bdbf9de0e985 182 success = read(address, data, length);
vargham 2:bdbf9de0e985 183 if (success)
vargham 2:bdbf9de0e985 184 {
vargham 2:bdbf9de0e985 185 getValueFromBuffer(data, param);
vargham 2:bdbf9de0e985 186 }
vargham 2:bdbf9de0e985 187 return success ? length : 0;
vargham 2:bdbf9de0e985 188 }
vargham 2:bdbf9de0e985 189
vargham 2:bdbf9de0e985 190 /** Writes data to the specified address.
vargham 0:19f9af07424a 191 * @param address The 16 bit destination address
vargham 0:19f9af07424a 192 * @param data Pointer to the data buffer to read from
vargham 0:19f9af07424a 193 * @param length Data length
vargham 0:19f9af07424a 194 *
vargham 0:19f9af07424a 195 * @return
vargham 0:19f9af07424a 196 * true on success
vargham 0:19f9af07424a 197 * false on fail
vargham 0:19f9af07424a 198 */
vargham 0:19f9af07424a 199 bool write(uint16_t address, char *data, int length);
vargham 0:19f9af07424a 200
vargham 2:bdbf9de0e985 201 /** Writes data to the SRAM. The first two bytes shall be the address.
vargham 2:bdbf9de0e985 202 * @param data Pointer to the data buffer to read from
vargham 2:bdbf9de0e985 203 * @param length Data length
vargham 2:bdbf9de0e985 204 *
vargham 2:bdbf9de0e985 205 * @return
vargham 2:bdbf9de0e985 206 * true on success
vargham 2:bdbf9de0e985 207 * false on fail
vargham 2:bdbf9de0e985 208 */
vargham 2:bdbf9de0e985 209 bool write(char *data, int length);
vargham 2:bdbf9de0e985 210
vargham 2:bdbf9de0e985 211 /** Reads data from the specified address.
vargham 0:19f9af07424a 212 * @param address The 16 bit source address
vargham 0:19f9af07424a 213 * @param data Pointer to the data buffer to read to
vargham 0:19f9af07424a 214 * @param length Data length
vargham 0:19f9af07424a 215 *
vargham 0:19f9af07424a 216 * @return
vargham 0:19f9af07424a 217 * true on success
vargham 0:19f9af07424a 218 * false on fail
vargham 0:19f9af07424a 219 */
vargham 0:19f9af07424a 220 bool read(uint16_t address, char *data, int length);
vargham 0:19f9af07424a 221
vargham 2:bdbf9de0e985 222 /** Reads data to the specified buffer. The first two bytes shall be the address. These bytes will be unchanged.
vargham 2:bdbf9de0e985 223 * @param data Pointer to the data buffer to read to
vargham 2:bdbf9de0e985 224 * @param length Data length
vargham 2:bdbf9de0e985 225 *
vargham 2:bdbf9de0e985 226 * @return
vargham 2:bdbf9de0e985 227 * true on success
vargham 2:bdbf9de0e985 228 * false on fail
vargham 2:bdbf9de0e985 229 */
vargham 2:bdbf9de0e985 230 bool read(char *data, int length);
vargham 2:bdbf9de0e985 231
vargham 0:19f9af07424a 232 /** Fills memory with the specified byte from the specified address
vargham 0:19f9af07424a 233 * @param address The 16 bit destination address
vargham 0:19f9af07424a 234 * @param data The byte to write
vargham 0:19f9af07424a 235 * @param length Memory are to fill with the data byte
vargham 0:19f9af07424a 236 *
vargham 0:19f9af07424a 237 * @return
vargham 0:19f9af07424a 238 * true on success
vargham 0:19f9af07424a 239 * false on fail
vargham 0:19f9af07424a 240 */
vargham 0:19f9af07424a 241 bool fillMemory(uint16_t address, char data, int length);
vargham 0:19f9af07424a 242
vargham 0:19f9af07424a 243 /** Fills the whole memory with the specified byte
vargham 0:19f9af07424a 244 * @param data The byte to write
vargham 0:19f9af07424a 245 *
vargham 0:19f9af07424a 246 * @return
vargham 0:19f9af07424a 247 * true on success
vargham 0:19f9af07424a 248 * false on fail
vargham 0:19f9af07424a 249 */
vargham 0:19f9af07424a 250 bool fillMemory(char data);
vargham 0:19f9af07424a 251
vargham 0:19f9af07424a 252 /** Continuously checks I2C EERAM device till ready or reaches timeout
vargham 0:19f9af07424a 253 * @param timeout_ms Timeout for busy-wait I2C device polling
vargham 0:19f9af07424a 254 *
vargham 0:19f9af07424a 255 * @return
vargham 0:19f9af07424a 256 * true on ready
vargham 0:19f9af07424a 257 * false on timeout
vargham 0:19f9af07424a 258 */
vargham 0:19f9af07424a 259 bool isReady(int timeout_ms);
vargham 0:19f9af07424a 260
vargham 0:19f9af07424a 261 /** Checks I2C EERAM device one time
vargham 0:19f9af07424a 262 *
vargham 0:19f9af07424a 263 * @return
vargham 0:19f9af07424a 264 * true on ready
vargham 0:19f9af07424a 265 * false on not ready
vargham 0:19f9af07424a 266 */
vargham 0:19f9af07424a 267 bool isReady();
vargham 0:19f9af07424a 268
vargham 0:19f9af07424a 269 /** Store SRAM in EEPROM
vargham 0:19f9af07424a 270 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 271 *
vargham 0:19f9af07424a 272 * @return
vargham 0:19f9af07424a 273 * true on success
vargham 0:19f9af07424a 274 * false on fail
vargham 0:19f9af07424a 275 */
vargham 0:19f9af07424a 276 bool store(bool block);
vargham 0:19f9af07424a 277
vargham 0:19f9af07424a 278 /** Recall SRAM from EEPROM
vargham 0:19f9af07424a 279 * @param block If true, busy waits for operation end.
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 0:19f9af07424a 285 bool recall(bool block);
vargham 0:19f9af07424a 286
vargham 0:19f9af07424a 287 /** Reads status register
vargham 0:19f9af07424a 288 *
vargham 0:19f9af07424a 289 * @return
vargham 0:19f9af07424a 290 * true on success
vargham 0:19f9af07424a 291 * false on fail
vargham 0:19f9af07424a 292 */
vargham 0:19f9af07424a 293 bool readStatus();
vargham 0:19f9af07424a 294
vargham 0:19f9af07424a 295 /** Gets status register
vargham 0:19f9af07424a 296 *
vargham 0:19f9af07424a 297 * @return The content of the 8 bit status register
vargham 0:19f9af07424a 298 */
vargham 0:19f9af07424a 299 inline uint8_t getStatus() const
vargham 0:19f9af07424a 300 {
vargham 0:19f9af07424a 301 return _status;
vargham 0:19f9af07424a 302 }
vargham 0:19f9af07424a 303
vargham 0:19f9af07424a 304 /** Writes the 8 bit status register to the I2C device
vargham 0:19f9af07424a 305 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 306 *
vargham 0:19f9af07424a 307 * @return
vargham 0:19f9af07424a 308 * true on success
vargham 0:19f9af07424a 309 * false on fail
vargham 0:19f9af07424a 310 */
vargham 0:19f9af07424a 311 bool writeStatus(bool block);
vargham 0:19f9af07424a 312
vargham 0:19f9af07424a 313 /** Writes the 8 bit status register to the I2C device if changed
vargham 0:19f9af07424a 314 * @param block If true, busy waits for operation end.
vargham 0:19f9af07424a 315 */
vargham 0:19f9af07424a 316 void writeStatusIfChanged(bool block);
vargham 0:19f9af07424a 317
vargham 0:19f9af07424a 318 /** Sets protected memory area. The selected area will be write protected.
vargham 0:19f9af07424a 319 * @param protectedMemoryArea The enum represents the area from NONE through upper 64, 32, 16, 8, 4, 2 to ALL
vargham 0:19f9af07424a 320 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 321 */
vargham 0:19f9af07424a 322 void setProtectedMemoryArea(ProtectedMemoryArea protectedMemoryArea, bool store = false);
vargham 0:19f9af07424a 323
vargham 0:19f9af07424a 324 /** Gets protected memory area
vargham 0:19f9af07424a 325 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 326 */
vargham 0:19f9af07424a 327 ProtectedMemoryArea getProtectedMemoryArea();
vargham 0:19f9af07424a 328
vargham 0:19f9af07424a 329 /** Gets Memory Modified
vargham 0:19f9af07424a 330 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 331 * @return
vargham 0:19f9af07424a 332 * true The SRAM memory have been modified since the last store or recall operation
vargham 0:19f9af07424a 333 * false The SRAM memory have not been modified since the last store or recall operation
vargham 0:19f9af07424a 334 */
vargham 0:19f9af07424a 335 bool isMemoryModified();
vargham 0:19f9af07424a 336
vargham 0:19f9af07424a 337 /** Sets Auto Store Enabled
vargham 0:19f9af07424a 338 * @param enabled Auto store SRAM to EEPROM on power down enabled
vargham 0:19f9af07424a 339 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 340 */
vargham 0:19f9af07424a 341 void setAutoStoreEnabled(bool enabled, bool store = false);
vargham 0:19f9af07424a 342
vargham 0:19f9af07424a 343 /** Gets Auto Store Enabled
vargham 0:19f9af07424a 344 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 345 * @return
vargham 0:19f9af07424a 346 * true Auto Store is Enabled
vargham 0:19f9af07424a 347 * false Auto Store is not Enabled
vargham 0:19f9af07424a 348 */
vargham 0:19f9af07424a 349 bool isAutoStoreEnabled();
vargham 0:19f9af07424a 350
vargham 0:19f9af07424a 351 /** Sets event detected
vargham 0:19f9af07424a 352 * @param detected The value of the detected bit
vargham 0:19f9af07424a 353 * @param store If true, calls writeStatusIfChanged()
vargham 0:19f9af07424a 354 */
vargham 0:19f9af07424a 355 void setEventDetected(bool detected, bool store);
vargham 0:19f9af07424a 356
vargham 0:19f9af07424a 357 /** Gets event detected
vargham 0:19f9af07424a 358 * Have to call readStatus() to read register's fresh value.
vargham 0:19f9af07424a 359 * @return
vargham 0:19f9af07424a 360 * true External store event detected (The HS pin pulled up)
vargham 0:19f9af07424a 361 * false External event not detected
vargham 0:19f9af07424a 362 */
vargham 0:19f9af07424a 363 bool isEventDetected();
vargham 0:19f9af07424a 364
vargham 0:19f9af07424a 365 /** 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 366 * @param serial The port to print to
vargham 0:19f9af07424a 367 */
vargham 0:19f9af07424a 368 void dump(Serial &serial, uint16_t start, uint16_t length, int lineSize = 16);
vargham 0:19f9af07424a 369
vargham 0:19f9af07424a 370 /** Prints the whole SRAM content to the given serial port in human readable format in 16 byte long lines
vargham 0:19f9af07424a 371 * @param serial The port to print to
vargham 0:19f9af07424a 372 */
vargham 0:19f9af07424a 373 void dump(Serial &serial);
vargham 0:19f9af07424a 374
vargham 0:19f9af07424a 375 /** Prints the 8 bit status register's contents to the given serial port in human readable format
vargham 0:19f9af07424a 376 * @param serial The port to print to
vargham 0:19f9af07424a 377 */
vargham 0:19f9af07424a 378 void dumpRegisters(Serial &serial);
vargham 0:19f9af07424a 379
vargham 0:19f9af07424a 380 private:
vargham 0:19f9af07424a 381 static const uint8_t RW_BIT = 0;
vargham 0:19f9af07424a 382 static const uint8_t A1_BIT = 2;
vargham 0:19f9af07424a 383 static const uint8_t A2_BIT = 3;
vargham 0:19f9af07424a 384 static const uint8_t STATUS_AM_BIT = 7;
vargham 0:19f9af07424a 385 static const uint8_t STATUS_ASE_BIT = 1;
vargham 0:19f9af07424a 386 static const uint8_t STATUS_EVENT_BIT = 0;
vargham 0:19f9af07424a 387 static const uint8_t OPCODE_SRAM = 0b10100000;
vargham 0:19f9af07424a 388 static const uint8_t OPCODE_CONTROL = 0b00110000;
vargham 0:19f9af07424a 389 static const uint8_t REGISTER_STATUS = 0x0;
vargham 0:19f9af07424a 390 static const uint8_t REGISTER_COMMAND = 0x55;
vargham 0:19f9af07424a 391 static const uint8_t COMMAND_STORE = 0b00110011;
vargham 0:19f9af07424a 392 static const uint8_t COMMAND_RECALL = 0b11011101;
vargham 0:19f9af07424a 393 static const int TIME_RECALL_16_MS = 5;
vargham 0:19f9af07424a 394 static const int TIME_RECALL_04_MS = 2;
vargham 0:19f9af07424a 395 static const int TIME_STORE_16_MS = 25;
vargham 0:19f9af07424a 396 static const int TIME_STORE_04_MS = 8;
vargham 0:19f9af07424a 397 static const int TIME_STORE_STATUS_MS = 1;
vargham 0:19f9af07424a 398 I2C _i2c;
vargham 0:19f9af07424a 399 uint16_t _memorySize;
vargham 0:19f9af07424a 400 uint8_t _sramAddressWrite;
vargham 0:19f9af07424a 401 uint8_t _sramAddressRead;
vargham 0:19f9af07424a 402 uint8_t _controlAddressWrite;
vargham 0:19f9af07424a 403 uint8_t _controlAddressRead;
vargham 0:19f9af07424a 404 uint8_t _status;
vargham 0:19f9af07424a 405 uint8_t _statusToWrite;
vargham 0:19f9af07424a 406 void initialize(bool A1 = false, bool A2 = false);
vargham 0:19f9af07424a 407 bool checkAddressRange(uint16_t start, uint16_t length);
vargham 0:19f9af07424a 408 bool writeRegister(uint8_t registerAddress, uint8_t data);
vargham 0:19f9af07424a 409 bool setMemoryPointer(uint16_t address, bool stop = true);
vargham 2:bdbf9de0e985 410 bool setMemoryPointer(uint8_t address_0, uint8_t address_1, bool stop = true);
vargham 0:19f9af07424a 411 };
vargham 0:19f9af07424a 412
vargham 0:19f9af07424a 413 #endif //EERAM_h