Skyward boardcore
Loading...
Searching...
No Matches
PWM.h
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#pragma once
24
26
27namespace Boardcore
28{
29
51class PWM
52{
53public:
54 enum class Polarity : uint16_t
55 {
56 NORMAL = 0,
57 REVERSED = 0x1
58 };
59
67 explicit PWM(TIM_TypeDef* const pulseTimer, uint16_t pulseFrequency = 50);
68
69 ~PWM();
70
71 void setFrequency(uint16_t pulseFrequency);
72
73 void setDutyCycleResolution(uint16_t dutyCycleResolution);
74
76 Polarity polarity = Polarity::NORMAL);
77
79
81
90 void setDutyCycle(TimerUtils::Channel channel, float dutyCycle);
91
95 float getDutyCycle(TimerUtils::Channel channel);
96
101
102private:
103 // This class is not copyable!
104 PWM& operator=(const PWM&) = delete;
105 PWM(const PWM& p) = delete;
106
107 void configureTimer();
108
109 GP16bitTimer pulseTimer;
110
111 uint16_t pulseFrequency;
112 uint16_t dutyCycleResolution = 10000;
113};
114
115} // namespace Boardcore
Driver for easy access to the PWM capabilities of the general purpose timers.
Definition PWM.h:52
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
@ NORMAL
Signal is high for the duty cycle time.
@ REVERSED
Signal is low for the duty cycle time.
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
This file includes all the types the logdecoder script will decode.