Added Standby mode into WakeUp library. This is a checking program for the standby mode on Nucleo-F446RE and other STM32 series.
Fork of study_step0 by
Please refer notebook.
/users/kenjiArai/notebook/standby-mode-current-consumption-on-nucleo-f446re/#
main.cpp@1:abe49c3395cd, 2017-09-21 (annotated)
- Committer:
- kenjiArai
- Date:
- Thu Sep 21 22:10:34 2017 +0000
- Revision:
- 1:abe49c3395cd
- Parent:
- 0:1672d0903bdc
Added Standby mode into WakeUP library. This is a checking program for the standby mode on Nucleo-F446RE and other STM32 series.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:1672d0903bdc | 1 | /* |
kenjiArai | 1:abe49c3395cd | 2 | * Mbed Application program / check StandBy function |
kenjiArai | 0:1672d0903bdc | 3 | * |
kenjiArai | 0:1672d0903bdc | 4 | * Copyright (c) 2017 Kenji Arai / JH1PJL |
kenjiArai | 0:1672d0903bdc | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:1672d0903bdc | 6 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 1:abe49c3395cd | 7 | * Created: September 19th, 2017 |
kenjiArai | 1:abe49c3395cd | 8 | * Revised: September 21st, 2017 |
kenjiArai | 0:1672d0903bdc | 9 | */ |
kenjiArai | 0:1672d0903bdc | 10 | |
kenjiArai | 1:abe49c3395cd | 11 | #if defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F411RE)\ |
kenjiArai | 1:abe49c3395cd | 12 | || defined(TARGET_NUCLEO_F401RE)\ |
kenjiArai | 1:abe49c3395cd | 13 | || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L073RZ)\ |
kenjiArai | 1:abe49c3395cd | 14 | || defined(TARGET_NUCLEO_L053R8) |
kenjiArai | 1:abe49c3395cd | 15 | #else |
kenjiArai | 1:abe49c3395cd | 16 | #error "This program does NOT run your target!" |
kenjiArai | 1:abe49c3395cd | 17 | #endif |
kenjiArai | 1:abe49c3395cd | 18 | |
kenjiArai | 1:abe49c3395cd | 19 | // Include -------------------------------------------------------------------- |
kenjiArai | 0:1672d0903bdc | 20 | #include "mbed.h" |
kenjiArai | 1:abe49c3395cd | 21 | #include "WakeUp.h" |
kenjiArai | 1:abe49c3395cd | 22 | |
kenjiArai | 1:abe49c3395cd | 23 | // Definition ----------------------------------------------------------------- |
kenjiArai | 1:abe49c3395cd | 24 | #define TRY_STANDBY_MODE |
kenjiArai | 0:1672d0903bdc | 25 | |
kenjiArai | 1:abe49c3395cd | 26 | // Object --------------------------------------------------------------------- |
kenjiArai | 1:abe49c3395cd | 27 | DigitalOut myled(LED1); |
kenjiArai | 1:abe49c3395cd | 28 | DigitalOut out(D7); |
kenjiArai | 1:abe49c3395cd | 29 | Serial pc(USBTX, USBRX); // Communication with Host |
kenjiArai | 1:abe49c3395cd | 30 | |
kenjiArai | 1:abe49c3395cd | 31 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 1:abe49c3395cd | 32 | |
kenjiArai | 1:abe49c3395cd | 33 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 1:abe49c3395cd | 34 | char *const msg0 = "Is a time correct? If no, please hit any key. "; |
kenjiArai | 1:abe49c3395cd | 35 | char *const msg1 = "<Push USER SW then enter sleep mode> "; |
kenjiArai | 0:1672d0903bdc | 36 | |
kenjiArai | 1:abe49c3395cd | 37 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 1:abe49c3395cd | 38 | static void time_enter_mode(void); |
kenjiArai | 1:abe49c3395cd | 39 | static void chk_and_set_time(char *ptr); |
kenjiArai | 1:abe49c3395cd | 40 | static int xatoi (char **str, unsigned long *res); |
kenjiArai | 1:abe49c3395cd | 41 | static void get_line (char *buff, int len); |
kenjiArai | 1:abe49c3395cd | 42 | #ifndef TRY_STANDBY_MODE |
kenjiArai | 1:abe49c3395cd | 43 | static void wakeup_action(void); |
kenjiArai | 1:abe49c3395cd | 44 | #endif |
kenjiArai | 1:abe49c3395cd | 45 | |
kenjiArai | 1:abe49c3395cd | 46 | //------------------------------------------------------------------------------ |
kenjiArai | 1:abe49c3395cd | 47 | // Control Program |
kenjiArai | 1:abe49c3395cd | 48 | //------------------------------------------------------------------------------ |
kenjiArai | 0:1672d0903bdc | 49 | int main() { |
kenjiArai | 1:abe49c3395cd | 50 | char buf[64]; // data buffer for text |
kenjiArai | 1:abe49c3395cd | 51 | time_t seconds; |
kenjiArai | 1:abe49c3395cd | 52 | uint32_t count = 0; |
kenjiArai | 1:abe49c3395cd | 53 | |
kenjiArai | 1:abe49c3395cd | 54 | pc.printf("\r\nStart program\r\n"); |
kenjiArai | 1:abe49c3395cd | 55 | while(true){ |
kenjiArai | 1:abe49c3395cd | 56 | seconds = time(NULL); |
kenjiArai | 1:abe49c3395cd | 57 | strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); |
kenjiArai | 1:abe49c3395cd | 58 | pc.printf("[Time] %s", buf); |
kenjiArai | 1:abe49c3395cd | 59 | pc.printf(msg0); |
kenjiArai | 1:abe49c3395cd | 60 | pc.printf("%s\r", msg1); |
kenjiArai | 1:abe49c3395cd | 61 | while (seconds == time(NULL)){;} |
kenjiArai | 1:abe49c3395cd | 62 | if (pc.readable() == 1){ |
kenjiArai | 1:abe49c3395cd | 63 | buf[0] = pc.getc(); // dummy read |
kenjiArai | 1:abe49c3395cd | 64 | time_enter_mode(); |
kenjiArai | 1:abe49c3395cd | 65 | break; |
kenjiArai | 1:abe49c3395cd | 66 | } |
kenjiArai | 1:abe49c3395cd | 67 | uint8_t n = strlen(msg0) + strlen(msg1); |
kenjiArai | 1:abe49c3395cd | 68 | for (uint8_t i = 0; i < n; i++){ |
kenjiArai | 1:abe49c3395cd | 69 | pc.putc(' '); |
kenjiArai | 1:abe49c3395cd | 70 | } |
kenjiArai | 1:abe49c3395cd | 71 | pc.printf(" \r"); // Not use '\n' |
kenjiArai | 1:abe49c3395cd | 72 | if(++count >= 9){ |
kenjiArai | 1:abe49c3395cd | 73 | break; |
kenjiArai | 1:abe49c3395cd | 74 | } |
kenjiArai | 1:abe49c3395cd | 75 | } |
kenjiArai | 1:abe49c3395cd | 76 | seconds = time(NULL); |
kenjiArai | 1:abe49c3395cd | 77 | strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); |
kenjiArai | 1:abe49c3395cd | 78 | pc.printf("[Time] %s", buf); |
kenjiArai | 1:abe49c3395cd | 79 | #ifdef TRY_STANDBY_MODE |
kenjiArai | 1:abe49c3395cd | 80 | pc.printf("Check Stand-By function using internal RTC\r\n"); |
kenjiArai | 1:abe49c3395cd | 81 | #else |
kenjiArai | 1:abe49c3395cd | 82 | pc.printf("Check Deep Sleep function using internal RTC\r\n"); |
kenjiArai | 1:abe49c3395cd | 83 | #endif |
kenjiArai | 1:abe49c3395cd | 84 | wait(1); |
kenjiArai | 1:abe49c3395cd | 85 | #ifdef TRY_STANDBY_MODE |
kenjiArai | 1:abe49c3395cd | 86 | while(true){ |
kenjiArai | 1:abe49c3395cd | 87 | WakeUp::standby_then_reset(10000); |
kenjiArai | 1:abe49c3395cd | 88 | // not come to here |
kenjiArai | 1:abe49c3395cd | 89 | while(true){;} |
kenjiArai | 1:abe49c3395cd | 90 | } |
kenjiArai | 1:abe49c3395cd | 91 | #else |
kenjiArai | 1:abe49c3395cd | 92 | WakeUp::attach(wakeup_action); |
kenjiArai | 1:abe49c3395cd | 93 | uint32_t n = 0; |
kenjiArai | 1:abe49c3395cd | 94 | while(true){ |
kenjiArai | 1:abe49c3395cd | 95 | pc.printf("main_Step(%u)\r\n", __LINE__); |
kenjiArai | 1:abe49c3395cd | 96 | myled = 0; |
kenjiArai | 1:abe49c3395cd | 97 | WakeUp::set_ms(6000); |
kenjiArai | 1:abe49c3395cd | 98 | deepsleep(); |
kenjiArai | 1:abe49c3395cd | 99 | myled = 1; |
kenjiArai | 1:abe49c3395cd | 100 | wait(4); |
kenjiArai | 1:abe49c3395cd | 101 | pc.printf("\r\nmain_Step(%u)\r\n", __LINE__); |
kenjiArai | 1:abe49c3395cd | 102 | if (++n >= 5){ |
kenjiArai | 1:abe49c3395cd | 103 | NVIC_SystemReset(); |
kenjiArai | 1:abe49c3395cd | 104 | } |
kenjiArai | 0:1672d0903bdc | 105 | } |
kenjiArai | 1:abe49c3395cd | 106 | #endif |
kenjiArai | 0:1672d0903bdc | 107 | } |
kenjiArai | 1:abe49c3395cd | 108 | |
kenjiArai | 1:abe49c3395cd | 109 | #ifndef TRY_STANDBY_MODE |
kenjiArai | 1:abe49c3395cd | 110 | void wakeup_action() |
kenjiArai | 1:abe49c3395cd | 111 | { |
kenjiArai | 1:abe49c3395cd | 112 | out = !out; |
kenjiArai | 1:abe49c3395cd | 113 | } |
kenjiArai | 1:abe49c3395cd | 114 | #endif |
kenjiArai | 1:abe49c3395cd | 115 | |
kenjiArai | 1:abe49c3395cd | 116 | void time_enter_mode(void) |
kenjiArai | 1:abe49c3395cd | 117 | { |
kenjiArai | 1:abe49c3395cd | 118 | char *ptr; |
kenjiArai | 1:abe49c3395cd | 119 | char linebuf[64]; |
kenjiArai | 1:abe49c3395cd | 120 | |
kenjiArai | 1:abe49c3395cd | 121 | pc.printf("\r\nSet time into RTC\r\n"); |
kenjiArai | 1:abe49c3395cd | 122 | pc.printf(" e.g. >17 1 16 20 55 23 -> January 16th,'17, 20:55:23\r\n"); |
kenjiArai | 1:abe49c3395cd | 123 | pc.printf(" If time is fine, just hit enter\r\n"); |
kenjiArai | 1:abe49c3395cd | 124 | pc.putc('>'); |
kenjiArai | 1:abe49c3395cd | 125 | ptr = linebuf; |
kenjiArai | 1:abe49c3395cd | 126 | get_line(ptr, sizeof(linebuf)); |
kenjiArai | 1:abe49c3395cd | 127 | pc.printf("\r"); |
kenjiArai | 1:abe49c3395cd | 128 | chk_and_set_time(ptr); |
kenjiArai | 1:abe49c3395cd | 129 | } |
kenjiArai | 1:abe49c3395cd | 130 | |
kenjiArai | 1:abe49c3395cd | 131 | // Get key input data |
kenjiArai | 1:abe49c3395cd | 132 | void get_line (char *buff, int len) |
kenjiArai | 1:abe49c3395cd | 133 | { |
kenjiArai | 1:abe49c3395cd | 134 | char c; |
kenjiArai | 1:abe49c3395cd | 135 | int idx = 0; |
kenjiArai | 1:abe49c3395cd | 136 | |
kenjiArai | 1:abe49c3395cd | 137 | for (;;) { |
kenjiArai | 1:abe49c3395cd | 138 | c = pc.getc(); |
kenjiArai | 1:abe49c3395cd | 139 | if (c == '\r') { |
kenjiArai | 1:abe49c3395cd | 140 | buff[idx++] = c; |
kenjiArai | 1:abe49c3395cd | 141 | break; |
kenjiArai | 1:abe49c3395cd | 142 | } |
kenjiArai | 1:abe49c3395cd | 143 | if ((c == '\b') && idx) { |
kenjiArai | 1:abe49c3395cd | 144 | idx--; |
kenjiArai | 1:abe49c3395cd | 145 | pc.putc(c); |
kenjiArai | 1:abe49c3395cd | 146 | pc.putc(' '); |
kenjiArai | 1:abe49c3395cd | 147 | pc.putc(c); |
kenjiArai | 1:abe49c3395cd | 148 | } |
kenjiArai | 1:abe49c3395cd | 149 | if (((uint8_t)c >= ' ') && (idx < len - 1)) { |
kenjiArai | 1:abe49c3395cd | 150 | buff[idx++] = c; |
kenjiArai | 1:abe49c3395cd | 151 | pc.putc(c); |
kenjiArai | 1:abe49c3395cd | 152 | } |
kenjiArai | 1:abe49c3395cd | 153 | } |
kenjiArai | 1:abe49c3395cd | 154 | buff[idx] = 0; |
kenjiArai | 1:abe49c3395cd | 155 | pc.putc('\n'); |
kenjiArai | 1:abe49c3395cd | 156 | } |
kenjiArai | 1:abe49c3395cd | 157 | |
kenjiArai | 1:abe49c3395cd | 158 | void chk_and_set_time(char *ptr) |
kenjiArai | 1:abe49c3395cd | 159 | { |
kenjiArai | 1:abe49c3395cd | 160 | unsigned long p1; |
kenjiArai | 1:abe49c3395cd | 161 | struct tm t; |
kenjiArai | 1:abe49c3395cd | 162 | time_t seconds; |
kenjiArai | 1:abe49c3395cd | 163 | |
kenjiArai | 1:abe49c3395cd | 164 | if (xatoi(&ptr, &p1)) { |
kenjiArai | 1:abe49c3395cd | 165 | t.tm_year = (uint8_t)p1 + 100; |
kenjiArai | 1:abe49c3395cd | 166 | pc.printf("Year:%d ",p1); |
kenjiArai | 1:abe49c3395cd | 167 | xatoi( &ptr, &p1 ); |
kenjiArai | 1:abe49c3395cd | 168 | t.tm_mon = (uint8_t)p1 - 1; |
kenjiArai | 1:abe49c3395cd | 169 | pc.printf("Month:%d ",p1); |
kenjiArai | 1:abe49c3395cd | 170 | xatoi( &ptr, &p1 ); |
kenjiArai | 1:abe49c3395cd | 171 | t.tm_mday = (uint8_t)p1; |
kenjiArai | 1:abe49c3395cd | 172 | pc.printf("Day:%d ",p1); |
kenjiArai | 1:abe49c3395cd | 173 | xatoi( &ptr, &p1 ); |
kenjiArai | 1:abe49c3395cd | 174 | t.tm_hour = (uint8_t)p1; |
kenjiArai | 1:abe49c3395cd | 175 | pc.printf("Hour:%d ",p1); |
kenjiArai | 1:abe49c3395cd | 176 | xatoi( &ptr, &p1 ); |
kenjiArai | 1:abe49c3395cd | 177 | t.tm_min = (uint8_t)p1; |
kenjiArai | 1:abe49c3395cd | 178 | pc.printf("Min:%d ",p1); |
kenjiArai | 1:abe49c3395cd | 179 | xatoi( &ptr, &p1 ); |
kenjiArai | 1:abe49c3395cd | 180 | t.tm_sec = (uint8_t)p1; |
kenjiArai | 1:abe49c3395cd | 181 | pc.printf("Sec: %d \r\n",p1); |
kenjiArai | 1:abe49c3395cd | 182 | } else { |
kenjiArai | 1:abe49c3395cd | 183 | return; |
kenjiArai | 1:abe49c3395cd | 184 | } |
kenjiArai | 1:abe49c3395cd | 185 | seconds = mktime(&t); |
kenjiArai | 1:abe49c3395cd | 186 | set_time(seconds); |
kenjiArai | 1:abe49c3395cd | 187 | pc.printf( |
kenjiArai | 1:abe49c3395cd | 188 | "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", |
kenjiArai | 1:abe49c3395cd | 189 | t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec |
kenjiArai | 1:abe49c3395cd | 190 | ); |
kenjiArai | 1:abe49c3395cd | 191 | } |
kenjiArai | 1:abe49c3395cd | 192 | |
kenjiArai | 1:abe49c3395cd | 193 | // Change string -> integer |
kenjiArai | 1:abe49c3395cd | 194 | int xatoi (char **str, unsigned long *res) |
kenjiArai | 1:abe49c3395cd | 195 | { |
kenjiArai | 1:abe49c3395cd | 196 | unsigned long val; |
kenjiArai | 1:abe49c3395cd | 197 | unsigned char c, radix, s = 0; |
kenjiArai | 1:abe49c3395cd | 198 | |
kenjiArai | 1:abe49c3395cd | 199 | while ((c = **str) == ' '){ (*str)++;} |
kenjiArai | 1:abe49c3395cd | 200 | if (c == '-'){ |
kenjiArai | 1:abe49c3395cd | 201 | s = 1; |
kenjiArai | 1:abe49c3395cd | 202 | c = *(++(*str)); |
kenjiArai | 1:abe49c3395cd | 203 | } |
kenjiArai | 1:abe49c3395cd | 204 | if (c == '0'){ |
kenjiArai | 1:abe49c3395cd | 205 | c = *(++(*str)); |
kenjiArai | 1:abe49c3395cd | 206 | if (c <= ' '){ |
kenjiArai | 1:abe49c3395cd | 207 | *res = 0; |
kenjiArai | 1:abe49c3395cd | 208 | return 1; |
kenjiArai | 1:abe49c3395cd | 209 | } |
kenjiArai | 1:abe49c3395cd | 210 | if (c == 'x'){ |
kenjiArai | 1:abe49c3395cd | 211 | radix = 16; |
kenjiArai | 1:abe49c3395cd | 212 | c = *(++(*str)); |
kenjiArai | 1:abe49c3395cd | 213 | } else { |
kenjiArai | 1:abe49c3395cd | 214 | if (c == 'b'){ |
kenjiArai | 1:abe49c3395cd | 215 | radix = 2; |
kenjiArai | 1:abe49c3395cd | 216 | c = *(++(*str)); |
kenjiArai | 1:abe49c3395cd | 217 | } else { |
kenjiArai | 1:abe49c3395cd | 218 | if ((c >= '0')&&(c <= '9')){ |
kenjiArai | 1:abe49c3395cd | 219 | radix = 8; |
kenjiArai | 1:abe49c3395cd | 220 | } else { |
kenjiArai | 1:abe49c3395cd | 221 | return 0; |
kenjiArai | 1:abe49c3395cd | 222 | } |
kenjiArai | 1:abe49c3395cd | 223 | } |
kenjiArai | 1:abe49c3395cd | 224 | } |
kenjiArai | 1:abe49c3395cd | 225 | } else { |
kenjiArai | 1:abe49c3395cd | 226 | if ((c < '1')||(c > '9')){ return 0;} |
kenjiArai | 1:abe49c3395cd | 227 | radix = 10; |
kenjiArai | 1:abe49c3395cd | 228 | } |
kenjiArai | 1:abe49c3395cd | 229 | val = 0; |
kenjiArai | 1:abe49c3395cd | 230 | while (c > ' ') { |
kenjiArai | 1:abe49c3395cd | 231 | if (c >= 'a'){ c -= 0x20;} |
kenjiArai | 1:abe49c3395cd | 232 | c -= '0'; |
kenjiArai | 1:abe49c3395cd | 233 | if (c >= 17) { |
kenjiArai | 1:abe49c3395cd | 234 | c -= 7; |
kenjiArai | 1:abe49c3395cd | 235 | if (c <= 9){ return 0;} |
kenjiArai | 1:abe49c3395cd | 236 | } |
kenjiArai | 1:abe49c3395cd | 237 | if (c >= radix){ return 0;} |
kenjiArai | 1:abe49c3395cd | 238 | val = val * radix + c; |
kenjiArai | 1:abe49c3395cd | 239 | c = *(++(*str)); |
kenjiArai | 1:abe49c3395cd | 240 | } |
kenjiArai | 1:abe49c3395cd | 241 | if (s){ val = -val;} |
kenjiArai | 1:abe49c3395cd | 242 | *res = val; |
kenjiArai | 1:abe49c3395cd | 243 | return 1; |
kenjiArai | 1:abe49c3395cd | 244 | } |