Yann Garcia / Mbed 2 deprecated DS130x_I2CApp

Dependencies:   mbed DebugLibrary

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <string>
00002 #include <iostream>
00003 #include <iomanip>
00004 
00005 #include "DS130x_I2C.h"
00006 #include "EthernetNetIf.h"
00007 #include "NTPClient.h"
00008 
00009 /*
00010  * Declare functions
00011  */
00012 void AvailableIndicator(); // LED1 flashing for program while program is alive
00013 void TimeUpdate(); // Update time from SNTP server
00014 void RtcClockEvent(); // Event rise on each RTC.SQW/OUT change
00015 char DisplayMenuAndGetChoice(); // Display and get the user choice
00016 // Time functions
00017 void SetTimeFromSNTP();
00018 void ForceTime();
00019 void GetTime();
00020 void GetTimeFieldByFiled();
00021 // Memory functions
00022 void WriteStdStringValue(const unsigned char p_address, const std::string & p_string); // Write a string
00023 void ReadStdStringValue(const unsigned char p_address); // Read a string
00024 void WriteShortValue(const unsigned char p_address, const short p_short, CDS130X_I2C::Mode p_mode); // Write a short value
00025 void ReadShortValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode); // Read a short value
00026 void WriteIntegerValue(const unsigned char p_address, const int p_int, CDS130X_I2C::Mode p_mode); // Write an integer value
00027 void ReadIntegerValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode); // Read an integer value
00028 
00029 /*
00030  * Declare statics
00031  */
00032 EthernetNetIf g_eth;
00033 Host g_ntpServer(IpAddr(), 123, "ntp.unice.fr");
00034 NTPClient g_ntp;
00035 
00036 DigitalOut g_availableLed(LED1); // To verify if program in running
00037 InterruptIn g_rtcClock(p5); // Used to trigger event from RTC.SQW/OUT 
00038 Ticker g_available; // LED1 will flash with a period of 2s
00039 Ticker g_timeUpdate; // Update time from SNTP server
00040 
00041 CDS130X_I2C g_myRTC(0xd0, p28, p27, CDS130X_I2C::One_Hz); // Create an instance of the class C24LCXX_I2C, p28: SDA, p29:SDL, Oscillator mode: 1Hz, on 5V I2C bus
00042 
00043 int main() {
00044 
00045     // Launch available indicator
00046     g_available.attach(&AvailableIndicator, 2.0);
00047     // Initialize the module    
00048     if (!g_myRTC.Initialize()) {
00049         error("Module initialization failed");
00050     }
00051     // Initialize Ethernet
00052     std::cout << "Getting IP address..." << "\r" << std::endl;
00053     g_eth.setup();   
00054     wait(1); // Needed after Ethernet startup   
00055     IpAddr ip = g_eth.getIp();
00056     std::cout << "IP address: " << (int)ip[0] << "." << (int)ip[1] << "." << (int)ip[2] << "." << (int)ip[3] << "\r" << std::endl;
00057     // Update current time
00058     g_ntp.setTime(g_ntpServer);
00059     time_t ctTime = time(NULL);  
00060     std::cout << "Time is now: " << ctime(&ctTime) << " UTC\r" << std::endl; 
00061     // Start ticker
00062     g_timeUpdate.attach(&TimeUpdate, 60.0);
00063     
00064     // Start RTC.SQW/OUT trigger
00065     g_rtcClock.rise(&RtcClockEvent);
00066 
00067     while (true) {
00068         switch (DisplayMenuAndGetChoice()) {
00069             case 'a':
00070                 SetTimeFromSNTP();
00071                 break;
00072             case 'b':
00073                 GetTime();
00074                 break;
00075             case 'c':
00076                 ForceTime();
00077                 break;
00078             case 'd':
00079                 GetTimeFieldByFiled();
00080                 break;
00081 
00082             case 'j': {
00083                     std::string str("Wellcome to Evil...");
00084                     WriteStdStringValue(0x04, str);
00085 #if defined(__DEBUG)
00086                     g_myRTC.DumpMemoryArea(0x04, str.length() + 4);
00087 #else // __DEBUG
00088                     std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl;
00089 #endif // __DEBUG
00090                 }
00091                 break;
00092             case 'k':
00093                 ReadStdStringValue(0x04);
00094                 break;
00095             case 'l':
00096                 WriteShortValue(0x21, (short)0xbeef, CDS130X_I2C::BigEndian);
00097                 break;
00098             case 'm':
00099                 ReadShortValue(0x21, CDS130X_I2C::BigEndian);
00100                 break;
00101             case 'n':
00102                 WriteIntegerValue(0x23, (int)0xcafedeca, CDS130X_I2C::BigEndian);
00103                 break;
00104             case 'o':
00105                 ReadIntegerValue(0x23, CDS130X_I2C::BigEndian);
00106                 break;
00107             case 'p':
00108 #if defined(__DEBUG)
00109                 std::cout << "Enter the addess to dump: " << std::flush;
00110                 int address;
00111                 scanf("%d", &address);                 
00112                 std::cout << "\r\r" << std::endl;
00113                 g_myRTC.DumpMemoryArea((unsigned char)address, 16);
00114 #else // __DEBUG
00115                 std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl;
00116 #endif // __DEBUG
00117                 break;
00118             case 'q':
00119                 g_myRTC.EraseMemoryArea(0x21, 16);
00120                 break;
00121             default:
00122                 std::cout << "Invalid user choice\r" << std::endl;
00123                 break;
00124         } // End of 'switch' statement
00125     } // End of 'while' statement
00126 } // End of program - nerver reached
00127 
00128 void AvailableIndicator()
00129 {
00130     g_availableLed = !g_availableLed;
00131 } // End of AvailableIndicator
00132 
00133 void TimeUpdate()
00134 {
00135     // Update current time
00136     g_ntp.setTime(g_ntpServer);
00137 } // End of TimeUpdate funtion
00138 
00139 void RtcClockEvent() { /* Www Mmm dd hh:mm:ss yyyy */
00140     struct tm t = g_myRTC.GetTime();
00141     char buffer[32];
00142     strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", &t);
00143     DEBUG("RtcClockEvent: '%s'", buffer)
00144 } // End of RtcClockEvent() funtion
00145 
00146 char DisplayMenuAndGetChoice()
00147 {
00148     char value;
00149     std::cout << "\r" << std::endl << "\r" << std::endl << "DS13X_I2C v0.2\r" << std::endl;
00150     std::cout << "\tSet time from SNTP server:\t\ta\r" << std::endl;
00151     std::cout << "\tRead time from RTC:\t\t\tb\r" << std::endl;
00152     std::cout << "\tForce time:\t\t\t\tc\r" << std::endl;
00153     std::cout << "\tRead time field:\t\t\td\r" << std::endl;
00154     std::cout << "\tSet second:\t\t\t\te\r" << std::endl;
00155     std::cout << "\tInc hour:\t\t\t\tf\r" << std::endl;
00156     std::cout << "\tDec hour:\t\t\t\tg\r" << std::endl;
00157     std::cout << "\tInc month:\t\t\t\th\r" << std::endl;
00158     std::cout << "\tDec month:\t\t\t\ti\r" << std::endl;
00159     std::cout << "\tWrite a string at address 0x04:\t\tj\r" << std::endl;
00160     std::cout << "\tRead a string at address 0x04:\t\tk\r" << std::endl;
00161     std::cout << "\tWrite a short value at address 0x21:\tl\r" << std::endl;
00162     std::cout << "\tRead a short value at address 0x21:\tm\r" << std::endl;
00163     std::cout << "\tWrite an int value at address 0x23:\tn\r" << std::endl;
00164     std::cout << "\tRead an int value at address 0x23:\to\r" << std::endl;
00165     std::cout << "\tHexadump from address 0x21:\t\tp\r" << std::endl;
00166     std::cout << "\tErase from address 0x21:\t\tq\r" << std::endl;
00167     std::cout << "Enter your choice: " << std::flush;
00168     value = getchar(); 
00169     std::cout << "\r" << std::endl;
00170     return value;
00171 }
00172 
00173 void SetTimeFromSNTP() {
00174     DEBUG_ENTER("SetTimeFromSNTP")
00175 
00176     char buffer[32];
00177     time_t seconds = time(NULL);
00178     strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", localtime(&seconds)); /* Www Mmm dd hh:mm:ss yyyy */
00179     DEBUG("GetTime: Current date is '%s'", buffer)
00180 
00181     std::string str(buffer);
00182     g_myRTC.SetTime(str);
00183 
00184     DEBUG_LEAVE("SetTimeFromSNTP")
00185 }
00186 
00187 void ForceTime() {
00188     DEBUG_ENTER("ForceTime")
00189 
00190     // Get time frome user
00191     std::cout << "Enter the new date using the format 'Www Mmm dd hh:mm:ss yyyy': ";
00192     std::string line;
00193     std::cin >> line;
00194     int removeCR;
00195     if ((removeCR = line.rfind("\r")) != -1) {
00196         line.erase(removeCR);
00197     }
00198     // Set it
00199     g_myRTC.SetTime(line);
00200 
00201     DEBUG_LEAVE("ForceTime")
00202 }
00203 
00204 void GetTime() {
00205     struct tm t = g_myRTC.GetTime();
00206     DEBUG("GetTime: %d %d %d %d:%d:%d %d", /* ww mm dd hh:mm:ss yyyy */
00207           t.tm_wday,
00208           t.tm_mon,
00209           t.tm_mday,
00210           t.tm_hour,
00211           t.tm_min,
00212           t.tm_sec,
00213           t.tm_year);
00214     char buffer[32];
00215     strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", &t);
00216     DEBUG("GetTime: Current date is '%s'", buffer)
00217 }
00218 
00219 void GetTimeFieldByFiled() {
00220     unsigned char value = 0xff;
00221 
00222     // Get seconds in BCD format
00223     g_myRTC.Read(CDS130X_I2C::SecondsAddress, &value);
00224     DEBUG("\tSeconds in BCD: 0x%02x", value)
00225     std::cout << "\tSeconds in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
00226     // Get seconds in BCD format
00227     g_myRTC.Read(CDS130X_I2C::MinutesAddress, &value);
00228     DEBUG("\tMinutes in BCD: 0x%02x", value)
00229     std::cout << "\tMinutes in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
00230     // Get seconds in BCD format
00231     g_myRTC.Read(CDS130X_I2C::HoursAddress, &value);
00232     DEBUG("\tHours in BCD: 0x%02x", value)
00233     std::cout << "\tHours in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
00234     // Get seconds in BCD format
00235     g_myRTC.Read(CDS130X_I2C::DayAddress, &value);
00236     DEBUG("\tDay in BCD: 0x%02x", value)
00237     std::cout << "\tDay in BCD: 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << (unsigned char)value << "\r" << std::endl;
00238 }
00239 
00240 void WriteStdStringValue(const unsigned char p_address, const std::string & p_string) {
00241     std::cout << "Write string '" << p_string << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
00242     g_myRTC.WriteMemory(p_address, p_string);
00243 }
00244 
00245 void ReadStdStringValue(const unsigned char p_address) {
00246     std::string str;
00247     g_myRTC.ReadMemory(p_address, str);
00248     std::cout << "String at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << " is: " << str << "\r" << std::endl;
00249 }
00250 
00251 void WriteShortValue(const unsigned char p_address, const short p_short, CDS130X_I2C::Mode p_mode) {
00252     std::cout << "Write short '" << p_short << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
00253     if (p_mode == CDS130X_I2C::BigEndian) {
00254         g_myRTC.WriteMemory(p_address, p_short);
00255     } else {
00256         g_myRTC.WriteMemory(p_address, p_short, p_mode);
00257     }
00258 }
00259 
00260 void ReadShortValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode) {
00261     short value;
00262     if (p_mode == CDS130X_I2C::BigEndian) {
00263         g_myRTC.ReadMemory(p_address, (short *)&value);
00264     } else {
00265         g_myRTC.ReadMemory(p_address, (short *)&value, p_mode);
00266     }
00267     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;
00268 }
00269 
00270 void WriteIntegerValue(const unsigned char p_address, const int p_int, CDS130X_I2C::Mode p_mode) {
00271     std::cout << "Write integer '" << p_int << "' at address 0x" << std::setw(2) << std::setfill('0') << std::ios::hex << p_address << "\r" << std::endl;
00272     g_myRTC.WriteMemory(p_address, p_int, p_mode);
00273 }
00274 
00275 void ReadIntegerValue(const unsigned char p_address, CDS130X_I2C::Mode p_mode) {
00276     int value;
00277     if (p_mode == CDS130X_I2C::BigEndian) {
00278         g_myRTC.ReadMemory(p_address, (int *)&value);
00279     } else {
00280         g_myRTC.ReadMemory(p_address, (int *)&value, p_mode);
00281     }
00282     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;
00283 }
00284