Made timer constructor unaccessable by user
This commit is contained in:
@@ -3,12 +3,17 @@
|
||||
//
|
||||
|
||||
#include "SHAL_TIM.h"
|
||||
#include <cassert>
|
||||
|
||||
Timer::Timer(Timer_Key t) : timer(t), timer_reg(getTimerRegister(t)){
|
||||
RCC_Peripheral rcc = getTimerRCC(timer);
|
||||
*rcc.reg |= rcc.bitmask;
|
||||
}
|
||||
|
||||
Timer::Timer() : timer(Timer_Key::S_TIM_INVALID), timer_reg(nullptr){
|
||||
|
||||
}
|
||||
|
||||
void Timer::start() {
|
||||
timer_reg->CR1 |= TIM_CR1_CEN;
|
||||
timer_reg->EGR |= TIM_EGR_UG; //load prescaler reg and ARR
|
||||
@@ -33,3 +38,17 @@ void Timer::enableInterrupt() {
|
||||
}
|
||||
|
||||
|
||||
Timer &TimerManager::get(Timer_Key timer_key) {
|
||||
|
||||
//Ensure that we don't try to get invalid timers
|
||||
assert(timer_key != Timer_Key::S_TIM_INVALID && timer_key != Timer_Key::NUM_TIMERS);
|
||||
|
||||
Timer& selected = timers[static_cast<int>(timer_key)];
|
||||
|
||||
//Timer queried is not initialized yet (defaults to invalid)
|
||||
if(selected.timer == Timer_Key::S_TIM_INVALID){
|
||||
timers[static_cast<int>(timer_key)] = Timer(timer_key); //Initialize timer
|
||||
}
|
||||
|
||||
return timers[static_cast<int>(timer_key)];
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@ int main() {
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
|
||||
|
||||
auto timer2 = Timer(Timer_Key::S_TIM2);
|
||||
Timer timer2 = getTimer(Timer_Key::S_TIM2);
|
||||
|
||||
timer2.setPrescaler(8000 - 1);
|
||||
timer2.setARR(250 - 1);
|
||||
timer2.setARR(500 - 1);
|
||||
timer2.setCallbackFunc(tim2Handler);
|
||||
timer2.start();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user