Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 10:806cfec92eb6
- Parent:
- 9:6a64b0207f72
- Child:
- 11:2e514d955a2b
--- a/main.cpp Mon Jan 16 09:38:03 2017 +0000
+++ b/main.cpp Sat Jul 18 22:46:14 2020 +0000
@@ -2,19 +2,11 @@
* mbed Application program
* RTC (inside STM32x CPU) test program
*
- * Copyright (c) 2015,'16,'17 Kenji Arai / JH1PJL
- * http://www.page.sannet.ne.jp/kenjia/index.html
- * http://mbed.org/users/kenjiArai/
+ * Copyright (c) 2015,'16,'17,'20 Kenji Arai / JH1PJL
+ * http://www7b.biglobe.ne.jp/~kenjia/
+ * https://os.mbed.com/users/kenjiArai/
* Created: January 17th, 2015
- * Revised: January 16th, 2017
- *
- * 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 AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
- * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * Revised: July 19th, 2020
*/
/* mbed library now suports RTC continuous operation at Reset & Power ON/OFF
@@ -23,7 +15,7 @@
when user push a reset buttom or terninate a power.
Even if user configures a back-up circuit for RTC, mbed board could not
keep proper time.
- I have checked mbed rev.133 and mbed-dev rev.155.
+ --> Right now, you don't worry thoese issue.
*/
/*
@@ -34,6 +26,7 @@
Nucleo-F446RE / ok / ok / ok (need following Back-up Circuit)
Nucleo-F334R8 / ok / ok / ok (need following Back-up Circuit)
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)
@@ -41,6 +34,10 @@
< 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/
*/
// Include --------------------------------------------------------------------
@@ -53,47 +50,49 @@
#define PUSHED_SW 0 // Active low
#endif
+#define LONGLONGTIME 2147483647
+
// Object ---------------------------------------------------------------------
DigitalIn userSW(USER_BUTTON);
-DigitalOut myled(LED1); // Indicate the sampling period
-Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1); // Indicate the sampling period
+static RawSerial pc(USBTX, USBRX);
// RAM ------------------------------------------------------------------------
// ROM / Constant data --------------------------------------------------------
-char *const msg0 = "Is a time correct? If no, please hit any key. ";
-char *const msg1 = "<Push USER SW then enter sleep mode> ";
-char *const msg2 = "\r\nEnter Standby Mode, please push RESET to wake-up\r\n";
+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 --------------------------------------------------------
static void time_enter_mode(void);
static void chk_and_set_time(char *ptr);
-static void goto_standby(void);
-static int xatoi (char **str, unsigned long *res);
-static void get_line (char *buff, int len);
+static int32_t xatoi (char **str, int32_t *res);
+static void get_line (char *buff, int32_t len);
+static void usr_sw_irq(void);
+extern void print_revision(void);
//------------------------------------------------------------------------------
// Control Program
//------------------------------------------------------------------------------
int main()
{
- char buf[64]; // data buffer for text
+ char buf[64]; // data buffer for text
time_t seconds;
uint8_t wait_counter = 0;
- wait(2.0);
pc.printf("\r\n\r\nTest Nucleo RTC Function\r\n");
+ print_revision();
myled = !myled;
- // waiting for Initial screen
- myled = 1;
- wait(1.0);
+ thread_sleep_for(500);
myled = !myled;
- wait(1.0);
- while(1) {
+ thread_sleep_for(500);
+ 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(msg0);
+ pc.printf("%s", msg0);
pc.printf("%s\r", msg1);
wait_counter = 0;
while (seconds == time(NULL)){
@@ -101,13 +100,22 @@
buf[0] = pc.getc(); // dummy read
time_enter_mode();
}
- if (userSW == PUSHED_SW){
- pc.printf(msg2);
- wait(1.0);
+ if (userSW == PUSHED_SW){ // goto sleep
+ while (userSW == PUSHED_SW){
+ thread_sleep_for(10);
+ }
+ thread_sleep_for(10);
+ pc.printf("%s%s", msg2, msg3);
myled = 0;
- goto_standby();
+ InterruptIn usr_sw(USER_BUTTON);
+ thread_sleep_for(1000);
+ DigitalIn dmy0(LED1);
+ DigitalIn dmy1(USBTX);
+ DigitalIn dmy2(USBRX);
+ usr_sw.fall(&usr_sw_irq);
+ thread_sleep_for(LONGLONGTIME);
}
- wait(0.05);
+ thread_sleep_for(50);
if (++wait_counter > (2000 / 50)){
break;
}
@@ -121,14 +129,21 @@
}
}
-void time_enter_mode(void)
+// Interrupt for wake up
+static void usr_sw_irq(void)
+{
+ system_reset(); // restart from RESET condition
+}
+
+// Time adjustment
+static void time_enter_mode(void)
{
char *ptr;
char linebuf[64];
pc.printf("\r\nSet time into RTC\r\n");
- pc.printf(" e.g. >17 1 16 20 55 23 -> January 16th,'17, 20:55:23\r\n");
- pc.printf(" If time is fine, just hit enter\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('>');
ptr = linebuf;
get_line(ptr, sizeof(linebuf));
@@ -136,16 +151,11 @@
chk_and_set_time(ptr);
}
-void goto_standby(void)
+// Change string -> integer
+static int32_t xatoi(char **str, int32_t *res)
{
- deepsleep(); // Not Standby Mode but Deep Sleep Mode
-}
-
-// Change string -> integer
-int xatoi (char **str, unsigned long *res)
-{
- unsigned long val;
- unsigned char c, radix, s = 0;
+ uint32_t val;
+ uint8_t c, radix, s = 0;
while ((c = **str) == ' ') (*str)++;
if (c == '-') {
@@ -197,10 +207,10 @@
}
// Get key input data
-void get_line (char *buff, int len)
+static void get_line(char *buff, int32_t len)
{
char c;
- int idx = 0;
+ int32_t idx = 0;
for (;;) {
c = pc.getc();
@@ -223,9 +233,10 @@
pc.putc('\n');
}
-void chk_and_set_time(char *ptr)
+// Check key input strings and set time
+static void chk_and_set_time(char *ptr)
{
- unsigned long p1;
+ int32_t p1;
struct tm t;
time_t seconds;
@@ -252,17 +263,13 @@
}
seconds = mktime(&t);
set_time(seconds);
- // Show Time with several example
// ex.1
pc.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
- time_t seconds;
- char buf[40];
-
- seconds = mktime(&t);
+ // Show Time with several example
// ex.2
strftime(buf, 40, "%x %X", localtime(&seconds));
pc.printf("Date: %s\r\n", buf);