ChibiOS/RT Architecture - Reference Manual - Guides |
Generic PWM Driver. More...
Generic PWM Driver.
This module implements a generic PWM driver. The driver implements a state machine internally:
Data Structures | |
struct | PWMChannelConfig |
PWM driver channel configuration structure. More... | |
struct | PWMConfig |
Driver configuration structure. More... | |
struct | PWMDriver |
Structure representing an PWM driver. More... | |
Modules | |
PWM Low Level Driver | |
PWM Driver low level driver template. | |
Defines | |
#define | pwmEnableChannelI(pwmp, channel, width) pwm_lld_enable_channel(pwmp, channel, width) |
Enables a PWM channel. | |
#define | pwmDisableChannelI(pwmp, channel) pwm_lld_disable_channel(pwmp, channel) |
Disables a PWM channel. | |
#define | PWM_CHANNELS 4 |
Number of PWM channels per PWM driver. | |
#define | USE_STM32_PWM1 TRUE |
PWM1 driver enable switch. | |
#define | USE_STM32_PWM2 TRUE |
PWM2 driver enable switch. | |
#define | USE_STM32_PWM3 TRUE |
PWM3 driver enable switch. | |
#define | USE_STM32_PWM4 TRUE |
PWM4 driver enable switch. | |
#define | STM32_PWM1_IRQ_PRIORITY 7 |
PWM1 interrupt priority level setting. | |
#define | STM32_PWM2_IRQ_PRIORITY 7 |
PWM2 interrupt priority level setting. | |
#define | STM32_PWM3_IRQ_PRIORITY 7 |
PWM3 interrupt priority level setting. | |
#define | STM32_PWM4_IRQ_PRIORITY 7 |
PWM4 interrupt priority level setting. | |
#define | PWM_COMPUTE_PSC(clksrc, pwmclk) ((uint16_t)(((clksrc) / (pwmclk)) - 1)) |
PWM clock prescaler initialization utility. | |
#define | PWM_COMPUTE_ARR(pwmclk, pwmperiod) ((uint16_t)(((pwmclk) / (1000000000 / (pwmperiod))) - 1)) |
PWM cycle period initialization utility. | |
#define | PWM_FRACTION_TO_WIDTH(pwmp, numerator, denominator) |
Converts from fraction to pulse width. | |
#define | PWM_DEGREES_TO_WIDTH(pwmp, degrees) PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees) |
Converts from degrees to pulse width. | |
#define | PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage) |
Converts from percentage to pulse width. | |
Typedefs | |
typedef uint8_t | pwmchannel_t |
PWM channel type. | |
typedef uint16_t | pwmcnt_t |
PWM counter type. | |
typedef struct PWMDriver | PWMDriver |
Type of a structure representing an PWM driver. | |
typedef void(* | pwmcallback_t )(PWMDriver *pwmp) |
PWM notification callback type. | |
Enumerations | |
enum | pwmstate_t { PWM_UNINIT = 0, PWM_STOP = 1, PWM_READY = 2 } |
Driver state machine possible states. More... | |
enum | pwmmode_t { PWM_OUTPUT_DISABLED = 0, PWM_OUTPUT_ACTIVE_HIGH = 1, PWM_OUTPUT_ACTIVE_LOW = 2 } |
PWM logic mode. More... | |
Functions | |
void | pwmInit (void) |
PWM Driver initialization. | |
void | pwmObjectInit (PWMDriver *pwmp) |
Initializes the standard part of a PWMDriver structure. | |
void | pwmStart (PWMDriver *pwmp, const PWMConfig *config) |
Configures and activates the PWM peripheral. | |
void | pwmStop (PWMDriver *pwmp) |
Deactivates the PWM peripheral. | |
void | pwmEnableChannel (PWMDriver *pwmp, pwmchannel_t channel, pwmcnt_t width) |
Enables a PWM channel. | |
void | pwmDisableChannel (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a PWM channel. | |
void | pwm_lld_init (void) |
Low level PWM driver initialization. | |
void | pwm_lld_start (PWMDriver *pwmp) |
Configures and activates the PWM peripheral. | |
void | pwm_lld_stop (PWMDriver *pwmp) |
Deactivates the PWM peripheral. | |
void | pwm_lld_enable_channel (PWMDriver *pwmp, pwmchannel_t channel, pwmcnt_t width) |
Enables a PWM channel. | |
void | pwm_lld_disable_channel (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a PWM channel. |
#define pwmEnableChannelI | ( | pwmp, | ||
channel, | ||||
width | ||||
) | pwm_lld_enable_channel(pwmp, channel, width) |
#define pwmDisableChannelI | ( | pwmp, | ||
channel | ||||
) | pwm_lld_disable_channel(pwmp, channel) |
#define PWM_CHANNELS 4 |
Number of PWM channels per PWM driver.
Definition at line 40 of file platforms/STM32/pwm_lld.h.
#define USE_STM32_PWM1 TRUE |
PWM1 driver enable switch.
If set to TRUE
the support for PWM1 is included.
TRUE
. Definition at line 52 of file platforms/STM32/pwm_lld.h.
#define USE_STM32_PWM2 TRUE |
PWM2 driver enable switch.
If set to TRUE
the support for PWM2 is included.
TRUE
. Definition at line 61 of file platforms/STM32/pwm_lld.h.
#define USE_STM32_PWM3 TRUE |
PWM3 driver enable switch.
If set to TRUE
the support for PWM3 is included.
TRUE
. Definition at line 70 of file platforms/STM32/pwm_lld.h.
#define USE_STM32_PWM4 TRUE |
PWM4 driver enable switch.
If set to TRUE
the support for PWM4 is included.
TRUE
. Definition at line 79 of file platforms/STM32/pwm_lld.h.
#define STM32_PWM1_IRQ_PRIORITY 7 |
PWM1 interrupt priority level setting.
Definition at line 86 of file platforms/STM32/pwm_lld.h.
Referenced by pwm_lld_start().
#define STM32_PWM2_IRQ_PRIORITY 7 |
PWM2 interrupt priority level setting.
Definition at line 93 of file platforms/STM32/pwm_lld.h.
Referenced by pwm_lld_start().
#define STM32_PWM3_IRQ_PRIORITY 7 |
PWM3 interrupt priority level setting.
Definition at line 100 of file platforms/STM32/pwm_lld.h.
Referenced by pwm_lld_start().
#define STM32_PWM4_IRQ_PRIORITY 7 |
PWM4 interrupt priority level setting.
Definition at line 107 of file platforms/STM32/pwm_lld.h.
Referenced by pwm_lld_start().
#define PWM_COMPUTE_PSC | ( | clksrc, | ||
pwmclk | ||||
) | ((uint16_t)(((clksrc) / (pwmclk)) - 1)) |
PWM clock prescaler initialization utility.
[in] | clksrc | clock source frequency, depending on the target timer cell it can be one of:
Please refer to the STM32 HAL driver documentation and/or the STM32 Reference Manual for the right clock source. |
[in] | pwmclk | PWM clock frequency in cycles |
pc_psc
field of the PWMConfig
structure. Definition at line 245 of file platforms/STM32/pwm_lld.h.
#define PWM_COMPUTE_ARR | ( | pwmclk, | ||
pwmperiod | ||||
) | ((uint16_t)(((pwmclk) / (1000000000 / (pwmperiod))) - 1)) |
PWM cycle period initialization utility.
[in] | pwmclk | PWM clock frequency in cycles |
[in] | pwmperiod | PWM cycle period in nanoseconds |
pc_arr
field of the PWMConfig
structure. Definition at line 257 of file platforms/STM32/pwm_lld.h.
#define PWM_FRACTION_TO_WIDTH | ( | pwmp, | ||
numerator, | ||||
denominator | ||||
) |
((uint16_t)((((uint32_t)(pwmp)->pd_config->pc_arr + 1UL) * \ (uint32_t)(denominator)) / (uint32_t)(numerator)))
Converts from fraction to pulse width.
PWM_COMPUTE_PSC
.[in] | numerator | numerator of the fraction |
[in] | denominator | percentage as an integer between 0 and numerator |
pwmEnableChannel()
.Definition at line 273 of file platforms/STM32/pwm_lld.h.
#define PWM_DEGREES_TO_WIDTH | ( | pwmp, | ||
degrees | ||||
) | PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees) |
Converts from degrees to pulse width.
PWM_COMPUTE_PSC
.[in] | pwmp | pointer to a PWMDriver object |
[in] | degrees | degrees as an integer between 0 and 36000 |
pwmEnableChannel()
.Definition at line 290 of file platforms/STM32/pwm_lld.h.
#define PWM_PERCENTAGE_TO_WIDTH | ( | pwmp, | ||
percentage | ||||
) | PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage) |
Converts from percentage to pulse width.
PWM_COMPUTE_PSC
.[in] | pwmp | pointer to a PWMDriver object |
[in] | percentage | percentage as an integer between 0 and 10000 |
pwmEnableChannel()
.Definition at line 306 of file platforms/STM32/pwm_lld.h.
typedef uint8_t pwmchannel_t |
PWM channel type.
Definition at line 130 of file platforms/STM32/pwm_lld.h.
PWM counter type.
Definition at line 135 of file platforms/STM32/pwm_lld.h.
Type of a structure representing an PWM driver.
Definition at line 140 of file platforms/STM32/pwm_lld.h.
typedef void(* pwmcallback_t)(PWMDriver *pwmp) |
PWM notification callback type.
[in] | pwmp | pointer to a PWMDriver object |
Definition at line 147 of file platforms/STM32/pwm_lld.h.
enum pwmstate_t |
enum pwmmode_t |
void pwmInit | ( | void | ) |
PWM Driver initialization.
Definition at line 59 of file pwm.c.
References pwm_lld_init().
Referenced by halInit().
void pwmObjectInit | ( | PWMDriver * | pwmp | ) |
Initializes the standard part of a PWMDriver
structure.
[in] | pwmp | pointer to a PWMDriver object |
Definition at line 69 of file pwm.c.
References PWMDriver::pd_config, and PWMDriver::pd_state.
Referenced by pwm_lld_init().
Configures and activates the PWM peripheral.
Definition at line 84 of file pwm.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, PWMDriver::pd_config, PWMDriver::pd_state, pwm_lld_start(), PWM_READY, and PWM_STOP.
void pwmStop | ( | PWMDriver * | pwmp | ) |
Deactivates the PWM peripheral.
[in] | pwmp | pointer to a PWMDriver object |
Definition at line 102 of file pwm.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, PWMDriver::pd_state, pwm_lld_stop(), PWM_READY, and PWM_STOP.
void pwmEnableChannel | ( | PWMDriver * | pwmp, | |
pwmchannel_t | channel, | |||
pwmcnt_t | width | |||
) |
Enables a PWM channel.
Programs (or reprograms) a PWM channel.
[in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...PWM_CHANNELS-1) |
[in] | width | PWM pulse width as clock pulses number |
Definition at line 122 of file pwm.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, PWMDriver::pd_state, PWM_CHANNELS, pwm_lld_enable_channel(), and PWM_READY.
void pwmDisableChannel | ( | PWMDriver * | pwmp, | |
pwmchannel_t | channel | |||
) |
Disables a PWM channel.
The channel is disabled and its output line returned to the idle state.
[in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...PWM_CHANNELS-1) |
Definition at line 144 of file pwm.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, PWMDriver::pd_state, PWM_CHANNELS, pwm_lld_disable_channel(), and PWM_READY.
void pwm_lld_init | ( | void | ) |
Low level PWM driver initialization.
Definition at line 206 of file platforms/STM32/pwm_lld.c.
References PWMDriver::pd_enabled_channels, PWMDriver::pd_tim, and pwmObjectInit().
Referenced by pwmInit().
void pwm_lld_start | ( | PWMDriver * | pwmp | ) |
Configures and activates the PWM peripheral.
Definition at line 258 of file platforms/STM32/pwm_lld.c.
References CORTEX_PRIORITY_MASK, NVICEnableVector(), PWMConfig::pc_arr, PWMConfig::pc_callback, PWMConfig::pc_channels, PWMConfig::pc_cr2, PWMConfig::pc_psc, PWMChannelConfig::pcc_mode, PWMDriver::pd_config, PWMDriver::pd_enabled_channels, PWMDriver::pd_state, PWMDriver::pd_tim, PWM_OUTPUT_ACTIVE_HIGH, PWM_OUTPUT_ACTIVE_LOW, PWM_STOP, STM32_PWM1_IRQ_PRIORITY, STM32_PWM2_IRQ_PRIORITY, STM32_PWM3_IRQ_PRIORITY, and STM32_PWM4_IRQ_PRIORITY.
Referenced by pwmStart().
void pwm_lld_stop | ( | PWMDriver * | pwmp | ) |
Deactivates the PWM peripheral.
Definition at line 370 of file platforms/STM32/pwm_lld.c.
References NVICDisableVector(), PWMDriver::pd_enabled_channels, PWMDriver::pd_state, PWMDriver::pd_tim, and PWM_READY.
Referenced by pwmStop().
void pwm_lld_enable_channel | ( | PWMDriver * | pwmp, | |
pwmchannel_t | channel, | |||
pwmcnt_t | width | |||
) |
Enables a PWM channel.
[in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...PWM_CHANNELS-1) |
[in] | width | PWM pulse width as clock pulses number |
Definition at line 421 of file platforms/STM32/pwm_lld.c.
References PWMConfig::pc_channels, PWMChannelConfig::pcc_callback, PWMDriver::pd_config, PWMDriver::pd_enabled_channels, and PWMDriver::pd_tim.
Referenced by pwmEnableChannel().
void pwm_lld_disable_channel | ( | PWMDriver * | pwmp, | |
pwmchannel_t | channel | |||
) |
Disables a PWM channel.
The channel is disabled and its output line returned to the idle state.
[in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...PWM_CHANNELS-1) |
Definition at line 446 of file platforms/STM32/pwm_lld.c.
References PWMDriver::pd_enabled_channels, and PWMDriver::pd_tim.
Referenced by pwmDisableChannel().