This is an automated email from the ASF dual-hosted git repository. Donny9 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 56e9f7c35bf3b5dc44c6f081cc1e5e39bbace93f Author: yukangzhi <[email protected]> AuthorDate: Wed Apr 8 11:23:23 2026 +0800 drivers/timers/pwm: extend pwm_ops_s with optional lower-half callbacks Add 6 optional callbacks to pwm_ops_s for enhanced PWM control: - set_output_idle: force channel output to idle state (using dcpol) - get_output_state: read channel output level (HIGH/LOW) - enable_notify/disable_notify: edge notification control - get_power_state/set_power_state: power state management Add corresponding ioctl commands (PWMIOC 6~11, after the existing PWMIOC_FAULTS_FETCH_AND_CLEAR) and dispatch logic in the upper-half driver. All new ops are optional (NULL = not supported, returns -ENOSYS). New data structures: pwm_output_state_s, pwm_notify_s. New macros: PWM_EDGE_*, PWM_FULL_POWER. Signed-off-by: yukangzhi <[email protected]> --- drivers/timers/pwm.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++ include/nuttx/timers/pwm.h | 65 ++++++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 1 deletion(-) diff --git a/drivers/timers/pwm.c b/drivers/timers/pwm.c index f706abc7e5b..7be59b2335f 100644 --- a/drivers/timers/pwm.c +++ b/drivers/timers/pwm.c @@ -432,6 +432,101 @@ static int pwm_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } break; + case PWMIOC_SETOUTPUTIDLE: + { + if (lower->ops->set_output_idle == NULL) + { + ret = -ENOSYS; + break; + } + + ret = lower->ops->set_output_idle(lower, (int)arg); + } + break; + + case PWMIOC_GETOUTPUTSTATE: + { + FAR struct pwm_output_state_s *p = + (FAR struct pwm_output_state_s *)((uintptr_t)arg); + if (lower->ops->get_output_state == NULL) + { + ret = -ENOSYS; + } + else if (p == NULL) + { + ret = -EINVAL; + } + else + { + ret = lower->ops->get_output_state(lower, + p->channel, &p->state); + } + } + break; + + case PWMIOC_ENABLENOTIFY: + { + FAR const struct pwm_notify_s *p = + (FAR const struct pwm_notify_s *)((uintptr_t)arg); + if (lower->ops->enable_notify == NULL) + { + ret = -ENOSYS; + } + else if (p == NULL) + { + ret = -EINVAL; + } + else + { + ret = lower->ops->enable_notify(lower, + p->channel, p->edge); + } + } + break; + + case PWMIOC_DISABLENOTIFY: + { + if (lower->ops->disable_notify == NULL) + { + ret = -ENOSYS; + break; + } + + ret = lower->ops->disable_notify(lower, (int)arg); + } + break; + + case PWMIOC_GETPOWERSTATE: + { + FAR uint8_t *state = + (FAR uint8_t *)((uintptr_t)arg); + if (lower->ops->get_power_state == NULL) + { + ret = -ENOSYS; + } + else if (state == NULL) + { + ret = -EINVAL; + } + else + { + ret = lower->ops->get_power_state(lower, state); + } + } + break; + + case PWMIOC_SETPOWERSTATE: + { + if (lower->ops->set_power_state == NULL) + { + ret = -ENOSYS; + break; + } + + ret = lower->ops->set_power_state(lower, (uint8_t)arg); + } + break; + /* Any unrecognized IOCTL commands might be platform-specific ioctl * commands. */ diff --git a/include/nuttx/timers/pwm.h b/include/nuttx/timers/pwm.h index 3d7209ea099..bd1a50ff88e 100644 --- a/include/nuttx/timers/pwm.h +++ b/include/nuttx/timers/pwm.h @@ -120,6 +120,13 @@ #define PWMIOC_STOP _PWMIOC(4) #define PWMIOC_FAULTS_FETCH_AND_CLEAR _PWMIOC(5) +#define PWMIOC_SETOUTPUTIDLE _PWMIOC(6) +#define PWMIOC_GETOUTPUTSTATE _PWMIOC(7) +#define PWMIOC_ENABLENOTIFY _PWMIOC(8) +#define PWMIOC_DISABLENOTIFY _PWMIOC(9) +#define PWMIOC_GETPOWERSTATE _PWMIOC(10) +#define PWMIOC_SETPOWERSTATE _PWMIOC(11) + /* PWM channel polarity *****************************************************/ /* These are helper definitions for setting PWM channel output polarity to @@ -144,10 +151,36 @@ #define PWM_DCPOL_LOW 1 /* Logical zero */ #define PWM_DCPOL_HIGH 2 /* Logical one */ +/* Edge notification types */ + +#define PWM_EDGE_RISING 0x00 +#define PWM_EDGE_FALLING 0x01 +#define PWM_EDGE_BOTH 0x02 + +/* Power state definitions */ + +#define PWM_FULL_POWER 0x00 + /**************************************************************************** * Public Types ****************************************************************************/ +/* Output state query parameter */ + +struct pwm_output_state_s +{ + int8_t channel; + bool state; /* Output: true=HIGH, false=LOW */ +}; + +/* Edge notification parameter */ + +struct pwm_notify_s +{ + int8_t channel; + uint8_t edge; /* PWM_EDGE_RISING/FALLING/BOTH */ +}; + /* If the PWM peripheral supports multiple output channels, then this * structure describes the output state on one channel. */ @@ -165,7 +198,7 @@ struct pwm_chan_s #endif uint8_t cpol; uint8_t dcpol; - int8_t channel; + int8_t channel; /* Channel number: 1~N=active, 0=skip, -1=end */ }; /* This structure describes the characteristics of the pulsed output */ @@ -220,6 +253,36 @@ struct pwm_ops_s CODE int (*ioctl)(FAR struct pwm_lowerhalf_s *dev, int cmd, unsigned long arg); + + /* Set channel output to configured idle state immediately. */ + + CODE int (*set_output_idle)(FAR struct pwm_lowerhalf_s *dev, + int channel); + + /* Read channel output state (HIGH/LOW). */ + + CODE int (*get_output_state)(FAR struct pwm_lowerhalf_s *dev, + int channel, FAR bool *state); + + /* Enable edge notification, must cancel pending interrupts. */ + + CODE int (*enable_notify)(FAR struct pwm_lowerhalf_s *dev, + int channel, uint8_t edge); + + /* Disable edge notification. */ + + CODE int (*disable_notify)(FAR struct pwm_lowerhalf_s *dev, + int channel); + + /* Get current power state. */ + + CODE int (*get_power_state)(FAR struct pwm_lowerhalf_s *dev, + FAR uint8_t *state); + + /* Execute power state transition. */ + + CODE int (*set_power_state)(FAR struct pwm_lowerhalf_s *dev, + uint8_t state); }; /* This structure is the generic form of state structure used by lower half
