From 372eb51f371adb1e0556176bccf536446275cbd0 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 31 Mar 2019 17:14:53 -0400 Subject: [PATCH] The Big Refactoring of 2019: folder structure #723 --- firmware/controllers/engine_controller.cpp | 1 + firmware/util/containers/counter64.cpp | 42 ++++++++++++++++++++++ firmware/util/containers/counter64.h | 34 ++++++++++++++++++ firmware/util/containers/data_buffer.h | 2 +- firmware/util/efilib2.cpp | 33 ----------------- firmware/util/efilib2.h | 20 ----------- firmware/util/util.mk | 1 + unit_tests/tests/test_util.cpp | 1 + 8 files changed, 80 insertions(+), 54 deletions(-) create mode 100644 firmware/util/containers/counter64.cpp create mode 100644 firmware/util/containers/counter64.h diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index 4894e75dab..f879c8fd40 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -58,6 +58,7 @@ #include "settings.h" #include "aux_pid.h" #include "accelerometer.h" +#include "counter64.h" #if HAL_USE_ADC || defined(__DOXYGEN__) #include "AdcConfiguration.h" diff --git a/firmware/util/containers/counter64.cpp b/firmware/util/containers/counter64.cpp new file mode 100644 index 0000000000..62cb45a752 --- /dev/null +++ b/firmware/util/containers/counter64.cpp @@ -0,0 +1,42 @@ +/* + * counter64.cpp + * + * Created on: Mar 31, 2019 + * @author Andrey Belomutskiy, (c) 2012-2019 + */ + +#include "counter64.h" + +/** + * The main use-case of this class is to keep track of a 64-bit global number of CPU ticks from reset. + * + * stm32f4 hardware has a 32-bit Cycle Count Register (CYCCNT), which is incremented with every CPU cycle. + * With 32 bits and 168MHz speed this counter overflows every 4B/168M = 23 seconds. The job of this class is to + * keep track of the current CYCCNT value, detect these overflows, and provide a nice, + * clean 64 bit global cycle counter. + * + * In order for this to function, it's your responsibility to invoke offer() method at least once a second. + */ +Overflow64Counter::Overflow64Counter() { + state.highBits = 0; + state.lowBits = 0; +} + +/** + * in order to have atomic writes this should be invoked within a critical section + */ +void updateAndSet(State64 *state, uint32_t value) { + if (value < state->lowBits) { + // new value less than previous value means there was an overflow in that 32 bit counter + state->highBits += 0x100000000LL; + } + state->lowBits = value; +} + +#if EFI_UNIT_TEST +efitime_t Overflow64Counter::update(uint32_t value) { + updateAndSet(&state, value); + return state.highBits + state.lowBits; +} +#endif + diff --git a/firmware/util/containers/counter64.h b/firmware/util/containers/counter64.h new file mode 100644 index 0000000000..0422e8905a --- /dev/null +++ b/firmware/util/containers/counter64.h @@ -0,0 +1,34 @@ +/* + * counter64.h + * + * Created on: Mar 31, 2019 + * @author Andrey Belomutskiy, (c) 2012-2019 + */ + +#ifndef UTIL_CONTAINERS_COUNTER64_H_ +#define UTIL_CONTAINERS_COUNTER64_H_ + +#include "global.h" + +typedef struct { + // todo: would probably be better to keep the high bits as 32 bit field to be sure + volatile efitime_t highBits; + volatile uint32_t lowBits; +} State64; + +void updateAndSet(State64 *state, uint32_t value); + +class Overflow64Counter +{ + public: + Overflow64Counter(); + + efitime_t get(); +#if EFI_UNIT_TEST + efitime_t update(uint32_t value); +#endif + + State64 state; +}; + +#endif /* UTIL_CONTAINERS_COUNTER64_H_ */ diff --git a/firmware/util/containers/data_buffer.h b/firmware/util/containers/data_buffer.h index ea6a774ed0..865066f94d 100644 --- a/firmware/util/containers/data_buffer.h +++ b/firmware/util/containers/data_buffer.h @@ -2,7 +2,7 @@ * data_buffer.h * * @date Dec 8, 2012 - * @author Andrey Belomutskiy, (c) 2012-2017 + * @author Andrey Belomutskiy, (c) 2012-2019 */ #ifndef DATA_BUFFER_H_ diff --git a/firmware/util/efilib2.cpp b/firmware/util/efilib2.cpp index ded927ce9c..e933357039 100644 --- a/firmware/util/efilib2.cpp +++ b/firmware/util/efilib2.cpp @@ -7,39 +7,6 @@ #include "efilib2.h" -/** - * The main use-case of this class is to keep track of a 64-bit global number of CPU ticks from reset. - * - * stm32f4 hardware has a 32-bit Cycle Count Register (CYCCNT), which is incremented with every CPU cycle. - * With 32 bits and 168MHz speed this counter overflows every 4B/168M = 23 seconds. The job of this class is to - * keep track of the current CYCCNT value, detect these overflows, and provide a nice, - * clean 64 bit global cycle counter. - * - * In order for this to function, it's your responsibility to invoke offer() method at least once a second. - */ -Overflow64Counter::Overflow64Counter() { - state.highBits = 0; - state.lowBits = 0; -} - -/** - * in order to have atomic writes this should be invoked within a critical section - */ -void updateAndSet(State64 *state, uint32_t value) { - if (value < state->lowBits) { - // new value less than previous value means there was an overflow in that 32 bit counter - state->highBits += 0x100000000LL; - } - state->lowBits = value; -} - -#if EFI_UNIT_TEST -efitime_t Overflow64Counter::update(uint32_t value) { - updateAndSet(&state, value); - return state.highBits + state.lowBits; -} -#endif - /** * See also getRemainingStack() */ diff --git a/firmware/util/efilib2.h b/firmware/util/efilib2.h index e311d48e0e..18bbd49895 100644 --- a/firmware/util/efilib2.h +++ b/firmware/util/efilib2.h @@ -10,31 +10,11 @@ #include "global.h" -typedef struct { - // todo: would probably be better to keep the high bits as 32 bit field to be sure - volatile efitime_t highBits; - volatile uint32_t lowBits; -} State64; - -void updateAndSet(State64 *state, uint32_t value); /** * @return for a given stack memory region returns how much stack was ever used */ int getMaxUsedStack(uint8_t *ptr, int size); -class Overflow64Counter -{ - public: - Overflow64Counter(); - - efitime_t get(); -#if EFI_UNIT_TEST - efitime_t update(uint32_t value); -#endif - - State64 state; -}; - #if (EFI_PROD_CODE || EFI_SIMULATOR) #define GET_TIMESTAMP() port_rt_get_counter_value() #else diff --git a/firmware/util/util.mk b/firmware/util/util.mk index 6274a40dfa..a2dfb23474 100644 --- a/firmware/util/util.mk +++ b/firmware/util/util.mk @@ -9,6 +9,7 @@ UTILSRC_CPP = $(UTIL_DIR)/containers/cyclic_buffer.cpp \ $(PROJECT_DIR)/util/datalogging.cpp \ $(PROJECT_DIR)/util/loggingcentral.cpp \ $(UTIL_DIR)/containers/listener_array.cpp \ + $(UTIL_DIR)/containers/counter64.cpp \ $(PROJECT_DIR)/util/cli_registry.cpp \ $(PROJECT_DIR)/util/pid.cpp \ $(PROJECT_DIR)/util/biquad.cpp \ diff --git a/unit_tests/tests/test_util.cpp b/unit_tests/tests/test_util.cpp index 4a6714828d..a7cd54cc12 100644 --- a/unit_tests/tests/test_util.cpp +++ b/unit_tests/tests/test_util.cpp @@ -22,6 +22,7 @@ #include "crc.h" #include "fl_stack.h" #include "io_pins.h" +#include "counter64.h" #include "efi_gpio.h" #include "efilib.h"