Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
PWM.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_PWM_HXX_
35#define _FREERTOS_DRIVERS_COMMON_PWM_HXX_
36
37#include "utils/macros.h"
38
39#include "os/Gpio.hxx"
40
42class PWM
43{
44public:
47 virtual void set_period(uint32_t counts) = 0;
48
51 virtual uint32_t get_period() = 0;
52
55 virtual void set_duty(uint32_t counts) = 0;
56
59 virtual uint32_t get_duty() = 0;
60
63 void set_duty_percent(uint8_t percent)
64 {
65 HASSERT(percent <= 100);
66 set_duty(((get_period() * percent) + 50) / 100);
67 }
68
72 {
73 return (((get_duty() * 100) + (get_period() >> 1)) / get_period());
74 }
75
78 virtual uint32_t get_period_max() = 0;
79
82 virtual uint32_t get_period_min() = 0;
83
84protected:
86 constexpr PWM() = default;
87
92 ~PWM() = default;
93
94private:
95
97};
98
100class PWMGPO : public Gpio
101{
102public:
107 PWMGPO(PWM *instance, uint32_t on_counts, uint32_t off_counts)
108 : Gpio()
109 , instance_(instance)
110 , onCounts_(on_counts)
111 , offCounts_(off_counts)
112 {
113 }
114
115 virtual ~PWMGPO()
116 {
117 }
118
121 void write(Value new_state) const override
122 {
123 new_state ? set() : clr();
124 }
125
128 Value read() const override
129 {
130 return instance_->get_duty() == onCounts_ ? Gpio::SET : Gpio::CLR;
131 }
132
134 void set() const override
135 {
137 }
138
140 void clr() const override
141 {
143 }
144
147 void set_direction(Gpio::Direction dir) const override
148 {
149 HASSERT(dir == Gpio::Direction::DOUTPUT);
150 }
151
154 Direction direction() const override
155 {
156 return Gpio::Direction::DOUTPUT;
157 }
158
159 uint32_t get_on_counts() const
160 {
161 return onCounts_;
162 }
163
164 uint32_t get_off_counts() const
165 {
166 return offCounts_;
167 }
168
169private:
172
174 uint32_t onCounts_;
175
177 uint32_t offCounts_;
178
180};
181
182#endif // _FREERTOS_DRIVERS_COMMON_PWM_HXX_
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Value
Defines the options for GPIO level.
Definition Gpio.hxx:62
Direction
Defines the options for GPIO direction.
Definition Gpio.hxx:73
General Purpose Output specialization of a PWM Bit.
Definition PWM.hxx:101
void set() const override
Sets the GPO pin to high.
Definition PWM.hxx:134
uint32_t offCounts_
PWM counts for when the GPO is "off".
Definition PWM.hxx:177
void set_direction(Gpio::Direction dir) const override
Sets the GPO direction (does nothing).
Definition PWM.hxx:147
PWMGPO(PWM *instance, uint32_t on_counts, uint32_t off_counts)
Constructor.
Definition PWM.hxx:107
void write(Value new_state) const override
Writes a GPO pin (set or clear to a specific state).
Definition PWM.hxx:121
PWM * instance_
PWM instance.
Definition PWM.hxx:171
Direction direction() const override
Gets the GPO direction.
Definition PWM.hxx:154
void clr() const override
Clears the GPO pin to low.
Definition PWM.hxx:140
uint32_t onCounts_
PWM counts for when the GPO is "on".
Definition PWM.hxx:174
Value read() const override
Retrieves the current Value of a GPO output sate (requested).
Definition PWM.hxx:128
Abstract interface for a PWM driver.
Definition PWM.hxx:43
uint8_t get_duty_percent()
Gets the duty cycle.
Definition PWM.hxx:71
virtual void set_period(uint32_t counts)=0
Set PWM period.
void set_duty_percent(uint8_t percent)
Sets the duty cycle.
Definition PWM.hxx:63
virtual uint32_t get_period()=0
Get PWM period.
constexpr PWM()=default
Constructor.
virtual uint32_t get_period_max()=0
Get max period supported.
~PWM()=default
Destructor.
virtual uint32_t get_duty()=0
Gets the duty cycle.
virtual void set_duty(uint32_t counts)=0
Sets the duty cycle.
virtual uint32_t get_period_min()=0
Get min period supported.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171