Sample application for the WakeUp_STM32 library. This is a Fork from Kenji Arai Link: https://os.mbed.com/users/kenjiArai/code/Check_StandBy_os2/

Dependencies:   mbed WakeUp_STM32

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Mbed Application program
00003  *  Check Deep Sleep Mode
00004  *
00005  * Copyright (c) 2020 Kenji Arai / JH1PJL
00006  *  http://www7b.biglobe.ne.jp/~kenjia/
00007  *  https://os.mbed.com/users/kenjiArai/
00008  *      Revised:    March     12th, 2020
00009  *      Revised:    March     13th, 2020
00010  */
00011 
00012 /*
00013     Reference information:
00014         https://forums.mbed.com/t/how-to-deep-sleep/7551
00015 
00016     Tested on
00017         Nucleo-F401RE
00018         Nucleo-F411RE
00019         Nucleo-F446RE
00020         Nucleo-L053R8   only os2
00021         Nucleo-L073RZ   only os2
00022         Nucleo-L152RE
00023  */
00024 
00025 //  Include --------------------------------------------------------------------
00026 #include "mbed.h"
00027 #include "WakeUp_STM32.h"
00028 
00029 //  Definition -----------------------------------------------------------------
00030 //      https://keisan.casio.jp/exec/system/1526003938
00031 #define DATE_20200222_222222    1582377742      // 2020/2/22 22:22:22 
00032 
00033 #if (MBED_MAJOR_VERSION == 5)
00034 #define WAIT_MS(x)  ThisThread::sleep_for(x)
00035 #else
00036 #define WAIT_MS(x)  wait_ms(x)
00037 #endif
00038 
00039 //  Constructor ----------------------------------------------------------------
00040 DigitalIn   my_sw(USER_BUTTON);
00041 DigitalOut  myled(LED1,1);
00042 Serial      pc(USBTX, USBRX);
00043 AnalogIn    a_in(A0);
00044 Timer       t;
00045 
00046 //  RAM ------------------------------------------------------------------------
00047 
00048 //  ROM / Constant data --------------------------------------------------------
00049 
00050 //  Function prototypes --------------------------------------------------------
00051 void sw_irq(void);
00052 void time_enter_mode(void);
00053 void chk_and_set_time(char *ptr);
00054 int32_t  xatoi (char **str, int32_t *res);
00055 void get_line (char *buff, int len);
00056 void print_revision(void);
00057 
00058 //------------------------------------------------------------------------------
00059 //  Control Program
00060 //------------------------------------------------------------------------------
00061 int main()
00062 {
00063     pc.baud(115200);          // Set baudrate for UART
00064     time_t seconds;
00065     char buf[64];
00066     uint32_t t_pass = 0;
00067     uint32_t loop_count = 1;
00068     float ain;
00069     printf("\r\nCheck current consumption at Deep-sleep mode.\r\n");
00070     print_revision();
00071     seconds = time(NULL);
00072     if (seconds < DATE_20200222_222222) {
00073         strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds));
00074         pc.printf("[Time] %s\r\n", buf);
00075         time_enter_mode();
00076     }
00077     while (my_sw == 0) {;}
00078     WAIT_MS(10);
00079     while (true) {
00080         t.reset();
00081         t.start();
00082         if ((my_sw == 0) || (loop_count > 20)) {
00083             DigitalIn dmy0(LED1);
00084             DigitalIn dmy1(USBTX);
00085             DigitalIn dmy2(USBRX);
00086             WakeUp::standby_then_reset(10000);
00087             //--- I = 3.4 uA here ----  
00088             while(true) {;}         // never executing this line
00089         }
00090         //--- Imax = 50.4 mA here ----   
00091         ain = a_in.read();
00092         myled = !myled;
00093         seconds = time(NULL);
00094         strftime(buf, 50, "%H:%M:%S -> ", localtime(&seconds));
00095         pc.printf("%s", buf);
00096         pc.printf(
00097             "analog = %4.3f, loop_time=%3d, counter=%4d\r\n",
00098             ain, t_pass, loop_count++
00099         );
00100         t_pass = t.read_ms();
00101         WAIT_MS(1000 - t_pass);
00102     }
00103 }
00104 
00105 void time_enter_mode(void)
00106 {
00107     char *ptr;
00108     char linebuf[64];
00109 
00110     pc.printf("\r\nSet time into RTC\r\n");
00111     pc.printf(" e.g. >20 2 22 22 22 22 -> February 22,'20, 22:22:22\r\n");
00112     pc.putc('>');
00113     ptr = linebuf;
00114     get_line(ptr, sizeof(linebuf));
00115     pc.printf("\r");
00116     chk_and_set_time(ptr);
00117 }
00118 
00119 void get_line (char *buff, int len)
00120 {
00121     char c;
00122     uint32_t idx = 0;
00123 
00124     while(true) {
00125         c = pc.getc();
00126         if (c == '\r') {
00127             buff[idx++] = c;
00128             break;
00129         }
00130         if ((c == '\b') && idx) {
00131             idx--;
00132             pc.putc(c);
00133             pc.putc(' ');
00134             pc.putc(c);
00135         }
00136         if (((uint8_t)c >= ' ') && (idx < len - 1)) {
00137             buff[idx++] = c;
00138             pc.putc(c);
00139         }
00140     }
00141     buff[idx] = 0;
00142     pc.puts("\r\n");
00143 }
00144 
00145 void chk_and_set_time(char *ptr)
00146 {
00147     int32_t p1;
00148     struct tm t;
00149     time_t seconds;
00150 
00151     if (xatoi(&ptr, &p1)) {
00152         t.tm_year       = (uint8_t)p1 + 100;
00153         pc.printf("Year:%d ",p1);
00154         xatoi( &ptr, &p1 );
00155         t.tm_mon        = (uint8_t)p1 - 1;
00156         pc.printf("Month:%d ",p1);
00157         xatoi( &ptr, &p1 );
00158         t.tm_mday       = (uint8_t)p1;
00159         pc.printf("Day:%d ",p1);
00160         xatoi( &ptr, &p1 );
00161         t.tm_hour       = (uint8_t)p1;
00162         pc.printf("Hour:%d ",p1);
00163         xatoi( &ptr, &p1 );
00164         t.tm_min        = (uint8_t)p1;
00165         pc.printf("Min:%d ",p1);
00166         xatoi( &ptr, &p1 );
00167         t.tm_sec        = (uint8_t)p1;
00168         pc.printf("Sec: %d \r\n",p1);
00169     } else {
00170         return;
00171     }
00172     seconds = mktime(&t);
00173     set_time(seconds);
00174     pc.printf(
00175         "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n",
00176         t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec
00177     );
00178 }
00179 
00180 int32_t xatoi (char **str, int32_t *res)
00181 {
00182     int32_t val;
00183     uint8_t c, radix, s = 0;
00184 
00185     while ((c = **str) == ' ') {
00186         (*str)++;
00187     }
00188     if (c == '-') {
00189         s = 1;
00190         c = *(++(*str));
00191     }
00192     if (c == '0') {
00193         c = *(++(*str));
00194         if (c <= ' ') {
00195             *res = 0;
00196             return 1;
00197         }
00198         if (c == 'x') {
00199             radix = 16;
00200             c = *(++(*str));
00201         } else {
00202             if (c == 'b') {
00203                 radix = 2;
00204                 c = *(++(*str));
00205             } else {
00206                 if ((c >= '0')&&(c <= '9')) {
00207                     radix = 8;
00208                 } else {
00209                     return 0;
00210                 }
00211             }
00212         }
00213     } else {
00214         if ((c < '1')||(c > '9')) {
00215             return 0;
00216         }
00217         radix = 10;
00218     }
00219     val = 0;
00220     while (c > ' ') {
00221         if (c >= 'a') {
00222             c -= 0x20;
00223         }
00224         c -= '0';
00225         if (c >= 17) {
00226             c -= 7;
00227             if (c <= 9) {
00228                 return 0;
00229             }
00230         }
00231         if (c >= radix) {
00232             return 0;
00233         }
00234         val = val * radix + c;
00235         c = *(++(*str));
00236     }
00237     if (s) {
00238         val = -val;
00239     }
00240     *res = val;
00241     return 1;
00242 }