Made timer constructor unaccessable by user
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
#define SHAL_TIM_REG_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <stm32f072xb.h>
|
||||
|
||||
|
||||
enum class Bus {
|
||||
AHB,
|
||||
APB1,
|
||||
@@ -25,7 +27,8 @@ enum class Timer_Key { //For STM32F072
|
||||
S_TIM15,
|
||||
S_TIM16,
|
||||
S_TIM17,
|
||||
NUM_TIMERS
|
||||
NUM_TIMERS,
|
||||
S_TIM_INVALID
|
||||
};
|
||||
|
||||
|
||||
@@ -39,10 +42,13 @@ constexpr RCC_Peripheral getTimerRCC(Timer_Key t) {
|
||||
case Timer_Key::S_TIM15: return {Bus::APB2, &RCC->APB2ENR, RCC_APB2ENR_TIM15EN};
|
||||
case Timer_Key::S_TIM16: return {Bus::APB2, &RCC->APB2ENR, RCC_APB2ENR_TIM16EN};
|
||||
case Timer_Key::S_TIM17: return {Bus::APB2, &RCC->APB2ENR, RCC_APB2ENR_TIM17EN};
|
||||
case Timer_Key::NUM_TIMERS: return {Bus::INVALID, nullptr, 0};;
|
||||
case Timer_Key::NUM_TIMERS:
|
||||
case Timer_Key::S_TIM_INVALID:
|
||||
assert(false);
|
||||
return {Bus::INVALID, nullptr, 0};; //Unreachable
|
||||
}
|
||||
|
||||
return {Bus::APB2, &RCC->APB2ENR, RCC_APB2ENR_TIM1EN};
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
//Get actual register value based on enum
|
||||
@@ -55,9 +61,12 @@ constexpr volatile TIM_TypeDef* getTimerRegister(Timer_Key t) {
|
||||
case Timer_Key::S_TIM15: return TIM15;
|
||||
case Timer_Key::S_TIM16: return TIM16;
|
||||
case Timer_Key::S_TIM17: return TIM17;
|
||||
case Timer_Key::NUM_TIMERS: return nullptr;
|
||||
case Timer_Key::NUM_TIMERS:
|
||||
case Timer_Key::S_TIM_INVALID:
|
||||
assert(false);
|
||||
return nullptr; //Unreachable
|
||||
}
|
||||
return TIM1;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
constexpr IRQn_Type getIRQn(Timer_Key t) {
|
||||
@@ -69,9 +78,12 @@ constexpr IRQn_Type getIRQn(Timer_Key t) {
|
||||
case Timer_Key::S_TIM15: return TIM15_IRQn;
|
||||
case Timer_Key::S_TIM16: return TIM16_IRQn;
|
||||
case Timer_Key::S_TIM17: return TIM17_IRQn;
|
||||
case Timer_Key::NUM_TIMERS: return TIM1_BRK_UP_TRG_COM_IRQn;
|
||||
case Timer_Key::NUM_TIMERS:
|
||||
case Timer_Key::S_TIM_INVALID:
|
||||
assert(false);
|
||||
return TIM1_BRK_UP_TRG_COM_IRQn; //Unreachable
|
||||
}
|
||||
return TIM1_BRK_UP_TRG_COM_IRQn;
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
#include "SHAL_TIM_REG.h"
|
||||
#include "SHAL_TIM_CALLBACK.h"
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
#include <array>
|
||||
|
||||
explicit Timer(Timer_Key t);
|
||||
class Timer {
|
||||
friend class TimerManager;
|
||||
public:
|
||||
|
||||
//Starts the counter
|
||||
void start();
|
||||
@@ -31,9 +32,25 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
explicit Timer(Timer_Key t);
|
||||
Timer();
|
||||
|
||||
Timer_Key timer;
|
||||
volatile TIM_TypeDef* timer_reg;
|
||||
|
||||
};
|
||||
|
||||
#define getTimer(timer_key) TimerManager::get(timer_key);
|
||||
|
||||
//Manages all timers so user does not have to personally initialize
|
||||
class TimerManager{
|
||||
public:
|
||||
|
||||
static Timer& get(Timer_Key);
|
||||
TimerManager() = delete;
|
||||
|
||||
private:
|
||||
inline static Timer timers[static_cast<int>(Timer_Key::NUM_TIMERS)] = {};
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user