ChibiOS/HAL
6.1.0
|
Generic PWM Driver. More...
Generic PWM Driver.
This module implements a generic PWM (Pulse Width Modulation) driver.
HAL_USE_PWM
option must be enabled in halconf.h
.The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).
This driver abstracts a generic PWM timer composed of:
PWM_CHANNELS
PWM channels, each channel has an output, a comparator and is able to invoke an optional callback when a comparator match with the main counter happens.A PWM channel output can be in two different states:
Note that the two states can be associated to both logical zero or one in the PWMChannelConfig
structure.
Macros | |
#define | PWM_CHANNELS 4 |
Number of PWM channels per PWM driver. More... | |
#define | pwm_lld_change_period(pwmp, period) |
Changes the period the PWM peripheral. More... | |
PWM output mode macros | |
#define | PWM_OUTPUT_MASK 0x0FU |
Standard output modes mask. More... | |
#define | PWM_OUTPUT_DISABLED 0x00U |
Output not driven, callback only. More... | |
#define | PWM_OUTPUT_ACTIVE_HIGH 0x01U |
Positive PWM logic, active is logic level one. More... | |
#define | PWM_OUTPUT_ACTIVE_LOW 0x02U |
Inverse PWM logic, active is logic level zero. More... | |
PWM duty cycle conversion | |
#define | PWM_FRACTION_TO_WIDTH(pwmp, denominator, numerator) |
Converts from fraction to pulse width. More... | |
#define | PWM_DEGREES_TO_WIDTH(pwmp, degrees) PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees) |
Converts from degrees to pulse width. More... | |
#define | PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage) |
Converts from percentage to pulse width. More... | |
Macro Functions | |
#define | pwmChangePeriodI(pwmp, value) |
Changes the period the PWM peripheral. More... | |
#define | pwmEnableChannelI(pwmp, channel, width) |
Enables a PWM channel. More... | |
#define | pwmDisableChannelI(pwmp, channel) |
Disables a PWM channel. More... | |
#define | pwmIsChannelEnabledI(pwmp, channel) (((pwmp)->enabled & ((pwmchnmsk_t)1U << (pwmchnmsk_t)(channel))) != 0U) |
Returns a PWM channel status. More... | |
#define | pwmEnablePeriodicNotificationI(pwmp) pwm_lld_enable_periodic_notification(pwmp) |
Enables the periodic activation edge notification. More... | |
#define | pwmDisablePeriodicNotificationI(pwmp) pwm_lld_disable_periodic_notification(pwmp) |
Disables the periodic activation edge notification. More... | |
#define | pwmEnableChannelNotificationI(pwmp, channel) pwm_lld_enable_channel_notification(pwmp, channel) |
Enables a channel de-activation edge notification. More... | |
#define | pwmDisableChannelNotificationI(pwmp, channel) pwm_lld_disable_channel_notification(pwmp, channel) |
Disables a channel de-activation edge notification. More... | |
PLATFORM configuration options | |
#define | PLATFORM_PWM_USE_PWM1 FALSE |
PWMD1 driver enable switch. More... | |
Typedefs | |
typedef struct PWMDriver | PWMDriver |
Type of a structure representing a PWM driver. More... | |
typedef void(* | pwmcallback_t) (PWMDriver *pwmp) |
Type of a PWM notification callback. More... | |
typedef uint32_t | pwmmode_t |
Type of a PWM mode. More... | |
typedef uint8_t | pwmchannel_t |
Type of a PWM channel. More... | |
typedef uint32_t | pwmchnmsk_t |
Type of a channels mask. More... | |
typedef uint32_t | pwmcnt_t |
Type of a PWM counter. More... | |
Data Structures | |
struct | PWMChannelConfig |
Type of a PWM driver channel configuration structure. More... | |
struct | PWMConfig |
Type of a PWM driver configuration structure. More... | |
struct | PWMDriver |
Structure representing a PWM driver. More... | |
Functions | |
void | pwmInit (void) |
PWM Driver initialization. More... | |
void | pwmObjectInit (PWMDriver *pwmp) |
Initializes the standard part of a PWMDriver structure. More... | |
void | pwmStart (PWMDriver *pwmp, const PWMConfig *config) |
Configures and activates the PWM peripheral. More... | |
void | pwmStop (PWMDriver *pwmp) |
Deactivates the PWM peripheral. More... | |
void | pwmChangePeriod (PWMDriver *pwmp, pwmcnt_t period) |
Changes the period the PWM peripheral. More... | |
void | pwmEnableChannel (PWMDriver *pwmp, pwmchannel_t channel, pwmcnt_t width) |
Enables a PWM channel. More... | |
void | pwmDisableChannel (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a PWM channel and its notification. More... | |
void | pwmEnablePeriodicNotification (PWMDriver *pwmp) |
Enables the periodic activation edge notification. More... | |
void | pwmDisablePeriodicNotification (PWMDriver *pwmp) |
Disables the periodic activation edge notification. More... | |
void | pwmEnableChannelNotification (PWMDriver *pwmp, pwmchannel_t channel) |
Enables a channel de-activation edge notification. More... | |
void | pwmDisableChannelNotification (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a channel de-activation edge notification. More... | |
void | pwm_lld_init (void) |
Low level PWM driver initialization. More... | |
void | pwm_lld_start (PWMDriver *pwmp) |
Configures and activates the PWM peripheral. More... | |
void | pwm_lld_stop (PWMDriver *pwmp) |
Deactivates the PWM peripheral. More... | |
void | pwm_lld_enable_channel (PWMDriver *pwmp, pwmchannel_t channel, pwmcnt_t width) |
Enables a PWM channel. More... | |
void | pwm_lld_disable_channel (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a PWM channel and its notification. More... | |
void | pwm_lld_enable_periodic_notification (PWMDriver *pwmp) |
Enables the periodic activation edge notification. More... | |
void | pwm_lld_disable_periodic_notification (PWMDriver *pwmp) |
Disables the periodic activation edge notification. More... | |
void | pwm_lld_enable_channel_notification (PWMDriver *pwmp, pwmchannel_t channel) |
Enables a channel de-activation edge notification. More... | |
void | pwm_lld_disable_channel_notification (PWMDriver *pwmp, pwmchannel_t channel) |
Disables a channel de-activation edge notification. More... | |
Enumerations |
Variables | |
PWMDriver | PWMD1 |
PWMD1 driver identifier. More... | |
#define PWM_OUTPUT_DISABLED 0x00U |
#define PWM_OUTPUT_ACTIVE_HIGH 0x01U |
#define PWM_OUTPUT_ACTIVE_LOW 0x02U |
#define PWM_FRACTION_TO_WIDTH | ( | pwmp, | |
denominator, | |||
numerator | |||
) |
Converts from fraction to pulse width.
PWM_COMPUTE_PSC
.[in] | pwmp | pointer to a PWMDriver object |
[in] | denominator | denominator of the fraction |
[in] | numerator | numerator of the fraction |
pwmEnableChannel()
.#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()
.#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()
.#define pwmChangePeriodI | ( | pwmp, | |
value | |||
) |
Changes the period the PWM peripheral.
This function changes the period of a PWM unit that has already been activated using pwmStart()
.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | value | new cycle time in ticks |
Definition at line 172 of file hal_pwm.h.
Referenced by pwmChangePeriod().
#define pwmEnableChannelI | ( | pwmp, | |
channel, | |||
width | |||
) |
Enables a PWM channel.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
[in] | width | PWM pulse width as clock pulses number |
Definition at line 191 of file hal_pwm.h.
Referenced by pwmEnableChannel().
#define pwmDisableChannelI | ( | pwmp, | |
channel | |||
) |
Disables a PWM channel.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 210 of file hal_pwm.h.
Referenced by pwmDisableChannel().
#define pwmIsChannelEnabledI | ( | pwmp, | |
channel | |||
) | (((pwmp)->enabled & ((pwmchnmsk_t)1U << (pwmchnmsk_t)(channel))) != 0U) |
Returns a PWM channel status.
pwmStart()
.[in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
#define pwmEnablePeriodicNotificationI | ( | pwmp | ) | pwm_lld_enable_periodic_notification(pwmp) |
Enables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 236 of file hal_pwm.h.
Referenced by pwmEnablePeriodicNotification().
#define pwmDisablePeriodicNotificationI | ( | pwmp | ) | pwm_lld_disable_periodic_notification(pwmp) |
Disables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 248 of file hal_pwm.h.
Referenced by pwmDisablePeriodicNotification().
#define pwmEnableChannelNotificationI | ( | pwmp, | |
channel | |||
) | pwm_lld_enable_channel_notification(pwmp, channel) |
Enables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 262 of file hal_pwm.h.
Referenced by pwmEnableChannelNotification().
#define pwmDisableChannelNotificationI | ( | pwmp, | |
channel | |||
) | pwm_lld_disable_channel_notification(pwmp, channel) |
Disables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 276 of file hal_pwm.h.
Referenced by pwmDisableChannelNotification().
#define PWM_CHANNELS 4 |
Number of PWM channels per PWM driver.
Definition at line 37 of file hal_pwm_lld.h.
#define PLATFORM_PWM_USE_PWM1 FALSE |
PWMD1 driver enable switch.
If set to TRUE
the support for PWM1 is included.
FALSE
. Definition at line 53 of file hal_pwm_lld.h.
#define pwm_lld_change_period | ( | pwmp, | |
period | |||
) |
Changes the period the PWM peripheral.
This function changes the period of a PWM unit that has already been activated using pwmStart()
.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | period | new cycle time in ticks |
Definition at line 181 of file hal_pwm_lld.h.
typedef void(* pwmcallback_t) (PWMDriver *pwmp) |
typedef uint32_t pwmmode_t |
Type of a PWM mode.
Definition at line 68 of file hal_pwm_lld.h.
typedef uint8_t pwmchannel_t |
Type of a PWM channel.
Definition at line 73 of file hal_pwm_lld.h.
typedef uint32_t pwmchnmsk_t |
Type of a channels mask.
Definition at line 78 of file hal_pwm_lld.h.
typedef uint32_t pwmcnt_t |
Type of a PWM counter.
Definition at line 83 of file hal_pwm_lld.h.
enum pwmstate_t |
void pwmInit | ( | void | ) |
PWM Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 56 of file hal_pwm.c.
References pwm_lld_init().
Referenced by halInit().
void pwmObjectInit | ( | PWMDriver * | pwmp | ) |
Initializes the standard part of a PWMDriver
structure.
[out] | pwmp | pointer to a PWMDriver object |
Definition at line 68 of file hal_pwm.c.
References PWMDriver::channels, PWMDriver::config, PWMDriver::enabled, PWM_STOP, and PWMDriver::state.
Referenced by pwm_lld_init().
Configures and activates the PWM peripheral.
PWM_READY
state disables all the active channels.Definition at line 89 of file hal_pwm.c.
References PWMDriver::config, PWMDriver::enabled, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWMConfig::period, PWMDriver::period, pwm_lld_start(), PWM_READY, PWM_STOP, and PWMDriver::state.
void pwmStop | ( | PWMDriver * | pwmp | ) |
Deactivates the PWM peripheral.
[in] | pwmp | pointer to a PWMDriver object |
Definition at line 111 of file hal_pwm.c.
References PWMDriver::config, PWMDriver::enabled, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), pwm_lld_stop(), PWM_READY, PWM_STOP, and PWMDriver::state.
Changes the period the PWM peripheral.
This function changes the period of a PWM unit that has already been activated using pwmStart()
.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | period | new cycle time in ticks |
Definition at line 143 of file hal_pwm.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmChangePeriodI, and PWMDriver::state.
void pwmEnableChannel | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel, | ||
pwmcnt_t | width | ||
) |
Enables a PWM channel.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
[in] | width | PWM pulse width as clock pulses number |
Definition at line 167 of file hal_pwm.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmEnableChannelI, and PWMDriver::state.
void pwmDisableChannel | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Disables a PWM channel and its notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 196 of file hal_pwm.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmDisableChannelI, and PWMDriver::state.
void pwmEnablePeriodicNotification | ( | PWMDriver * | pwmp | ) |
Enables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 218 of file hal_pwm.c.
References PWMConfig::callback, PWMDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmEnablePeriodicNotificationI, and PWMDriver::state.
void pwmDisablePeriodicNotification | ( | PWMDriver * | pwmp | ) |
Disables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 241 of file hal_pwm.c.
References PWMConfig::callback, PWMDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmDisablePeriodicNotificationI, and PWMDriver::state.
void pwmEnableChannelNotification | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Enables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 266 of file hal_pwm.c.
References PWMChannelConfig::callback, PWMConfig::channels, PWMDriver::config, PWMDriver::enabled, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmEnableChannelNotificationI, and PWMDriver::state.
void pwmDisableChannelNotification | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Disables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 294 of file hal_pwm.c.
References PWMChannelConfig::callback, PWMConfig::channels, PWMDriver::config, PWMDriver::enabled, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), PWM_READY, pwmDisableChannelNotificationI, and PWMDriver::state.
void pwm_lld_init | ( | void | ) |
Low level PWM driver initialization.
Definition at line 66 of file hal_pwm_lld.c.
References pwmObjectInit().
Referenced by pwmInit().
void pwm_lld_start | ( | PWMDriver * | pwmp | ) |
Configures and activates the PWM peripheral.
PWM_READY
state disables all the active channels.[in] | pwmp | pointer to a PWMDriver object |
Definition at line 83 of file hal_pwm_lld.c.
References PWM_STOP, and PWMDriver::state.
Referenced by pwmStart().
void pwm_lld_stop | ( | PWMDriver * | pwmp | ) |
Deactivates the PWM peripheral.
[in] | pwmp | pointer to a PWMDriver object |
Definition at line 102 of file hal_pwm_lld.c.
References PWM_READY, and PWMDriver::state.
Referenced by pwmStop().
void pwm_lld_enable_channel | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel, | ||
pwmcnt_t | width | ||
) |
Enables a PWM channel.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
[in] | width | PWM pulse width as clock pulses number |
Definition at line 127 of file hal_pwm_lld.c.
void pwm_lld_disable_channel | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Disables a PWM channel and its notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 148 of file hal_pwm_lld.c.
void pwm_lld_enable_periodic_notification | ( | PWMDriver * | pwmp | ) |
Enables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 163 of file hal_pwm_lld.c.
void pwm_lld_disable_periodic_notification | ( | PWMDriver * | pwmp | ) |
Disables the periodic activation edge notification.
pwmStart()
. [in] | pwmp | pointer to a PWMDriver object |
Definition at line 177 of file hal_pwm_lld.c.
void pwm_lld_enable_channel_notification | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Enables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 193 of file hal_pwm_lld.c.
void pwm_lld_disable_channel_notification | ( | PWMDriver * | pwmp, |
pwmchannel_t | channel | ||
) |
Disables a channel de-activation edge notification.
pwmStart()
. pwmEnableChannel()
. [in] | pwmp | pointer to a PWMDriver object |
[in] | channel | PWM channel identifier (0...channels-1) |
Definition at line 211 of file hal_pwm_lld.c.
PWMDriver PWMD1 |
PWMD1 driver identifier.
Definition at line 42 of file hal_pwm_lld.c.