rusefi/firmware/hw_layer/rtc_helper.cpp

133 lines
3.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file rtc_helper.cpp
* @brief Real Time Clock helper
*
* @date Feb 5, 2014
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
2015-07-10 06:01:56 -07:00
*/
#include <string.h>
#include <time.h>
#include "main.h"
#include "rfiutil.h"
#include "rtc_helper.h"
#if EFI_RTC || defined(__DOXYGEN__)
2016-02-04 10:01:47 -08:00
#include "chrtclib.h"
2015-07-10 06:01:56 -07:00
static LoggingWithStorage logger("RTC");
#endif /* EFI_RTC */
void date_set_tm(struct tm *timp) {
(void)timp;
#if EFI_RTC || defined(__DOXYGEN__)
rtcSetTimeTm(&RTCD1, timp);
#endif /* EFI_RTC */
}
void date_get_tm(struct tm *timp) {
(void)timp;
#if EFI_RTC || defined(__DOXYGEN__)
rtcGetTimeTm(&RTCD1, timp);
#endif /* EFI_RTC */
}
2016-08-08 21:03:08 -07:00
static void put2(int offset, char *lcd_str, int value) {
static char buff[4];
itoa10(buff, value);
if (value < 10) {
lcd_str[offset] = '0';
lcd_str[offset + 1] = buff[0];
} else {
lcd_str[offset] = buff[0];
lcd_str[offset + 1] = buff[1];
}
}
bool dateToStringShort(char *lcd_str) {
#if EFI_RTC || defined(__DOXYGEN__)
strcpy(lcd_str, "0000_000000\0");
struct tm timp;
rtcGetTimeTm(&RTCD1, &timp);
if (timp.tm_year < 116 || timp.tm_year > 130) {
// 2016 to 2030 is the valid range
lcd_str[0] = 0;
return false;
}
put2(0, lcd_str, timp.tm_mon + 1);
put2(2, lcd_str, timp.tm_mday);
put2(5, lcd_str, timp.tm_hour);
put2(7, lcd_str, timp.tm_min);
put2(9, lcd_str, timp.tm_sec);
return true;
#else
lcd_str[0] = 0;
return false;
#endif
}
2015-07-10 06:01:56 -07:00
void dateToString(char *lcd_str) {
#if EFI_RTC || defined(__DOXYGEN__)
// todo:
// re-implement this along the lines of chvprintf("%04u-%02u-%02u %02u:%02u:%02u\r\n", timp.tm_year + 1900, timp.tm_mon + 1, timp.tm_mday, timp.tm_hour,
// timp.tm_min, timp.tm_sec);
// this would require a temporary mem stream - see datalogging and other existing usages
strcpy(lcd_str, "00/00 00:00:00\0");
struct tm timp;
rtcGetTimeTm(&RTCD1, &timp); // get RTC date/time
2016-08-08 21:03:08 -07:00
put2(0, lcd_str, timp.tm_mon + 1);
put2(3, lcd_str, timp.tm_mday);
put2(6, lcd_str, timp.tm_hour);
put2(9, lcd_str, timp.tm_min);
put2(12, lcd_str, timp.tm_sec);
2015-07-10 06:01:56 -07:00
#else
lcd_str[0] = 0;
#endif /* EFI_RTC */
}
#if EFI_RTC || defined(__DOXYGEN__)
void printDateTime(void) {
static time_t unix_time;
struct tm timp;
unix_time = rtcGetTimeUnixSec(&RTCD1);
if (unix_time == -1) {
scheduleMsg(&logger, "incorrect time in RTC cell");
} else {
scheduleMsg(&logger, "%D - unix time", unix_time);
rtcGetTimeTm(&RTCD1, &timp);
appendMsgPrefix(&logger);
appendPrintf(&logger, "Current RTC time in GMT is: %04u-%02u-%02u %02u:%02u:%02u", timp.tm_year + 1900, timp.tm_mon + 1, timp.tm_mday, timp.tm_hour,
timp.tm_min, timp.tm_sec);
appendMsgPostfix(&logger);
scheduleLogging(&logger);
}
}
void setDateTime(const char *strDate) {
if (strlen(strDate) > 0) {
2016-08-09 18:03:16 -07:00
time_t unix_time = atoi(strDate);
2015-07-10 06:01:56 -07:00
if (unix_time > 0) {
rtcSetTimeUnixSec(&RTCD1, unix_time);
printDateTime();
return;
}
}
scheduleMsg(&logger, "date_set Date parameter %s is wrong\r\n", strDate);
}
#endif /* EFI_RTC */
void initRtc(void) {
#if EFI_RTC || defined(__DOXYGEN__)
printMsg(&logger, "initRtc()");
// yes, it's my begin time and we always start from this one 1391894433 - 2014-02-08 21:20:03
// rtcSetTimeUnixSec(&RTCD1, 1391894433);
#endif
}