Several examples run on only mbed-os5.13.0 (not 5.14.0)

Dependencies:   BD_SD_DISCO_F769NI BSP_DISCO_F769NI LCD_DISCO_F769NI TS_DISCO_F769NI USBHost_F769NI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 1_rtc.cpp Source File

1_rtc.cpp

00001 /*
00002  * Mbed Application program / check RTC (Real Time Clock)
00003  *
00004  * Copyright (c) 2017,'18,'19 Kenji Arai / JH1PJL
00005  *  http://www.page.sannet.ne.jp/kenjia/index.html
00006  *  https://os.mbed.com/users/kenjiArai/
00007  *      Created:    August    11th, 2017
00008  *      Revised:    October   14th, 2019
00009  */
00010 
00011 #include "select_program.h"
00012 //#define EXAMPLE_1_CHECK_RTC
00013 #ifdef EXAMPLE_1_CHECK_RTC
00014 
00015 //  Include --------------------------------------------------------------------
00016 #include "mbed.h"
00017 
00018 //  Definition -----------------------------------------------------------------
00019 #define     USER_SW_ON      1
00020 
00021 //  Constructor ----------------------------------------------------------------
00022 DigitalIn   userSW(BUTTON1);
00023 DigitalOut  myled(LED1);                // Indicate the sampling period
00024 Serial pc(USBTX, USBRX, 115200);
00025 
00026 //  RAM ------------------------------------------------------------------------
00027 
00028 //  ROM / Constant data --------------------------------------------------------
00029 const char * msg0  = "Is a time correct? If no, please hit any key. ";
00030 const char * msg1  = "<Push USER SW then enter sleep mode> ";
00031 const char * msg2  = "\r\nEnter Standby Mode, please push RESET to wake-up\r\n";
00032 
00033 //  Function prototypes --------------------------------------------------------
00034 static void goto_standby(void);
00035 static void time_enter_mode(void);
00036 static void chk_and_set_time(char *ptr);
00037 static int  xatoi (char **str, unsigned long *res);
00038 static void get_line (char *buff, int len);
00039 
00040 //------------------------------------------------------------------------------
00041 //  Control Program
00042 //------------------------------------------------------------------------------
00043 int main()
00044 {
00045     char buf[64];               // data buffer for text
00046     time_t seconds;
00047     uint8_t wait_counter = 0;
00048 
00049     pc.printf("\x1b[2J\x1b[H %s\r\n %s %s (UTC)\r\n",
00050               __FILE__, __DATE__, __TIME__);
00051     printf(" RTC EXAMPLE FOR DISCO-F769NI:\r\n");
00052     myled = !myled;
00053     ThisThread::sleep_for(1000);
00054     myled = !myled;
00055     ThisThread::sleep_for(1000);
00056     while(1) {
00057         seconds = time(NULL);
00058         strftime(buf, 50, " %B %d,'%y, %H:%M:%S,", localtime(&seconds));
00059         pc.printf("[Time] %s", buf);
00060         pc.printf(" sec=%d\r\n", seconds);
00061         pc.printf("%s", msg0);
00062         pc.printf("%s\r", msg1);
00063         wait_counter = 0;
00064         while (seconds == time(NULL)) {
00065             if (pc.readable()) {
00066                 buf[0] = pc.getc();  // dummy read
00067                 time_enter_mode();
00068             }
00069             if (userSW == USER_SW_ON) {
00070                 pc.printf("%s", msg2);
00071                 ThisThread::sleep_for(1000);
00072                 myled = 0;
00073                 goto_standby();
00074             }
00075             ThisThread::sleep_for(50);
00076             if (++wait_counter > (2000 / 50)) {
00077                 break;
00078             }
00079         }
00080         uint8_t n = strlen(msg0) + strlen(msg1);
00081         for (uint8_t i = 0; i < n; i++) {
00082             pc.putc(' ');
00083         }
00084         pc.printf("      \r"); // Not use '\n'
00085         myled = !myled;
00086     }
00087 }
00088 
00089 void goto_standby(void)
00090 {
00091     ThisThread::sleep_for(0x7fffffff);    // sleep 24 days
00092 }
00093 
00094 void time_enter_mode(void)
00095 {
00096     char *ptr;
00097     char linebuf[64];
00098 
00099     pc.printf("\r\nSet time into RTC\r\n");
00100     pc.printf(" e.g. >19 10 14 10 11 12 -> October 14th,'19, 10:11:12\r\n");
00101     pc.printf(" If time is fine, just hit enter\r\n");
00102     pc.putc('>');
00103     ptr = linebuf;
00104     get_line(ptr, sizeof(linebuf));
00105     pc.printf("\r");
00106     chk_and_set_time(ptr);
00107 }
00108 
00109 //  Get key input data
00110 void get_line (char *buff, int len)
00111 {
00112     char c;
00113     int idx = 0;
00114 
00115     for (;;) {
00116         c = pc.getc();
00117         if (c == '\r') {
00118             buff[idx++] = c;
00119             break;
00120         }
00121         if ((c == '\b') && idx) {
00122             idx--;
00123             pc.putc(c);
00124             pc.putc(' ');
00125             pc.putc(c);
00126         }
00127         if (((uint8_t)c >= ' ') && (idx < len - 1)) {
00128             buff[idx++] = c;
00129             pc.putc(c);
00130         }
00131     }
00132     buff[idx] = 0;
00133     pc.putc('\n');
00134 }
00135 
00136 void chk_and_set_time(char *ptr)
00137 {
00138     unsigned long p1;
00139     struct tm t;
00140     time_t seconds;
00141 
00142     if (xatoi(&ptr, &p1)) {
00143         t.tm_year       = (uint8_t)p1 + 100;
00144         pc.printf("Year:%d ",(int)p1);
00145         xatoi( &ptr, &p1 );
00146         t.tm_mon        = (uint8_t)p1 - 1;
00147         pc.printf("Month:%d ",(int)p1);
00148         xatoi( &ptr, &p1 );
00149         t.tm_mday       = (uint8_t)p1;
00150         pc.printf("Day:%d ",(int)p1);
00151         xatoi( &ptr, &p1 );
00152         t.tm_hour       = (uint8_t)p1;
00153         pc.printf("Hour:%d ",(int)p1);
00154         xatoi( &ptr, &p1 );
00155         t.tm_min        = (uint8_t)p1;
00156         pc.printf("Min:%d ",(int)p1);
00157         xatoi( &ptr, &p1 );
00158         t.tm_sec        = (uint8_t)p1;
00159         pc.printf("Sec:%d \r\n",(int)p1);
00160     } else {
00161         return;
00162     }
00163     seconds = mktime(&t);
00164     set_time(seconds);
00165     pc.printf(
00166         "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n",
00167         t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
00168         t.tm_hour, t.tm_min, t.tm_sec
00169     );
00170 }
00171 
00172 //  Change string -> integer
00173 int xatoi (char **str, unsigned long *res)
00174 {
00175     unsigned long val;
00176     unsigned char c, radix, s = 0;
00177 
00178     while ((c = **str) == ' ') {
00179         (*str)++;
00180     }
00181     if (c == '-') {
00182         s = 1;
00183         c = *(++(*str));
00184     }
00185     if (c == '0') {
00186         c = *(++(*str));
00187         if (c <= ' ') {
00188             *res = 0;
00189             return 1;
00190         }
00191         if (c == 'x') {
00192             radix = 16;
00193             c = *(++(*str));
00194         } else {
00195             if (c == 'b') {
00196                 radix = 2;
00197                 c = *(++(*str));
00198             } else {
00199                 if ((c >= '0')&&(c <= '9')) {
00200                     radix = 8;
00201                 } else {
00202                     return 0;
00203                 }
00204             }
00205         }
00206     } else {
00207         if ((c < '1')||(c > '9')) {
00208             return 0;
00209         }
00210         radix = 10;
00211     }
00212     val = 0;
00213     while (c > ' ') {
00214         if (c >= 'a') {
00215             c -= 0x20;
00216         }
00217         c -= '0';
00218         if (c >= 17) {
00219             c -= 7;
00220             if (c <= 9) {
00221                 return 0;
00222             }
00223         }
00224         if (c >= radix) {
00225             return 0;
00226         }
00227         val = val * radix + c;
00228         c = *(++(*str));
00229     }
00230     if (s) {
00231         val = -val;
00232     }
00233     *res = val;
00234     return 1;
00235 }
00236 
00237 #endif