rtc cleanup (#4596)
* rtc formatting and code style * more cleanup * s * time example
This commit is contained in:
parent
af7e4f6fe5
commit
ff9ce3730a
|
@ -69,10 +69,6 @@
|
|||
#include "can_vss.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* #311 we want to test RTC before engine start so that we do not test it while engine is running
|
||||
*/
|
||||
bool rtcWorks = true;
|
||||
#if HAL_USE_SPI
|
||||
extern bool isSpiInitialized[5];
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
#define STM32_LSI_ENABLED TRUE
|
||||
#define STM32_HSE_ENABLED TRUE
|
||||
|
||||
// rusEfi would automatically detect if we have 32768 quarts osc - see 'rtcWorks'
|
||||
// rusEfi would automatically detect if we have 32768 quartz osc - see 'rtcWorks'
|
||||
// todo: apply LSE patch
|
||||
#define STM32_LSE_ENABLED FALSE
|
||||
|
||||
|
|
|
@ -14,26 +14,21 @@
|
|||
#include "rtc_helper.h"
|
||||
#include <sys/time.h>
|
||||
|
||||
#if EFI_RTC
|
||||
static RTCDateTime timespec;
|
||||
|
||||
extern bool rtcWorks;
|
||||
|
||||
#endif /* EFI_RTC */
|
||||
|
||||
void date_set_tm(struct tm *timp) {
|
||||
void date_set_tm(tm *timp) {
|
||||
(void)timp;
|
||||
#if EFI_RTC
|
||||
rtcConvertStructTmToDateTime(timp, 0, ×pec);
|
||||
RTCDateTime timespec;
|
||||
rtcConvertStructTmToDateTime(timp, 0, ×pec);
|
||||
rtcSetTime(&RTCD1, ×pec);
|
||||
#endif /* EFI_RTC */
|
||||
}
|
||||
|
||||
void date_get_tm(struct tm *timp) {
|
||||
void date_get_tm(tm *timp) {
|
||||
(void)timp;
|
||||
#if EFI_RTC
|
||||
RTCDateTime timespec;
|
||||
rtcGetTime(&RTCD1, ×pec);
|
||||
rtcConvertDateTimeToStructTm(×pec, timp, NULL);
|
||||
rtcConvertDateTimeToStructTm(×pec, timp, NULL);
|
||||
#endif /* EFI_RTC */
|
||||
}
|
||||
|
||||
|
@ -47,29 +42,33 @@ extern "C" int _gettimeofday(timeval* tv, void* tzvp) {
|
|||
|
||||
#if EFI_RTC
|
||||
static time_t GetTimeUnixSec() {
|
||||
struct tm tim;
|
||||
tm tim;
|
||||
RTCDateTime timespec;
|
||||
|
||||
rtcGetTime(&RTCD1, ×pec);
|
||||
rtcConvertDateTimeToStructTm(×pec, &tim, NULL);
|
||||
return mktime(&tim);
|
||||
rtcGetTime(&RTCD1, ×pec);
|
||||
rtcConvertDateTimeToStructTm(×pec, &tim, NULL);
|
||||
time_t result = mktime(&tim);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void SetTimeUnixSec(time_t unix_time) {
|
||||
struct tm tim;
|
||||
tm tim;
|
||||
|
||||
#if defined __GNUC__
|
||||
struct tm *canary;
|
||||
/* If the conversion is successful the function returns a pointer
|
||||
to the object the result was written into.*/
|
||||
canary = localtime_r(&unix_time, &tim);
|
||||
osalDbgCheck(&tim == canary);
|
||||
tm *canary;
|
||||
/* If the conversion is successful the function returns a pointer
|
||||
to the object the result was written into.*/
|
||||
canary = localtime_r(&unix_time, &tim);
|
||||
osalDbgCheck(&tim == canary);
|
||||
#else
|
||||
struct tm *t = localtime(&unix_time);
|
||||
memcpy(&tim, t, sizeof(struct tm));
|
||||
tm *t = localtime(&unix_time);
|
||||
memcpy(&tim, t, sizeof(tm));
|
||||
#endif
|
||||
|
||||
rtcConvertStructTmToDateTime(&tim, 0, ×pec);
|
||||
rtcSetTime(&RTCD1, ×pec);
|
||||
RTCDateTime timespec;
|
||||
rtcConvertStructTmToDateTime(&tim, 0, ×pec);
|
||||
rtcSetTime(&RTCD1, ×pec);
|
||||
}
|
||||
|
||||
static void put2(int offset, char *lcd_str, int value) {
|
||||
|
@ -90,7 +89,7 @@ static void put2(int offset, char *lcd_str, int value) {
|
|||
*/
|
||||
bool dateToStringShort(char *lcd_str) {
|
||||
strcpy(lcd_str, "000000_000000\0");
|
||||
struct tm timp;
|
||||
tm timp;
|
||||
date_get_tm(&timp);
|
||||
if (timp.tm_year < 116 || timp.tm_year > 130) {
|
||||
// 2016 to 2030 is the valid range
|
||||
|
@ -98,13 +97,13 @@ bool dateToStringShort(char *lcd_str) {
|
|||
return false;
|
||||
}
|
||||
|
||||
put2(0, lcd_str, timp.tm_year % 100); // Years since 1900 - format as just the last two digits
|
||||
put2(2, lcd_str, timp.tm_mon + 1); // months since January 0-11
|
||||
put2(4, lcd_str, timp.tm_mday); // day of the month 1-31
|
||||
put2(0, lcd_str, timp.tm_year % 100); // Years since 1900 - format as just the last two digits
|
||||
put2(2, lcd_str, timp.tm_mon + 1); // months since January 0-11
|
||||
put2(4, lcd_str, timp.tm_mday); // day of the month 1-31
|
||||
|
||||
put2(7, lcd_str, timp.tm_hour); // hours since midnight 0-23
|
||||
put2(9, lcd_str, timp.tm_min); // Minutes
|
||||
put2(11, lcd_str, timp.tm_sec); // seconds
|
||||
put2(7, lcd_str, timp.tm_hour); // hours since midnight 0-23
|
||||
put2(9, lcd_str, timp.tm_min); // Minutes
|
||||
put2(11, lcd_str, timp.tm_sec); // seconds
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -116,7 +115,7 @@ void dateToString(char *lcd_str) {
|
|||
// 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;
|
||||
tm timp;
|
||||
date_get_tm(&timp); // get RTC date/time
|
||||
|
||||
put2(0, lcd_str, timp.tm_mon + 1);
|
||||
|
@ -126,19 +125,22 @@ void dateToString(char *lcd_str) {
|
|||
put2(12, lcd_str, timp.tm_sec);
|
||||
}
|
||||
|
||||
void printDateTime(void) {
|
||||
static time_t unix_time;
|
||||
struct tm timp;
|
||||
|
||||
unix_time = GetTimeUnixSec();
|
||||
static const char* const months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
||||
|
||||
void printDateTime() {
|
||||
tm timp;
|
||||
|
||||
time_t unix_time = GetTimeUnixSec();
|
||||
if (unix_time == -1) {
|
||||
efiPrintf("incorrect time in RTC cell");
|
||||
efiPrintf("invalid time in RTC cell");
|
||||
} else {
|
||||
efiPrintf("%D - unix time", unix_time);
|
||||
efiPrintf("Current Unix time: %d", unix_time);
|
||||
date_get_tm(&timp);
|
||||
|
||||
efiPrintf("Current RTC localtime is: %04u-%02u-%02u %02u:%02u:%02u w=%d", timp.tm_year + 1900, timp.tm_mon + 1, timp.tm_mday, timp.tm_hour,
|
||||
timp.tm_min, timp.tm_sec, rtcWorks);
|
||||
auto month = months[timp.tm_mon];
|
||||
|
||||
// Prints something like "19 Sep 2022 21:19:55"
|
||||
efiPrintf("Current RTC time is: %02u %s %04u %02u:%02u:%02u", timp.tm_mday, month, timp.tm_year + 1900, timp.tm_hour, timp.tm_min, timp.tm_sec);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,9 +169,11 @@ void dateToString(char *lcd_str) {
|
|||
|
||||
#endif
|
||||
|
||||
void initRtc(void) {
|
||||
void initRtc() {
|
||||
#if EFI_RTC
|
||||
GetTimeUnixSec(); // this would test RTC, see 'rtcWorks' variable, see #311
|
||||
efiPrintf("initRtc()");
|
||||
|
||||
printDateTime();
|
||||
#endif /* EFI_RTC */
|
||||
}
|
||||
|
|
|
@ -8,9 +8,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
void printDateTime(void);
|
||||
void printDateTime();
|
||||
void setDateTime(const char *strDate);
|
||||
void initRtc(void);
|
||||
void initRtc();
|
||||
void date_set_tm(struct tm *);
|
||||
void date_get_tm(struct tm *);
|
||||
void dateToString(char *buffer);
|
||||
|
|
Loading…
Reference in New Issue