Skyward boardcore
Loading...
Searching...
No Matches
CountedPWM.h
Go to the documentation of this file.
1/* Copyright (c) 2023 Skyward Experimental Rocketry
2 * Authors: Emilio Corigliano, 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
25#include <assert.h>
27
28#include "PWM.h"
29#include "utils/Debug.h"
30
31namespace Boardcore
32{
33
60{
61public:
82 CountedPWM(TIM_TypeDef* const pulseTimer,
83 TimerUtils::Channel const pulseChannel,
84 TimerUtils::TriggerSource const pulseTriggerSource,
85 TIM_TypeDef* const counterTimer,
86 TimerUtils::Channel const counterChannel,
87 TimerUtils::TriggerSource const counterTriggerSource);
88
90
91 void setFrequency(unsigned int pulseFrequency);
92
100 void setDutyCycle(float dutyCycle);
101
106 void setDutyCycleResolution(unsigned int dutyCycleResolution);
107
116 void generatePulses(uint16_t pulses);
117
121 bool isGenerating();
122
127 void updateTargetCount(uint16_t pulses);
128
133 uint16_t getTargetCount();
134
139 void updateCurrentCount(uint16_t count);
140
144 uint16_t getCurrentCount();
145
149 void stop();
150
151private:
152 // This class is not copyable!
153 CountedPWM& operator=(const CountedPWM&) = delete;
154 CountedPWM(const CountedPWM& p) = delete;
155
156 void configureTimers();
157
158 GP16bitTimer pulseTimer;
159 TimerUtils::Channel pulseChannel;
160 TimerUtils::TriggerSource pulseTriggerSource;
161
162 GP16bitTimer counterTimer;
163 TimerUtils::Channel counterChannel;
164 TimerUtils::TriggerSource counterTriggerSource;
165
166 unsigned int pulseFrequency = 50;
167 float dutyCycle = 0.5;
168 unsigned int dutyCycleResolution =
169 20000;
171};
172
173inline void CountedPWM::updateTargetCount(uint16_t pulses)
174{
175 counterTimer.setCaptureCompareRegister(counterChannel, pulses);
176}
177
179{
180 return counterTimer.readCaptureCompareRegister(counterChannel);
181}
182
183inline void CountedPWM::updateCurrentCount(uint16_t count)
184{
185 return counterTimer.setCounter(count);
186}
187
189{
190 return counterTimer.readCounter();
191}
192
193inline void CountedPWM::stop()
194{
195 counterTimer.setCaptureCompareRegister(counterChannel, getCurrentCount());
196}
197
198} // namespace Boardcore
This class generates a PWM signal for a chosen number of pulses.
Definition CountedPWM.h:60
uint16_t getTargetCount()
Allows to get the target periods count while the timers are generating the signal.
Definition CountedPWM.h:178
void updateCurrentCount(uint16_t count)
Allows to update the current periods count while the timers are generating the signal.
Definition CountedPWM.h:183
void stop()
Stops the PWM signal generation if it is ongoing.
Definition CountedPWM.h:193
void generatePulses(uint16_t pulses)
Triggers the generation of a specific number of PWM periods.
void updateTargetCount(uint16_t pulses)
Allows to update the target periods count while the timers are generating the signal.
Definition CountedPWM.h:173
void setFrequency(unsigned int pulseFrequency)
void setDutyCycle(float dutyCycle)
Sets the duty cycle for the specified channel.
uint16_t getCurrentCount()
Returns the number of periods counted until now.
Definition CountedPWM.h:188
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.
void setDutyCycleResolution(unsigned int dutyCycleResolution)
Sets the granularity of the PulseTimer duty cycle. So it sets the Auto Reload Register of the PulseTi...
bool isGenerating()
Returns whether the timers are generating the PWM signal or not.
void setCaptureCompareRegister(TimerUtils::Channel channel, T value)
T readCaptureCompareRegister(TimerUtils::Channel channel)
TriggerSource
Trigger sources.
Definition TimerUtils.h:60
This file includes all the types the logdecoder script will decode.