Kenji Arai / Mbed 2 deprecated Nucleo_RTC_Clock_setting

Dependencies:   mbed CheckRTC

Revision:
2:8b6a8cfa173a
Parent:
1:eaa3c3f36c03
Child:
3:921a09e41147
--- a/main.cpp	Sat Oct 25 05:42:45 2014 +0000
+++ b/main.cpp	Sat Nov 01 00:35:50 2014 +0000
@@ -1,12 +1,11 @@
 /*
  * mbed Application program
- *      RTC Clock test
  *
  *  Copyright (c) 2010-2014 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
- *      Created:  October   24th, 2014
- *      Revised:  October   25th, 2014
+ *      Created:  March     27th, 2010
+ *      Revised:  November   1st, 2014
  *
  * 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
@@ -15,187 +14,298 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+//#define USE_TEXT_LCD
+
 //  Include ---------------------------------------------------------------------------------------
 #include "mbed.h"
-#include "rtc_api.h"
-
-//  Definition ------------------------------------------------------------------------------------
-#define STANDARD_FUNCTION // Use standard library prepared by STMicroelectronics
-//#define SPECIAL_FUNCTION  // Use special function. Time out is much longer than standard function
-
-#define BAUD(x)         pc.baud(x)
-#define GETC(x)         pc.getc(x)
-#define PUTC(x)         pc.putc(x)
-#define PRINTF(...)     pc.printf(__VA_ARGS__)
-#define READABLE(x)     pc.readable(x)
-
-#define TIMEOUT         ((uint32_t)5000)
+#ifdef USE_TEXT_LCD
+#include "TextLCD.h"
+#endif
+#include "CheckRTC.h"
 
 //  Object ----------------------------------------------------------------------------------------
 Serial pc(USBTX, USBRX);
-DigitalOut myled(LED1);
-Timer t;
+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
 
 //  RAM -------------------------------------------------------------------------------------------
 
 //  ROM / Constant data ---------------------------------------------------------------------------
 
 //  Function prototypes ---------------------------------------------------------------------------
-uint32_t Set_RTC_LSI(void);
-void rtc_external_osc_init(void);
-uint32_t Set_RTC_LSE(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);
+
+//  Definition ------------------------------------------------------------------------------------
+#define BAUD(x)                 pc.baud(x)
+#define GETC(x)                 pc.getc(x)
+#define PUTC(x)                 pc.putc(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
 
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
+//  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();
+}
+
 int main()
 {
-    while(true)
-    {
-        PRINTF("Select RTC clock source\r\n");
-        t.reset();
-        t.start();
-    #if defined(STANDARD_FUNCTION)
-        rtc_init();
-        t.stop();
-        PRINTF("Use standard function by STM\r\n");
-        PRINTF("The time taken was %f sec. (Time-out setting:%f sec.)\r\n",
-                t.read(), (float)LSE_TIMEOUT_VALUE/1000);
-    #elif defined(SPECIAL_FUNCTION)
-        rtc_external_osc_init();
-        t.stop();
-        PRINTF("Use special function (extend time-out)\r\n");
-        PRINTF("The time taken was %f sec. (Time-out setting:%f sec.)\r\n",
-                t.read(), (float)(float)TIMEOUT/1000);
-    #endif
-        switch ((RCC->BDCR >> 8) & 0x03) {
-            case 0: // no clock
-                PRINTF("No clock");
+    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();
+    seconds = time(NULL);
+    PRINTF("Current time is");
+    put_rn();
+#ifdef STYLE_COM
+    PRINTF("Time: %s", ctime(&seconds));
+#else
+    strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds));
+    PRINTF("Time: %s", 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();
+    for (;;) {
+        put_r();
+        PUTC('>');
+        ptr = linebuf;
+        get_line(ptr, sizeof(linebuf));
+        switch (*ptr++) {
+                //---------------------------------------------------------------------------------
+                //  check and set RTC
+                //---------------------------------------------------------------------------------
+            case 't' :
+                put_r();
+                chk_and_set_time(ptr);
                 break;
-            case 1: // LSE
-                PRINTF("Use LSE");
-                break;
-            case 2: // LSI
-                PRINTF("Use LSI");
+                //---------------------------------------------------------------------------------
+                //  check and set RTC
+                //---------------------------------------------------------------------------------
+            case '/' :
+                put_r();
+                PRINTF("Current Time -> Plese see LCD also");
+                put_rn();
+                while (1) {
+                    if (READABLE()) {
+                        break;
+                    }
+                    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
+                    strftime(buf,40, "%x %X ", localtime(&seconds));
+#endif
+#ifdef STYLE2
+                    //                 13:24:00 PM (2010/03/27)
+                    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));
+#else
+                    PRINTF("Time: %s", buf);
+#endif
+                    put_rn();
+                }
                 break;
-            case 3: // HSE
-                PRINTF("Use HSE");
-                break;
-            default:    // Not come here
-                PRINTF("?");
+                //---------------------------------------------------------------------------------
+                //  check and set RTC
+                //---------------------------------------------------------------------------------
+            case '?' :
+            default :
+                put_r();
+                msg_hlp();
                 break;
         }
-        PRINTF("\r\n");
-        Set_RTC_LSI();
-        if (((RCC->BDCR >> 8) & 0x03) != 2) {
-            PRINTF("Please remove USB then re-plug again");
-        } else {
-            PRINTF("Use LSI again", (float)TIMEOUT/1000);
-        }
-        PRINTF("\r\nRepeat again? [y/n]\r\n");
-        PRINTF(" If yes, please pusch [RESET] buttom.\r\n If no, please hit any key.\r\n");
-        if (GETC()){
-            break;
-        }
     }
-    PRINTF("next\r\n");
-#if defined(STANDARD_FUNCTION)
-    rtc_init();
-#elif defined(SPECIAL_FUNCTION)
-    rtc_external_osc_init();
-#endif 
+}
+
+//  Put \r\n
+void put_rn (void)
+{
+    PUTC('\r');
+    PUTC('\n');
+}
+
+//  Put \r
+void put_r (void)
+{
+    PUTC('\r');
 }
 
-uint32_t Set_RTC_LSE(void)
+//  Change string -> number
+int xatoi (char **str, int32_t *res)
 {
-    uint32_t timeout = 0;   
- 
-    //---------------------------- LSE Configuration ------------------------- 
-    // Enable Power Clock
-    __PWR_CLK_ENABLE();
-    // Enable write access to Backup domain 
-    PWR->CR |= PWR_CR_DBP;
-    // Wait for Backup domain Write protection disable 
-    timeout = HAL_GetTick() + DBP_TIMEOUT_VALUE;
-    while((PWR->CR & PWR_CR_DBP) == RESET){
-        if(HAL_GetTick() >= timeout){
-            return 0;
-        }      
+    unsigned long val;
+    unsigned char c, radix, s = 0;
+
+    while ((c = **str) == ' ') {
+        (*str)++;
+    }
+    if (c == '-') {
+        s = 1;
+        c = *(++(*str));
     }
-    // Reset LSEON and LSEBYP bits before configuring the LSE ----------------
-    __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
-    // Get timeout 
-    timeout = HAL_GetTick() + ((uint32_t)5000);
-    // Wait till LSE is ready   
-    while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET){
-        if(HAL_GetTick() >= timeout) {
+    if (c == '0') {
+        c = *(++(*str));
+        if (c <= ' ') {
+            *res = 0;
+            return 1;
+        }
+        if (c == 'x') {
+            radix = 16;
+            c = *(++(*str));
+        } else {
+            if (c == 'b') {
+                radix = 2;
+                c = *(++(*str));
+            } else {
+                if ((c >= '0')&&(c <= '9')) {
+                    radix = 8;
+                }   else {
+                    return 0;
+                }
+            }
+        }
+    } else {
+        if ((c < '1')||(c > '9')) {
             return 0;
-        }      
-    } 
-    // Set the new LSE configuration -----------------------------------------
-    __HAL_RCC_LSE_CONFIG(RCC_LSE_ON);
-    // Get timeout 
-    timeout = HAL_GetTick() + ((uint32_t)5000);
-    // Wait till LSE is ready   
-    while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET){
-        if(HAL_GetTick() >= timeout){
+        }
+        radix = 10;
+    }
+    val = 0;
+    while (c > ' ') {
+        if (c >= 'a') {
+            c -= 0x20;
+        }
+        c -= '0';
+        if (c >= 17) {
+            c -= 7;
+            if (c <= 9) {
+                return 0;
+            }
+        }
+        if (c >= radix) {
             return 0;
-        }      
+        }
+        val = val * radix + c;
+        c = *(++(*str));
     }
+    if (s) {
+        val = -val;
+    }
+    *res = val;
     return 1;
 }
 
-void rtc_external_osc_init(void)
+//  Get key input data
+void get_line (char *buff, int len)
 {
-    // Enable Power clock
-    __PWR_CLK_ENABLE();
-    // Enable access to Backup domain
-    HAL_PWR_EnableBkUpAccess();
-    // Reset Backup domain
-    __HAL_RCC_BACKUPRESET_FORCE();
-    __HAL_RCC_BACKUPRESET_RELEASE();
-    // Enable LSE Oscillator
-    if (Set_RTC_LSE()) {
-        // Connect LSE to RTC
-        __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
-        __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
+    char c;
+    int idx = 0;
+
+    for (;;) {
+        c = GETC();
+        if (c == '\r') {
+            buff[idx++] = c;
+            break;
+        }
+        if ((c == '\b') && idx) {
+            idx--;
+            PUTC(c);
+            PUTC(' ');
+            PUTC(c);
+        }
+        if (((uint8_t)c >= ' ') && (idx < len - 1)) {
+            buff[idx++] = c;
+            PUTC(c);
+        }
     }
+    buff[idx] = 0;
+    PUTC('\n');
 }
 
-uint32_t Set_RTC_LSI(void)
+// RTC related subroutines
+void chk_and_set_time(char *ptr)
 {
-    uint32_t timeout = 0;
+    int32_t p1;
+    struct tm t;
+    time_t seconds;
+    char buf[40];
 
-    // Enable Power clock
-    __PWR_CLK_ENABLE();
-    // Enable access to Backup domain
-    HAL_PWR_EnableBkUpAccess();
-    // Reset Backup domain
-    __HAL_RCC_BACKUPRESET_FORCE();
-    __HAL_RCC_BACKUPRESET_RELEASE();
-    // Enable Power Clock
-    __PWR_CLK_ENABLE();
-    // Enable write access to Backup domain
-    PWR->CR |= PWR_CR_DBP;
-    // Wait for Backup domain Write protection disable
-    timeout = HAL_GetTick() + DBP_TIMEOUT_VALUE;
-    while((PWR->CR & PWR_CR_DBP) == RESET) {
-        if(HAL_GetTick() >= timeout) {
-            return 0;
-        }
+    if (xatoi(&ptr, &p1)) {
+        t.tm_year       = (uint8_t)p1 + 100;
+        PRINTF("Year:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_mon        = (uint8_t)p1 - 1;
+        PRINTF("Month:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_mday       = (uint8_t)p1;
+        PRINTF("Day:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_hour       = (uint8_t)p1;
+        PRINTF("Hour:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_min        = (uint8_t)p1;
+        PRINTF("Min:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_sec        = (uint8_t)p1;
+        PRINTF("Sec: %d",p1);
+        put_rn();
+        seconds = mktime(&t);
+        set_time(seconds);
     }
-    __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
-    // Enable LSI 
-    __HAL_RCC_LSI_ENABLE();
-    timeout = HAL_GetTick() + TIMEOUT;
-    // Wait till LSI is ready
-    while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) {
-        if(HAL_GetTick() >= timeout) {
-            return 0;
-        }
-    }
-    // Connect LSI to RTC
-    __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
-    __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);    
-    return 1;
+    seconds = time(NULL);
+    strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
+    PRINTF("Time: %s", buf);
+    put_rn();
 }