In the past, you need modify rtc_api.c in mbed-dev source code. From this revision, you can just use RTC function all of conditions (Normal, Reset, Stand-by, Power OFF).
Note
From now on, you do NOT need any modification for mbed-dev library because STM team updates rtc_api.c source code and support RTC function under reset & standby condition includes power off condition (You need additional VBAT back-up hardware).
Please refer following NOTE information.
/users/kenjiArai/notebook/nucleo-series-rtc-control-under-power-onoff-and-re/
Diff: main.cpp
- Revision:
- 11:2e514d955a2b
- Parent:
- 10:806cfec92eb6
- Child:
- 12:8cb1f89541d1
--- a/main.cpp Sat Jul 18 22:46:14 2020 +0000 +++ b/main.cpp Sat Aug 01 07:26:13 2020 +0000 @@ -6,7 +6,7 @@ * http://www7b.biglobe.ne.jp/~kenjia/ * https://os.mbed.com/users/kenjiArai/ * Created: January 17th, 2015 - * Revised: July 19th, 2020 + * Revised: August 1st, 2020 */ /* mbed library now suports RTC continuous operation at Reset & Power ON/OFF @@ -24,17 +24,17 @@ Nucleo-F401RE / ok / ok / ok (need following Back-up Circuit) Nucleo-F411RE / ok / ok / ok (need following Back-up Circuit) Nucleo-F446RE / ok / ok / ok (need following Back-up Circuit) - Nucleo-F334R8 / ok / ok / ok (need following Back-up Circuit) + not support on Mbed-os6.2.0 Nucleo-F334R8 Nucleo-L476RG / ok / ok / ok (need following Back-up Circuit) DISCO-L45VG-IOT/ ok / ok / ok (Need additional hardware) Nucleo-L152RE / ok / ok / no check (Need additional hardware) Nucleo-L073RZ / ok / ok / no check (Need additional hardware) - Nucleo-L053R8 / ok / ok / no check (Need additional hardware) + not support on Mbed-os6.2.0 Nucleo-L053R8 < Back-up circuit > CN7 VIN <- SBD <- 330 Ohm <- CR2032 + - -> CN7 GND Remove SB45 Zero Ohm resistor - + ----- PLEASE REFER FOLLOWS ---- https://os.mbed.com/users/kenjiArai/notebook/ nucleo-series-rtc-control-under-power-onoff-and-re/ @@ -55,17 +55,17 @@ // Object --------------------------------------------------------------------- DigitalIn userSW(USER_BUTTON); DigitalOut myled(LED1); // Indicate the sampling period -static RawSerial pc(USBTX, USBRX); +// Create a BufferedSerial object to be used by the system I/O retarget code +static BufferedSerial pc(USBTX, USBRX, 9600); // RAM ------------------------------------------------------------------------ // ROM / Constant data -------------------------------------------------------- const char *const msg0 = "Is a time correct? If no, please hit any key. "; const char *const msg1 = "<Push USER SW then enter the Standby mode> "; -const char *const msg2 = "\r\nEntered the standby mode. "; -const char *const msg3 = "Please push USER BUTTON to wake-up(Reset).\r\n"; // Function prototypes -------------------------------------------------------- +FileHandle *mbed::mbed_override_console(int fd); static void time_enter_mode(void); static void chk_and_set_time(char *ptr); static int32_t xatoi (char **str, int32_t *res); @@ -78,11 +78,11 @@ //------------------------------------------------------------------------------ int main() { - char buf[64]; // data buffer for text + char buf[64]; time_t seconds; uint8_t wait_counter = 0; - pc.printf("\r\n\r\nTest Nucleo RTC Function\r\n"); + puts("\r\n\r\nTest Nucleo RTC Function."); print_revision(); myled = !myled; thread_sleep_for(500); @@ -91,21 +91,23 @@ while(true) { seconds = time(NULL); strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); - pc.printf("[Time] %s", buf); - pc.printf("%s", msg0); - pc.printf("%s\r", msg1); + printf("[Time] %s", buf); + printf("%s", msg0); + printf("%s", msg1); + puts("\r"); wait_counter = 0; - while (seconds == time(NULL)){ - if (pc.readable() == 1){ - buf[0] = pc.getc(); // dummy read + while (seconds == time(NULL)) { + if (pc.readable() == 1) { + pc.read(buf,1); // dummy read time_enter_mode(); } - if (userSW == PUSHED_SW){ // goto sleep - while (userSW == PUSHED_SW){ + if (userSW == PUSHED_SW) { // goto sleep + while (userSW == PUSHED_SW) { thread_sleep_for(10); } thread_sleep_for(10); - pc.printf("%s%s", msg2, msg3); + puts("Entered the standby mode. "); + puts("Please push USER BUTTON to wake-up(Reset)."); myled = 0; InterruptIn usr_sw(USER_BUTTON); thread_sleep_for(1000); @@ -116,19 +118,35 @@ thread_sleep_for(LONGLONGTIME); } thread_sleep_for(50); - if (++wait_counter > (2000 / 50)){ + if (++wait_counter > (2000 / 50)) { break; } } + // delete previous strings + printf("\033[2A"); + puts(""); // null uint8_t n = strlen(msg0) + strlen(msg1); - for (uint8_t i = 0; i < n; i++){ - pc.putc(' '); + memset(buf, ' ', 64); + if (n > 64) { + n -= 64; + pc.write(buf, 64); } - pc.printf(" \r"); // Not use '\n' + if (n > 64) { + pc.write(buf, 64); + } else { + pc.write(buf, n); + } + printf("\033[G"); myled = !myled; } } +// the system I/O retarget +FileHandle *mbed::mbed_override_console(int fd) +{ + return &pc; +} + // Interrupt for wake up static void usr_sw_irq(void) { @@ -141,13 +159,14 @@ char *ptr; char linebuf[64]; - pc.printf("\r\nSet time into RTC\r\n"); - pc.printf(" e.g. >20 7 7 20 21 22 -> July 7,'20, 20:21:22\r\n"); - pc.printf(" If time is fine, just hit enter key\r\n"); - pc.putc('>'); + puts("Set time into RTC."); + puts(" e.g. >20 8 1 12 34 56 -> August 01,'20, 12:34:56"); + puts(" If time is fine, just hit enter key."); + linebuf[0] = '>'; + pc.write(linebuf, 1); ptr = linebuf; get_line(ptr, sizeof(linebuf)); - pc.printf("\r"); + puts("\r"); chk_and_set_time(ptr); } @@ -210,27 +229,31 @@ static void get_line(char *buff, int32_t len) { char c; + char bf[8]; int32_t idx = 0; for (;;) { - c = pc.getc(); + pc.read(bf, 1); + c = bf[0]; + //printf("0x%x \r\n", c); if (c == '\r') { buff[idx++] = c; break; } if ((c == '\b') && idx) { idx--; - pc.putc(c); - pc.putc(' '); - pc.putc(c); + const char bf_bs[] = + {0x1b, '[', '1', 'D', ' ', 0x1b, '[', '1', 'D'}; + pc.write(bf_bs, 9); } if (((uint8_t)c >= ' ') && (idx < len - 1)) { buff[idx++] = c; - pc.putc(c); + pc.write(bf, 1); } } buff[idx] = 0; - pc.putc('\n'); + bf[0] = '\n'; + pc.write(bf, 1); } // Check key input strings and set time @@ -242,42 +265,41 @@ if (xatoi(&ptr, &p1)) { t.tm_year = (uint8_t)p1 + 100; - pc.printf("Year:%d ",p1); + printf("Year:%d ",p1); xatoi( &ptr, &p1 ); t.tm_mon = (uint8_t)p1 - 1; - pc.printf("Month:%d ",p1); + printf("Month:%d ",p1); xatoi( &ptr, &p1 ); t.tm_mday = (uint8_t)p1; - pc.printf("Day:%d ",p1); + printf("Day:%d ",p1); xatoi( &ptr, &p1 ); t.tm_hour = (uint8_t)p1; - pc.printf("Hour:%d ",p1); + printf("Hour:%d ",p1); xatoi( &ptr, &p1 ); t.tm_min = (uint8_t)p1; - pc.printf("Min:%d ",p1); + printf("Min:%d ",p1); xatoi( &ptr, &p1 ); t.tm_sec = (uint8_t)p1; - pc.printf("Sec: %d \r\n",p1); + printf("Sec: %d \r\n",p1); } else { return; } seconds = mktime(&t); set_time(seconds); + // Show Time with several example // ex.1 - pc.printf( + printf( "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec ); -#if 0 - // Show Time with several example + char buf[64]; // ex.2 strftime(buf, 40, "%x %X", localtime(&seconds)); - pc.printf("Date: %s\r\n", buf); + printf("Date: %s\r\n", buf); // ex.3 strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); - pc.printf("Date: %s\r\n", buf); + printf("Date: %s\r\n", buf); // ex.4 strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds)); - pc.printf("Date: %s\r\n", buf); -#endif + printf("Date: %s\r\n", buf); }