Timer test

This commit is contained in:
Ea-r-th
2026-03-11 00:31:40 -07:00
parent 43bdee4406
commit 5b0819a300
7 changed files with 134 additions and 36 deletions

View File

@@ -42,8 +42,13 @@ enum class SHAL_Result{
//Currently configures systick to count down in microseconds
inline uint32_t ticks = 0;
void systick_init();
extern "C" void SysTick_Handler();
static uint32_t millis();
//Max of 16ms, use SHAL_delay_ms for longer delay
void SHAL_delay_us(uint32_t us);
@@ -71,6 +76,8 @@ bool SHAL_wait_for_condition_ms(Condition cond, uint32_t timeout_ms) {
return false; // timeout
}
#define SHAL_set_bits(reg, size, bits, offset) \
do { \
if ((reg) != NULL) { \

View File

@@ -83,7 +83,7 @@ static SHAL_TIM_RCC_Register getTimerRCC(Timer_Key t) {
}
static inline SHAL_TIM_Status_Register getTimerStatusRegister(Timer_Key key){
static SHAL_TIM_Status_Register getTimerStatusRegister(Timer_Key key){
SHAL_TIM_Status_Register res = {nullptr, TIM_SR_UIF};
@@ -93,7 +93,7 @@ static inline SHAL_TIM_Status_Register getTimerStatusRegister(Timer_Key key){
return res;
}
static inline SHAL_TIM_Control_Register_1 getTimerControlRegister1(Timer_Key key){
static SHAL_TIM_Control_Register_1 getTimerControlRegister1(Timer_Key key){
SHAL_TIM_Control_Register_1 res = {nullptr, TIM_CR1_CEN_Msk,
TIM_CR1_UDIS,
@@ -107,7 +107,7 @@ static inline SHAL_TIM_Control_Register_1 getTimerControlRegister1(Timer_Key key
return res;
}
static inline SHAL_TIM_DMA_Interrupt_Enable_Register getTimerDMAInterruptEnableRegister(Timer_Key key){
static SHAL_TIM_DMA_Interrupt_Enable_Register getTimerDMAInterruptEnableRegister(Timer_Key key){
SHAL_TIM_DMA_Interrupt_Enable_Register res = {nullptr, TIM_DIER_UIE};
@@ -117,7 +117,7 @@ static inline SHAL_TIM_DMA_Interrupt_Enable_Register getTimerDMAInterruptEnableR
return res;
}
static inline SHAL_TIM_Event_Generation_Register getTimerEventGenerationRegister(Timer_Key key){
static SHAL_TIM_Event_Generation_Register getTimerEventGenerationRegister(Timer_Key key){
SHAL_TIM_Event_Generation_Register res = {nullptr, TIM_EGR_UG};
@@ -127,7 +127,7 @@ static inline SHAL_TIM_Event_Generation_Register getTimerEventGenerationRegister
return res;
}
static inline SHAL_TIM_Break_Dead_Time_Register getTimerBreakDeadTimeRegister(Timer_Key key) {
static SHAL_TIM_Break_Dead_Time_Register getTimerBreakDeadTimeRegister(Timer_Key key) {
SHAL_TIM_Break_Dead_Time_Register res = {nullptr,
TIM_BDTR_DTG_Pos,
@@ -145,7 +145,7 @@ static inline SHAL_TIM_Break_Dead_Time_Register getTimerBreakDeadTimeRegister(Ti
return res;
}
static inline SHAL_TIM_Prescaler_Register getTimerPrescalerRegister(Timer_Key key){
static SHAL_TIM_Prescaler_Register getTimerPrescalerRegister(Timer_Key key){
SHAL_TIM_Prescaler_Register res = {nullptr, 1UL << 15};
@@ -155,7 +155,7 @@ static inline SHAL_TIM_Prescaler_Register getTimerPrescalerRegister(Timer_Key ke
return res;
}
static inline SHAL_TIM_Auto_Reload_Register getTimerAutoReloadRegister(Timer_Key key){
static SHAL_TIM_Auto_Reload_Register getTimerAutoReloadRegister(Timer_Key key){
SHAL_TIM_Auto_Reload_Register res = {nullptr, 1UL << 15};
@@ -165,8 +165,8 @@ static inline SHAL_TIM_Auto_Reload_Register getTimerAutoReloadRegister(Timer_Key
return res;
}
static inline SHAL_TIM_Capture_Compare_Register getTimerCaptureCompareRegister(Timer_Key key, SHAL_Timer_Channel channel){
auto channel_num = static_cast<uint8_t>(channel);
static SHAL_TIM_Capture_Compare_Register getTimerCaptureCompareRegister(Timer_Key key, SHAL_Timer_Channel channel){
const auto channel_num = static_cast<uint8_t>(channel);
volatile TIM_TypeDef* tim = TIM_INFO_TABLE[static_cast<uint8_t>(key)].timer;
@@ -178,17 +178,18 @@ static inline SHAL_TIM_Capture_Compare_Register getTimerCaptureCompareRegister(T
case SHAL_Timer_Channel::CH3: return {&tim->CCR3,0};
case SHAL_Timer_Channel::CH4: return {&tim->CCR4,0};
}
__builtin_unreachable();
}
static inline SHAL_TIM_Capture_Compare_Enable_Register getTimerCaptureCompareEnableRegister(Timer_Key key, SHAL_Timer_Channel channel){
static SHAL_TIM_Capture_Compare_Enable_Register getTimerCaptureCompareEnableRegister(Timer_Key key, SHAL_Timer_Channel channel){
constexpr uint8_t channel_stride = 3;
const auto channel_num = static_cast<uint8_t>(channel);
uint8_t channel_stride = 3;
auto channel_num = static_cast<uint8_t>(channel);
auto output_enable = TIM_CCER_CC1E << (channel_stride * (channel_num - 1));
auto output_polarity = TIM_CCER_CC1P << (channel_stride * channel_num);
auto output_complimentary_enable = TIM_CCER_CC1NE << (channel_stride * channel_num);
auto output_complimentary_polarity = TIM_CCER_CC1NP << (channel_stride * channel_num);
const auto output_enable = TIM_CCER_CC1E << (channel_stride * (channel_num - 1));
const auto output_polarity = TIM_CCER_CC1P << (channel_stride * channel_num);
const auto output_complimentary_enable = TIM_CCER_CC1NE << (channel_stride * channel_num);
const auto output_complimentary_polarity = TIM_CCER_CC1NP << (channel_stride * channel_num);
SHAL_TIM_Capture_Compare_Enable_Register res = {nullptr,
output_enable,
@@ -203,7 +204,7 @@ static inline SHAL_TIM_Capture_Compare_Enable_Register getTimerCaptureCompareEna
return res;
}
static inline SHAL_TIM_Output_Capture_Compare_Mode_Register getTimerOutputCaptureCompareModeRegister(Timer_Key key, SHAL_Timer_Channel channel) {
static SHAL_TIM_Output_Capture_Compare_Mode_Register getTimerOutputCaptureCompareModeRegister(Timer_Key key, SHAL_Timer_Channel channel) {
SHAL_TIM_Output_Capture_Compare_Mode_Register res = {
nullptr,
TIM_CCMR1_CC1S_Pos, //Channel 1 Capture/Compare selection
@@ -219,11 +220,11 @@ static inline SHAL_TIM_Output_Capture_Compare_Mode_Register getTimerOutputCaptur
};
volatile TIM_TypeDef* tim = TIM_INFO_TABLE[static_cast<uint8_t>(key)].timer;
uint8_t num_tim_channels = TIM_INFO_TABLE[static_cast<uint8_t>(key)].numChannels;
const uint8_t num_tim_channels = TIM_INFO_TABLE[static_cast<uint8_t>(key)].numChannels;
volatile uint32_t* reg = nullptr;
uint8_t channelNum = static_cast<uint32_t>(channel);
const auto channelNum = static_cast<uint32_t>(channel);
assert(num_tim_channels >= channelNum); //Assert that we don't access undefined memory trying to initialize a non-existent channel

View File

@@ -23,6 +23,17 @@ public:
/// \param autoReload The number of timer counts before the count is reset and IRQ is called
void init(uint16_t prescaler, uint16_t autoReload);
/// Simple function to set a timer in basic PWM mode
/// @param channel Channel to output on
/// @param prescaler Divider from sysclock
/// @param autoReload Counter value to reset at
/// @param captureCompareThreshold PWM trigger value (duty cycle = this / autoReload)
void configurePWM(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold);
void configureOneshot(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold);
//Starts the counter
void start();
@@ -30,10 +41,10 @@ public:
void stop();
//Set prescaler value
void setPrescaler(uint16_t presc);
void setPrescaler(uint16_t presc) const;
//Set auto reload register
void setARR(uint16_t arr);
void setARR(uint16_t arr) const;
//Enable interrupts
void enableInterrupt();
@@ -43,12 +54,8 @@ public:
void enableChannel(SHAL_Timer_Channel channel, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode, SHAL_Timer_Channel_Complimentary_Output_Mode complimentaryOutputMode);
void setOutputCompareMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode);
/// Set the duty cycle for PWM
/// \param dutyCycle 10 * percentage (e.g. 500 = 50%)
void setPWMDutyCycle(uint32_t dutyCycle);
//Set TIMER_KEY IRQ callback function
void setCallbackFunc(TimerCallback callback){
void setCallbackFunc(TimerCallback callback) const {
registerTimerCallback(m_key, callback);
}