diff --git a/CMakeLists.txt b/CMakeLists.txt index 36194644..9dfdc9a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,7 +272,6 @@ set(HALAL_CPP_NO_ETH ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/DMA/DMA2.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/PinModel/Pin.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/TimerDomain/TimerDomain.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp # ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/ADC/ADC.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/CORDIC/CORDIC.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Communication/FDCAN/FDCAN.cpp @@ -282,20 +281,13 @@ set(HALAL_CPP_NO_ETH ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/DigitalInputService/DigitalInputService.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/DigitalOutputService/DigitalOutputService.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/EXTI/EXTI.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Encoder/Encoder.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/FMAC/FMAC.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Flash/Flash.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Flash/FlashTests/Flash_Test.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/InfoWarning/InfoWarning.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/InputCapture/InputCapture.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/PWM/DualPWM/DualPWM.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/PWM/PWM/PWM.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/PWM/PhasedPWM/PhasedPWM.cpp + # ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/InputCapture/InputCapture.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Time/RTC.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Time/Scheduler.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Time/Time.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Watchdog/Watchdog.cpp ) @@ -363,18 +355,14 @@ set(STLIB_LOW_CPP_ETH set(STLIB_LOW_C_NO_ETH) set(STLIB_LOW_CPP_NO_ETH - ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Clocks/Counter.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Clocks/Stopwatch.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/DigitalOutput/DigitalOutput.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/ErrorHandler/ErrorHandler.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/HalfBridge/HalfBridge.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Math/Math.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/ST-LIB_LOW.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sd/Sd.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sensors/DigitalSensor/DigitalSensor.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sensors/LookupSensor/LookupSensor.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sensors/NTC/NTC.cpp - ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sensors/Sensor/Sensor.cpp ${CMAKE_CURRENT_LIST_DIR}/Src/ST-LIB_LOW/Sensors/SensorInterrupt/SensorInterrupt.cpp ) diff --git a/Inc/HALAL/HALAL.hpp b/Inc/HALAL/HALAL.hpp index ceedb7ef..bd26e7d0 100644 --- a/Inc/HALAL/HALAL.hpp +++ b/Inc/HALAL/HALAL.hpp @@ -14,11 +14,9 @@ #include "HALAL/Services/ADC/NewADC.hpp" -#include "HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp" -#include "HALAL/Services/PWM/DualPWM/DualPWM.hpp" -#include "HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp" -#include "HALAL/Services/PWM/PWM/PWM.hpp" -#include "HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp" +// To be implemented +// #include "HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp" +// #include "HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp" #include "HALAL/Services/PWM/DualPWM.hpp" #include "HALAL/Services/PWM/PWM.hpp" @@ -30,8 +28,7 @@ // #include "HALAL/Services/Encoder/Encoder.hpp" #include "HALAL/Services/EXTI/EXTI.hpp" #include "HALAL/Services/Encoder/Encoder.hpp" -#include "HALAL/Services/Encoder/NewEncoder.hpp" -#include "HALAL/Services/InputCapture/InputCapture.hpp" +// #include "HALAL/Services/InputCapture/InputCapture.hpp" // To be implemented #include "HALAL/Services/Communication/FDCAN/FDCAN.hpp" #include "HALAL/Services/Communication/I2C/I2C.hpp" diff --git a/Inc/HALAL/Models/TimerPeripheral/TimerPeripheral.hpp b/Inc/HALAL/Models/TimerPeripheral/TimerPeripheral.hpp.old similarity index 100% rename from Inc/HALAL/Models/TimerPeripheral/TimerPeripheral.hpp rename to Inc/HALAL/Models/TimerPeripheral/TimerPeripheral.hpp.old diff --git a/Inc/HALAL/Services/Communication/SNTP/SNTP.hpp b/Inc/HALAL/Services/Communication/SNTP/SNTP.hpp index 987b09e7..16e79391 100644 --- a/Inc/HALAL/Services/Communication/SNTP/SNTP.hpp +++ b/Inc/HALAL/Services/Communication/SNTP/SNTP.hpp @@ -9,7 +9,6 @@ #include "sntp.h" #include "HALAL/Models/IPV4/IPV4.hpp" -#include "HALAL/Services/Time/Time.hpp" #include "C++Utilities/CppUtils.hpp" class SNTP { diff --git a/Inc/HALAL/Services/EXTI/EXTI.hpp b/Inc/HALAL/Services/EXTI/EXTI.hpp index 77daaefd..83bef263 100644 --- a/Inc/HALAL/Services/EXTI/EXTI.hpp +++ b/Inc/HALAL/Services/EXTI/EXTI.hpp @@ -149,4 +149,4 @@ struct EXTIDomain { } // namespace ST_LIB -#endif // EXTI_HPP \ No newline at end of file +#endif // EXTI_HPP diff --git a/Inc/HALAL/Services/Encoder/Encoder.hpp b/Inc/HALAL/Services/Encoder/Encoder.hpp index 52671ee3..b4137003 100644 --- a/Inc/HALAL/Services/Encoder/Encoder.hpp +++ b/Inc/HALAL/Services/Encoder/Encoder.hpp @@ -1,84 +1,114 @@ -/* - * Encoder.hpp - * - * Created on: 27 oct. 2022 - * Author: Pablo - */ - -#pragma once - -#include "HALAL/Models/PinModel/Pin.hpp" -#include "HALAL/Models/TimerPeripheral/TimerPeripheral.hpp" -#include "C++Utilities/CppUtils.hpp" -#include "ErrorHandler/ErrorHandler.hpp" - -#define NANO_SECOND 1000000000.0 -#define CLOCK_MAX_VALUE 4294967295 // here goes the tim23 counter period - -#ifdef HAL_TIM_MODULE_ENABLED -/** - * @brief Encoder service class. Abstracts the use of the encoder with the HAL library. - * - */ -class Encoder { -public: - static uint8_t id_counter; - static map, TimerPeripheral*> pin_timer_map; - static map> registered_encoder; - - /** - * @brief This method registers a new encoder - * - * @param pin1 First pin of the encoder - * @param pin2 Second pin of the encoder - * - * @retval uint8_t Id of the service - */ - static uint8_t inscribe(Pin& pin1, Pin& pin2); - - static void start(); - - /** - * @brief Starts the timer of the encoder - * - * @param id Id of the encoder - */ - static void turn_on(uint8_t id); - - /** - * @brief Stop the timer of the encoder - * - * @param id Id of the encoder - */ - static void turn_off(uint8_t id); - - /** - * @brief Resets the encoder by setting the CNT register to 0 - * - * @param id Id of the encoder - */ - static void reset(uint8_t id); - - /** - * @brief Get the CNT value of the encoder - * - * @param id Id of the encoder - * @return uint32_t CNT value if the id is valid - */ - static uint32_t get_counter(uint8_t id); - - /** - * @brief Get the encoder direction - * - * @param id Id - * @return bool Encoder direction if id is valid - */ - static bool get_direction(uint8_t id); - - static void init(TimerPeripheral* encoder); - - static uint32_t get_initial_counter_value(uint8_t id); - - static int64_t get_delta_clock(uint64_t clock_time, uint64_t last_clock_time); -}; -#endif +#pragma once + +#include "HALAL/Models/TimerDomain/TimerDomain.hpp" + +#ifdef HAL_TIM_MODULE_ENABLED + +#define NANO_SECOND 1000000000.0 +#define CLOCK_MAX_VALUE 4294967295 // here goes the tim23 counter period + +namespace ST_LIB { + +template struct TimerWrapper; + +template class Encoder { + static_assert( + dev.e.pin_count == 2, + "Encoder must have exactly 2 encoder pins, as it uses the whole timer" + ); + static_assert(dev.e.pins[0].af == TimerAF::Encoder, "Pin 0 must be declared as encoder"); + static_assert(dev.e.pins[1].af == TimerAF::Encoder, "Pin 1 must be declared as encoder"); + static_assert( + dev.e.pins[0].channel != dev.e.pins[1].channel, + "Pins must be of different channels" + ); + + inline static TimerWrapper* timer; + inline static bool is_on = false; + +public: + Encoder(TimerWrapper* tim) { + if (timer == nullptr) { + init(tim); + } + } + + static void init(TimerWrapper* tim) { + timer = tim; + TIM_Encoder_InitTypeDef sConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + tim->instance->hal_tim->Init.Prescaler = 5; + tim->instance->hal_tim->Init.CounterMode = TIM_COUNTERMODE_UP; + tim->instance->hal_tim->Init.Period = 55000; + tim->instance->hal_tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + tim->instance->hal_tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + + sConfig.EncoderMode = TIM_ENCODERMODE_TI12; + sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; + sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; + sConfig.IC1Prescaler = TIM_ICPSC_DIV1; + sConfig.IC1Filter = 0; + sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; + sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; + sConfig.IC2Prescaler = TIM_ICPSC_DIV1; + sConfig.IC2Filter = 0; + + if (HAL_TIM_Encoder_Init(tim->instance->hal_tim, &sConfig) != HAL_OK) { + ErrorHandler("Unable to init encoder"); + } + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(tim->instance->hal_tim, &sMasterConfig) != + HAL_OK) { + ErrorHandler("Unable to config master synchronization in encoder"); + } + } + + static void turn_on() { + if (is_on) + return; + + if (HAL_TIM_Encoder_GetState(timer->instance->hal_tim) == HAL_TIM_STATE_RESET) { + ErrorHandler("Unable to get state from encoder"); + return; + } + if (HAL_TIM_Encoder_Start(timer->instance->hal_tim, TIM_CHANNEL_ALL) != HAL_OK) { + ErrorHandler("Unable to start encoder"); + return; + } + is_on = true; + reset(); + } + + static void turn_off() { + if (!is_on) + return; + if (HAL_TIM_Encoder_Stop(timer->instance->hal_tim, TIM_CHANNEL_ALL) != HAL_OK) { + ErrorHandler("Unable to stop encoder"); + } + is_on = false; + } + + static inline void reset() { timer->instance->tim->CNT = UINT32_MAX / 2; } + + static inline uint32_t get_counter() { return timer->instance->tim->CNT; } + + static inline bool get_direction() { return ((timer->instance->tim->CR1 & 0b10000) >> 4); } + + static inline uint32_t get_initial_counter_value() { return timer->instance->tim->ARR / 2; } + + static int64_t get_delta_clock(uint64_t clock_time, uint64_t last_clock_time) { + int64_t delta_clock = clock_time - last_clock_time; + if (clock_time < last_clock_time) { // overflow handle + delta_clock = clock_time + + CLOCK_MAX_VALUE * NANO_SECOND / timer->get_clock_frequency() - + last_clock_time; + } + return delta_clock; + } +}; + +} // namespace ST_LIB +#endif diff --git a/Inc/HALAL/Services/Encoder/NewEncoder.hpp b/Inc/HALAL/Services/Encoder/NewEncoder.hpp deleted file mode 100644 index b4137003..00000000 --- a/Inc/HALAL/Services/Encoder/NewEncoder.hpp +++ /dev/null @@ -1,114 +0,0 @@ -#pragma once - -#include "HALAL/Models/TimerDomain/TimerDomain.hpp" - -#ifdef HAL_TIM_MODULE_ENABLED - -#define NANO_SECOND 1000000000.0 -#define CLOCK_MAX_VALUE 4294967295 // here goes the tim23 counter period - -namespace ST_LIB { - -template struct TimerWrapper; - -template class Encoder { - static_assert( - dev.e.pin_count == 2, - "Encoder must have exactly 2 encoder pins, as it uses the whole timer" - ); - static_assert(dev.e.pins[0].af == TimerAF::Encoder, "Pin 0 must be declared as encoder"); - static_assert(dev.e.pins[1].af == TimerAF::Encoder, "Pin 1 must be declared as encoder"); - static_assert( - dev.e.pins[0].channel != dev.e.pins[1].channel, - "Pins must be of different channels" - ); - - inline static TimerWrapper* timer; - inline static bool is_on = false; - -public: - Encoder(TimerWrapper* tim) { - if (timer == nullptr) { - init(tim); - } - } - - static void init(TimerWrapper* tim) { - timer = tim; - TIM_Encoder_InitTypeDef sConfig = {0}; - TIM_MasterConfigTypeDef sMasterConfig = {0}; - - tim->instance->hal_tim->Init.Prescaler = 5; - tim->instance->hal_tim->Init.CounterMode = TIM_COUNTERMODE_UP; - tim->instance->hal_tim->Init.Period = 55000; - tim->instance->hal_tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - tim->instance->hal_tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - - sConfig.EncoderMode = TIM_ENCODERMODE_TI12; - sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC1Prescaler = TIM_ICPSC_DIV1; - sConfig.IC1Filter = 0; - sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC2Prescaler = TIM_ICPSC_DIV1; - sConfig.IC2Filter = 0; - - if (HAL_TIM_Encoder_Init(tim->instance->hal_tim, &sConfig) != HAL_OK) { - ErrorHandler("Unable to init encoder"); - } - - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if (HAL_TIMEx_MasterConfigSynchronization(tim->instance->hal_tim, &sMasterConfig) != - HAL_OK) { - ErrorHandler("Unable to config master synchronization in encoder"); - } - } - - static void turn_on() { - if (is_on) - return; - - if (HAL_TIM_Encoder_GetState(timer->instance->hal_tim) == HAL_TIM_STATE_RESET) { - ErrorHandler("Unable to get state from encoder"); - return; - } - if (HAL_TIM_Encoder_Start(timer->instance->hal_tim, TIM_CHANNEL_ALL) != HAL_OK) { - ErrorHandler("Unable to start encoder"); - return; - } - is_on = true; - reset(); - } - - static void turn_off() { - if (!is_on) - return; - if (HAL_TIM_Encoder_Stop(timer->instance->hal_tim, TIM_CHANNEL_ALL) != HAL_OK) { - ErrorHandler("Unable to stop encoder"); - } - is_on = false; - } - - static inline void reset() { timer->instance->tim->CNT = UINT32_MAX / 2; } - - static inline uint32_t get_counter() { return timer->instance->tim->CNT; } - - static inline bool get_direction() { return ((timer->instance->tim->CR1 & 0b10000) >> 4); } - - static inline uint32_t get_initial_counter_value() { return timer->instance->tim->ARR / 2; } - - static int64_t get_delta_clock(uint64_t clock_time, uint64_t last_clock_time) { - int64_t delta_clock = clock_time - last_clock_time; - if (clock_time < last_clock_time) { // overflow handle - delta_clock = clock_time + - CLOCK_MAX_VALUE * NANO_SECOND / timer->get_clock_frequency() - - last_clock_time; - } - return delta_clock; - } -}; - -} // namespace ST_LIB -#endif diff --git a/Inc/HALAL/Services/InfoWarning/InfoWarning.hpp b/Inc/HALAL/Services/InfoWarning/InfoWarning.hpp index 3a7a57af..60bf49bf 100644 --- a/Inc/HALAL/Services/InfoWarning/InfoWarning.hpp +++ b/Inc/HALAL/Services/InfoWarning/InfoWarning.hpp @@ -8,7 +8,6 @@ #pragma once #include "C++Utilities/CppUtils.hpp" -#include "HALAL/Services/Time/Time.hpp" class InfoWarning { private: diff --git a/Inc/HALAL/Services/InputCapture/InputCapture.hpp b/Inc/HALAL/Services/InputCapture/InputCapture.hpp.old similarity index 100% rename from Inc/HALAL/Services/InputCapture/InputCapture.hpp rename to Inc/HALAL/Services/InputCapture/InputCapture.hpp.old diff --git a/Inc/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp b/Inc/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp deleted file mode 100644 index 3e7bebc6..00000000 --- a/Inc/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "HALAL/Services/PWM/DualPWM/DualPWM.hpp" - -class DualCenterPWM : public DualPWM { -public: - DualCenterPWM(Pin& pin, Pin& pin_negated); - - void set_frequency(uint32_t freq_in_hz); -}; diff --git a/Inc/HALAL/Services/PWM/DualPWM/DualPWM.hpp b/Inc/HALAL/Services/PWM/DualPWM/DualPWM.hpp deleted file mode 100644 index 8ba5243c..00000000 --- a/Inc/HALAL/Services/PWM/DualPWM/DualPWM.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * DualPWMInstance.hpp - * - * Created on: Feb 27, 2023 - * Author: aleja - */ - -#pragma once - -#include "HALAL/Services/PWM/PWM/PWM.hpp" - -class DualPWM { -protected: - DualPWM() = default; - TimerPeripheral* peripheral; - uint32_t channel; - float duty_cycle; - uint32_t frequency; - bool is_on = false; - static constexpr float CLOCK_FREQ_MHZ_WITHOUT_PRESCALER = 275; - static constexpr float clock_period_ns = (1 / CLOCK_FREQ_MHZ_WITHOUT_PRESCALER) * 1'000; - bool is_initialized = false; - -public: - DualPWM(Pin& pin, Pin& pin_negated); - - void turn_on(); - void turn_on_positive(); - void turn_on_negated(); - void turn_off(); - void turn_off_positive(); - void turn_off_negated(); - void set_duty_cycle(float duty_cycle); - void set_frequency(uint32_t freq_in_hz); - uint32_t get_frequency() const; - float get_duty_cycle() const; - void set_dead_time(std::chrono::nanoseconds dead_time_ns); -}; diff --git a/Inc/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp b/Inc/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp.old similarity index 100% rename from Inc/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp rename to Inc/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.hpp.old diff --git a/Inc/HALAL/Services/PWM/PWM/PWM.hpp b/Inc/HALAL/Services/PWM/PWM/PWM.hpp deleted file mode 100644 index 1ac3dc2a..00000000 --- a/Inc/HALAL/Services/PWM/PWM/PWM.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * PWM.hpp - * - * Created on: Feb 27, 2023 - * Author: aleja - */ - -#pragma once - -#include "HALAL/Models/PinModel/Pin.hpp" -#include "HALAL/Models/TimerPeripheral/TimerPeripheral.hpp" -#include "ErrorHandler/ErrorHandler.hpp" - -#ifdef HAL_TIM_MODULE_ENABLED - -class PWM { -protected: - TimerPeripheral* peripheral; - uint32_t channel; - float duty_cycle; - uint32_t frequency; - bool is_on = false; - bool is_center_aligned = false; - static constexpr float CLOCK_FREQ_MHZ_WITHOUT_PRESCALER = 275; - static constexpr float clock_period_ns = (1 / CLOCK_FREQ_MHZ_WITHOUT_PRESCALER) * 1'000; - bool is_initialized = false; - -public: - PWM() = default; - PWM(Pin& pin); - - void turn_on(); - void turn_off(); - void set_duty_cycle(float duty_cycle); - void set_frequency(uint32_t frequency); - uint32_t get_frequency(); - float get_duty_cycle(); - - /** - * @brief function that sets a deadtime, in which the PWM wouldn t be on HIGH no matter the duty - * cycle - * - * This function has to be called while the PWM is turned off. - * This function actually substracts from the HIGH state of the PWM the amount of ns, pulling - * it down; thus effectively reducing the duty cycle by an amount dependant on the frequency and - * the dead time. - */ - void set_dead_time(std::chrono::nanoseconds dead_time_ns); -}; - -#endif diff --git a/Inc/HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp b/Inc/HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp.old similarity index 100% rename from Inc/HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp rename to Inc/HALAL/Services/PWM/PhasedPWM/PhasedPWM.hpp.old diff --git a/Inc/HALAL/Services/Time/Scheduler.hpp b/Inc/HALAL/Services/Time/Scheduler.hpp index f4bb7b44..ae7bcdb1 100644 --- a/Inc/HALAL/Services/Time/Scheduler.hpp +++ b/Inc/HALAL/Services/Time/Scheduler.hpp @@ -39,7 +39,7 @@ struct Scheduler { static void start(); static void update(); - static inline uint64_t get_global_tick(); + static inline uint64_t get_global_tick() { return global_tick_us_; } static uint16_t register_task(uint32_t period_us, callback_t func); static bool unregister_task(uint16_t id); diff --git a/Inc/HALAL/Services/Time/Time.hpp b/Inc/HALAL/Services/Time/Time.hpp deleted file mode 100644 index d613e3fe..00000000 --- a/Inc/HALAL/Services/Time/Time.hpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Time.hpp - * - * Created on: 30 oct. 2022 - * Author: Dani - */ - -#pragma once - -#include "stm32h7xx_hal.h" - -#define RTC_MAX_COUNTER 32767 - -#ifdef HAL_TIM_MODULE_ENABLED - -#include "C++Utilities/CppUtils.hpp" -#include "HALAL/Services/Time/RTC.hpp" -// HIGH RESOLUTION TIMERS -extern TIM_HandleTypeDef htim2; // Used for the global timer (3,36nS step) -extern TIM_HandleTypeDef htim5; // Used for the high precision alarms (1uS) - -// MID RESOLUTION TIMERS -extern TIM_HandleTypeDef htim24; // Used for the high precision alarms (50uS) - -// LOW RESOLUTION TIMERS -extern TIM_HandleTypeDef htim7; // Used for the low precision alarms (1mS) - -class Time { - -private: - static uint8_t high_precision_ids; - static uint8_t low_precision_ids; - static uint8_t mid_precision_ids; - - struct Alarm { - uint32_t period; - TIM_HandleTypeDef* tim; - function alarm; - uint64_t offset; - bool is_on = false; - }; - - static constexpr uint32_t HIGH_PRECISION_MAX_ARR = 4294967295; - static constexpr uint32_t MID_PRECISION_MAX_ARR = 4294967295; - static constexpr uint32_t mid_precision_step_in_us = 50; - static uint64_t global_tick; - static uint64_t low_precision_tick; - static uint64_t mid_precision_tick; - static bool mid_precision_registered; - - static unordered_map timer32_by_timer32handler_map; - static unordered_map timer32interrupt_by_timer32handler_map; - - static unordered_map high_precision_alarms_by_id; - static unordered_map high_precision_alarms_by_timer; - static unordered_map low_precision_alarms_by_id; - static unordered_map mid_precision_alarms_by_id; - - static stack low_precision_erasable_ids; - static stack mid_precision_erasable_ids; - - static void stop_timer(TIM_HandleTypeDef* htim); - static void start_timer(TIM_HandleTypeDef* htim, uint32_t prescaler, uint32_t period); - static void init_timer( - TIM_TypeDef* tim, - TIM_HandleTypeDef* htim, - uint32_t prescaler, - uint32_t period, - IRQn_Type interrupt_channel - ); - static void ConfigTimer(TIM_HandleTypeDef* tim, uint32_t period_in_us); - static bool is_valid_timer(TIM_HandleTypeDef* tim); - static void hal_enable_timer(TIM_HandleTypeDef* tim); - -public: - static TIM_HandleTypeDef* global_timer; - - static set high_precision_timers; - static stack available_high_precision_timers; - - static TIM_HandleTypeDef* low_precision_timer; - static TIM_HandleTypeDef* mid_precision_timer; - - static void high_precision_timer_callback(TIM_HandleTypeDef* tim); - static void global_timer_callback(); - static void low_precision_timer_callback(); - static void mid_precision_timer_callback(); - - /** - * @brief Initializes instances registered with init_timer. - * - * @return void - */ - static void start(); - - static uint64_t get_global_tick(); - - /** - * @brief Tries to register a high_precision_alarm that will execute a function cyclically - * until unregistered. - * - * @param period_in_us period in microseconds until timeout. - * @param func function to be executed on timeout. - * @return optional Returns id of the alarm if succesful, nullopt if it is able - * to register the timer correctly (probably because there aren't any timer available). - */ - static uint8_t register_high_precision_alarm(uint32_t period_in_us, function func); - - static bool unregister_high_precision_alarm(uint8_t id); - - /** - * @brief Registers a low_precision_alarm that will execute a function cyclically until - * unregistered. - * - * @param period_in_ms period in milliseconds until timeout. - * @param func function to be executed on timeout. - * @return uint8_t Returns id of the alarm. - */ - static uint8_t register_low_precision_alarm(uint32_t period_in_ms, function func); - static uint8_t register_low_precision_alarm(uint32_t period_in_ms, void (*func)()); - static bool unregister_low_precision_alarm(uint8_t id); - - /** - * @brief Registers a low_precision_alarm, executes an action ONLY ONE TIME - * and unregisters the alarm. - * - * @param milliseconds time until the action is executed. - * @param func function to be executed on timeout. - * @return void - */ - - static uint8_t register_mid_precision_alarm(uint32_t period_in_us, function func); - static bool unregister_mid_precision_alarm(uint8_t id); - - /** - * @brief Creates a timeout that will execute a function after a specified time - * - * @param milliseconds the time to wait before executing - * @param callback the function to be executed - * @return uint8_t the id of the order, if it didnot succeed it will return 255 - */ - static uint8_t set_timeout(int milliseconds, function callback); - - /** - * @brief Cancels a timeout by derigstering the alarm bound to it - * - * @param id The id of the timeout to cancel - */ - static void cancel_timeout(uint8_t id); -}; - -#endif diff --git a/Inc/HALAL/Services/Time/TimerWrapper.hpp b/Inc/HALAL/Services/Time/TimerWrapper.hpp index 70a13f5f..0a81056a 100644 --- a/Inc/HALAL/Services/Time/TimerWrapper.hpp +++ b/Inc/HALAL/Services/Time/TimerWrapper.hpp @@ -12,7 +12,7 @@ #ifdef HAL_TIM_MODULE_ENABLED #include "HALAL/Models/TimerDomain/TimerDomain.hpp" -#include "HALAL/Services/Encoder/NewEncoder.hpp" +#include "HALAL/Services/Encoder/Encoder.hpp" #include "HALAL/Services/PWM/DualPWM.hpp" #include "HALAL/Services/PWM/PWM.hpp" #include "HALAL/Models/GPIO.hpp" diff --git a/Inc/ST-LIB_HIGH/ST-LIB_HIGH.hpp b/Inc/ST-LIB_HIGH/ST-LIB_HIGH.hpp index 6e1d499d..a74d4098 100644 --- a/Inc/ST-LIB_HIGH/ST-LIB_HIGH.hpp +++ b/Inc/ST-LIB_HIGH/ST-LIB_HIGH.hpp @@ -7,8 +7,8 @@ #pragma once -// #include "Protections/Protection.hpp" -// #include "Protections/ProtectionManager.hpp" +#include "Protections/Protection.hpp" +#include "Protections/ProtectionManager.hpp" #include "Control/ControlBlock.hpp" #include "Control/FeedbackControlBlock.hpp" #include "Control/SplitterBlock.hpp" diff --git a/Inc/ST-LIB_LOW/Clocks/Counter.hpp b/Inc/ST-LIB_LOW/Clocks/Counter.hpp deleted file mode 100644 index 1b6c4363..00000000 --- a/Inc/ST-LIB_LOW/Clocks/Counter.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Time.hpp - * - * Created on: 11 nov. 2022 - * Author: Dani - */ - -#pragma once -#include "C++Utilities/CppUtils.hpp" - -class Counter { - -private: - uint16_t update_period_ms = 125; - uint8_t time_id; - void update(); - -public: - uint64_t counter = 0; - double freq = 0.0; - - ~Counter(); - Counter(uint64_t update_period_ms); - void count(); -}; diff --git a/Inc/ST-LIB_LOW/Clocks/Stopwatch.hpp b/Inc/ST-LIB_LOW/Clocks/Stopwatch.hpp.old similarity index 100% rename from Inc/ST-LIB_LOW/Clocks/Stopwatch.hpp rename to Inc/ST-LIB_LOW/Clocks/Stopwatch.hpp.old diff --git a/Inc/ST-LIB_LOW/ErrorHandler/ErrorHandler.hpp b/Inc/ST-LIB_LOW/ErrorHandler/ErrorHandler.hpp index 09ffefca..29752308 100644 --- a/Inc/ST-LIB_LOW/ErrorHandler/ErrorHandler.hpp +++ b/Inc/ST-LIB_LOW/ErrorHandler/ErrorHandler.hpp @@ -10,7 +10,6 @@ #include "C++Utilities/CppUtils.hpp" #ifndef SIM_ON -#include "HALAL/Services/Time/Time.hpp" #include "HALAL/Services/Communication/UART/UART.hpp" #endif // !defined(SIM_ON) diff --git a/Inc/ST-LIB_LOW/HalfBridge/HalfBridge.hpp b/Inc/ST-LIB_LOW/HalfBridge/HalfBridge.hpp.old similarity index 100% rename from Inc/ST-LIB_LOW/HalfBridge/HalfBridge.hpp rename to Inc/ST-LIB_LOW/HalfBridge/HalfBridge.hpp.old diff --git a/Inc/ST-LIB_LOW/ST-LIB_LOW.hpp b/Inc/ST-LIB_LOW/ST-LIB_LOW.hpp index 148b1931..3438be8d 100644 --- a/Inc/ST-LIB_LOW/ST-LIB_LOW.hpp +++ b/Inc/ST-LIB_LOW/ST-LIB_LOW.hpp @@ -5,23 +5,20 @@ #include "ST-LIB_LOW/Sd/Sd.hpp" -#include "Clocks/Counter.hpp" -#include "Clocks/Stopwatch.hpp" #include "Sensors/LinearSensor/LinearSensor.hpp" #include "Sensors/LinearSensor/FilteredLinearSensor.hpp" #include "StateMachine/StateMachine.hpp" #include "DigitalOutput/DigitalOutput.hpp" -#include "HalfBridge/HalfBridge.hpp" +// #include "HalfBridge/HalfBridge.hpp" // To be implemented when PhasedPWM is done #include "ErrorHandler/ErrorHandler.hpp" #include "Math/Math.hpp" -#include "Sensors/Sensor/Sensor.hpp" +// #include "Sensors/Sensor/Sensor.hpp" // To be implemented when InputCapture is done #include "Sensors/DigitalSensor/DigitalSensor.hpp" #include "Sensors/SensorInterrupt/SensorInterrupt.hpp" #include "Sensors/LinearSensor/LinearSensor.hpp" #include "Sensors/LookupSensor/LookupSensor.hpp" -// #include "Sensors/EncoderSensor/EncoderSensor.hpp" #include "Sensors/EncoderSensor/NewEncoderSensor.hpp" -#include "Sensors/PWMSensor/PWMSensor.hpp" +// #include "Sensors/PWMSensor/PWMSensor.hpp" // To be implemented when InputCapture is done #include "Sensors/NTC/NTC.hpp" #ifdef STLIB_ETH diff --git a/Inc/ST-LIB_LOW/Sensors/PWMSensor/PWMSensor.hpp b/Inc/ST-LIB_LOW/Sensors/PWMSensor/PWMSensor.hpp.old similarity index 100% rename from Inc/ST-LIB_LOW/Sensors/PWMSensor/PWMSensor.hpp rename to Inc/ST-LIB_LOW/Sensors/PWMSensor/PWMSensor.hpp.old diff --git a/Inc/ST-LIB_LOW/Sensors/Sensor/Sensor.hpp b/Inc/ST-LIB_LOW/Sensors/Sensor/Sensor.hpp.old similarity index 100% rename from Inc/ST-LIB_LOW/Sensors/Sensor/Sensor.hpp rename to Inc/ST-LIB_LOW/Sensors/Sensor/Sensor.hpp.old diff --git a/Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp b/Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp.old similarity index 100% rename from Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp rename to Src/HALAL/Models/TimerPeripheral/TimerPeripheral.cpp.old diff --git a/Src/HALAL/Services/Communication/SNTP/SNTP.cpp b/Src/HALAL/Services/Communication/SNTP/SNTP.cpp index 132e872f..3c37b415 100644 --- a/Src/HALAL/Services/Communication/SNTP/SNTP.cpp +++ b/Src/HALAL/Services/Communication/SNTP/SNTP.cpp @@ -6,6 +6,7 @@ */ #include "HALAL/Services/Communication/SNTP/SNTP.hpp" +#include "HALAL/Services/Time/RTC.hpp" #define SUBSECONDS_PER_SECOND 32767 #define TRANSFORMATION_FACTOR (SUBSECONDS_PER_SECOND / 999999.0) diff --git a/Src/HALAL/Services/Communication/SPI/SPI.cpp b/Src/HALAL/Services/Communication/SPI/SPI.cpp index 439b7dc4..ed71c54e 100644 --- a/Src/HALAL/Services/Communication/SPI/SPI.cpp +++ b/Src/HALAL/Services/Communication/SPI/SPI.cpp @@ -6,6 +6,7 @@ */ #include "HALAL/Services/Communication/SPI/SPI.hpp" +#include "HALAL/Services/Time/Scheduler.hpp" #include "HALAL/Models/MPUManager/MPUManager.hpp" @@ -348,9 +349,9 @@ void SPI::Order_update() { // slave again if it has the correct Order ID ready if (known_slave_ready(spi)) { master_check_available_end(spi); - } else if (Time::get_global_tick() - spi->last_end_check > MASTER_SPI_CHECK_DELAY) { + } else if (Scheduler::get_global_tick() - spi->last_end_check > MASTER_SPI_CHECK_DELAY) { master_check_available_end(spi); - spi->last_end_check = Time::get_global_tick(); + spi->last_end_check = Scheduler::get_global_tick(); } } } @@ -459,12 +460,12 @@ void SPI::init(SPI::Instance* spi) { // spi->try_count++; // switch (*(spi->available_end)) { // case NO_ORDER_ID: { -// spi->last_end_check = Time::get_global_tick(); +// spi->last_end_check = Scheduler::get_global_tick(); // SPI::turn_on_chip_select(spi); // } break; // default: // case ERROR_ORDER_ID: { -// spi->last_end_check = Time::get_global_tick(); +// spi->last_end_check = Scheduler::get_global_tick(); // spi->error_count++; // SPI::turn_on_chip_select(spi); // } break; diff --git a/Src/HALAL/Services/Encoder/Encoder.cpp b/Src/HALAL/Services/Encoder/Encoder.cpp deleted file mode 100644 index dd82c355..00000000 --- a/Src/HALAL/Services/Encoder/Encoder.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Encoder.cpp - * - * Created on: 27 oct. 2022 - * Author: Pablo - */ - -#include "HALAL/Services/Encoder/Encoder.hpp" - -#ifdef HAL_TIM_MODULE_ENABLED - -map> Encoder::registered_encoder = {}; - -uint8_t Encoder::id_counter = 0; - -uint8_t Encoder::inscribe(Pin& pin1, Pin& pin2) { - pair doublepin = {pin1, pin2}; - if (not Encoder::pin_timer_map.contains(doublepin)) { - ErrorHandler( - " The pin %s and the pin %s are already used or aren't " - "configurated for encoder usage", - pin1.to_string().c_str(), - pin2.to_string().c_str() - ); - return 0; - } - - Pin::inscribe(pin1, ALTERNATIVE); - Pin::inscribe(pin2, ALTERNATIVE); - - uint8_t id = ++Encoder::id_counter; - Encoder::registered_encoder[id] = doublepin; - - return id; -} - -void Encoder::start() { - for (pair> instance : registered_encoder) { - init(Encoder::pin_timer_map[instance.second]); - } -} - -void Encoder::turn_on(uint8_t id) { - if (not Encoder::registered_encoder.contains(id)) { - ErrorHandler("No encoder registered with id %u", id); - return; - } - - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - - if (HAL_TIM_Encoder_GetState(timer->handle) == HAL_TIM_STATE_RESET) { - ErrorHandler("Unable to get state from encoder in timer %s", timer->name.c_str()); - return; - } - - if (HAL_TIM_Encoder_Start(timer->handle, TIM_CHANNEL_ALL) != HAL_OK) { - ErrorHandler("Unable to start encoder in timer %s", timer->name.c_str()); - return; - } - - reset(id); -} - -void Encoder::turn_off(uint8_t id) { - if (not Encoder::registered_encoder.contains(id)) { - ErrorHandler("No encoder registered with id %u", id); - return; - } - - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - - if (HAL_TIM_Encoder_Stop(timer->handle, TIM_CHANNEL_ALL) != HAL_OK) { - ErrorHandler("Unable to stop encoder in timer %s", timer->name.c_str()); - } -} - -void Encoder::reset(uint8_t id) { - if (not Encoder::registered_encoder.contains(id)) { - ErrorHandler("No encoder registered with id %u", id); - return; - } - - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - - timer->handle->Instance->CNT = UINT32_MAX / 2; -} - -uint32_t Encoder::get_counter(uint8_t id) { - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - - return timer->handle->Instance->CNT; -} - -bool Encoder::get_direction(uint8_t id) { - if (not Encoder::registered_encoder.contains(id)) { - ErrorHandler("No encoder registered with id %u", id); - return false; - } - - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - - return ((timer->handle->Instance->CR1 & 0b10000) >> 4); -} - -uint32_t Encoder::get_initial_counter_value(uint8_t id) { - TimerPeripheral* timer = pin_timer_map[registered_encoder[id]]; - return (timer->handle->Instance->ARR / 2); -} - -void Encoder::init(TimerPeripheral* encoder) { - TIM_Encoder_InitTypeDef sConfig = {0}; - TIM_MasterConfigTypeDef sMasterConfig = {0}; - - encoder->handle->Instance = TimerPeripheral::handle_to_timer[encoder->handle]; - encoder->handle->Init.Prescaler = encoder->init_data.prescaler; - encoder->handle->Init.CounterMode = TIM_COUNTERMODE_UP; - encoder->handle->Init.Period = encoder->init_data.period; - encoder->handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - encoder->handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - sConfig.EncoderMode = TIM_ENCODERMODE_TI12; - sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC1Prescaler = TIM_ICPSC_DIV1; - sConfig.IC1Filter = 0; - sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC2Prescaler = TIM_ICPSC_DIV1; - sConfig.IC2Filter = 0; - if (HAL_TIM_Encoder_Init(encoder->handle, &sConfig) != HAL_OK) { - ErrorHandler("Unable to init encoder in timer %s", encoder->name.c_str()); - } - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if (HAL_TIMEx_MasterConfigSynchronization(encoder->handle, &sMasterConfig) != HAL_OK) { - ErrorHandler( - "Unable to config master synchronization in encoder in timer %s", - encoder->name.c_str() - ); - } -} - -int64_t Encoder::get_delta_clock(uint64_t clock_time, uint64_t last_clock_time) { - int64_t delta_clock = clock_time - last_clock_time; - if (clock_time < last_clock_time) { // overflow handle - delta_clock = clock_time + CLOCK_MAX_VALUE * NANO_SECOND / HAL_RCC_GetPCLK1Freq() * 2 - - last_clock_time; - } - return delta_clock; -} - -#endif diff --git a/Src/HALAL/Services/InfoWarning/InfoWarning.cpp b/Src/HALAL/Services/InfoWarning/InfoWarning.cpp index d60c0698..fb3e8cbc 100644 --- a/Src/HALAL/Services/InfoWarning/InfoWarning.cpp +++ b/Src/HALAL/Services/InfoWarning/InfoWarning.cpp @@ -6,6 +6,7 @@ */ #include "HALAL/Services/InfoWarning/InfoWarning.hpp" +#include "HALAL/Services/Time/Scheduler.hpp" string InfoWarning::description = "Error-No-Description-Found"; string InfoWarning::line = "Error-No-Line-Found"; @@ -46,7 +47,7 @@ void InfoWarning::InfoWarningTrigger(string format, ...) { " Function: '" + InfoWarning::func + "' File: " + InfoWarning::file; #ifdef HAL_TIM_MODULE_ENABLED - description += " | TimeStamp: " + to_string(Time::get_global_tick()); + description += " | TimeStamp: " + to_string(Scheduler::get_global_tick()); #endif } @@ -54,5 +55,5 @@ void InfoWarning::InfoWarningUpdate() { if (!InfoWarning::warning_triggered) { return; } - printf("Warning: %s%s", InfoWarning::description.c_str(), endl); + printf("Warning: %s\n", InfoWarning::description.c_str()); } diff --git a/Src/HALAL/Services/InputCapture/InputCapture.cpp b/Src/HALAL/Services/InputCapture/InputCapture.cpp.old similarity index 100% rename from Src/HALAL/Services/InputCapture/InputCapture.cpp rename to Src/HALAL/Services/InputCapture/InputCapture.cpp.old diff --git a/Src/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.cpp b/Src/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.cpp deleted file mode 100644 index 74a48933..00000000 --- a/Src/HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "HALAL/Services/PWM/DualCenterPWM/DualCenterPWM.hpp" - -DualCenterPWM::DualCenterPWM(Pin& pin, Pin& pin_negated) { - if (not TimerPeripheral::available_dual_pwms.contains({pin, pin_negated})) { - ErrorHandler( - "Pins %s and %s are not registered as an available Dual PWM", - pin.to_string(), - pin_negated.to_string() - ); - } - - TimerPeripheral& timer = TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).first; - TimerPeripheral::PWMData& pwm_data = - TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).second; - - peripheral = &timer; - channel = pwm_data.channel; - - if (pwm_data.mode != TimerPeripheral::PWM_MODE::CENTER_ALIGNED) { - ErrorHandler( - "Pins %s and %s are not registered as a CENTER ALIGNED PWM", - pin.to_string(), - pin_negated.to_string() - ); - } - - Pin::inscribe(pin, TIMER_ALTERNATE_FUNCTION); - Pin::inscribe(pin_negated, TIMER_ALTERNATE_FUNCTION); - timer.init_data.pwm_channels.push_back(pwm_data); - - duty_cycle = 0; -} - -void DualCenterPWM::set_frequency(uint32_t freq_in_hz) { - this->frequency = freq_in_hz; - TIM_TypeDef& timer = *peripheral->handle->Instance; - timer.ARR = (HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / (frequency * 2); - set_duty_cycle(duty_cycle); -} diff --git a/Src/HALAL/Services/PWM/DualPWM/DualPWM.cpp b/Src/HALAL/Services/PWM/DualPWM/DualPWM.cpp deleted file mode 100644 index 86225b14..00000000 --- a/Src/HALAL/Services/PWM/DualPWM/DualPWM.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * DualPWM.cpp - * - * Created on: Feb 27, 2023 - * Author: aleja - */ - -#include "HALAL/Services/PWM/DualPWM/DualPWM.hpp" -#include "ErrorHandler/ErrorHandler.hpp" -#include "stm32h7xx_hal_def.h" - -DualPWM::DualPWM(Pin& pin, Pin& pin_negated) { - if (not TimerPeripheral::available_dual_pwms.contains({pin, pin_negated})) { - ErrorHandler( - "Pins %s and %s are not registered as an available Dual PWM", - pin.to_string(), - pin_negated.to_string() - ); - } - - TimerPeripheral& timer = TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).first; - TimerPeripheral::PWMData& pwm_data = - TimerPeripheral::available_dual_pwms.at({pin, pin_negated}).second; - - peripheral = &timer; - channel = pwm_data.channel; - - if (pwm_data.mode != TimerPeripheral::PWM_MODE::NORMAL) { - ErrorHandler( - "Pins %s and %s are not registered as a DUAL PWM", - pin.to_string(), - pin_negated.to_string() - ); - } - - Pin::inscribe(pin, TIMER_ALTERNATE_FUNCTION); - Pin::inscribe(pin_negated, TIMER_ALTERNATE_FUNCTION); - timer.init_data.pwm_channels.push_back(pwm_data); - - duty_cycle = 0; -} - -void DualPWM::turn_on() { - if (HAL_TIM_PWM_Start(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM positive channel did not start correctly", 0); - } - - if (HAL_TIMEx_PWMN_Start(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM negative channel did not start correctly", 0); - } -} - -void DualPWM::turn_off() { - - if (HAL_TIM_PWM_Stop(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM positive channel did not stop correctly", 0); - } - - if (HAL_TIMEx_PWMN_Stop(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM negative channel did not stop correctly", 0); - } -} - -void DualPWM::turn_on_positive() { - if (HAL_TIM_PWM_Start(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM positive channel did not start correctly", 0); - } -} - -void DualPWM::turn_on_negated() { - if (HAL_TIMEx_PWMN_Start(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM negative channel did not start correctly", 0); - } -} - -void DualPWM::turn_off_positive() { - if (HAL_TIM_PWM_Stop(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM positive channel did not stop correctly", 0); - } -} - -void DualPWM::turn_off_negated() { - if (HAL_TIMEx_PWMN_Stop(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("Dual PWM negative channel did not stop correctly", 0); - } -} - -void DualPWM::set_duty_cycle(float duty_cycle) { - uint16_t raw_duty = __HAL_TIM_GET_AUTORELOAD(peripheral->handle) / 100.0 * duty_cycle; - __HAL_TIM_SET_COMPARE(peripheral->handle, channel, raw_duty); - this->duty_cycle = duty_cycle; -} -void DualPWM::set_frequency(uint32_t freq_in_hz) { - this->frequency = freq_in_hz; - TIM_TypeDef& timer = *peripheral->handle->Instance; - timer.ARR = (HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / frequency; - set_duty_cycle(duty_cycle); -} -uint32_t DualPWM::get_frequency() const { return frequency; } -float DualPWM::get_duty_cycle() const { return duty_cycle; } -void DualPWM::set_dead_time(std::chrono::nanoseconds dead_time_ns) { - TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; - - // per https://hasanyavuz.ozderya.net/?p=437 - auto time = dead_time_ns.count(); - - if (time <= 127 * clock_period_ns) { - sBreakDeadTimeConfig.DeadTime = time / clock_period_ns; - } else if (time > 127 * clock_period_ns && time <= 2 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (2 * clock_period_ns) - 64 + 128; - } else if (time > 2 * clock_period_ns * 127 && time <= 8 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (8 * clock_period_ns) - 32 + 192; - } else if (time > 8 * clock_period_ns * 127 && time <= 16 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (16 * clock_period_ns) - 32 + 224; - } else { - ErrorHandler("Invalid dead time configuration"); - } - // sBreakDeadTimeConfig.LockLevel = 0; - // sBreakDeadTimeConfig.BreakState = 1; - HAL_TIMEx_ConfigBreakDeadTime(peripheral->handle, &sBreakDeadTimeConfig); - peripheral->handle->Instance->BDTR |= TIM_BDTR_MOE; - return; -} diff --git a/Src/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.cpp b/Src/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.cpp.old similarity index 100% rename from Src/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.cpp rename to Src/HALAL/Services/PWM/DualPhasedPWM/DualPhasedPWM.cpp.old diff --git a/Src/HALAL/Services/PWM/PWM/PWM.cpp b/Src/HALAL/Services/PWM/PWM/PWM.cpp deleted file mode 100644 index 6874578b..00000000 --- a/Src/HALAL/Services/PWM/PWM/PWM.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * PWM.cpp - * - * Created on: Feb 27, 2023 - * Author: aleja - */ - -#include "HALAL/Services/PWM/PWM/PWM.hpp" -#include "ErrorHandler/ErrorHandler.hpp" -#include "stm32h7xx_hal_def.h" - -PWM::PWM(Pin& pin) { - if (not TimerPeripheral::available_pwm.contains(pin)) { - - ErrorHandler("Pin %s is not registered as an available PWM", pin.to_string()); - return; - } - - TimerPeripheral& timer = TimerPeripheral::available_pwm.at(pin).first; - TimerPeripheral::PWMData& pwm_data = TimerPeripheral::available_pwm.at(pin).second; - - if (pwm_data.mode == TimerPeripheral::PWM_MODE::CENTER_ALIGNED) { - is_center_aligned = true; - } - if (pwm_data.mode == TimerPeripheral::PWM_MODE::PHASED) { - ErrorHandler("Pin %s is registered as Phased being a single PWM", pin.to_string()); - } - - peripheral = &timer; - channel = pwm_data.channel; - - Pin::inscribe(pin, TIMER_ALTERNATE_FUNCTION); - timer.init_data.pwm_channels.push_back(pwm_data); - - duty_cycle = 0; - is_initialized = true; -} - -void PWM::turn_on() { - if (not is_initialized) { - ErrorHandler("PWM was not initialized"); - } - if (is_on) - return; - if (HAL_TIM_PWM_Start(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("PWM did not turn on correctly", 0); - } - is_on = true; -} - -void PWM::turn_off() { - if (not is_on) - return; - if (HAL_TIM_PWM_Stop(peripheral->handle, channel) != HAL_OK) { - ErrorHandler("PWM did not stop correctly", 0); - } - is_on = false; -} - -void PWM::set_duty_cycle(float duty_cycle) { - uint16_t raw_duty = __HAL_TIM_GET_AUTORELOAD(peripheral->handle) / 100.0 * duty_cycle; - __HAL_TIM_SET_COMPARE(peripheral->handle, channel, raw_duty); - this->duty_cycle = duty_cycle; -} - -void PWM::set_frequency(uint32_t frequency) { - if (is_center_aligned) { - frequency = 2 * frequency; - } - this->frequency = frequency; - TIM_TypeDef& timer = *peripheral->handle->Instance; - timer.ARR = (HAL_RCC_GetPCLK1Freq() * 2 / (timer.PSC + 1)) / frequency; - set_duty_cycle(duty_cycle); -} - -uint32_t PWM::get_frequency() { return frequency; } - -float PWM::get_duty_cycle() { return duty_cycle; } -void PWM::set_dead_time(std::chrono::nanoseconds dead_time_ns) { - TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; - - // per https://hasanyavuz.ozderya.net/?p=437 - auto time = dead_time_ns.count(); - - if (time <= 127 * clock_period_ns) { - sBreakDeadTimeConfig.DeadTime = time / clock_period_ns; - } else if (time > 127 * clock_period_ns && time <= 2 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (2 * clock_period_ns) - 64 + 128; - } else if (time > 2 * clock_period_ns * 127 && time <= 8 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (8 * clock_period_ns) - 32 + 192; - } else if (time > 8 * clock_period_ns * 127 && time <= 16 * clock_period_ns * 127) { - sBreakDeadTimeConfig.DeadTime = time / (16 * clock_period_ns) - 32 + 224; - } else { - ErrorHandler("Invalid dead time configuration"); - } - - sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; - sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; - sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; - sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE; - sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; - sBreakDeadTimeConfig.BreakFilter = 0; - sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; - sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; - sBreakDeadTimeConfig.Break2Filter = 0; - sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - HAL_TIMEx_ConfigBreakDeadTime(peripheral->handle, &sBreakDeadTimeConfig); - peripheral->handle->Instance->BDTR |= TIM_BDTR_MOE; - - return; -} diff --git a/Src/HALAL/Services/PWM/PhasedPWM/PhasedPWM.cpp b/Src/HALAL/Services/PWM/PhasedPWM/PhasedPWM.cpp.old similarity index 100% rename from Src/HALAL/Services/PWM/PhasedPWM/PhasedPWM.cpp rename to Src/HALAL/Services/PWM/PhasedPWM/PhasedPWM.cpp.old diff --git a/Src/HALAL/Services/Time/Time.cpp b/Src/HALAL/Services/Time/Time.cpp deleted file mode 100644 index b79dd6e2..00000000 --- a/Src/HALAL/Services/Time/Time.cpp +++ /dev/null @@ -1,431 +0,0 @@ -/* - * Time.cpp - * - * Created on: 30 oct. 2022 - * Author: Dani - */ - -#include "HALAL/Services/Time/Time.hpp" - -#include "ErrorHandler/ErrorHandler.hpp" -#include "HALAL/Models/TimerPeripheral/TimerPeripheral.hpp" - -TIM_HandleTypeDef* Time::low_precision_timer = &htim7; - -uint8_t Time::high_precision_ids = 0; -uint8_t Time::low_precision_ids = 0; -uint8_t Time::mid_precision_ids = 0; - -bool Time::mid_precision_registered = false; - -stack Time::available_high_precision_timers; - -unordered_map Time::high_precision_alarms_by_id; -unordered_map Time::low_precision_alarms_by_id; -unordered_map Time::mid_precision_alarms_by_id; -unordered_map Time::high_precision_alarms_by_timer; - -unordered_map Time::timer32_by_timer32handler_map = - {{&htim2, TIM2}, {&htim5, TIM5}, {&htim23, TIM23}, {&htim24, TIM24}}; -unordered_map Time::timer32interrupt_by_timer32handler_map = - {{&htim2, TIM2_IRQn}, {&htim5, TIM5_IRQn}, {&htim23, TIM23_IRQn}, {&htim24, TIM24_IRQn}}; - -stack Time::low_precision_erasable_ids; -stack Time::mid_precision_erasable_ids; - -uint64_t Time::global_tick = 0; -uint64_t Time::low_precision_tick = 0; -uint64_t Time::mid_precision_tick = 0; - -void Time::init_timer( - TIM_TypeDef* tim, - TIM_HandleTypeDef* htim, - uint32_t prescaler, - uint32_t period, - IRQn_Type interrupt_channel -) { - TIM_ClockConfigTypeDef sClockSourceConfig = {0}; - TIM_MasterConfigTypeDef sMasterConfig = {0}; - - htim->Instance = tim; - htim->Init.Prescaler = prescaler; - htim->Init.CounterMode = TIM_COUNTERMODE_UP; - htim->Init.Period = period; - htim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - htim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - HAL_TIM_Base_Init(htim); - - sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - HAL_TIM_ConfigClockSource(htim, &sClockSourceConfig); - - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - - HAL_TIMEx_MasterConfigSynchronization(htim, &sMasterConfig); - - HAL_NVIC_SetPriority(interrupt_channel, 0, 0); - HAL_NVIC_EnableIRQ(interrupt_channel); -} - -void Time::start() { - __HAL_RCC_TIM7_CLK_ENABLE(); - __HAL_RCC_TIM24_CLK_ENABLE(); - __HAL_RCC_TIM23_CLK_ENABLE(); - Time::init_timer(TIM2, &htim2, 0, HIGH_PRECISION_MAX_ARR, TIM2_IRQn); - Time::init_timer(TIM5, &htim5, 0, HIGH_PRECISION_MAX_ARR, TIM5_IRQn); - Time::init_timer(TIM24, &htim24, 0, HIGH_PRECISION_MAX_ARR, TIM24_IRQn); - Time::init_timer(TIM7, &htim7, 275, 1000, TIM7_IRQn); - HAL_TIM_Base_Start_IT(low_precision_timer); - - if (global_timer != nullptr) { - if (timer32_by_timer32handler_map.find(global_timer) == - timer32_by_timer32handler_map.end()) { - ErrorHandler("Global Timer pointer is not valid"); - global_timer = nullptr; - } else { - hal_enable_timer(global_timer); - Time::init_timer( - timer32_by_timer32handler_map[global_timer], - global_timer, - 0, - HIGH_PRECISION_MAX_ARR, - timer32interrupt_by_timer32handler_map[global_timer] - ); - HAL_TIM_Base_Start_IT(global_timer); - } - } - for (auto timer : high_precision_timers) { - if (timer32_by_timer32handler_map.find(timer) == timer32_by_timer32handler_map.end()) { - ErrorHandler("Global Timer pointer is not valid"); - high_precision_timers.erase(timer); - } else { - hal_enable_timer(timer); - Time::init_timer( - timer32_by_timer32handler_map[timer], - timer, - 0, - HIGH_PRECISION_MAX_ARR, - timer32interrupt_by_timer32handler_map[timer] - ); - Time::available_high_precision_timers.push(timer); - } - } -} - -bool Time::is_valid_timer(TIM_HandleTypeDef* tim) { - if (tim == global_timer || tim == low_precision_timer || tim == mid_precision_timer || - high_precision_timers.contains(tim)) - return true; - return false; -} - -void Time::hal_enable_timer(TIM_HandleTypeDef* tim) { - if (tim == &htim2) { - __HAL_RCC_TIM2_CLK_ENABLE(); - } else if (tim == &htim5) { - __HAL_RCC_TIM5_CLK_ENABLE(); - } else if (tim == &htim23) { - __HAL_RCC_TIM23_CLK_ENABLE(); - } else if (tim == &htim24) { - __HAL_RCC_TIM24_CLK_ENABLE(); - } -} - -// PUBLIC SERVICE METHODS - -uint64_t Time::get_global_tick() { - if (global_timer == nullptr) { - ErrorHandler("tried to use global tick without global timer configured"); - return 0; - } - uint64_t current_tick = Time::global_tick + global_timer->Instance->CNT; - uint32_t apb1_tim_freq = HAL_RCC_GetPCLK1Freq() * 2; - double to_nanoseconds = 1.0 / apb1_tim_freq * 1000000000.0; - return current_tick * to_nanoseconds; -} - -void Time::start_timer(TIM_HandleTypeDef* handle, uint32_t prescaler, uint32_t arr) { - handle->Instance->ARR = arr; - handle->Instance->PSC = prescaler; - HAL_TIM_Base_Start_IT(handle); -} - -void Time::stop_timer(TIM_HandleTypeDef* handle) { HAL_TIM_Base_Stop_IT(handle); } - -uint8_t Time::register_high_precision_alarm(uint32_t period_in_us, function func) { - if (available_high_precision_timers.size() == 0) { - ErrorHandler("There are no available high precision timers left"); - return 0; - } - - TIM_HandleTypeDef* tim = Time::available_high_precision_timers.top(); - Time::available_high_precision_timers.pop(); - - Time::Alarm alarm = {.period = period_in_us, .tim = tim, .alarm = [&]() {}, .is_on = true}; - - for (auto timer : high_precision_timers) { - NVIC_DisableIRQ(timer32interrupt_by_timer32handler_map[timer]); - } - Time::high_precision_alarms_by_id[high_precision_ids] = alarm; - Time::high_precision_alarms_by_timer[tim] = alarm; - - Time::ConfigTimer(tim, period_in_us); - - alarm.alarm = func; - Time::high_precision_alarms_by_id[high_precision_ids] = alarm; - Time::high_precision_alarms_by_timer[tim] = alarm; - for (auto timer : high_precision_timers) { - NVIC_EnableIRQ(timer32interrupt_by_timer32handler_map[timer]); - } - - return high_precision_ids++; -} - -bool Time::unregister_high_precision_alarm(uint8_t id) { - if (not Time::high_precision_alarms_by_id.contains(id)) { - return false; - } - - for (auto timer : high_precision_timers) { - NVIC_DisableIRQ(timer32interrupt_by_timer32handler_map[timer]); - } - Time::Alarm& alarm = high_precision_alarms_by_id[id]; - Time::stop_timer(alarm.tim); - Time::available_high_precision_timers.push(alarm.tim); - - Time::high_precision_alarms_by_timer.erase(alarm.tim); - Time::high_precision_alarms_by_id.erase(id); - for (auto timer : high_precision_timers) { - NVIC_EnableIRQ(timer32interrupt_by_timer32handler_map[timer]); - } - - return true; -} - -uint8_t Time::register_mid_precision_alarm(uint32_t period_in_us, function func) { - if (mid_precision_timer == nullptr) { - ErrorHandler("Cannot register mid precision alarm if no timer is used for mid " - "precision"); - return 0; - } - if (mid_precision_alarms_by_id.size() == std::numeric_limits::max()) { - ErrorHandler("Cannot register mid precision alarm as all id's are already " - "occupied"); - return 0; - } - if (std::find_if( - TimerPeripheral::timers.begin(), - TimerPeripheral::timers.end(), - [&](TimerPeripheral& tim) -> bool { - return tim.handle == mid_precision_timer && (tim.is_occupied()); - } - ) != TimerPeripheral::timers.end()) { - ErrorHandler("a timer cannot be used as mid precision timer and PWM or Input " - "Capture Simultaneously"); - return 0; // TODO: put this check in high precision timers and global - // timer too - } - - Time::Alarm alarm = { - .period = period_in_us / Time::mid_precision_step_in_us, - .tim = Time::mid_precision_timer, - .alarm = [&]() {}, - .offset = Time::mid_precision_tick, - .is_on = true - }; - - while (mid_precision_alarms_by_id.contains(mid_precision_ids)) - mid_precision_ids++; - - NVIC_DisableIRQ(timer32interrupt_by_timer32handler_map[mid_precision_timer]); - Time::mid_precision_alarms_by_id[mid_precision_ids] = alarm; - - if (not Time::mid_precision_registered) { - hal_enable_timer(mid_precision_timer); - Time::init_timer( - timer32_by_timer32handler_map[mid_precision_timer], - Time::mid_precision_timer, - 275, - Time::mid_precision_step_in_us, - timer32interrupt_by_timer32handler_map[mid_precision_timer] - ); - Time::ConfigTimer(Time::mid_precision_timer, Time::mid_precision_step_in_us); - NVIC_DisableIRQ(timer32interrupt_by_timer32handler_map[mid_precision_timer]); - Time::mid_precision_registered = true; - } - - alarm.alarm = func; - Time::mid_precision_alarms_by_id[mid_precision_ids] = alarm; - NVIC_EnableIRQ(timer32interrupt_by_timer32handler_map[mid_precision_timer]); - - return mid_precision_ids; -} - -bool Time::unregister_mid_precision_alarm(uint8_t id) { - if (not Time::mid_precision_alarms_by_id.contains(id)) { - return false; - } - - NVIC_DisableIRQ(timer32interrupt_by_timer32handler_map[mid_precision_timer]); - Time::Alarm& alarm = Time::mid_precision_alarms_by_id[id]; - alarm.is_on = false; - Time::mid_precision_erasable_ids.push(id); - NVIC_EnableIRQ(timer32interrupt_by_timer32handler_map[mid_precision_timer]); - - return true; -} - -uint8_t Time::register_low_precision_alarm(uint32_t period_in_ms, function func) { - if (low_precision_alarms_by_id.size() == std::numeric_limits::max()) { - ErrorHandler("Cannot register low precision alarm as all id's are already " - "occupied"); - return 0; - } - Time::Alarm alarm = { - .period = period_in_ms, - .tim = low_precision_timer, - .alarm = func, - .offset = low_precision_tick, - .is_on = true - }; - - while (low_precision_alarms_by_id.contains(low_precision_ids)) - low_precision_ids++; - - NVIC_DisableIRQ(TIM7_IRQn); - Time::low_precision_alarms_by_id[low_precision_ids] = alarm; - NVIC_EnableIRQ(TIM7_IRQn); - return low_precision_ids++; -} - -uint8_t Time::register_low_precision_alarm(uint32_t period_in_ms, void (*func)()) { - if (low_precision_alarms_by_id.size() == std::numeric_limits::max()) { - ErrorHandler("Cannot register low precision alarm as all id's are already " - "occupied"); - return 0; - } - Time::Alarm alarm = { - .period = period_in_ms, - .tim = low_precision_timer, - .alarm = func, - .offset = low_precision_tick, - .is_on = true - }; - - while (low_precision_alarms_by_id.contains(low_precision_ids)) - low_precision_ids++; - - NVIC_DisableIRQ(TIM7_IRQn); - Time::low_precision_alarms_by_id[low_precision_ids] = alarm; - NVIC_EnableIRQ(TIM7_IRQn); - return low_precision_ids++; -} - -bool Time::unregister_low_precision_alarm(uint8_t id) { - if (not Time::low_precision_alarms_by_id.contains(id)) { - return false; - } - - NVIC_DisableIRQ(TIM7_IRQn); - Time::Alarm& alarm = Time::low_precision_alarms_by_id[id]; - alarm.is_on = false; - Time::low_precision_erasable_ids.push(id); - NVIC_EnableIRQ(TIM7_IRQn); - - return true; -} - -uint8_t Time::set_timeout(int milliseconds, function callback) { - if (low_precision_alarms_by_id.size() == std::numeric_limits::max()) { - ErrorHandler("Cannot register timeout as all low precision alarms id's are " - "already occupied"); - return 255; - } - while (low_precision_alarms_by_id.contains(low_precision_ids)) - low_precision_ids++; - uint8_t id = low_precision_ids; - uint64_t tick_on_register = low_precision_tick; - Time::register_low_precision_alarm(milliseconds, [&, id, callback, tick_on_register]() { - if (tick_on_register == low_precision_tick) { - return; - } - callback(); - Time::unregister_low_precision_alarm(id); - }); - return id; -} - -void Time::cancel_timeout(uint8_t id) { - // we can take advantage of the fact that if we try to - // unregister an alarm that is not registered, it will return false - // instead of going HardFault or ErrorHandler - Time::unregister_low_precision_alarm(id); -} - -void Time::global_timer_callback() { Time::global_tick += Time::HIGH_PRECISION_MAX_ARR; } - -void Time::high_precision_timer_callback(TIM_HandleTypeDef* tim) { - Time::high_precision_alarms_by_timer[tim].alarm(); -} - -void Time::mid_precision_timer_callback() { - for (pair& pair : Time::mid_precision_alarms_by_id) { - Time::Alarm& alarm = pair.second; - if (alarm.is_on == false || alarm.offset == Time::mid_precision_tick) - continue; - if ((Time::mid_precision_tick - alarm.offset) % alarm.period == 0) { - alarm.alarm(); - } - } - Time::mid_precision_tick++; - while (not Time::mid_precision_erasable_ids.empty()) { - Time::mid_precision_alarms_by_id.erase(Time::mid_precision_erasable_ids.top()); - Time::mid_precision_erasable_ids.pop(); - } -} - -void Time::low_precision_timer_callback() { - for (auto& pair : Time::low_precision_alarms_by_id) { - Time::Alarm& alarm = pair.second; - if (alarm.is_on == false || alarm.offset == Time::low_precision_tick) - continue; - if ((Time::low_precision_tick - alarm.offset) % alarm.period == 0) { - alarm.alarm(); - } - } - low_precision_tick++; - while (not Time::low_precision_erasable_ids.empty()) { - Time::low_precision_alarms_by_id.erase(Time::low_precision_erasable_ids.top()); - Time::low_precision_erasable_ids.pop(); - } -} - -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* tim) { - if (tim == Time::global_timer) { - Time::global_timer_callback(); - } else if (tim == Time::mid_precision_timer) { - Time::mid_precision_timer_callback(); - } else if (tim == Time::low_precision_timer) { - Time::low_precision_timer_callback(); - } - - else if (Time::high_precision_timers.contains(tim)) { - Time::high_precision_timer_callback(tim); - } -} - -void Time::ConfigTimer(TIM_HandleTypeDef* tim, uint32_t period_in_us) { - __HAL_TIM_DISABLE_IT(tim, TIM_IT_UPDATE); - tim->Instance->CR1 = ~(TIM_CR1_DIR | TIM_CR1_CMS) & tim->Instance->CR1; - tim->Instance->CR1 = TIM_COUNTERMODE_UP | tim->Instance->CR1; - tim->Instance->CR1 = ~TIM_CR1_CKD & tim->Instance->CR1; - tim->Instance->CR1 = TIM_CLOCKDIVISION_DIV1 | tim->Instance->CR1; - tim->Instance->ARR = period_in_us; - tim->Instance->PSC = 275; - tim->Instance->EGR = TIM_EGR_UG; - tim->Instance->SMCR = 0; - tim->Instance->BDTR = 0; - tim->Instance->CR1 = TIM_CR1_CEN; - tim->Instance->CNT = 1; - tim->Instance->DIER = TIM_IT_UPDATE; -} diff --git a/Src/ST-LIB_HIGH/Protections/ProtectionManager.cpp b/Src/ST-LIB_HIGH/Protections/ProtectionManager.cpp index 058f7c1d..cc1b964e 100644 --- a/Src/ST-LIB_HIGH/Protections/ProtectionManager.cpp +++ b/Src/ST-LIB_HIGH/Protections/ProtectionManager.cpp @@ -1,6 +1,7 @@ #include "Protections/ProtectionManager.hpp" #include "HALAL/Services/Communication/FDCAN/FDCAN.hpp" +#include "HALAL/Services/Time/Scheduler.hpp" #include "Protections/Notification.hpp" @@ -81,10 +82,10 @@ void ProtectionManager::check_protections() { ProtectionManager::to_fault(); } Global_RTC::update_rtc_data(); - if (Time::get_global_tick() > + if (Scheduler::get_global_tick() > protection.get_last_notify_tick() + notify_delay_in_nanoseconds) { ProtectionManager::notify(protection); - protection.update_last_notify_tick(Time::get_global_tick()); + protection.update_last_notify_tick(Scheduler::get_global_tick()); } } } @@ -101,10 +102,10 @@ void ProtectionManager::check_high_frequency_protections() { ProtectionManager::to_fault(); } Global_RTC::update_rtc_data(); - if (Time::get_global_tick() > + if (Scheduler::get_global_tick() > protection.get_last_notify_tick() + notify_delay_in_nanoseconds) { ProtectionManager::notify(protection); - protection.update_last_notify_tick(Time::get_global_tick()); + protection.update_last_notify_tick(Scheduler::get_global_tick()); } } } diff --git a/Src/ST-LIB_LOW/Clocks/Counter.cpp b/Src/ST-LIB_LOW/Clocks/Counter.cpp deleted file mode 100644 index 2f4d9896..00000000 --- a/Src/ST-LIB_LOW/Clocks/Counter.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Time.hpp - * - * Created on: 11 nov. 2022 - * Author: Dani - */ - -#include - -#include "HALAL/Services/Time/Time.hpp" - -void Counter::update() { - this->freq = (this->counter + 0.0) / (this->update_period_ms / 1000.0); - this->counter = 0; -} - -Counter::Counter(uint64_t update_period_ms) : update_period_ms(update_period_ms) { - this->time_id = - Time::register_low_precision_alarm(this->update_period_ms, [&]() { this->update(); }); -} - -Counter::~Counter() { Time::unregister_low_precision_alarm(this->time_id); } - -void Counter::count() { this->counter++; } diff --git a/Src/ST-LIB_LOW/Clocks/Stopwatch.cpp b/Src/ST-LIB_LOW/Clocks/Stopwatch.cpp.old similarity index 100% rename from Src/ST-LIB_LOW/Clocks/Stopwatch.cpp rename to Src/ST-LIB_LOW/Clocks/Stopwatch.cpp.old diff --git a/Src/ST-LIB_LOW/ErrorHandler/ErrorHandler.cpp b/Src/ST-LIB_LOW/ErrorHandler/ErrorHandler.cpp index df73b875..77937b94 100644 --- a/Src/ST-LIB_LOW/ErrorHandler/ErrorHandler.cpp +++ b/Src/ST-LIB_LOW/ErrorHandler/ErrorHandler.cpp @@ -6,6 +6,7 @@ */ #include "ErrorHandler/ErrorHandler.hpp" +#include "HALAL/Services/Time/Scheduler.hpp" string ErrorHandlerModel::description = "Error-No-Description-Found"; string ErrorHandlerModel::line = "Error-No-Line-Found"; @@ -51,7 +52,7 @@ void ErrorHandlerModel::ErrorHandlerTrigger(string format, ...) { ErrorHandlerModel::func + "' File: " + ErrorHandlerModel::file; #ifdef HAL_TIM_MODULE_ENABLED - description += " | TimeStamp: " + to_string(Time::get_global_tick()); + description += " | TimeStamp: " + to_string(Scheduler::get_global_tick()); #endif } diff --git a/Src/ST-LIB_LOW/HalfBridge/HalfBridge.cpp b/Src/ST-LIB_LOW/HalfBridge/HalfBridge.cpp.old similarity index 100% rename from Src/ST-LIB_LOW/HalfBridge/HalfBridge.cpp rename to Src/ST-LIB_LOW/HalfBridge/HalfBridge.cpp.old diff --git a/Src/ST-LIB_LOW/ST-LIB_LOW.cpp b/Src/ST-LIB_LOW/ST-LIB_LOW.cpp index 7d2b43cb..9b4365f4 100644 --- a/Src/ST-LIB_LOW/ST-LIB_LOW.cpp +++ b/Src/ST-LIB_LOW/ST-LIB_LOW.cpp @@ -7,4 +7,6 @@ #include "ST-LIB_LOW.hpp" -void STLIB_LOW::start() { Sensor::start(); } +void STLIB_LOW::start() { + // Sensor::start(); +} diff --git a/Src/ST-LIB_LOW/Sensors/Sensor/Sensor.cpp b/Src/ST-LIB_LOW/Sensors/Sensor/Sensor.cpp.old similarity index 100% rename from Src/ST-LIB_LOW/Sensors/Sensor/Sensor.cpp rename to Src/ST-LIB_LOW/Sensors/Sensor/Sensor.cpp.old