Example program for Microchip 47x04 and 47x16 EERAM devices.

Dependencies:   EERAM mbed-dev

Files at this revision

API Documentation at this revision

Comitter:
vargham
Date:
Fri Apr 28 13:39:54 2017 +0000
Parent:
3:9b5473254d82
Commit message:
Follow lib's API changes.

Changed in this revision

EERAM.lib Show annotated file Show diff for this revision Revisions of this file
eeram_main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/EERAM.lib	Thu Apr 27 17:56:56 2017 +0000
+++ b/EERAM.lib	Fri Apr 28 13:39:54 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/vargham/code/EERAM/#a869096d7a5d
+https://developer.mbed.org/users/vargham/code/EERAM/#fd84e2f0d1de
--- a/eeram_main.cpp	Thu Apr 27 17:56:56 2017 +0000
+++ b/eeram_main.cpp	Fri Apr 28 13:39:54 2017 +0000
@@ -25,7 +25,15 @@
 #define PIN_I2C_SDA PC_9
 #define PIN_I2C_SCL PA_8
 
-#define I2C_FREQUENCY 1000000
+//#define I2C_FREQUENCY 1000000
+#define I2C_FREQUENCY 400000
+
+struct StorageContainer
+{
+    float f;
+    int i;
+    short s[4];
+};
 
 Serial serial(PA_9, PA_10);  //Tx, Rx
 I2C i2c(PIN_I2C_SDA, PIN_I2C_SCL); //SDA, SCL
@@ -74,16 +82,16 @@
     char data[testDataLength];
 
     //Write
-    //eeram.fillMemory(0xFF);
+    eeram.fillMemory(0xFF);
 
     fillTestData(data, 0x0, testDataLength);
-    serial.printf("Write %d bytes to 0x0: %d\r\n", testDataLength, eeram.write(0x0, data, testDataLength));
+    serial.printf("Write %d bytes to 0x0: %d\r\n", testDataLength, eeram.writeBytes(0x0, data, testDataLength));
 
     fillTestData(data, 0x50, testDataLength);
-    serial.printf("Write %d bytes to 0x500: %d\r\n", testDataLength, eeram.write(0x500, data, testDataLength));
+    serial.printf("Write %d bytes to 0x500: %d\r\n", testDataLength, eeram.writeBytes(0x500, data, testDataLength));
 
     fillTestData(data, 0x70, testDataLength);
-    serial.printf("Write %d bytes to 0x700: %d\r\n", testDataLength, eeram.write(0x700, data, testDataLength));
+    serial.printf("Write %d bytes to 0x700: %d\r\n", testDataLength, eeram.writeBytes(0x700, data, testDataLength));
 
     //Dump
     serial.printf("Dump contents 0x0, 16\r\n");
@@ -98,7 +106,7 @@
 
     //Read back
     fillTestData(data, 0x0, testDataLength);
-    serial.printf("Read back 16 bytes from 0x500: %d\r\n", eeram.read(0x500, data, testDataLength));
+    serial.printf("Read back 16 bytes from 0x500: %d\r\n", eeram.readBytes(0x500, data, testDataLength));
     serial.printf("%.4X ", 0x500);
     for (int i = 0; i < testDataLength; i++)
     {
@@ -117,17 +125,17 @@
     serial.printf("Int to write: %d\r\n", intToWrite);
     serial.printf("Unsigned short to write: %d\r\n", ushortToWrite);
     serial.printf("Float to write: %f\r\n", floatToWrite);
-    addressPtr += eeram.writeValue(addressPtr, &intToWrite);
-    addressPtr += eeram.writeValue(addressPtr, &ushortToWrite);
-    addressPtr += eeram.writeValue(addressPtr, &floatToWrite);
+    addressPtr += eeram.write(addressPtr, &intToWrite);
+    addressPtr += eeram.write(addressPtr, &ushortToWrite);
+    addressPtr += eeram.write(addressPtr, &floatToWrite);
 
     addressPtr = START_ADDRESS;
     int intToRead = 0;
     unsigned short ushortToRead = 0;
     float floatToRead = 0.0;
-    addressPtr += eeram.readValue(addressPtr, &intToRead);
-    addressPtr += eeram.readValue(addressPtr, &ushortToRead);
-    addressPtr += eeram.readValue(addressPtr, &floatToRead);
+    addressPtr += eeram.read(addressPtr, &intToRead);
+    addressPtr += eeram.read(addressPtr, &ushortToRead);
+    addressPtr += eeram.read(addressPtr, &floatToRead);
     serial.printf("Read back individual values\r\n");
     serial.printf("Int to read: %d\r\n", intToRead);
     serial.printf("Unsigned short to read: %d\r\n", ushortToRead);
@@ -143,11 +151,35 @@
     float floatArrayToWrite[arraySize] = {1976.09f, 1979.04f, 2013.04f, 2015.11f};
     for (int i = 0; i < arraySize; i++) serial.printf("%f ", floatArrayToWrite[i]);
     serial.printf("\r\nRead:\r\n");
-    eeram.writeArray(address, floatArrayToWrite, arraySize);
+    eeram.write(address, floatArrayToWrite, arraySize);
     float floatArrayToRead[arraySize];
-    eeram.readArray(address, floatArrayToRead, arraySize);
+    eeram.read(address, floatArrayToRead, arraySize);
     for (int i = 0; i < arraySize; i++) serial.printf("%f ", floatArrayToRead[i]);
     serial.printf("\r\n");
+
+    //Read and write custom types
+    serial.printf("# # #\r\nTest custom types\r\n");
+    const int customTypeStart = 0x200;
+    StorageContainer containerToWrite;
+    containerToWrite.f = 1976.09;
+    containerToWrite.i = 2015;
+    containerToWrite.s[0] = -1;
+    containerToWrite.s[1] = -20;
+    containerToWrite.s[2] = -30;
+    containerToWrite.s[3] = -40;
+    serial.printf("Write container's float: %f\r\n", containerToWrite.f);
+    serial.printf("Write container's int: %d\r\n", containerToWrite.i);
+    serial.printf("Write container's short array: ");
+    for (int i = 0; i < 4; i++) serial.printf("%d ", containerToWrite.s[i]);
+    serial.printf("\r\n");
+    eeram.write(customTypeStart, &containerToWrite);
+    StorageContainer containerToRead;
+    eeram.read(customTypeStart, &containerToRead);
+    serial.printf("Read container's float: %f\r\n", containerToRead.f);
+    serial.printf("Read container's int: %d\r\n", containerToRead.i);
+    serial.printf("Read container's short array: ");
+    for (int i = 0; i < 4; i++) serial.printf("%d ", containerToRead.s[i]);
+    serial.printf("\r\n");
 }
 
 void eeramRegisterTest()
@@ -161,8 +193,9 @@
     serial.baud(460800);
     i2c.frequency(I2C_FREQUENCY); //Hz
     serial.printf("\r\nI2C EERAM example\r\n");
+    serial.printf("MBED version: %d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
 
-    //printI2C();
+    printI2C();
 
     serial.printf("Is EERAM device ready?\r\n");
     while (!eeram.isReady());
@@ -205,18 +238,19 @@
     while (1)
     {
         char dataStore[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
-        eeram.write(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently.
+        eeram.writeBytes(0x100, dataStore, 16); //We can not wear EEPROM out, so it is ok to write data to the device frequently.
         wait(2.0);
         char dataRead[16];
-        eeram.read(0x100, dataRead, 16);
+        eeram.readBytes(0x100, dataRead, 16);
         wait(2.0);
         float floatToWrite = -1.976f;
         const uint16_t address = 0x200;
-        eeram.writeValue(address, &floatToWrite);
+        eeram.write(address, &floatToWrite);
         wait(2.0);
         float floatToRead = 0;
-        eeram.readValue(address, &floatToWrite);
+        eeram.read(address, &floatToWrite);
         wait(2.0);
     }
 }
-*/
+
+*/
\ No newline at end of file