Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changesets/init_timer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release: minor
summary: move initialization outside constructor in PWM, DualPWM, Encoder and InputCapture
16 changes: 9 additions & 7 deletions Inc/HALAL/Services/Encoder/Encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ template <const TimerDomain::Timer& dev> class Encoder {

inline static TimerWrapper<dev>* timer;
inline static bool is_on = false;
inline static bool is_initialized = false;

Encoder(TimerWrapper<dev>* tim) {
if (timer == nullptr) {
init(tim);
}
}
Encoder(TimerWrapper<dev>* tim) { timer = tim; }

public:
static void init(TimerWrapper<dev>* tim) {
if (tim == nullptr || tim->instance == nullptr) {
ErrorHandler("Timer instance is not set for encoder");
}
Comment thread
oganigl marked this conversation as resolved.
TIM_Encoder_InitTypeDef sConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};

Expand Down Expand Up @@ -70,11 +70,13 @@ template <const TimerDomain::Timer& dev> class Encoder {
}
timer = tim;
}

static void turn_on() {
if (!is_initialized) {
init(timer);
is_initialized = true;
}
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;
Expand Down
24 changes: 17 additions & 7 deletions Inc/HALAL/Services/InputCapture/InputCapture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ class InputCapture {
TimerWrapper<dev>* timer = nullptr;
TimerDomain::InputCaptureInfo* info = nullptr;
bool is_on = false;

InputCapture(TimerWrapper<dev>* tim) {
timer = tim;

bool is_initialized = false;
InputCapture(TimerWrapper<dev>* tim) : timer(tim) {}
void init() {
if (is_initialized)
return;
if (timer == nullptr || timer->instance == nullptr || timer->instance->hal_tim == nullptr) {
ErrorHandler("Timer instance is not set for input capture");
return;
}
// Setup TimerDomain
uint8_t ch_rising = static_cast<uint8_t>(pin_rising.channel) - 1;
uint8_t ch_falling = static_cast<uint8_t>(channel_falling) - 1;
info = &TimerDomain::input_capture_info_backing[tim->instance->timer_idx][ch_rising];
TimerDomain::input_capture_info[tim->instance->timer_idx][ch_rising] = info;
TimerDomain::input_capture_info[tim->instance->timer_idx][ch_falling] = info;
info = &TimerDomain::input_capture_info_backing[timer->instance->timer_idx][ch_rising];
TimerDomain::input_capture_info[timer->instance->timer_idx][ch_rising] = info;
TimerDomain::input_capture_info[timer->instance->timer_idx][ch_falling] = info;

info->channel_rising = ch_rising;
info->channel_falling = ch_falling;
Expand All @@ -61,10 +66,15 @@ class InputCapture {
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
timer->template config_input_compare_channel<channel_falling>(&sConfigIC);
is_initialized = true;
}

public:
void turn_on(void) {
if (is_initialized == false) {
init();
return;
}
if (is_on)
return;

Expand Down
20 changes: 15 additions & 5 deletions Inc/HALAL/Services/PWM/DualPWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ class DualPWM {
friend TimerWrapper<dev>;

TimerWrapper<dev>* timer;
uint32_t* frequency;
float* duty_cycle = nullptr;
uint32_t* frequency;
uint32_t polarity;
uint32_t negated_polarity;
bool is_on_positive = false;
bool is_on_negative = false;
bool is_initialized = false;

/* This constructor is private for a reason. Use TimerWrapper<dev>::get_dual_pwm */
DualPWM(
Expand All @@ -35,10 +38,16 @@ class DualPWM {
float* duty_ptr,
uint32_t* frequency_ptr
)
: timer(tim) {
duty_cycle = duty_ptr;
frequency = frequency_ptr;
: timer(tim), duty_cycle(duty_ptr), frequency(frequency_ptr), polarity(polarity),
negated_polarity(negated_polarity) {}

inline void initialize() {
if (is_initialized)
return;

if (timer == nullptr || timer->instance == nullptr || timer->instance->hal_tim == nullptr) {
ErrorHandler("Timer instance is not set for DualPWM");
}
TIM_OC_InitTypeDef sConfigOC = {
Comment thread
oganigl marked this conversation as resolved.
.OCMode = TIM_OCMODE_PWM1,
.Pulse = 0,
Expand All @@ -53,10 +62,12 @@ class DualPWM {

timer->template config_output_compare_channel<pin.channel>(&sConfigOC);
timer->template set_output_compare_preload_enable<pin.channel>();
is_initialized = true;
}

public:
inline void turn_on() {
initialize();
turn_on_positive();
turn_on_negative();
}
Expand All @@ -65,7 +76,6 @@ class DualPWM {
turn_off_positive();
turn_off_negative();
}

void turn_on_positive() {
if (this->is_on_positive)
return;
Expand Down
19 changes: 15 additions & 4 deletions Inc/HALAL/Services/PWM/PWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ template <const TimerDomain::Timer& dev, const ST_LIB::TimerPin pin> class PWM {
uint32_t* frequency;
float* duty_cycle = nullptr;
bool is_on = false;
bool is_initialized = false;
uint32_t polarity;
uint32_t negated_polarity;

/* This constructor is private for a reason. Use TimerWrapper<dev>::get_pwm */
PWM(TimerWrapper<dev>* tim,
uint32_t polarity,
uint32_t negated_polarity,
float* duty_ptr,
uint32_t* frequency_ptr)
: timer(tim) {
duty_cycle = duty_ptr;
frequency = frequency_ptr;

: timer(tim), polarity(polarity), negated_polarity(negated_polarity), duty_cycle(duty_ptr),
frequency(frequency_ptr) {}
void init() {
if (is_initialized)
return;
Comment thread
oganigl marked this conversation as resolved.
if (timer == nullptr || timer->instance == nullptr || timer->instance->hal_tim == nullptr) {
ErrorHandler("Timer instance is not set for PWM");
}
TIM_OC_InitTypeDef sConfigOC = {
.OCMode = TIM_OCMODE_PWM1,
.Pulse = 0,
Expand All @@ -45,10 +52,14 @@ template <const TimerDomain::Timer& dev, const ST_LIB::TimerPin pin> class PWM {
};
timer->template config_output_compare_channel<pin.channel>(&sConfigOC);
timer->template set_output_compare_preload_enable<pin.channel>();
is_initialized = true;
}

public:
void turn_on() {
if (this->is_initialized == false) {
init();
}
if (this->is_on)
return;

Expand Down
Loading