Skyward boardcore
|
This class generates a PWM signal for a chosen number of pulses. More...
#include <CountedPWM.h>
Public Member Functions | |
CountedPWM (TIM_TypeDef *const pulseTimer, TimerUtils::Channel const pulseChannel, TimerUtils::TriggerSource const pulseTriggerSource, TIM_TypeDef *const counterTimer, TimerUtils::Channel const counterChannel, TimerUtils::TriggerSource const counterTriggerSource) | |
Constructor that initializes the timers. | |
~CountedPWM () | |
void | setFrequency (unsigned int pulseFrequency) |
void | setDutyCycle (float dutyCycle) |
Sets the duty cycle for the specified channel. | |
void | setDutyCycleResolution (unsigned int dutyCycleResolution) |
Sets the granularity of the PulseTimer duty cycle. So it sets the Auto Reload Register of the PulseTimer. | |
void | generatePulses (uint16_t pulses) |
Triggers the generation of a specific number of PWM periods. | |
bool | isGenerating () |
Returns whether the timers are generating the PWM signal or not. | |
void | updateTargetCount (uint16_t pulses) |
Allows to update the target periods count while the timers are generating the signal. | |
uint16_t | getTargetCount () |
Allows to get the target periods count while the timers are generating the signal. | |
void | updateCurrentCount (uint16_t count) |
Allows to update the current periods count while the timers are generating the signal. | |
uint16_t | getCurrentCount () |
Returns the number of periods counted until now. | |
void | stop () |
Stops the PWM signal generation if it is ongoing. | |
This class generates a PWM signal for a chosen number of pulses.
Two timers are used: PulseTimer and CounterTimer.
+---------—+ OverflowEvent UpdateCounter +-----------—+
PulseTimer | ------------------------------> | CounterTimer |
---|---|---|
PWM, GATED | <---------------------------— | ExtClkMode |
+---------—+ StopCounter OverflowEvent +-----------—+
As seen in the previous scheme, PulseTimer generates the PWM signal. This is seen both as output on the respective GPIO and as input for the CounterTimer. So, in this way, CounterTimer counts the pulses generated and, when a threshold is passed, the CounterTimer changes its output polarity. Since PulseTimer has as GATED input the CounterTimer, when the N pulses are exceeded, the PulseTimer is stopped and no other cycles are generated. This works because since we disable the PulseTimer, the CounterTimer doesn't exceed the Capture and Compare value set (which corresponds to the wanted pulses).
The requirement for this technique to work is that the two timers should have an "internal trigger input" (ITR) connection to each other.
Definition at line 59 of file CountedPWM.h.
Boardcore::CountedPWM::CountedPWM | ( | TIM_TypeDef *const | pulseTimer, |
TimerUtils::Channel const | pulseChannel, | ||
TimerUtils::TriggerSource const | pulseTriggerSource, | ||
TIM_TypeDef *const | counterTimer, | ||
TimerUtils::Channel const | counterChannel, | ||
TimerUtils::TriggerSource const | counterTriggerSource ) |
Constructor that initializes the timers.
In particular, the pulseTimer will be used to create the output PWM signal while the counterTimer will just count for the occurred pulses. The PWM signal will be transmitted on the pulseChannel
channel while the counterTimer signal will be accessible on the counterChannel
channel (for debug purposes).
pulseTimer | Timer used for the generation of the PWM signal. |
pulseChannel | Channel of pulseTimer on which the PWM signal will be generated. |
pulseTriggerSource | The corresponding ITR mode of the pulseTimer for having as external trigger input the counterTimer. |
counterTimer | Timer used for counting the pulses generated by the PWM. |
counterChannel | Channel of pulseTimer on which the counter signal will be visible. |
counterTriggerSource | The corresponding ITR mode of the counterTimer for having as external trigger input the pulseTimer. |
Definition at line 28 of file CountedPWM.cpp.
Boardcore::CountedPWM::~CountedPWM | ( | ) |
Definition at line 50 of file CountedPWM.cpp.
void Boardcore::CountedPWM::generatePulses | ( | uint16_t | pulses | ) |
Triggers the generation of a specific number of PWM periods.
Note that the function returns immediately and the PWM signal is generated in background by the timers.
pulses | The number of pulses to generate. |
Definition at line 86 of file CountedPWM.cpp.
|
inline |
Returns the number of periods counted until now.
Definition at line 188 of file CountedPWM.h.
|
inline |
Allows to get the target periods count while the timers are generating the signal.
Definition at line 178 of file CountedPWM.h.
bool Boardcore::CountedPWM::isGenerating | ( | ) |
Returns whether the timers are generating the PWM signal or not.
Definition at line 96 of file CountedPWM.cpp.
void Boardcore::CountedPWM::setDutyCycle | ( | float | dutyCycle | ) |
Sets the duty cycle for the specified channel.
Changes the duty cycle only if the specified value is in the range [0,1].
dutyCycle | Relative duty cycle, ranges from 0 to 1. |
Definition at line 68 of file CountedPWM.cpp.
void Boardcore::CountedPWM::setDutyCycleResolution | ( | unsigned int | dutyCycleResolution | ) |
Sets the granularity of the PulseTimer duty cycle. So it sets the Auto Reload Register of the PulseTimer.
Definition at line 80 of file CountedPWM.cpp.
void Boardcore::CountedPWM::setFrequency | ( | unsigned int | pulseFrequency | ) |
Definition at line 56 of file CountedPWM.cpp.
|
inline |
Stops the PWM signal generation if it is ongoing.
Definition at line 193 of file CountedPWM.h.
|
inline |
Allows to update the current periods count while the timers are generating the signal.
Definition at line 183 of file CountedPWM.h.
|
inline |
Allows to update the target periods count while the timers are generating the signal.
Definition at line 173 of file CountedPWM.h.