Example program for Microchip 47x04 and 47x16 EERAM devices.
eeram_main.cpp@4:8cc86b207f25, 2017-04-28 (annotated)
- Committer:
- vargham
- Date:
- Fri Apr 28 13:39:54 2017 +0000
- Revision:
- 4:8cc86b207f25
- Parent:
- 3:9b5473254d82
Follow lib's API changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vargham | 0:f7121193ff7e | 1 | /** |
vargham | 0:f7121193ff7e | 2 | * @file eeram_main.cpp |
vargham | 0:f7121193ff7e | 3 | * @brief Example program for Microchip I2C EERAM devices (47x04 and 47x16) |
vargham | 0:f7121193ff7e | 4 | * @author Mark Peter Vargha, vmp@varghamarkpeter.hu |
vargham | 0:f7121193ff7e | 5 | * @version 1.0.0 |
vargham | 0:f7121193ff7e | 6 | * |
vargham | 0:f7121193ff7e | 7 | * Copyright (c) 2017 |
vargham | 0:f7121193ff7e | 8 | * |
vargham | 0:f7121193ff7e | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vargham | 0:f7121193ff7e | 10 | * you may not use this file except in compliance with the License. |
vargham | 0:f7121193ff7e | 11 | * You may obtain a copy of the License at |
vargham | 0:f7121193ff7e | 12 | * |
vargham | 0:f7121193ff7e | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
vargham | 0:f7121193ff7e | 14 | * |
vargham | 0:f7121193ff7e | 15 | * Unless required by applicable law or agreed to in writing, software |
vargham | 0:f7121193ff7e | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
vargham | 0:f7121193ff7e | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vargham | 0:f7121193ff7e | 18 | * See the License for the specific language governing permissions and |
vargham | 0:f7121193ff7e | 19 | * limitations under the License. |
vargham | 0:f7121193ff7e | 20 | */ |
vargham | 0:f7121193ff7e | 21 | |
vargham | 0:f7121193ff7e | 22 | #include "mbed.h" |
vargham | 0:f7121193ff7e | 23 | #include "EERAM.h" |
vargham | 0:f7121193ff7e | 24 | |
vargham | 0:f7121193ff7e | 25 | #define PIN_I2C_SDA PC_9 |
vargham | 0:f7121193ff7e | 26 | #define PIN_I2C_SCL PA_8 |
vargham | 0:f7121193ff7e | 27 | |
vargham | 4:8cc86b207f25 | 28 | //#define I2C_FREQUENCY 1000000 |
vargham | 4:8cc86b207f25 | 29 | #define I2C_FREQUENCY 400000 |
vargham | 4:8cc86b207f25 | 30 | |
vargham | 4:8cc86b207f25 | 31 | struct StorageContainer |
vargham | 4:8cc86b207f25 | 32 | { |
vargham | 4:8cc86b207f25 | 33 | float f; |
vargham | 4:8cc86b207f25 | 34 | int i; |
vargham | 4:8cc86b207f25 | 35 | short s[4]; |
vargham | 4:8cc86b207f25 | 36 | }; |
vargham | 0:f7121193ff7e | 37 | |
vargham | 0:f7121193ff7e | 38 | Serial serial(PA_9, PA_10); //Tx, Rx |
vargham | 0:f7121193ff7e | 39 | I2C i2c(PIN_I2C_SDA, PIN_I2C_SCL); //SDA, SCL |
vargham | 0:f7121193ff7e | 40 | EERAM eeram(i2c, 2048); |
vargham | 0:f7121193ff7e | 41 | |
vargham | 0:f7121193ff7e | 42 | void printI2C() |
vargham | 0:f7121193ff7e | 43 | { |
vargham | 0:f7121193ff7e | 44 | //0x41 Discovery Touch |
vargham | 0:f7121193ff7e | 45 | //0x18 EERAM control |
vargham | 0:f7121193ff7e | 46 | //0x50 EERAM memory |
vargham | 0:f7121193ff7e | 47 | |
vargham | 0:f7121193ff7e | 48 | int error; |
vargham | 0:f7121193ff7e | 49 | int address; |
vargham | 0:f7121193ff7e | 50 | int nDevices = 0; |
vargham | 0:f7121193ff7e | 51 | |
vargham | 0:f7121193ff7e | 52 | serial.printf("Scanning I2C devices...\r\n"); |
vargham | 0:f7121193ff7e | 53 | |
vargham | 0:f7121193ff7e | 54 | for(address = 1; address < 127; address++ ) |
vargham | 0:f7121193ff7e | 55 | { |
vargham | 0:f7121193ff7e | 56 | i2c.start(); |
vargham | 0:f7121193ff7e | 57 | error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses |
vargham | 0:f7121193ff7e | 58 | i2c.stop(); |
vargham | 0:f7121193ff7e | 59 | if (error == 1) |
vargham | 0:f7121193ff7e | 60 | { |
vargham | 0:f7121193ff7e | 61 | serial.printf("I2C device found at address 0x%X\r\n", address); //Returns 7-bit addres |
vargham | 0:f7121193ff7e | 62 | nDevices++; |
vargham | 0:f7121193ff7e | 63 | } |
vargham | 0:f7121193ff7e | 64 | |
vargham | 0:f7121193ff7e | 65 | } |
vargham | 0:f7121193ff7e | 66 | serial.printf("I2C scan finished.\r\n"); |
vargham | 0:f7121193ff7e | 67 | if (nDevices == 0) |
vargham | 0:f7121193ff7e | 68 | { |
vargham | 0:f7121193ff7e | 69 | serial.printf("No I2C devices found.\r\n"); |
vargham | 0:f7121193ff7e | 70 | } |
vargham | 0:f7121193ff7e | 71 | |
vargham | 0:f7121193ff7e | 72 | } |
vargham | 0:f7121193ff7e | 73 | |
vargham | 0:f7121193ff7e | 74 | void fillTestData(char data[], uint8_t start, int length) |
vargham | 0:f7121193ff7e | 75 | { |
vargham | 0:f7121193ff7e | 76 | for (int i = 0; i < length; i++) data[i] = start + i; |
vargham | 0:f7121193ff7e | 77 | } |
vargham | 0:f7121193ff7e | 78 | |
vargham | 0:f7121193ff7e | 79 | void eeramDataTest() |
vargham | 0:f7121193ff7e | 80 | { |
vargham | 0:f7121193ff7e | 81 | const int testDataLength = 16; |
vargham | 0:f7121193ff7e | 82 | char data[testDataLength]; |
vargham | 0:f7121193ff7e | 83 | |
vargham | 0:f7121193ff7e | 84 | //Write |
vargham | 4:8cc86b207f25 | 85 | eeram.fillMemory(0xFF); |
vargham | 0:f7121193ff7e | 86 | |
vargham | 0:f7121193ff7e | 87 | fillTestData(data, 0x0, testDataLength); |
vargham | 4:8cc86b207f25 | 88 | serial.printf("Write %d bytes to 0x0: %d\r\n", testDataLength, eeram.writeBytes(0x0, data, testDataLength)); |
vargham | 0:f7121193ff7e | 89 | |
vargham | 0:f7121193ff7e | 90 | fillTestData(data, 0x50, testDataLength); |
vargham | 4:8cc86b207f25 | 91 | serial.printf("Write %d bytes to 0x500: %d\r\n", testDataLength, eeram.writeBytes(0x500, data, testDataLength)); |
vargham | 0:f7121193ff7e | 92 | |
vargham | 0:f7121193ff7e | 93 | fillTestData(data, 0x70, testDataLength); |
vargham | 4:8cc86b207f25 | 94 | serial.printf("Write %d bytes to 0x700: %d\r\n", testDataLength, eeram.writeBytes(0x700, data, testDataLength)); |
vargham | 0:f7121193ff7e | 95 | |
vargham | 0:f7121193ff7e | 96 | //Dump |
vargham | 0:f7121193ff7e | 97 | serial.printf("Dump contents 0x0, 16\r\n"); |
vargham | 0:f7121193ff7e | 98 | eeram.dump(serial, 0x0, testDataLength); |
vargham | 0:f7121193ff7e | 99 | serial.printf("Dump contents 0x500, 16\r\n"); |
vargham | 0:f7121193ff7e | 100 | eeram.dump(serial, 0x500, testDataLength); |
vargham | 0:f7121193ff7e | 101 | serial.printf("Dump contents 0x700, 16\r\n"); |
vargham | 0:f7121193ff7e | 102 | eeram.dump(serial, 0x700, testDataLength); |
vargham | 0:f7121193ff7e | 103 | //serial.printf("Dump all\r\n"); |
vargham | 0:f7121193ff7e | 104 | //eeram.dump(serial); |
vargham | 0:f7121193ff7e | 105 | serial.printf("Dump done\r\n"); |
vargham | 0:f7121193ff7e | 106 | |
vargham | 0:f7121193ff7e | 107 | //Read back |
vargham | 0:f7121193ff7e | 108 | fillTestData(data, 0x0, testDataLength); |
vargham | 4:8cc86b207f25 | 109 | serial.printf("Read back 16 bytes from 0x500: %d\r\n", eeram.readBytes(0x500, data, testDataLength)); |
vargham | 0:f7121193ff7e | 110 | serial.printf("%.4X ", 0x500); |
vargham | 0:f7121193ff7e | 111 | for (int i = 0; i < testDataLength; i++) |
vargham | 0:f7121193ff7e | 112 | { |
vargham | 0:f7121193ff7e | 113 | serial.printf("%.2X ", data[i]); |
vargham | 0:f7121193ff7e | 114 | } |
vargham | 0:f7121193ff7e | 115 | serial.printf("\r\n"); |
vargham | 2:e7a7a5328184 | 116 | |
vargham | 2:e7a7a5328184 | 117 | //Read and write individual values |
vargham | 2:e7a7a5328184 | 118 | const uint16_t START_ADDRESS = 0x400; |
vargham | 2:e7a7a5328184 | 119 | uint16_t addressPtr = START_ADDRESS; |
vargham | 2:e7a7a5328184 | 120 | int intToWrite = -76324; |
vargham | 2:e7a7a5328184 | 121 | unsigned short ushortToWrite = 4395; |
vargham | 2:e7a7a5328184 | 122 | float floatToWrite = 0.1976f; |
vargham | 2:e7a7a5328184 | 123 | serial.printf("# # #\r\n"); |
vargham | 2:e7a7a5328184 | 124 | serial.printf("Write individual values, start address = 0x%X\r\n", START_ADDRESS); |
vargham | 2:e7a7a5328184 | 125 | serial.printf("Int to write: %d\r\n", intToWrite); |
vargham | 2:e7a7a5328184 | 126 | serial.printf("Unsigned short to write: %d\r\n", ushortToWrite); |
vargham | 2:e7a7a5328184 | 127 | serial.printf("Float to write: %f\r\n", floatToWrite); |
vargham | 4:8cc86b207f25 | 128 | addressPtr += eeram.write(addressPtr, &intToWrite); |
vargham | 4:8cc86b207f25 | 129 | addressPtr += eeram.write(addressPtr, &ushortToWrite); |
vargham | 4:8cc86b207f25 | 130 | addressPtr += eeram.write(addressPtr, &floatToWrite); |
vargham | 2:e7a7a5328184 | 131 | |
vargham | 2:e7a7a5328184 | 132 | addressPtr = START_ADDRESS; |
vargham | 2:e7a7a5328184 | 133 | int intToRead = 0; |
vargham | 2:e7a7a5328184 | 134 | unsigned short ushortToRead = 0; |
vargham | 2:e7a7a5328184 | 135 | float floatToRead = 0.0; |
vargham | 4:8cc86b207f25 | 136 | addressPtr += eeram.read(addressPtr, &intToRead); |
vargham | 4:8cc86b207f25 | 137 | addressPtr += eeram.read(addressPtr, &ushortToRead); |
vargham | 4:8cc86b207f25 | 138 | addressPtr += eeram.read(addressPtr, &floatToRead); |
vargham | 2:e7a7a5328184 | 139 | serial.printf("Read back individual values\r\n"); |
vargham | 2:e7a7a5328184 | 140 | serial.printf("Int to read: %d\r\n", intToRead); |
vargham | 2:e7a7a5328184 | 141 | serial.printf("Unsigned short to read: %d\r\n", ushortToRead); |
vargham | 2:e7a7a5328184 | 142 | serial.printf("Float to read: %f\r\n", floatToRead); |
vargham | 2:e7a7a5328184 | 143 | int length = addressPtr - START_ADDRESS + 6; |
vargham | 2:e7a7a5328184 | 144 | serial.printf("Dump %d bytes from 0x%X:\r\n", length, START_ADDRESS); |
vargham | 2:e7a7a5328184 | 145 | eeram.dump(serial, START_ADDRESS, length); |
vargham | 3:9b5473254d82 | 146 | |
vargham | 3:9b5473254d82 | 147 | //Read and write array |
vargham | 3:9b5473254d82 | 148 | serial.printf("# # #\r\nTest arrays\r\nWrite:\r\n"); |
vargham | 3:9b5473254d82 | 149 | const int arraySize = 4; |
vargham | 3:9b5473254d82 | 150 | const uint16_t address = 0x300; |
vargham | 3:9b5473254d82 | 151 | float floatArrayToWrite[arraySize] = {1976.09f, 1979.04f, 2013.04f, 2015.11f}; |
vargham | 3:9b5473254d82 | 152 | for (int i = 0; i < arraySize; i++) serial.printf("%f ", floatArrayToWrite[i]); |
vargham | 3:9b5473254d82 | 153 | serial.printf("\r\nRead:\r\n"); |
vargham | 4:8cc86b207f25 | 154 | eeram.write(address, floatArrayToWrite, arraySize); |
vargham | 3:9b5473254d82 | 155 | float floatArrayToRead[arraySize]; |
vargham | 4:8cc86b207f25 | 156 | eeram.read(address, floatArrayToRead, arraySize); |
vargham | 3:9b5473254d82 | 157 | for (int i = 0; i < arraySize; i++) serial.printf("%f ", floatArrayToRead[i]); |
vargham | 3:9b5473254d82 | 158 | serial.printf("\r\n"); |
vargham | 4:8cc86b207f25 | 159 | |
vargham | 4:8cc86b207f25 | 160 | //Read and write custom types |
vargham | 4:8cc86b207f25 | 161 | serial.printf("# # #\r\nTest custom types\r\n"); |
vargham | 4:8cc86b207f25 | 162 | const int customTypeStart = 0x200; |
vargham | 4:8cc86b207f25 | 163 | StorageContainer containerToWrite; |
vargham | 4:8cc86b207f25 | 164 | containerToWrite.f = 1976.09; |
vargham | 4:8cc86b207f25 | 165 | containerToWrite.i = 2015; |
vargham | 4:8cc86b207f25 | 166 | containerToWrite.s[0] = -1; |
vargham | 4:8cc86b207f25 | 167 | containerToWrite.s[1] = -20; |
vargham | 4:8cc86b207f25 | 168 | containerToWrite.s[2] = -30; |
vargham | 4:8cc86b207f25 | 169 | containerToWrite.s[3] = -40; |
vargham | 4:8cc86b207f25 | 170 | serial.printf("Write container's float: %f\r\n", containerToWrite.f); |
vargham | 4:8cc86b207f25 | 171 | serial.printf("Write container's int: %d\r\n", containerToWrite.i); |
vargham | 4:8cc86b207f25 | 172 | serial.printf("Write container's short array: "); |
vargham | 4:8cc86b207f25 | 173 | for (int i = 0; i < 4; i++) serial.printf("%d ", containerToWrite.s[i]); |
vargham | 4:8cc86b207f25 | 174 | serial.printf("\r\n"); |
vargham | 4:8cc86b207f25 | 175 | eeram.write(customTypeStart, &containerToWrite); |
vargham | 4:8cc86b207f25 | 176 | StorageContainer containerToRead; |
vargham | 4:8cc86b207f25 | 177 | eeram.read(customTypeStart, &containerToRead); |
vargham | 4:8cc86b207f25 | 178 | serial.printf("Read container's float: %f\r\n", containerToRead.f); |
vargham | 4:8cc86b207f25 | 179 | serial.printf("Read container's int: %d\r\n", containerToRead.i); |
vargham | 4:8cc86b207f25 | 180 | serial.printf("Read container's short array: "); |
vargham | 4:8cc86b207f25 | 181 | for (int i = 0; i < 4; i++) serial.printf("%d ", containerToRead.s[i]); |
vargham | 4:8cc86b207f25 | 182 | serial.printf("\r\n"); |
vargham | 0:f7121193ff7e | 183 | } |
vargham | 0:f7121193ff7e | 184 | |
vargham | 0:f7121193ff7e | 185 | void eeramRegisterTest() |
vargham | 0:f7121193ff7e | 186 | { |
vargham | 2:e7a7a5328184 | 187 | serial.printf("# # #\r\n"); |
vargham | 0:f7121193ff7e | 188 | eeram.dumpRegisters(serial); |
vargham | 0:f7121193ff7e | 189 | } |
vargham | 0:f7121193ff7e | 190 | |
vargham | 0:f7121193ff7e | 191 | int main() |
vargham | 0:f7121193ff7e | 192 | { |
vargham | 0:f7121193ff7e | 193 | serial.baud(460800); |
vargham | 0:f7121193ff7e | 194 | i2c.frequency(I2C_FREQUENCY); //Hz |
vargham | 0:f7121193ff7e | 195 | serial.printf("\r\nI2C EERAM example\r\n"); |
vargham | 4:8cc86b207f25 | 196 | serial.printf("MBED version: %d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
vargham | 0:f7121193ff7e | 197 | |
vargham | 4:8cc86b207f25 | 198 | printI2C(); |
vargham | 0:f7121193ff7e | 199 | |
vargham | 0:f7121193ff7e | 200 | serial.printf("Is EERAM device ready?\r\n"); |
vargham | 0:f7121193ff7e | 201 | while (!eeram.isReady()); |
vargham | 0:f7121193ff7e | 202 | serial.printf("Device is ready.\r\n"); |
vargham | 0:f7121193ff7e | 203 | |
vargham | 0:f7121193ff7e | 204 | eeram.readStatus(); |
vargham | 0:f7121193ff7e | 205 | eeram.setAutoStoreEnabled(true); |
vargham | 0:f7121193ff7e | 206 | eeram.setProtectedMemoryArea(U64); |
vargham | 0:f7121193ff7e | 207 | eeram.writeStatusIfChanged(true); |
vargham | 0:f7121193ff7e | 208 | serial.printf("Status: %.2X\r\n", eeram.getStatus()); |
vargham | 0:f7121193ff7e | 209 | |
vargham | 0:f7121193ff7e | 210 | eeramDataTest(); |
vargham | 0:f7121193ff7e | 211 | |
vargham | 0:f7121193ff7e | 212 | eeramRegisterTest(); |
vargham | 0:f7121193ff7e | 213 | |
vargham | 0:f7121193ff7e | 214 | //eeram.store(true); |
vargham | 0:f7121193ff7e | 215 | //eeram.recall(true); |
vargham | 0:f7121193ff7e | 216 | |
vargham | 0:f7121193ff7e | 217 | while (true) |
vargham | 0:f7121193ff7e | 218 | { |
vargham | 0:f7121193ff7e | 219 | |
vargham | 0:f7121193ff7e | 220 | } |
vargham | 0:f7121193ff7e | 221 | } |
vargham | 0:f7121193ff7e | 222 | |
vargham | 0:f7121193ff7e | 223 | /* |
vargham | 0:f7121193ff7e | 224 | #include "mbed.h" |
vargham | 0:f7121193ff7e | 225 | #include "EERAM.h" |
vargham | 0:f7121193ff7e | 226 | |
vargham | 0:f7121193ff7e | 227 | EERAM eeram(PC_9, PA_8, 2048); //SDA, SCL |
vargham | 0:f7121193ff7e | 228 | |
vargham | 0:f7121193ff7e | 229 | int main() |
vargham | 0:f7121193ff7e | 230 | { |
vargham | 0:f7121193ff7e | 231 | if (!eeram.isReady(100)) //Checks device with 100 ms timeout |
vargham | 0:f7121193ff7e | 232 | { |
vargham | 0:f7121193ff7e | 233 | printf("Device is not present."); |
vargham | 0:f7121193ff7e | 234 | while (1); |
vargham | 0:f7121193ff7e | 235 | } |
vargham | 0:f7121193ff7e | 236 | eeram.readStatus(); //Reads status register |
vargham | 0:f7121193ff7e | 237 | eeram.setAutoStoreEnabled(true, true); //Set auto store on power down to true and stores if not stored before |
vargham | 0:f7121193ff7e | 238 | while (1) |
vargham | 0:f7121193ff7e | 239 | { |
vargham | 0:f7121193ff7e | 240 | char dataStore[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
vargham | 4:8cc86b207f25 | 241 | eeram.writeBytes(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently. |
vargham | 0:f7121193ff7e | 242 | wait(2.0); |
vargham | 0:f7121193ff7e | 243 | char dataRead[16]; |
vargham | 4:8cc86b207f25 | 244 | eeram.readBytes(0x100, dataRead, 16); |
vargham | 0:f7121193ff7e | 245 | wait(2.0); |
vargham | 2:e7a7a5328184 | 246 | float floatToWrite = -1.976f; |
vargham | 2:e7a7a5328184 | 247 | const uint16_t address = 0x200; |
vargham | 4:8cc86b207f25 | 248 | eeram.write(address, &floatToWrite); |
vargham | 2:e7a7a5328184 | 249 | wait(2.0); |
vargham | 2:e7a7a5328184 | 250 | float floatToRead = 0; |
vargham | 4:8cc86b207f25 | 251 | eeram.read(address, &floatToWrite); |
vargham | 2:e7a7a5328184 | 252 | wait(2.0); |
vargham | 0:f7121193ff7e | 253 | } |
vargham | 0:f7121193ff7e | 254 | } |
vargham | 4:8cc86b207f25 | 255 | |
vargham | 4:8cc86b207f25 | 256 | */ |