Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
eeram_main.cpp@2:e7a7a5328184, 2017-04-27 (annotated)
- Committer:
- vargham
- Date:
- Thu Apr 27 13:27:13 2017 +0000
- Revision:
- 2:e7a7a5328184
- Parent:
- 0:f7121193ff7e
- Child:
- 3:9b5473254d82
Updated EERAM lib to 1.1
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 | 0:f7121193ff7e | 28 | #define I2C_FREQUENCY 1000000 |
vargham | 0:f7121193ff7e | 29 | |
vargham | 0:f7121193ff7e | 30 | Serial serial(PA_9, PA_10); //Tx, Rx |
vargham | 0:f7121193ff7e | 31 | I2C i2c(PIN_I2C_SDA, PIN_I2C_SCL); //SDA, SCL |
vargham | 0:f7121193ff7e | 32 | EERAM eeram(i2c, 2048); |
vargham | 0:f7121193ff7e | 33 | |
vargham | 0:f7121193ff7e | 34 | void printI2C() |
vargham | 0:f7121193ff7e | 35 | { |
vargham | 0:f7121193ff7e | 36 | //0x41 Discovery Touch |
vargham | 0:f7121193ff7e | 37 | //0x18 EERAM control |
vargham | 0:f7121193ff7e | 38 | //0x50 EERAM memory |
vargham | 0:f7121193ff7e | 39 | |
vargham | 0:f7121193ff7e | 40 | int error; |
vargham | 0:f7121193ff7e | 41 | int address; |
vargham | 0:f7121193ff7e | 42 | int nDevices = 0; |
vargham | 0:f7121193ff7e | 43 | |
vargham | 0:f7121193ff7e | 44 | serial.printf("Scanning I2C devices...\r\n"); |
vargham | 0:f7121193ff7e | 45 | |
vargham | 0:f7121193ff7e | 46 | for(address = 1; address < 127; address++ ) |
vargham | 0:f7121193ff7e | 47 | { |
vargham | 0:f7121193ff7e | 48 | i2c.start(); |
vargham | 0:f7121193ff7e | 49 | error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses |
vargham | 0:f7121193ff7e | 50 | i2c.stop(); |
vargham | 0:f7121193ff7e | 51 | if (error == 1) |
vargham | 0:f7121193ff7e | 52 | { |
vargham | 0:f7121193ff7e | 53 | serial.printf("I2C device found at address 0x%X\r\n", address); //Returns 7-bit addres |
vargham | 0:f7121193ff7e | 54 | nDevices++; |
vargham | 0:f7121193ff7e | 55 | } |
vargham | 0:f7121193ff7e | 56 | |
vargham | 0:f7121193ff7e | 57 | } |
vargham | 0:f7121193ff7e | 58 | serial.printf("I2C scan finished.\r\n"); |
vargham | 0:f7121193ff7e | 59 | if (nDevices == 0) |
vargham | 0:f7121193ff7e | 60 | { |
vargham | 0:f7121193ff7e | 61 | serial.printf("No I2C devices found.\r\n"); |
vargham | 0:f7121193ff7e | 62 | } |
vargham | 0:f7121193ff7e | 63 | |
vargham | 0:f7121193ff7e | 64 | } |
vargham | 0:f7121193ff7e | 65 | |
vargham | 0:f7121193ff7e | 66 | void fillTestData(char data[], uint8_t start, int length) |
vargham | 0:f7121193ff7e | 67 | { |
vargham | 0:f7121193ff7e | 68 | for (int i = 0; i < length; i++) data[i] = start + i; |
vargham | 0:f7121193ff7e | 69 | } |
vargham | 0:f7121193ff7e | 70 | |
vargham | 0:f7121193ff7e | 71 | void eeramDataTest() |
vargham | 0:f7121193ff7e | 72 | { |
vargham | 0:f7121193ff7e | 73 | const int testDataLength = 16; |
vargham | 0:f7121193ff7e | 74 | char data[testDataLength]; |
vargham | 0:f7121193ff7e | 75 | |
vargham | 0:f7121193ff7e | 76 | //Write |
vargham | 0:f7121193ff7e | 77 | //eeram.fillMemory(0xFF); |
vargham | 0:f7121193ff7e | 78 | |
vargham | 0:f7121193ff7e | 79 | fillTestData(data, 0x0, testDataLength); |
vargham | 0:f7121193ff7e | 80 | serial.printf("Write %d bytes to 0x0: %d\r\n", testDataLength, eeram.write(0x0, data, testDataLength)); |
vargham | 0:f7121193ff7e | 81 | |
vargham | 0:f7121193ff7e | 82 | fillTestData(data, 0x50, testDataLength); |
vargham | 0:f7121193ff7e | 83 | serial.printf("Write %d bytes to 0x500: %d\r\n", testDataLength, eeram.write(0x500, data, testDataLength)); |
vargham | 0:f7121193ff7e | 84 | |
vargham | 0:f7121193ff7e | 85 | fillTestData(data, 0x70, testDataLength); |
vargham | 0:f7121193ff7e | 86 | serial.printf("Write %d bytes to 0x700: %d\r\n", testDataLength, eeram.write(0x700, data, testDataLength)); |
vargham | 0:f7121193ff7e | 87 | |
vargham | 0:f7121193ff7e | 88 | //Dump |
vargham | 0:f7121193ff7e | 89 | serial.printf("Dump contents 0x0, 16\r\n"); |
vargham | 0:f7121193ff7e | 90 | eeram.dump(serial, 0x0, testDataLength); |
vargham | 0:f7121193ff7e | 91 | serial.printf("Dump contents 0x500, 16\r\n"); |
vargham | 0:f7121193ff7e | 92 | eeram.dump(serial, 0x500, testDataLength); |
vargham | 0:f7121193ff7e | 93 | serial.printf("Dump contents 0x700, 16\r\n"); |
vargham | 0:f7121193ff7e | 94 | eeram.dump(serial, 0x700, testDataLength); |
vargham | 0:f7121193ff7e | 95 | //serial.printf("Dump all\r\n"); |
vargham | 0:f7121193ff7e | 96 | //eeram.dump(serial); |
vargham | 0:f7121193ff7e | 97 | serial.printf("Dump done\r\n"); |
vargham | 0:f7121193ff7e | 98 | |
vargham | 0:f7121193ff7e | 99 | //Read back |
vargham | 0:f7121193ff7e | 100 | fillTestData(data, 0x0, testDataLength); |
vargham | 0:f7121193ff7e | 101 | serial.printf("Read back 16 bytes from 0x500: %d\r\n", eeram.read(0x500, data, testDataLength)); |
vargham | 0:f7121193ff7e | 102 | serial.printf("%.4X ", 0x500); |
vargham | 0:f7121193ff7e | 103 | for (int i = 0; i < testDataLength; i++) |
vargham | 0:f7121193ff7e | 104 | { |
vargham | 0:f7121193ff7e | 105 | serial.printf("%.2X ", data[i]); |
vargham | 0:f7121193ff7e | 106 | } |
vargham | 0:f7121193ff7e | 107 | serial.printf("\r\n"); |
vargham | 2:e7a7a5328184 | 108 | |
vargham | 2:e7a7a5328184 | 109 | //Read and write individual values |
vargham | 2:e7a7a5328184 | 110 | const uint16_t START_ADDRESS = 0x400; |
vargham | 2:e7a7a5328184 | 111 | uint16_t addressPtr = START_ADDRESS; |
vargham | 2:e7a7a5328184 | 112 | int intToWrite = -76324; |
vargham | 2:e7a7a5328184 | 113 | unsigned short ushortToWrite = 4395; |
vargham | 2:e7a7a5328184 | 114 | float floatToWrite = 0.1976f; |
vargham | 2:e7a7a5328184 | 115 | serial.printf("# # #\r\n"); |
vargham | 2:e7a7a5328184 | 116 | serial.printf("Write individual values, start address = 0x%X\r\n", START_ADDRESS); |
vargham | 2:e7a7a5328184 | 117 | serial.printf("Int to write: %d\r\n", intToWrite); |
vargham | 2:e7a7a5328184 | 118 | serial.printf("Unsigned short to write: %d\r\n", ushortToWrite); |
vargham | 2:e7a7a5328184 | 119 | serial.printf("Float to write: %f\r\n", floatToWrite); |
vargham | 2:e7a7a5328184 | 120 | addressPtr += eeram.writeValue(addressPtr, &intToWrite); |
vargham | 2:e7a7a5328184 | 121 | addressPtr += eeram.writeValue(addressPtr, &ushortToWrite); |
vargham | 2:e7a7a5328184 | 122 | addressPtr += eeram.writeValue(addressPtr, &floatToWrite); |
vargham | 2:e7a7a5328184 | 123 | |
vargham | 2:e7a7a5328184 | 124 | addressPtr = START_ADDRESS; |
vargham | 2:e7a7a5328184 | 125 | int intToRead = 0; |
vargham | 2:e7a7a5328184 | 126 | unsigned short ushortToRead = 0; |
vargham | 2:e7a7a5328184 | 127 | float floatToRead = 0.0; |
vargham | 2:e7a7a5328184 | 128 | addressPtr += eeram.readValue(addressPtr, &intToRead); |
vargham | 2:e7a7a5328184 | 129 | addressPtr += eeram.readValue(addressPtr, &ushortToRead); |
vargham | 2:e7a7a5328184 | 130 | addressPtr += eeram.readValue(addressPtr, &floatToRead); |
vargham | 2:e7a7a5328184 | 131 | serial.printf("Read back individual values\r\n"); |
vargham | 2:e7a7a5328184 | 132 | serial.printf("Int to read: %d\r\n", intToRead); |
vargham | 2:e7a7a5328184 | 133 | serial.printf("Unsigned short to read: %d\r\n", ushortToRead); |
vargham | 2:e7a7a5328184 | 134 | serial.printf("Float to read: %f\r\n", floatToRead); |
vargham | 2:e7a7a5328184 | 135 | int length = addressPtr - START_ADDRESS + 6; |
vargham | 2:e7a7a5328184 | 136 | serial.printf("Dump %d bytes from 0x%X:\r\n", length, START_ADDRESS); |
vargham | 2:e7a7a5328184 | 137 | eeram.dump(serial, START_ADDRESS, length); |
vargham | 0:f7121193ff7e | 138 | } |
vargham | 0:f7121193ff7e | 139 | |
vargham | 0:f7121193ff7e | 140 | void eeramRegisterTest() |
vargham | 0:f7121193ff7e | 141 | { |
vargham | 2:e7a7a5328184 | 142 | serial.printf("# # #\r\n"); |
vargham | 0:f7121193ff7e | 143 | eeram.dumpRegisters(serial); |
vargham | 0:f7121193ff7e | 144 | } |
vargham | 0:f7121193ff7e | 145 | |
vargham | 0:f7121193ff7e | 146 | int main() |
vargham | 0:f7121193ff7e | 147 | { |
vargham | 0:f7121193ff7e | 148 | serial.baud(460800); |
vargham | 0:f7121193ff7e | 149 | i2c.frequency(I2C_FREQUENCY); //Hz |
vargham | 0:f7121193ff7e | 150 | serial.printf("\r\nI2C EERAM example\r\n"); |
vargham | 0:f7121193ff7e | 151 | |
vargham | 0:f7121193ff7e | 152 | //printI2C(); |
vargham | 0:f7121193ff7e | 153 | |
vargham | 0:f7121193ff7e | 154 | serial.printf("Is EERAM device ready?\r\n"); |
vargham | 0:f7121193ff7e | 155 | while (!eeram.isReady()); |
vargham | 0:f7121193ff7e | 156 | serial.printf("Device is ready.\r\n"); |
vargham | 0:f7121193ff7e | 157 | |
vargham | 0:f7121193ff7e | 158 | eeram.readStatus(); |
vargham | 0:f7121193ff7e | 159 | eeram.setAutoStoreEnabled(true); |
vargham | 0:f7121193ff7e | 160 | eeram.setProtectedMemoryArea(U64); |
vargham | 0:f7121193ff7e | 161 | eeram.writeStatusIfChanged(true); |
vargham | 0:f7121193ff7e | 162 | serial.printf("Status: %.2X\r\n", eeram.getStatus()); |
vargham | 0:f7121193ff7e | 163 | |
vargham | 0:f7121193ff7e | 164 | eeramDataTest(); |
vargham | 0:f7121193ff7e | 165 | |
vargham | 0:f7121193ff7e | 166 | eeramRegisterTest(); |
vargham | 0:f7121193ff7e | 167 | |
vargham | 0:f7121193ff7e | 168 | //eeram.store(true); |
vargham | 0:f7121193ff7e | 169 | //eeram.recall(true); |
vargham | 0:f7121193ff7e | 170 | |
vargham | 0:f7121193ff7e | 171 | while (true) |
vargham | 0:f7121193ff7e | 172 | { |
vargham | 0:f7121193ff7e | 173 | |
vargham | 0:f7121193ff7e | 174 | } |
vargham | 0:f7121193ff7e | 175 | } |
vargham | 0:f7121193ff7e | 176 | |
vargham | 0:f7121193ff7e | 177 | /* |
vargham | 0:f7121193ff7e | 178 | #include "mbed.h" |
vargham | 0:f7121193ff7e | 179 | #include "EERAM.h" |
vargham | 0:f7121193ff7e | 180 | |
vargham | 0:f7121193ff7e | 181 | EERAM eeram(PC_9, PA_8, 2048); //SDA, SCL |
vargham | 0:f7121193ff7e | 182 | |
vargham | 0:f7121193ff7e | 183 | int main() |
vargham | 0:f7121193ff7e | 184 | { |
vargham | 0:f7121193ff7e | 185 | if (!eeram.isReady(100)) //Checks device with 100 ms timeout |
vargham | 0:f7121193ff7e | 186 | { |
vargham | 0:f7121193ff7e | 187 | printf("Device is not present."); |
vargham | 0:f7121193ff7e | 188 | while (1); |
vargham | 0:f7121193ff7e | 189 | } |
vargham | 0:f7121193ff7e | 190 | eeram.readStatus(); //Reads status register |
vargham | 0:f7121193ff7e | 191 | eeram.setAutoStoreEnabled(true, true); //Set auto store on power down to true and stores if not stored before |
vargham | 0:f7121193ff7e | 192 | while (1) |
vargham | 0:f7121193ff7e | 193 | { |
vargham | 0:f7121193ff7e | 194 | char dataStore[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
vargham | 0:f7121193ff7e | 195 | eeram.write(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently. |
vargham | 0:f7121193ff7e | 196 | wait(2.0); |
vargham | 0:f7121193ff7e | 197 | char dataRead[16]; |
vargham | 0:f7121193ff7e | 198 | eeram.read(0x100, dataRead, 16); |
vargham | 0:f7121193ff7e | 199 | wait(2.0); |
vargham | 2:e7a7a5328184 | 200 | float floatToWrite = -1.976f; |
vargham | 2:e7a7a5328184 | 201 | const uint16_t address = 0x200; |
vargham | 2:e7a7a5328184 | 202 | eeram.writeValue(address, &floatToWrite); |
vargham | 2:e7a7a5328184 | 203 | wait(2.0); |
vargham | 2:e7a7a5328184 | 204 | float floatToRead = 0; |
vargham | 2:e7a7a5328184 | 205 | eeram.readValue(address, &floatToWrite); |
vargham | 2:e7a7a5328184 | 206 | wait(2.0); |
vargham | 0:f7121193ff7e | 207 | } |
vargham | 0:f7121193ff7e | 208 | } |
vargham | 0:f7121193ff7e | 209 | */ |