only:timer.h goes to libfirmware!

This commit is contained in:
rusefi 2023-08-30 23:11:24 -04:00
parent b85ce91479
commit 78e48d3718
22 changed files with 18 additions and 220 deletions

View File

@ -1,7 +1,7 @@
#pragma once
#include "ac_control_generated.h"
#include "timer.h"
#include <rusefi/timer.h>
class AcController final : public ac_control_s, public EngineModule {
public:

View File

@ -6,7 +6,7 @@
#include "engine_module.h"
#include "fuel_pump_control_generated.h"
#include "timer.h"
#include <rusefi/timer.h>
class FuelPumpController : public EngineModule, public fuel_pump_control_s {
public:

View File

@ -7,7 +7,7 @@
#pragma once
#include "timer.h"
#include <rusefi/timer.h>
#include "antilag_system_state_generated.h"
void initAntilagSystem();

View File

@ -8,7 +8,7 @@
#pragma once
#include "static_vector.h"
#include "timer.h"
#include <rusefi/timer.h>
#define MOCK_ADC_SIZE 26

View File

@ -4,7 +4,7 @@
#pragma once
#include "engine_module.h"
#include "timer.h"
#include <rusefi/timer.h>
// DFCO = deceleration fuel cut off, ie, save gas when your foot is off the pedal
class DfcoController : public EngineModule {

View File

@ -7,7 +7,7 @@
#pragma once
#include "timer.h"
#include <rusefi/timer.h>
#include "launch_control_state_generated.h"
void initLaunchControl();

View File

@ -32,26 +32,6 @@
#define STOICH_RATIO 14.7f
#define CONST_PI 3.14159265358979323846
// time in seconds
using efitimesec_t = time_t;
/**
* We use a signed type here so that subtraction result is a proper negative value.
* A typical use-case negative result is when we do 'timeNow() - timeOfEvent' where timeOfEvent
* is actually after timeNow() due to interrupt context switches
*
* See getTimeNowNt()
* See US2NT
* See MS2US
*/
/**
* 64 bit time in microseconds (1/1_000_000 of a second), since boot
*/
using efitimeus_t = int64_t;
/**
* 64 bit time in milliseconds (1/1_000 of a second), since boot
*/

View File

@ -10,7 +10,7 @@
#include "scheduler.h"
#include "stored_value_sensor.h"
#include "timer.h"
#include <rusefi/timer.h>
#include "rpm_calculator_api.h"
#include "trigger_decoder.h"

View File

@ -2,7 +2,7 @@
#include "engine_module.h"
#include "timer.h"
#include <rusefi/timer.h>
class IgnitionController : public EngineModule {
public:

View File

@ -2,7 +2,7 @@
* @file frequency_sensor.h
*/
#include "functional_sensor.h"
#include "timer.h"
#include <rusefi/timer.h>
#include "biquad.h"
class FrequencySensor : public FunctionalSensor {

View File

@ -5,7 +5,7 @@
#pragma once
#include "timer.h"
#include <rusefi/timer.h>
void doScheduleStopEngine();

View File

@ -11,7 +11,7 @@
#include "io_pins.h"
#include "persistent_configuration.h"
#include "engine_configuration_generated_structures.h"
#include "timer.h"
#include <rusefi/timer.h>
#if EFI_TCU
class TransmissionControllerBase {

View File

@ -12,7 +12,7 @@
#include "trigger_decoder.h"
#include "instant_rpm_calculator.h"
#include "trigger_central_generated.h"
#include "timer.h"
#include <rusefi/timer.h>
#include "pin_repository.h"
#include "local_version_holder.h"
#include "cyclic_buffer.h"

View File

@ -10,7 +10,7 @@
#include "trigger_structure.h"
#include "trigger_state_generated.h"
#include "trigger_state_primary_generated.h"
#include "timer.h"
#include <rusefi/timer.h>
const char *getTrigger_event_e(trigger_event_e value);
const char *getTrigger_value_e(TriggerValue value);

@ -1 +1 @@
Subproject commit 946a418680df7fbb298016f45c4160d79c18c89c
Subproject commit 6af2fd11d7d35abdd893ab5520409c7c3ba29ee7

View File

@ -12,36 +12,18 @@
#include "efifeatures.h"
#include "rusefi_types.h"
#include "global.h"
#include <rusefi/rusefi_time_math.h>
#if EFI_PROD_CODE
// for US_TO_NT_MULTIPLIER which is port-specific
#include "port_mpu_util.h"
#endif
#define MS_PER_SECOND 1000
#define US_PER_SECOND 1000000
#define US_PER_SECOND_F 1000000.0
#define US_PER_SECOND_LL 1000000LL
#define NT_PER_SECOND (US2NT(US_PER_SECOND_LL))
#define MS2US(MS_TIME) ((MS_TIME) * 1000)
#define US2MS(US_TIME) ((US_TIME) / 1000)
// microseconds to ticks
// since only about 20 seconds of ticks fit in 32 bits this macro is casting parameter into 64 bits 'efitick_t' type
// please note that int64 <-> float is a heavy operation thus we have 'USF2NT' below
#define US2NT(us) (((efitick_t)(us)) * US_TO_NT_MULTIPLIER)
// microseconds to ticks, but floating point
// If converting a floating point time period, use this macro to avoid
// the expensive conversions from int64 <-> float
#define USF2NT(us_float) ((us_float) * US_TO_NT_MULTIPLIER)
#define USF2MS(us_float) (0.001f * (us_float))
// And back
#define NT2US(x) ((x) / US_TO_NT_MULTIPLIER)
#define NT2USF(x) (((float)(x)) / US_TO_NT_MULTIPLIER)
// milliseconds to ticks
#define MS2NT(msTime) US2NT(MS2US(msTime))
// See USF2NT above for when to use MSF2NT
@ -85,26 +67,6 @@ private:
*/
uint32_t getTimeNowLowerNt();
/**
* 64-bit counter CPU/timer cycles since MCU reset
*
* See getTimeNowLowerNt for a quicker version which returns only lower 32 bits
* Lower 32 bits are enough if all we need is to measure relatively short time durations
* (BTW 2^32 cpu cycles at 168MHz is 25.59 seconds)
*/
efitick_t getTimeNowNt();
/**
* 64-bit counter of microseconds (1/1 000 000 of a second) since MCU reset
*
* By using 64 bit, we can achieve a very precise timestamp which does not overflow.
* The primary implementation counts the number of CPU cycles from MCU reset.
*
* WARNING: you should use getTimeNowNt where possible for performance reasons.
* The heaviest part is '__aeabi_ildivmod' - non-native 64 bit division
*/
efitimeus_t getTimeNowUs();
/**
* @brief Returns the number of milliseconds since the board initialization.
*/

View File

@ -1,82 +0,0 @@
#include "pch.h"
#include "timer.h"
Timer::Timer() {
init();
}
void Timer::reset() {
m_lastReset = getTimeNowNt();
}
void Timer::init() {
m_lastReset = INT64_MIN / 8;
}
void Timer::reset(efitick_t nowNt) {
m_lastReset = nowNt;
}
bool Timer::hasElapsedSec(float seconds) const {
return hasElapsedMs(seconds * 1000);
}
bool Timer::hasElapsedMs(float milliseconds) const {
return hasElapsedUs(milliseconds * 1000);
}
bool Timer::hasElapsedUs(float microseconds) const {
auto delta = getTimeNowNt() - m_lastReset;
// If larger than 32 bits, timer has certainly expired
if (delta >= UINT32_MAX) {
return true;
}
auto delta32 = (uint32_t)delta;
return delta32 > USF2NT(microseconds);
}
float Timer::getElapsedSeconds() const {
return getElapsedSeconds(getTimeNowNt());
}
float Timer::getElapsedSeconds(efitick_t nowNt) const {
return 1 / US_PER_SECOND_F * getElapsedUs(nowNt);
}
float Timer::getElapsedUs() const {
return getElapsedUs(getTimeNowNt());
}
uint32_t Timer::getElapsedNt(efitick_t nowNt) const {
auto deltaNt = nowNt - m_lastReset;
// Yes, things can happen slightly in the future if we get a lucky interrupt between
// the timestamp and this subtraction, that updates m_lastReset to what's now "the future",
// resulting in a negative delta.
if (deltaNt < 0) {
return 0;
}
if (deltaNt > UINT32_MAX - 1) {
deltaNt = UINT32_MAX - 1;
}
return deltaNt;
}
float Timer::getElapsedUs(efitick_t nowNt) const {
auto delta32 = getElapsedNt(nowNt);
return NT2US(delta32);
}
float Timer::getElapsedSecondsAndReset(efitick_t nowNt) {
float result = getElapsedSeconds(nowNt);
reset(nowNt);
return result;
}

View File

@ -1,49 +0,0 @@
/**
* @file timer.h
*/
#pragma once
#include <rusefi/rusefi_time_types.h>
/**
* Helper class with "has X amount of time elapsed since most recent reset" methods
* Brand new instances have most recent reset time far in the past, i.e. "hasElapse" is true for any reasonable range
*/
class Timer final {
public:
Timer();
void reset();
// Reset the timer to a known timestamp (don't take a timestamp internally)
void reset(efitick_t nowNt);
// returns timer to the most original-as-constructed state
void init();
bool hasElapsedSec(float seconds) const;
bool hasElapsedMs(float ms) const;
bool hasElapsedUs(float us) const;
// Return the elapsed time since the last reset.
// If the elapsed time is longer than 2^32 timer tick counts,
// then a time period representing 2^32 counts will be returned.
float getElapsedSeconds() const;
float getElapsedSeconds(efitick_t nowNt) const;
float getElapsedUs() const;
// WOW yes returns US while parameter is NT
float getElapsedUs(efitick_t nowNt) const;
// too many options for the API probably?
uint32_t getElapsedNt(efitick_t nowNt) const;
// Perform an atomic update event based on the passed timestamp,
// returning the delta between the last reset and the provided timestamp
float getElapsedSecondsAndReset(efitick_t nowNt);
efitick_t get() {
return m_lastReset;
}
private:
// Use not-quite-minimum value to avoid overflow
efitick_t m_lastReset;
};

View File

@ -17,7 +17,7 @@ UTILSRC_CPP = \
$(PROJECT_DIR)/util/loggingcentral.cpp \
$(PROJECT_DIR)/util/cli_registry.cpp \
$(PROJECT_DIR)/util/efilib.cpp \
$(PROJECT_DIR)/util/timer.cpp \
$(RUSEFI_LIB)/util/src/timer.cpp \
$(UTIL_DIR)/os_util.cpp \

View File

@ -41,6 +41,7 @@ CPPSRC += $(ALLCPPSRC) \
$(PROJECT_DIR)/../unit_tests/main.cpp \
$(PROJECT_DIR)/../unit_tests/global_mocks.cpp \
$(PROJECT_DIR)/../unit_tests/mocks.cpp \
$(RUSEFI_LIB)/mock/lib-time-mocks.cpp \
$(RUSEFI_LIB_CPP) \
$(RUSEFI_LIB_CPP_TEST) \

View File

@ -8,20 +8,6 @@
bool verboseMode = false;
int timeNowUs = 0;
efitimeus_t getTimeNowUs() {
return timeNowUs;
}
efitimesec_t getTimeNowS() {
return getTimeNowUs() / 1000 / 1000;
}
efitick_t getTimeNowNt() {
return getTimeNowUs() * US_TO_NT_MULTIPLIER;
}
void initLogging(LoggingWithStorage *logging, const char *name) {
}

View File

@ -1,5 +1,5 @@
#include "pch.h"
#include "timer.h"
#include <rusefi/timer.h>
TEST(util, timer) {
EngineTestHelper eth(engine_type_e::TEST_ENGINE);