Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Stm32PWM.hxx
Go to the documentation of this file.
1
34#ifndef _FREEERTOS_DRIVERS_ST_STM32PWM_HXX_
35#define _FREEERTOS_DRIVERS_ST_STM32PWM_HXX_
36
37#include "PWM.hxx"
39
40#include "stm32f_hal_conf.hxx"
41
46{
47public:
54 Stm32PWMGroup(TIM_TypeDef *base, uint16_t prescaler, uint32_t period_counts)
55 {
56 memset(&timHandle_, 0, sizeof(timHandle_));
57 timHandle_.Instance = base;
58 timHandle_.Init.Prescaler = prescaler;
59 timHandle_.Init.ClockDivision = 0;
60 timHandle_.Init.CounterMode = TIM_COUNTERMODE_UP;
61 timHandle_.Init.RepetitionCounter = 0;
62 timHandle_.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
63 set_period(period_counts); // will call hal init.
64 channels_[0].emplace(this, TIM_CHANNEL_1);
65 channels_[1].emplace(this, TIM_CHANNEL_2);
66 channels_[2].emplace(this, TIM_CHANNEL_3);
67 channels_[3].emplace(this, TIM_CHANNEL_4);
68 }
69
72 PWM *get_channel(unsigned id)
73 {
74 HASSERT(id > 0 && id < 5);
75 return &*channels_[id - 1];
76 }
77
81 static constexpr PWM *get_channel(Stm32PWMGroup *parent, unsigned id)
82 {
83 return uninitialized<Channel>::cast_data(&parent->channels_[id - 1]);
84 }
85
86private:
87 class Channel : public PWM
88 {
89 public:
90 Channel(Stm32PWMGroup *parent, uint32_t channel)
91 : parent_(parent)
92 , channel_(channel)
93 {
94 }
95
96 void set_period(uint32_t counts) override
97 {
98 parent_->set_period(counts);
99 }
100 uint32_t get_period() override
101 {
102 return parent_->timHandle_.Init.Prescaler *
103 parent_->timHandle_.Init.Period;
104 }
105 void set_duty(uint32_t counts) override
106 {
107 lastDuty_ = counts;
108 counts /= parent_->timHandle_.Init.Prescaler;
109 HASSERT(counts <= 65535);
110 TIM_OC_InitTypeDef config;
111 config.OCMode = TIM_OCMODE_PWM1;
112 config.OCPolarity = TIM_OCPOLARITY_HIGH;
113 config.OCFastMode = TIM_OCFAST_DISABLE;
114 config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
115 config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
116 config.OCIdleState = TIM_OCIDLESTATE_RESET;
117 config.Pulse = counts;
118
119 HASSERT(HAL_TIM_PWM_ConfigChannel(
120 &parent_->timHandle_, &config, channel_) == HAL_OK);
121 HASSERT(
122 HAL_TIM_PWM_Start(&parent_->timHandle_, channel_) == HAL_OK);
123 }
124 uint32_t get_duty() override
125 {
126 return lastDuty_;
127 return 0;
128 }
129 uint32_t get_period_max() override
130 {
131 return 65535 * parent_->timHandle_.Init.Prescaler;
132 }
133 uint32_t get_period_min() override
134 {
135 return 2 * parent_->timHandle_.Init.Prescaler;
136 }
137
138 private:
139 Stm32PWMGroup *parent_;
140 uint32_t channel_;
142 uint32_t lastDuty_{0};
143 };
144
145 friend class Channel;
146
147 void set_period(uint32_t counts)
148 {
149 counts /= timHandle_.Init.Prescaler;
150 timHandle_.Init.Period = counts;
151 HASSERT(HAL_TIM_PWM_Init(&timHandle_) == HAL_OK);
152 }
153
155 TIM_HandleTypeDef timHandle_;
156
157 uninitialized<Channel> channels_[4];
158};
159
160#endif // _FREEERTOS_DRIVERS_ST_STM32PWM_HXX_
Abstract interface for a PWM driver.
Definition PWM.hxx:43
uint32_t get_period_min() override
Get min period supported.
Definition Stm32PWM.hxx:133
uint32_t get_period() override
Get PWM period.
Definition Stm32PWM.hxx:100
void set_duty(uint32_t counts) override
Sets the duty cycle.
Definition Stm32PWM.hxx:105
void set_period(uint32_t counts) override
Set PWM period.
Definition Stm32PWM.hxx:96
uint32_t get_period_max() override
Get max period supported.
Definition Stm32PWM.hxx:129
uint32_t lastDuty_
Last set duty cycle.
Definition Stm32PWM.hxx:142
uint32_t get_duty() override
Gets the duty cycle.
Definition Stm32PWM.hxx:124
Set of 4 PWM channels that belong to a single timer resource in the STM32 microcontrollers.
Definition Stm32PWM.hxx:46
PWM * get_channel(unsigned id)
Definition Stm32PWM.hxx:72
Stm32PWMGroup(TIM_TypeDef *base, uint16_t prescaler, uint32_t period_counts)
Constructor.
Definition Stm32PWM.hxx:54
TIM_HandleTypeDef timHandle_
HAL handle structure. Note this is extremely wasteful of RAM.
Definition Stm32PWM.hxx:155
static constexpr PWM * get_channel(Stm32PWMGroup *parent, unsigned id)
Definition Stm32PWM.hxx:81
Template class that allows allocating storage for an object but not calling its constructor.
static constexpr T * cast_data(uninitialized< T > *parent)
Public API to convert the pointer in a linker-initialized way.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138