2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file efitime.h
|
|
|
|
*
|
|
|
|
* By the way, there are 86400000 milliseconds in a day
|
|
|
|
*
|
|
|
|
* @date Apr 14, 2014
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2020-01-20 22:40:11 -08:00
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "efifeatures.h"
|
|
|
|
#include "rusefi_types.h"
|
2022-10-26 07:37:07 -07:00
|
|
|
#include "global.h"
|
2024-05-12 07:44:04 -07:00
|
|
|
#include "error_handling.h"
|
2023-08-30 20:11:24 -07:00
|
|
|
#include <rusefi/rusefi_time_math.h>
|
2024-05-12 07:44:04 -07:00
|
|
|
#include <limits.h>
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2022-09-11 10:42:07 -07:00
|
|
|
#if EFI_PROD_CODE
|
2022-09-11 10:06:03 -07:00
|
|
|
// for US_TO_NT_MULTIPLIER which is port-specific
|
|
|
|
#include "port_mpu_util.h"
|
2022-09-11 10:42:07 -07:00
|
|
|
#endif
|
2022-09-11 10:06:03 -07:00
|
|
|
|
2024-04-29 12:44:09 -07:00
|
|
|
inline int time2print(int64_t time) {
|
|
|
|
return static_cast<int>(time);
|
|
|
|
}
|
|
|
|
|
2024-05-12 07:44:04 -07:00
|
|
|
inline int32_t assertFloatFitsInto32BitsAndCast(const char *msg, float value) {
|
|
|
|
if (value < INT_MIN || value > INT_MAX) {
|
|
|
|
criticalError("%s value out of range: %f", msg, value);
|
|
|
|
}
|
|
|
|
return (int32_t)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline /*64bit*/ efitick_t sumTickAndFloat(/*64bit*/efitick_t ticks, /*32bit*/float extra) {
|
2024-05-11 20:35:21 -07:00
|
|
|
// we have a peculiar case of type compiler uses to evaluate expression vs precision here
|
|
|
|
// we need 64 bit precision not (lower) float precision
|
2024-05-11 12:05:42 -07:00
|
|
|
// 32 bits is 11 or 23 seconds if US_TO_NT_MULTIPLIER = 168 like on kinetis/cypress
|
|
|
|
// 32 bits is 500 or 1000 seconds if US_TO_NT_MULTIPLIER = 4 like on stm32
|
|
|
|
// 'extra' is below 10 seconds here so we use 32 bit type for performance reasons
|
|
|
|
return ticks + (int32_t) extra;
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:33:46 -08:00
|
|
|
// microseconds to ticks
|
2022-09-11 13:08:11 -07:00
|
|
|
// since only about 20 seconds of ticks fit in 32 bits this macro is casting parameter into 64 bits 'efitick_t' type
|
2020-12-03 07:31:16 -08:00
|
|
|
// please note that int64 <-> float is a heavy operation thus we have 'USF2NT' below
|
2022-09-11 13:08:11 -07:00
|
|
|
#define US2NT(us) (((efitick_t)(us)) * US_TO_NT_MULTIPLIER)
|
2020-12-03 07:26:50 -08:00
|
|
|
|
2019-12-21 18:18:38 -08:00
|
|
|
// milliseconds to ticks
|
|
|
|
#define MS2NT(msTime) US2NT(MS2US(msTime))
|
2024-05-11 12:15:18 -07:00
|
|
|
// See USF2NT above for when to use MSF2NT. ***WARNING*** please be aware of sumTickAndFloat
|
2020-11-23 20:33:46 -08:00
|
|
|
#define MSF2NT(msTimeFloat) USF2NT(MS2US(msTimeFloat))
|
2019-12-21 18:18:38 -08:00
|
|
|
|
2022-09-11 10:06:03 -07:00
|
|
|
/**
|
|
|
|
* Get a monotonically increasing (but wrapping) 32-bit timer value
|
|
|
|
* Implemented at port level, based on timer or CPU tick counter
|
|
|
|
* Main source of EFI clock, SW-extended to 64bits
|
2024-04-03 11:50:21 -07:00
|
|
|
*
|
|
|
|
* 2147483648 / ~168MHz = ~12 seconds to overflow
|
|
|
|
*
|
2022-09-11 10:06:03 -07:00
|
|
|
*/
|
|
|
|
uint32_t getTimeNowLowerNt();
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2024-04-03 11:50:21 -07:00
|
|
|
* @brief Returns the 32 bit number of milliseconds since the board initialization.
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2022-09-11 10:06:03 -07:00
|
|
|
efitimems_t getTimeNowMs();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
2024-04-03 11:50:21 -07:00
|
|
|
* @brief Current system time in seconds (32 bits)
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2022-09-11 10:06:03 -07:00
|
|
|
efitimesec_t getTimeNowS();
|
2024-04-27 07:06:14 -07:00
|
|
|
|
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
void setTimeNowUs(int us);
|
|
|
|
void advanceTimeUs(int us);
|
|
|
|
#endif
|