libDaisy
Hardware Library for Daisy
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pwm.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_PWM_H
3#define DSY_PWM_H
4
5#include "daisy_core.h"
6#include "stm32h7xx_hal.h"
7#include <cstdint>
8
9namespace daisy
10{
49{
50 public:
51 class Impl;
52
57 struct Config
58 {
60 enum class Peripheral
61 {
62 TIM_3 = 0,
63 TIM_4 = 1,
64 TIM_5 = 2
65 };
71 uint32_t prescaler;
72
79 uint32_t period;
80
83 uint32_t prescaler_ = 0,
84 uint32_t period_ = 0xffff)
85 : periph(periph_), prescaler(prescaler_), period(period_)
86 {
87 }
88 };
89
91 enum class Result
92 {
93 OK = 0,
94 ERR = 1,
95 };
96
97 class Channel
98 {
99 public:
104 struct Config
105 {
111
113 enum class Polarity
114 {
115 HIGH = 0,
116 LOW
117 };
119
122 : pin(pin_), polarity(polarity_)
123 {
124 }
125 };
126
128 Channel(PWMHandle *owner, uint32_t channel)
129 : owner_(*owner), channel_(channel), scale_(65535.0f), handle_(nullptr)
130 {
131 }
132
134 inline const Config &GetConfig() const { return config_; }
135
139
143
147
151 inline void SetRaw(uint32_t raw)
152 {
153 __HAL_TIM_SET_COMPARE(handle_, channel_, raw);
154 }
155
161 inline void Set(float val)
162 {
163 if(val < 0.0f)
164 val = 0.0f;
165 if(val > 1.0f)
166 val = 1.0f;
167 SetRaw(static_cast<uint32_t>(val * scale_));
168 }
169
170 private:
171 PWMHandle & owner_;
172 const uint32_t channel_;
173 Channel::Config config_;
174 float scale_;
175 TIM_HandleTypeDef *handle_;
176 };
177
179
180 PWMHandle(const PWMHandle &other) = default;
181 PWMHandle &operator=(const PWMHandle &other) = default;
183
185 Result Init(const Config &config);
186
189
191 const Config &GetConfig() const;
192
195 inline Channel &Channel1() { return ch1_; }
196
199 inline Channel &Channel2() { return ch2_; }
200
203 inline Channel &Channel3() { return ch3_; }
204
207 inline Channel &Channel4() { return ch4_; };
208
210 void SetPrescaler(uint32_t prescaler);
211
213 void SetPeriod(uint32_t period);
214
215 private:
216 Impl *pimpl_;
217
218 // NOTE: These are stored here, not in the Impl class, so that channel
219 // references are valid even if taken before a call to PWMHandle::Init
220 Channel ch1_;
221 Channel ch2_;
222 Channel ch3_;
223 Channel ch4_;
224};
225
226} // namespace daisy
227
228#endif
Definition pwm.h:98
void SetRaw(uint32_t raw)
Set the duty cycle for the PWM channel.
Definition pwm.h:151
void Set(float val)
Set the duty cycle for the PWM channel. Automatically normalized to the timer's period.
Definition pwm.h:161
Channel(PWMHandle *owner, uint32_t channel)
Private constructor for channel. Do not use.
Definition pwm.h:128
PWMHandle::Result Init()
Initialize the channel using all defaults. Must be called manually, after PWMHandle::Init
PWMHandle::Result Init(const Channel::Config &config)
Initialize the channel. Must be called manually, after PWMHandle::Init
PWMHandle::Result DeInit()
Deinitialize the channel. Called automatically by PWMHandle::DeInit.
const Config & GetConfig() const
Returns a const reference to the Config struct.
Definition pwm.h:134
Hardware PWM using the timer peripheral.
Definition pwm.h:49
Channel & Channel3()
Get a reference to CH3 of this peripheral. Must be initialized before use.
Definition pwm.h:203
Result
Return values for PWM functions.
Definition pwm.h:92
const Config & GetConfig() const
Returns a const reference to the Config struct.
~PWMHandle()
Definition pwm.h:182
Result Init(const Config &config)
Initialize the PWM peripheral according to the config.
Channel & Channel1()
Get a reference to CH1 of this peripheral. Must be initialized before use.
Definition pwm.h:195
Channel & Channel4()
Get a reference to CH4 of this peripheral. Must be initialized before use.
Definition pwm.h:207
PWMHandle(const PWMHandle &other)=default
PWMHandle & operator=(const PWMHandle &other)=default
void SetPeriod(uint32_t period)
Set the period.
void SetPrescaler(uint32_t prescaler)
Set the prescaler.
Result DeInit()
Deinitialize the peripheral.
Channel & Channel2()
Get a reference to CH2 of this peripheral. Must be initialized before use.
Definition pwm.h:199
Hardware defines and helpers for daisy field platform.
Definition index.h:2
Configuration struct for an individual channel.
Definition pwm.h:105
Config(Pin pin_, Polarity polarity_=Polarity::HIGH)
Definition pwm.h:121
Polarity polarity
Definition pwm.h:118
Pin pin
Pin to use for this channel. Ensure that this is the proper pin for the timer and channel....
Definition pwm.h:110
Config()
Definition pwm.h:120
Polarity
Output polarity.
Definition pwm.h:114
Configuration struct for the timer peripheral.
Definition pwm.h:58
Peripheral
Hardware Timer to use for PWM.
Definition pwm.h:61
uint32_t prescaler
Prescaler that divides the PWM timer frequency. The final frequency will be sysclk / (2 * (period + 1...
Definition pwm.h:71
uint32_t period
period in ticks at TIM frequency before the counter resets. Affects both the frequency and resolution...
Definition pwm.h:79
Config(Peripheral periph_, uint32_t prescaler_=0, uint32_t period_=0xffff)
Definition pwm.h:82
Config()
Definition pwm.h:81
Peripheral periph
Definition pwm.h:66
representation of hardware port/pin combination
Definition daisy_core.h:193