Finished Timer IRQ abstraction
This commit is contained in:
@@ -2,25 +2,25 @@
|
||||
// Created by Luca on 8/28/2025.
|
||||
//
|
||||
|
||||
#ifndef SHMINGO_HAL_SHAL_TIMER_CALLBACK_H
|
||||
#define SHMINGO_HAL_SHAL_TIMER_CALLBACK_H
|
||||
#ifndef SHMINGO_HAL_SHAL_TIM_CALLBACK_H
|
||||
#define SHMINGO_HAL_SHAL_TIM_CALLBACK_H
|
||||
|
||||
#include "SHAL_TIM_REG.h"
|
||||
|
||||
#define DEFINE_TIMER_IRQ(key, irq_handler) \
|
||||
extern "C" void irq_handler(void) { \
|
||||
auto tim_reg = getTimerRegister(key) \
|
||||
auto tim_reg = getTimerRegister(key); \
|
||||
if (tim_reg->SR & TIM_SR_UIF) { \
|
||||
tim_reg->SR &= ~TIM_SR_UIF; /* clear flag */ \
|
||||
auto cb = timer_callbacks[static_cast<int>(key)]; \
|
||||
if (cb) cb(); \
|
||||
} \
|
||||
}
|
||||
}; \
|
||||
};
|
||||
|
||||
typedef void (*TimerCallback)(); //Typedef for callback function
|
||||
|
||||
static TimerCallback timer_callbacks[static_cast<int>(Timer_Key::NUM_TIMERS)] = {nullptr}; //Timer IRQ Callback table
|
||||
[[maybe_unused]] static TimerCallback timer_callbacks[static_cast<int>(Timer_Key::NUM_TIMERS)] = {nullptr}; //Timer IRQ Callback table
|
||||
|
||||
void registerTimerCallback(Timer_Key key, TimerCallback callback);
|
||||
|
||||
#endif //SHMINGO_HAL_SHAL_TIMER_CALLBACK_H
|
||||
#endif //SHMINGO_HAL_SHAL_TIM_CALLBACK_H
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
enum class Bus {
|
||||
AHB,
|
||||
APB1,
|
||||
APB2
|
||||
APB2,
|
||||
INVALID
|
||||
};
|
||||
|
||||
struct RCC_Peripheral {
|
||||
@@ -23,9 +24,11 @@ enum class Timer_Key { //For STM32F072
|
||||
S_TIM14,
|
||||
S_TIM15,
|
||||
S_TIM16,
|
||||
S_TIM17
|
||||
S_TIM17,
|
||||
NUM_TIMERS
|
||||
};
|
||||
|
||||
|
||||
//Get timer peripheral struct including bus register, enable mask, timer mask
|
||||
constexpr RCC_Peripheral getTimerRCC(Timer_Key t) {
|
||||
switch(t) {
|
||||
@@ -36,6 +39,7 @@ 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};;
|
||||
}
|
||||
|
||||
return {Bus::APB2, &RCC->APB2ENR, RCC_APB2ENR_TIM1EN};
|
||||
@@ -51,6 +55,7 @@ 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;
|
||||
}
|
||||
return TIM1;
|
||||
}
|
||||
@@ -64,6 +69,7 @@ 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;
|
||||
}
|
||||
return TIM1_BRK_UP_TRG_COM_IRQn;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
#define SHAL_TIM_H
|
||||
|
||||
#include "SHAL_TIM_REG.h"
|
||||
|
||||
|
||||
#include "SHAL_TIM_CALLBACK.h"
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
@@ -16,12 +15,20 @@ public:
|
||||
//Stops the counter
|
||||
void stop();
|
||||
|
||||
//Set prescaler value
|
||||
void setPrescaler(uint16_t presc);
|
||||
|
||||
//Set auto reload register
|
||||
void setARR(uint16_t arr);
|
||||
|
||||
//Enable interrupts
|
||||
void enableInterrupt();
|
||||
|
||||
//Set timer IRQ callback function
|
||||
void setCallbackFunc(TimerCallback callback){
|
||||
registerTimerCallback(timer, callback);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Timer_Key timer;
|
||||
|
||||
Reference in New Issue
Block a user