Skyward boardcore
Loading...
Searching...
No Matches
PWM.cpp
Go to the documentation of this file.
1/* Copyright (c) 2017-2021 Skyward Experimental Rocketry
2 * Authors: Andrea Palumbo, Luca Erbetta, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "PWM.h"
24
25using namespace Boardcore::TimerUtils;
26
27namespace Boardcore
28{
29
30PWM::PWM(TIM_TypeDef* const pulseTimer, uint16_t pulseFrequency)
31 : pulseTimer(pulseTimer), pulseFrequency(pulseFrequency)
32{
33 // Erase the previous timer configuration
34 this->pulseTimer.reset();
35
36 configureTimer();
37
38 // Keep the timer always enabled
39 this->pulseTimer.enable();
40}
41
42PWM::~PWM() { pulseTimer.reset(); }
43
44void PWM::setFrequency(uint16_t pulseFrequency)
45{
46 this->pulseFrequency = pulseFrequency;
47 configureTimer();
48}
49
50void PWM::setDutyCycleResolution(uint16_t dutyCycleResolution)
51{
52 this->dutyCycleResolution = dutyCycleResolution;
53 configureTimer();
54}
55
56void PWM::enableChannel(Channel channel, Polarity polarity)
57{
58 pulseTimer.setOutputCompareMode(channel, OutputCompareMode::PWM_MODE_1);
60 channel, static_cast<OutputComparePolarity>(polarity));
61
62 // This will ensure that the duty cycle will change only at the next period
63 pulseTimer.enableCaptureComparePreload(channel);
64
65 pulseTimer.enableCaptureCompareOutput(channel);
66}
67
69{
70 pulseTimer.disableCaptureCompareOutput(channel);
71}
72
74{
75 return pulseTimer.isCaptureCompareOutputEnabled(channel);
76}
77
78void PWM::setDutyCycle(Channel channel, float dutyCycle)
79{
80 if (dutyCycle >= 0 && dutyCycle <= 1)
81 {
83 channel,
84 static_cast<uint16_t>(
85 dutyCycle * pulseTimer.readAutoReloadRegister() + 0.5));
86 }
87}
88
90{
91 return static_cast<float>(pulseTimer.readCaptureCompareRegister(channel)) /
92 static_cast<float>(pulseTimer.readAutoReloadRegister());
93}
94
95GP16bitTimer& PWM::getTimer() { return pulseTimer; }
96
97void PWM::configureTimer()
98{
99 pulseTimer.setFrequency(dutyCycleResolution * pulseFrequency);
100 pulseTimer.setAutoReloadRegister(getFrequency(pulseTimer.getTimer()) /
101 pulseFrequency);
102
103 // Force the timer to update its configuration
104 pulseTimer.generateUpdate();
105}
106
107} // namespace Boardcore
virtual void generateUpdate() final
Re-initializes the timer counter and generate an update of the registers (the prescaler is cleared to...
Definition BasicTimer.h:286
TIM_TypeDef * getTimer()
Definition BasicTimer.h:214
virtual void setFrequency(int frequency) final
Allows to set directly the frequency of the timer's clock.
Definition BasicTimer.h:307
void enableCaptureCompareOutput(TimerUtils::Channel channel)
void setCaptureCompareRegister(TimerUtils::Channel channel, T value)
void enableCaptureComparePreload(TimerUtils::Channel channel)
The capture/compare register is buffered.
void setOutputCompareMode(TimerUtils::Channel channel, TimerUtils::OutputCompareMode modeChannel)
void setCaptureComparePolarity(TimerUtils::Channel channel, TimerUtils::OutputComparePolarity polarity)
void reset() override
Resets the timer configuration to the default state.
void setAutoReloadRegister(T autoReloadValue)
T readCaptureCompareRegister(TimerUtils::Channel channel)
void disableCaptureCompareOutput(TimerUtils::Channel channel)
bool isCaptureCompareOutputEnabled(TimerUtils::Channel channel)
void disableChannel(TimerUtils::Channel channel)
Definition PWM.cpp:68
void setDutyCycle(TimerUtils::Channel channel, float dutyCycle)
Sets the duty cycle for the specified channel.
Definition PWM.cpp:78
bool isChannelEnabled(TimerUtils::Channel channel)
Definition PWM.cpp:73
float getDutyCycle(TimerUtils::Channel channel)
Return the channel's duty cycle in the range [0,1].
Definition PWM.cpp:89
void setDutyCycleResolution(uint16_t dutyCycleResolution)
Definition PWM.cpp:50
void enableChannel(TimerUtils::Channel channel, Polarity polarity=Polarity::NORMAL)
Definition PWM.cpp:56
void setFrequency(uint16_t pulseFrequency)
Definition PWM.cpp:44
PWM(TIM_TypeDef *const pulseTimer, uint16_t pulseFrequency=50)
Sets up and enables the PWM timer.
Definition PWM.cpp:30
GP16bitTimer & getTimer()
Returns the timer used to generate the pwm signal.
Definition PWM.cpp:95
Timer utilities.
Definition TimerUtils.h:36
uint32_t getFrequency(TIM_TypeDef *timer)
Return the timer clock frequency.
Definition TimerUtils.h:392
This file includes all the types the logdecoder script will decode.