Check RTC LSE(External Xtal, 32.768kHz) mode

Dependencies:   mbed CheckRTC

Please refer follow links.
My notebook
http://developer.mbed.org/users/kenjiArai/notebook/nucleo-series-clock-structure-and-xtal-oscillation/
Q&A for RTC Xtal
http://developer.mbed.org/questions/4531/RTC-External-Crystal-Nucleo/

Revision:
4:56a0cbad89b0
Parent:
3:921a09e41147
--- a/main.cpp	Sat Nov 01 01:48:56 2014 +0000
+++ b/main.cpp	Sat May 16 23:38:13 2015 +0000
@@ -1,11 +1,11 @@
 /*
  * mbed Application program
  *
- *  Copyright (c) 2010-2014 Kenji Arai / JH1PJL
+ *  Copyright (c) 2010-2015 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created:  March     27th, 2010
- *      Revised:  November   1st, 2014
+ *      Revised:  May       16th, 2015
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -14,24 +14,13 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-//#define USE_TEXT_LCD
-
-#define CHECK_RTC
-//#define SET_RTC
-
 //  Include ---------------------------------------------------------------------------------------
 #include "mbed.h"
-#ifdef USE_TEXT_LCD
-#include "TextLCD.h"
-#endif
 #include "CheckRTC.h"
 
 //  Object ----------------------------------------------------------------------------------------
 Serial pc(USBTX, USBRX);
 DigitalOut myled1(LED1);                                // Assign LED1 output port
-#ifdef USE_TEXT_LCD
-TextLCD lcd(p22, p21, p8, p7, p6, p5,TextLCD::LCD40x2);  // rs, e, d4-d7
-#endif
 Timer t;
 
 //  RAM -------------------------------------------------------------------------------------------
@@ -39,10 +28,9 @@
 //  ROM / Constant data ---------------------------------------------------------------------------
 
 //  Function prototypes ---------------------------------------------------------------------------
+int set_time_and_check_time(void);
 void msg_hlp (void);
 void chk_and_set_time(char *ptr);
-void put_rn (void);
-void put_r (void);
 int xatoi (char **str, int32_t *res);
 void get_line (char *buff, int len);
 
@@ -50,19 +38,10 @@
 #define BAUD(x)                 pc.baud(x)
 #define GETC(x)                 pc.getc(x)
 #define PUTC(x)                 pc.putc(x)
+#define PUTS(x)                 pc.puts(x)
 #define PRINTF(...)             pc.printf(__VA_ARGS__)
 #define READABLE(x)             pc.readable(x)
 
-#ifdef USE_TEXT_LCD
-#define L_CLS(x)                lcd.cls(x) 
-#define L_LOC(x,y)              lcd.locate(0, 0);
-#define L_PRINTF(...)           lcd.printf(__VA_ARGS__)
-#else
-#define L_CLS(x)                {;} 
-#define L_LOC(x,y)              {;}
-#define L_PRINTF(...)           {;}
-#endif
-
 #define STYLE1
 //#define STYLE2
 //#define STYLE_COM
@@ -70,96 +49,109 @@
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
-#if defined(CHECK_RTC)
 #if defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_L152RE)
 int main()
 {
-    while(true)
-    {
-        while (!READABLE()){
-            PRINTF("Please hit any key to start measure\r");
-        }
-        PRINTF("\r\nSelect RTC clock source\r\n");
-        t.reset();
-        t.start();
-        CheckRTC();
-        t.stop();
-        PRINTF("Use special function (extend time-out)\r\n");
-        PRINTF("Start-up time was %f sec. (Time-out setting:%f sec.)\r\n",
-                t.read(), (float)(float)TIMEOUT/1000);
-        switch (get_RTCSEL()) {
+    uint32_t state;
+
+    while(true) {
+        PUTS("\r\n\r\nCurrent Nucleo mbed Power-on sequence does NOT consider ");
+        PUTS("External Xtal start-up.\r\n");
+        PUTS("Setting time is 5 seconds (improved) ");
+        PUTS("but sometimes External Xtal 32.768KHz (LSE) does NOT work!\r\n");
+        PUTS("Current setting: ");
+        state = get_RTCSEL();
+        switch (state) {
             case 0: // no clock
-                PRINTF("No clock");
+                PUTS("No clock");
                 break;
             case 1: // LSE
-                PRINTF("Use LSE");
+                PUTS("Use LSE(external Xtal)=32.768KHz");
                 break;
             case 2: // LSI
-                PRINTF("Use LSI");
+                PUTS("Use LSI(internal RC/Low speed), RC = 17 to 47, typ.32KHz");
                 break;
             case 3: // HSE
-                PRINTF("Use HSE");
+                PUTS("Use HSE(with prescaler)");
                 break;
             default:    // Not come here
-                PRINTF("?");
+                PRINTF("No clock");
                 break;
         }
-        PRINTF("\r\n");
-        CheckRTC();
-        if (get_RTCSEL() == 2) {
-            PRINTF("Use LSI again");
+        PUTS("\r\n\r\n");
+        if (state == 1) {
+            PUTS("LSE is using! No addtional effort but just in case please try follows.\r\n");
+        } else {
+            PUTS("Please hit any key to use RTC external clock.\r\n");
+            while (!READABLE()) {
+                wait(0.2);
+            }
+            PRINTF("\r\nStart measure\r\n");
+            t.reset();
+            t.start();
+            CheckRTC();
+            t.stop();
+            PRINTF("Start-up time was %f sec. (Time-out setting:%f sec.)\r\n",
+                   t.read(), (float)(float)TIMEOUT/1000);
+            PUTS("\r\n");
+            state = get_RTCSEL();
+            switch (state) {
+                case 0: // no clock
+                    PRINTF("No clock");
+                    break;
+                case 1: // LSE
+                    PRINTF("Use LSE");
+                    break;
+                case 2: // LSI
+                    PRINTF("Use LSI");
+                    break;
+                case 3: // HSE
+                    PRINTF("Use HSE");
+                    break;
+                default:    // Not come here
+                    PRINTF("?");
+                    break;
+            }
+            PRINTF("\r\n");
+            CheckRTC();
+            if (state == 1) {
+                PUTS("Looks okay right now but always okay or not?!\r\n");
+            }
         }
-        PRINTF("\r\nRepeat again? [y/n]\r\n");
-        PRINTF(" If yes, please pusch [RESET] buttom.\r\n");
-        PRINTF("   recomend to remove USB connecter then re-plug it.\r\n");
-        PRINTF(" If no, please hit any key.\r\n");
-        if (GETC()){
-            break;
-        }
+        PUTS("If you would like to masure again,");
+        PUTS("Please remove JP6/IDO on the mbed board then re-plug it again!.\r\n\r\n");
+        set_time_and_check_time();
     }
-    PRINTF("next\r\n");
-    CheckRTC();
 }
 
 #else
 #warning "Cannot use this function. Please comment out #define CHECK_RTC and use #define SET_RTC
 #endif
-#elif defined(SET_RTC)  // defined(CHECK_RTC)
 
-int main()
+int set_time_and_check_time()
 {
     char *ptr;
     char linebuf[64];
     char buf[40];
     time_t seconds;
 
-    CheckRTC();
-    L_CLS();
-    L_LOC(0, 0);
-    L_LOC(0, 0);   // 1st line top
-    //          1234567890123456789012345678901234567890
-    L_PRINTF("  Waiting for time adjustment via com ");
-    put_rn();
+    if (READABLE()) {
+        GETC();
+    }
     seconds = time(NULL);
-    PRINTF("Current time is");
-    put_rn();
+    PRINTF("Current time is ");
 #ifdef STYLE_COM
-    PRINTF("Time: %s", ctime(&seconds));
+    PRINTF("Time: %s\r\n", ctime(&seconds));
 #else
     strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds));
-    PRINTF("Time: %s", buf);
+    PRINTF("Time: %s\r\n", buf);
 #endif
-    put_rn();
-    PRINTF("Is it correct time?");
-    put_rn();
-    PRINTF("YES -> please enter '/'");
-    put_rn();
-    PRINTF("NO -> please enter t yy mm dd hh mm ss <ret>");
-    put_rn();
-    PRINTF("e.g. >t 14 11 1 8 5 15<ret>");
-    put_rn();
+    PRINTF("Is it correct time?\r\n");
+    PRINTF("YES -> please enter '/'\r\n");
+    PRINTF("NO -> please enter t yy mm dd hh mm ss <ret>   ");
+    PRINTF("e.g. >t 15 5 17 10 5 15<ret>\r\n");
     for (;;) {
-        put_r();
+        PUTC('\r');
         PUTC('>');
         ptr = linebuf;
         get_line(ptr, sizeof(linebuf));
@@ -168,16 +160,15 @@
                 //  check and set RTC
                 //---------------------------------------------------------------------------------
             case 't' :
-                put_r();
+                PUTC('\r');
                 chk_and_set_time(ptr);
                 break;
                 //---------------------------------------------------------------------------------
                 //  check and set RTC
                 //---------------------------------------------------------------------------------
             case '/' :
-                put_r();
-                PRINTF("Current Time -> Plese see LCD also");
-                put_rn();
+                PUTC('\r');
+                PRINTF("Current Time -> Plese see LCD also\r\n");
                 while (1) {
                     if (READABLE()) {
                         break;
@@ -185,25 +176,19 @@
                     while ( seconds == time(NULL)) ;
                     seconds = time(NULL);
                     myled1 = !myled1;
-                    L_CLS();
-                    L_LOC(0, 0);   // 1st line top
-                    L_PRINTF("It is %d sec since Jan.1,1970\n", seconds);
-                    L_LOC(0, 1);   // 2nd line top
 #ifdef STYLE1
-                    //                  27 Mar 2010 13:24:00
+                    //                  17 May 2015 13:24:00
                     strftime(buf,40, "%x %X ", localtime(&seconds));
 #endif
 #ifdef STYLE2
-                    //                 13:24:00 PM (2010/03/27)
+                    //                 13:24:00 PM (2015/05/17)
                     strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds));
 #endif
-                    L_PRINTF("Time = %s", buf);
 #ifdef STYLE_COM
-                    PRINTF("Time: %s", ctime(&seconds));
+                    PRINTF("Time: %s\r\n", ctime(&seconds));
 #else
-                    PRINTF("Time: %s", buf);
+                    PRINTF("Time: %s\r\n", buf);
 #endif
-                    put_rn();
                 }
                 break;
                 //---------------------------------------------------------------------------------
@@ -211,37 +196,19 @@
                 //---------------------------------------------------------------------------------
             case '?' :
             default :
-                put_r();
+                PUTC('\r');
                 msg_hlp();
                 break;
         }
     }
 }
 
-#endif  // defined(SET_RTC)
-
 //  Help Massage
 void msg_hlp (void)
 {
-    PRINTF("t - Check and set RTC");
-    put_rn();
-    PRINTF("/ - Show time every second (Esc -> hit any key)");
-    put_rn();
-    PRINTF("? - Help");
-    put_rn();
-}
-
-//  Put \r\n
-void put_rn (void)
-{
-    PUTC('\r');
-    PUTC('\n');
-}
-
-//  Put \r
-void put_r (void)
-{
-    PUTC('\r');
+    PUTS("t - Check and set RTC\r\n");
+    PUTS("/ - Show time every second (Esc -> hit any key)\r\n");
+    PUTS("? - Help\r\n");
 }
 
 //  Change string -> number
@@ -361,13 +328,11 @@
         PRINTF("Min:%d ",p1);
         xatoi( &ptr, &p1 );
         t.tm_sec        = (uint8_t)p1;
-        PRINTF("Sec: %d",p1);
-        put_rn();
+        PRINTF("Sec: %d\r\n",p1);
         seconds = mktime(&t);
         set_time(seconds);
     }
     seconds = time(NULL);
     strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
-    PRINTF("Time: %s", buf);
-    put_rn();
+    PRINTF("Time: %s\r\n", buf);
 }