This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Files at this revision

API Documentation at this revision

Comitter:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Parent:
0:f30e2135b0db
Commit message:
V0.0.0.2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Feb 09 13:57:49 2011 +0000
+++ b/main.cpp	Fri Feb 11 10:17:20 2011 +0000
@@ -13,12 +13,18 @@
 void TimeUpdate(); // Update time from SNTP server
 void RtcClockEvent(); // Event rise on each RTC.SQW/OUT change
 char DisplayMenuAndGetChoice(); // Display and get the user choice
+// Time functions
 void SetTimeFromSNTP();
 void ForceTime();
 void GetTime();
 void GetTimeFieldByFiled();
+// Memory functions
 void WriteStdStringValue(const unsigned char p_address, const std::string & p_string); // Write a string
 void ReadStdStringValue(const unsigned char p_address); // Read a string
+void WriteShortValue(const unsigned char p_address, const short p_short, CDS130X_I2C::Mode p_mode); // Write a short value
+void ReadShortValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode); // Read a short value
+void WriteIntegerValue(const unsigned char p_address, const int p_int, CDS130X_I2C::Mode p_mode); // Write an integer value
+void ReadIntegerValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode); // Read an integer value
 
 /*
  * Declare statics
@@ -43,6 +49,7 @@
         error("Module initialization failed");
     }
     // Initialize Ethernet
+    std::cout << "Getting IP address..." << "\r" << std::endl;
     g_eth.setup();   
     wait(1); // Needed after Ethernet startup   
     IpAddr ip = g_eth.getIp();
@@ -72,20 +79,44 @@
                 GetTimeFieldByFiled();
                 break;
 
-            case 'l': {
+            case 'j': {
                     std::string str("Wellcome to Evil...");
                     WriteStdStringValue(0x04, str);
+#if defined(__DEBUG)
+                    g_myRTC.DumpMemoryArea(0x04, str.length() + 4);
+#else // __DEBUG
+                    std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl;
+#endif // __DEBUG
                 }
                 break;
+            case 'k':
+                ReadStdStringValue(0x04);
+                break;
+            case 'l':
+                WriteShortValue(0x21, (short)0xbeef, CDS130X_I2C::BigEndian);
+                break;
             case 'm':
+                ReadShortValue(0x21, CDS130X_I2C::BigEndian);
+                break;
+            case 'n':
+                WriteIntegerValue(0x23, (int)0xcafedeca, CDS130X_I2C::BigEndian);
+                break;
+            case 'o':
+                ReadIntegerValue(0x23, CDS130X_I2C::BigEndian);
+                break;
+            case 'p':
 #if defined(__DEBUG)
-                g_myRTC.DumpMemoryArea(0x04, 0x14);
+                std::cout << "Enter the addess to dump: " << std::flush;
+                int address;
+                scanf("%d", &address);                 
+                std::cout << "\r\r" << std::endl;
+                g_myRTC.DumpMemoryArea((unsigned char)address, 16);
 #else // __DEBUG
                 std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl;
 #endif // __DEBUG
                 break;
-            case 'n':
-                g_myRTC.EraseMemoryArea(0x04, 0x14);
+            case 'q':
+                g_myRTC.EraseMemoryArea(0x21, 16);
                 break;
             default:
                 std::cout << "Invalid user choice\r" << std::endl;
@@ -114,27 +145,29 @@
 
 char DisplayMenuAndGetChoice()
 {
-    std::cout << "\r" << std::endl << "\r" << std::endl << "DS13X_I2C v0.1\r" << std::endl;
-    std::cout << "\tSet time from SNTP server:\ta\r" << std::endl;
-    std::cout << "\tRead time from RTC:\t\tb\r" << std::endl;
-    std::cout << "\tForce time:\t\t\tc\r" << std::endl;
-    std::cout << "\tRead time field:\t\td\r" << std::endl;
-    std::cout << "\tSet second:\t\te\r" << std::endl;
-    std::cout << "\tInc hour:\t\tf\r" << std::endl;
-    std::cout << "\tDec hour:\t\tg\r" << std::endl;
-    std::cout << "\tInc month:\t\th\r" << std::endl;
-    std::cout << "\tDec month:\t\ti\r" << std::endl;
-
-
-
-
-
-    std::cout << "\tWrite a string at address 0x04:\tk\r" << std::endl;
-    std::cout << "\tRead a string is address 0x04:\tl\r" << std::endl;
-    std::cout << "\tHexadump from address 0x04:\tm\r" << std::endl;
-    std::cout << "\tErase from address 0x04:\tn\r" << std::endl;
+    char value;
+    std::cout << "\r" << std::endl << "\r" << std::endl << "DS13X_I2C v0.2\r" << std::endl;
+    std::cout << "\tSet time from SNTP server:\t\ta\r" << std::endl;
+    std::cout << "\tRead time from RTC:\t\t\tb\r" << std::endl;
+    std::cout << "\tForce time:\t\t\t\tc\r" << std::endl;
+    std::cout << "\tRead time field:\t\t\td\r" << std::endl;
+    std::cout << "\tSet second:\t\t\t\te\r" << std::endl;
+    std::cout << "\tInc hour:\t\t\t\tf\r" << std::endl;
+    std::cout << "\tDec hour:\t\t\t\tg\r" << std::endl;
+    std::cout << "\tInc month:\t\t\t\th\r" << std::endl;
+    std::cout << "\tDec month:\t\t\t\ti\r" << std::endl;
+    std::cout << "\tWrite a string at address 0x04:\t\tj\r" << std::endl;
+    std::cout << "\tRead a string at address 0x04:\t\tk\r" << std::endl;
+    std::cout << "\tWrite a short value at address 0x21:\tl\r" << std::endl;
+    std::cout << "\tRead a short value at address 0x21:\tm\r" << std::endl;
+    std::cout << "\tWrite an int value at address 0x23:\tn\r" << std::endl;
+    std::cout << "\tRead an int value at address 0x23:\to\r" << std::endl;
+    std::cout << "\tHexadump from address 0x21:\t\tp\r" << std::endl;
+    std::cout << "\tErase from address 0x21:\t\tq\r" << std::endl;
     std::cout << "Enter your choice: " << std::flush;
-    return getchar();
+    value = getchar(); 
+    std::cout << "\r" << std::endl;
+    return value;
 }
 
 void SetTimeFromSNTP() {
@@ -170,6 +203,14 @@
 
 void GetTime() {
     struct tm t = g_myRTC.GetTime();
+    DEBUG("GetTime: %d %d %d %d:%d:%d %d", /* ww mm dd hh:mm:ss yyyy */
+          t.tm_wday,
+          t.tm_mon,
+          t.tm_mday,
+          t.tm_hour,
+          t.tm_min,
+          t.tm_sec,
+          t.tm_year);
     char buffer[32];
     strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", &t);
     DEBUG("GetTime: Current date is '%s'", buffer)
@@ -180,21 +221,64 @@
 
     // Get seconds in BCD format
     g_myRTC.Read(CDS130X_I2C::SecondsAddress, &value);
-    std::cout << "\tSeconds in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    DEBUG("\tSeconds in BCD: 0x%02x", value)
+    std::cout << "\tSeconds in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
     // Get seconds in BCD format
     g_myRTC.Read(CDS130X_I2C::MinutesAddress, &value);
-    std::cout << "\tMinutes in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    DEBUG("\tMinutes in BCD: 0x%02x", value)
+    std::cout << "\tMinutes in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
     // Get seconds in BCD format
     g_myRTC.Read(CDS130X_I2C::HoursAddress, &value);
-    std::cout << "\tHours in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    DEBUG("\tHours in BCD: 0x%02x", value)
+    std::cout << "\tHours in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
     // Get seconds in BCD format
     g_myRTC.Read(CDS130X_I2C::DayAddress, &value);
-    std::cout << "\tDay in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    DEBUG("\tDay in BCD: 0x%02x", value)
+    std::cout << "\tDay in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
 }
 
 void WriteStdStringValue(const unsigned char p_address, const std::string & p_string) {
+    std::cout << "Write string '" << p_string << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
     g_myRTC.WriteMemory(p_address, p_string);
 }
 
 void ReadStdStringValue(const unsigned char p_address) {
+    std::string str;
+    g_myRTC.ReadMemory(p_address, str);
+    std::cout << "String at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << " is: " << str << "\r" << std::endl;
 }
+
+void WriteShortValue(const unsigned char p_address, const short p_short, CDS130X_I2C::Mode p_mode) {
+    std::cout << "Write short '" << p_short << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
+    if (p_mode == CDS130X_I2C::BigEndian) {
+        g_myRTC.WriteMemory(p_address, p_short);
+    } else {
+        g_myRTC.WriteMemory(p_address, p_short, p_mode);
+    }
+}
+
+void ReadShortValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode) {
+    short value;
+    if (p_mode == CDS130X_I2C::BigEndian) {
+        g_myRTC.ReadMemory(p_address, (short *)&value);
+    } else {
+        g_myRTC.ReadMemory(p_address, (short *)&value, p_mode);
+    }
+    std::cout << "Short at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << " is: " << std::setw(4) << value << "\r" << std::endl;
+}
+
+void WriteIntegerValue(const unsigned char p_address, const int p_int, CDS130X_I2C::Mode p_mode) {
+    std::cout << "Write integer '" << p_int << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
+    g_myRTC.WriteMemory(p_address, p_int, p_mode);
+}
+
+void ReadIntegerValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode) {
+    int value;
+    if (p_mode == CDS130X_I2C::BigEndian) {
+        g_myRTC.ReadMemory(p_address, (int *)&value);
+    } else {
+        g_myRTC.ReadMemory(p_address, (int *)&value, p_mode);
+    }
+    std::cout << "Integer at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << " is: " << std::setw(8) << value << "\r" << std::endl;
+}
+