diff --git a/application.c b/application.c index 567800e..16346eb 100644 --- a/application.c +++ b/application.c @@ -15,13 +15,15 @@ /** STM32F1 application example * @file application.c * @author King Kévin - * @date 2016-2018 + * @date 2016-2019 */ /* standard libraries */ #include // standard integer types #include // standard utilities #include // string utilities +#include // date/time utilities +#include // utilities to check chars /* STM32 (including CM3) libraries */ #include // Cortex M3 utilities @@ -47,6 +49,13 @@ #include "menu.h" // menu utilities #define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */ +/** set to 0 if the RTC is reset when the board is powered on, only indicates the uptime + * set to 1 if VBAT can keep the RTC running when the board is unpowered, indicating the date and time + */ +#define RTC_DATE_TIME 1 + +/** RTC time when device is started */ +static time_t time_start = 0; /** @defgroup main_flags flag set in interrupts to be processed in main task * @{ @@ -91,6 +100,13 @@ static void command_version(void* argument); */ static void command_uptime(void* argument); +#if RTC_DATE_TIME +/** show date and time + * @param[in] argument date and time to set + */ +static void command_datetime(void* argument); +#endif + /** reset board * @param[in] argument no argument required */ @@ -127,6 +143,16 @@ static const struct menu_command_t menu_commands[] = { .argument_description = NULL, .command_handler = &command_uptime, }, +#if RTC_DATE_TIME + { + .shortcut = 'd', + .name = "date", + .command_description = "show date and time", + .argument = MENU_ARGUMENT_STRING, + .argument_description = "[YYYY-MM-DD HH:MM:SS]", + .command_handler = &command_datetime, + }, +#endif { .shortcut = 'r', .name = "reset", @@ -200,10 +226,43 @@ static void command_version(void* argument) static void command_uptime(void* argument) { (void)argument; // we won't use the argument - uint32_t uptime = rtc_get_counter_val(); // get time from internal RTC - printf("uptime: %u.%02u:%02u:%02u\n", uptime/(24*60*60), (uptime/(60*60))%24, (uptime/60)%60, uptime%60); + uint32_t uptime = rtc_get_counter_val() - time_start; // get time from internal RTC + printf("uptime: %u.%02u:%02u:%02u\n", uptime / (24 * 60 * 60), (uptime / (60 * 60)) % 24, (uptime / 60) % 60, uptime % 60); } +#if RTC_DATE_TIME +static void command_datetime(void* argument) +{ + char* datetime = (char*)argument; // argument is optional date time + if (NULL == argument) { // no date and time provided, just show the current day and time + time_t time_rtc = rtc_get_counter_val(); // get time from internal RTC + struct tm* time_tm = localtime(&time_rtc); // convert time + printf("date: %d-%02d-%02d %02d:%02d:%02d\n", 1900 + time_tm->tm_year, time_tm->tm_mon, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec); + } else { // date and time provided, set it + const char* malformed = "date and time malformed, expecting YYYY-MM-DD HH:MM:SS\n"; + struct tm time_tm; // to store the parsed date time + if (strlen(datetime) != (4 + 1 + 2 + 1 + 2) + 1 + (2 + 1 + 2 + 1 + 2)) { // verify date/time is long enough + printf(malformed); + return; + } + if (!(isdigit((int8_t)datetime[0]) && isdigit((int8_t)datetime[1]) && isdigit((int8_t)datetime[2]) && isdigit((int8_t)datetime[3]) && '-' == datetime[4] && isdigit((int8_t)datetime[5]) && isdigit((int8_t)datetime[6]) && '-' == datetime[7] && isdigit((int8_t)datetime[8]) && isdigit((int8_t)datetime[9]) && ' ' == datetime[10] && isdigit((int8_t)datetime[11]) && isdigit((int8_t)datetime[12]) && ':' == datetime[13] && isdigit((int8_t)datetime[14]) && isdigit((int8_t)datetime[15]) && ':' == datetime[16] && isdigit((int8_t)datetime[17]) && isdigit((int8_t)datetime[18]))) { // verify format (good enough to not fail parsing) + printf(malformed); + return; + } + time_tm.tm_year = strtol(&datetime[0], NULL, 10) - 1900; // parse year + time_tm.tm_mon = strtol(&datetime[5], NULL, 10); // parse month + time_tm.tm_mday = strtol(&datetime[8], NULL, 10); // parse day + time_tm.tm_hour = strtol(&datetime[11], NULL, 10); // parse hour + time_tm.tm_min = strtol(&datetime[14], NULL, 10); // parse minutes + time_tm.tm_sec = strtol(&datetime[17], NULL, 10); // parse seconds + time_t time_rtc = mktime(&time_tm); // get back seconds + time_start = time_rtc + (rtc_get_counter_val() - time_start); // update uptime with current date + rtc_set_counter_val(time_rtc); // save date/time to internal RTC + printf("date and time saved: %d-%02d-%02d %02d:%02d:%02d\n", 1900 + time_tm.tm_year, time_tm.tm_mon, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); + } +} +#endif + static void command_reset(void* argument) { (void)argument; // we won't use the argument @@ -291,6 +350,7 @@ void main(void) #endif rtc_interrupt_enable(RTC_SEC); // enable RTC interrupt on "seconds" nvic_enable_irq(NVIC_RTC_IRQ); // allow the RTC to interrupt + time_start = rtc_get_counter_val(); // get start time from internal RTC printf("OK\n"); // setup terminal