semf
semf::Stm32Pwm Class Reference

Pwm implementation for STM32. More...

#include <stm32pwm.h>

Inheritance diagram for semf::Stm32Pwm:
Inheritance graph
Collaboration diagram for semf::Stm32Pwm:
Collaboration graph

Public Types

enum class  ErrorCode : uint8_t {
  Set_HalError = 0 , Set_HalBusy , Set_HalTimeout , Start_HalError ,
  Start_HalBusy , Start_HalTimeout , Stop_HalError , Stop_HalBusy ,
  Stop_HalTimeout
}
 
enum class  Mode : uint8_t { Mode1 , Mode2 }
 Defines the start level of a PWM cycle. See also OCxM setting IMx_CCMR1 register description. More...
 

Public Member Functions

 Stm32Pwm (TIM_HandleTypeDef &hwHandle, uint32_t channel, bool fastMode=false, Mode mode=Mode::Mode1)
 Constructor. More...
 
 Stm32Pwm (const Stm32Pwm &other)=delete
 
virtual ~Stm32Pwm ()=default
 
void setMaxValue (unsigned int maxValue) override
 Set the maximum possible PWM value. More...
 
unsigned int maxValue () const override
 Returns the maximum possible PWM value. More...
 
void set (unsigned int value, bool inverted=false) override
 Set a PWM value. Duty cycle is value / maxValue. More...
 
unsigned int value () const override
 Returns the current PWM value. More...
 
void start () override
 
void stop () override
 
bool isEnabled () const override
 Returns the current status of PWM module. More...
 
- Public Member Functions inherited from semf::Pwm
virtual ~Pwm ()=default
 
virtual void setMaxValue (unsigned int maxValue)=0
 Set the maximum possible PWM value. More...
 
virtual unsigned int maxValue () const =0
 Returns the maximum possible PWM value. More...
 
virtual void set (unsigned int value, bool inverted=false)=0
 Set a PWM value. Duty cycle is value / maxValue. More...
 
virtual unsigned int value () const =0
 Returns the current PWM value. More...
 
virtual void start ()=0
 
virtual void stop ()=0
 
virtual bool isEnabled () const =0
 Returns the current status of PWM module. More...
 

Additional Inherited Members

- Public Attributes inherited from semf::Pwm
Signal< Errorerror
 

Detailed Description

Pwm implementation for STM32.

Definition at line 22 of file stm32pwm.h.

Member Enumeration Documentation

◆ ErrorCode

enum class semf::Stm32Pwm::ErrorCode : uint8_t
strong

Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring).

Enumerator
Set_HalError 
Set_HalBusy 
Set_HalTimeout 
Start_HalError 
Start_HalBusy 
Start_HalTimeout 
Stop_HalError 
Stop_HalBusy 
Stop_HalTimeout 

Definition at line 26 of file stm32pwm.h.

◆ Mode

enum class semf::Stm32Pwm::Mode : uint8_t
strong

Defines the start level of a PWM cycle. See also OCxM setting IMx_CCMR1 register description.

Attention
The inversion option finds application here, too.
Enumerator
Mode1 

If counting up, level is high active as long as counter value is smaller than pulse value. If counting down, level is low active as long as counter value is smaller than pulse value.

Mode2 

If counting up, level is low active as long as counter value is smaller than pulse value. If counting down, level is high active as long as counter value is smaller than pulse value.

Definition at line 43 of file stm32pwm.h.

Constructor & Destructor Documentation

◆ Stm32Pwm() [1/2]

semf::Stm32Pwm::Stm32Pwm ( TIM_HandleTypeDef &  hwHandle,
uint32_t  channel,
bool  fastMode = false,
Mode  mode = Mode::Mode1 
)

Constructor.

Parameters
hwHandleReference to timer hardware handle.
channelChannel selection define from STM timer HAL description. Use TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3 or TIM_CHANNEL_4.
fastModeFlag to enable or disable fast mode.
modeMode setting.

Definition at line 16 of file stm32pwm.cpp.

◆ Stm32Pwm() [2/2]

semf::Stm32Pwm::Stm32Pwm ( const Stm32Pwm other)
explicitdelete

◆ ~Stm32Pwm()

virtual semf::Stm32Pwm::~Stm32Pwm ( )
virtualdefault

Member Function Documentation

◆ isEnabled()

bool semf::Stm32Pwm::isEnabled ( ) const
overridevirtual

Returns the current status of PWM module.

Returns
  • true: PWM is enabled and active,
  • false: PWM is disabled and not active.

Implements semf::Pwm.

Definition at line 160 of file stm32pwm.cpp.

◆ maxValue()

unsigned int semf::Stm32Pwm::maxValue ( ) const
overridevirtual

Returns the maximum possible PWM value.

Returns
The maximum possible PWM value.

Implements semf::Pwm.

Definition at line 30 of file stm32pwm.cpp.

Here is the caller graph for this function:

◆ set()

void semf::Stm32Pwm::set ( unsigned int  value,
bool  inverted = false 
)
overridevirtual

Set a PWM value. Duty cycle is value / maxValue.

Parameters
valueThe new PWM value.
invertedInvert the PWM output.
Exceptions
Set_HalErrorIf the ST-HAL returns a hal error.
Set_HalBusyIf the ST-HAL returns a hal busy.
Set_HalTimeoutIf the ST-HAL returns a hal timeout.

Implements semf::Pwm.

Definition at line 35 of file stm32pwm.cpp.

Here is the call graph for this function:

◆ setMaxValue()

void semf::Stm32Pwm::setMaxValue ( unsigned int  maxValue)
overridevirtual

Set the maximum possible PWM value.

Parameters
maxValueThe maximum possible PWM value.

Implements semf::Pwm.

Definition at line 24 of file stm32pwm.cpp.

Here is the call graph for this function:

◆ start()

void semf::Stm32Pwm::start ( )
overridevirtual

Start the PWM hardware.

Exceptions
Start_HalErrorIf the ST-HAL returns a hal error.
Start_HalBusyIf the ST-HAL returns a hal busy.
Start_HalTimeoutIf the ST-HAL returns a hal timeout.

Implements semf::Pwm.

Definition at line 103 of file stm32pwm.cpp.

Here is the caller graph for this function:

◆ stop()

void semf::Stm32Pwm::stop ( )
overridevirtual

Start the PWM hardware.

Exceptions
Stop_HalErrorIf the ST-HAL returns a hal error.
Stop_HalBusyIf the ST-HAL returns a hal busy.
Stop_HalTimeoutIf the ST-HAL returns a hal timeout.

Implements semf::Pwm.

Definition at line 134 of file stm32pwm.cpp.

◆ value()

unsigned int semf::Stm32Pwm::value ( ) const
overridevirtual

Returns the current PWM value.

Returns
The current PWM value.

Implements semf::Pwm.

Definition at line 129 of file stm32pwm.cpp.

Here is the caller graph for this function: