This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 97d4eda4c81d4ee890f47d82ebef66ad4d42bd6d Author: fangpeina <[email protected]> AuthorDate: Wed Jul 15 17:02:36 2026 +0800 drivers/timers/capture: add ioctl pass-through and ops extension Add generic ioctl pass-through to lower-half via ops->ioctl for vendor-specific functionality (edge mode control, signal measurement, edge counting lifecycle). Add ops->get_input_state for reading input pin level. Define new ioctl commands for edge control, signal measurement, and state query. The upper half already had ops->ioctl. It now passes arg to the lower half unchanged (cap_register() always uses one channel, so this is the same value as before) and returns -ENOTTY when the lower half has no ioctl method. The new commands use _CAPIOC(12) to _CAPIOC(15), after the existing ones. Signed-off-by: fangpeina <[email protected]> --- drivers/timers/capture.c | 89 ++++++++++++++++++++++++++++++++++++------ include/nuttx/timers/capture.h | 65 +++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 14 deletions(-) diff --git a/drivers/timers/capture.c b/drivers/timers/capture.c index bb3f3379d76..4e98a5efcc7 100644 --- a/drivers/timers/capture.c +++ b/drivers/timers/capture.c @@ -525,25 +525,90 @@ static int cap_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; #endif - /* Any unrecognized IOCTL commands might be platform-specific ioctl - * commands - */ + /* Start capture on all channels */ - default: + case CAPIOC_START_CAPTURE: + { + for (i = 0; i < upper->nchan; i++) + { + if (!lower[i]->ops->start_capture) + { + ret = -ENOSYS; + break; + } + + ret = lower[i]->ops->start_capture(lower[i]); + if (ret < 0) + { + break; + } + } + } + break; + + case CAPIOC_STOP_CAPTURE: + { + for (i = 0; i < upper->nchan; i++) + { + if (lower[i]->ops->stop_capture) + { + ret = lower[i]->ops->stop_capture(lower[i]); + } + else + { + ret = -ENOSYS; + } + + if (ret < 0) + { + break; + } + } + } + break; + + /* State query */ + + case CAPIOC_GET_INPUT_STATE: { - cperr("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg); + FAR uint8_t *ptr = (FAR uint8_t *)(uintptr_t)arg; + DEBUGASSERT(ptr); + for (i = 0; i < upper->nchan; i++) + { + if (lower[i]->ops->get_input_state) + { + ret = lower[i]->ops->get_input_state(lower[i], &ptr[i]); + } + else + { + ret = -ENOSYS; + } - /* Unrecognized cmd will be forwarded to lower half driver for - * specific use cases (e.g Pulse Counter (PCNT)) - */ + if (ret < 0) + { + break; + } + } + } + break; - FAR unsigned long int *ptr = - (FAR unsigned long int *)arg; + /* Any unrecognized IOCTL commands are passed through to the + * lower-half driver via ops->ioctl. + */ + default: + { for (i = 0; i < upper->nchan; i++) { - ret = lower[i]->ops->ioctl(lower[i], cmd, - (unsigned long)&ptr[i]); + if (lower[i]->ops->ioctl) + { + ret = lower[i]->ops->ioctl(lower[i], cmd, arg); + } + else + { + ret = -ENOTTY; + } + if (ret < 0) { break; diff --git a/include/nuttx/timers/capture.h b/include/nuttx/timers/capture.h index 2c0b1b171d8..f5d09739c5f 100644 --- a/include/nuttx/timers/capture.h +++ b/include/nuttx/timers/capture.h @@ -31,8 +31,6 @@ #include <nuttx/fs/ioctl.h> #include <nuttx/signal.h> -#include <nuttx/signal.h> - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -134,10 +132,60 @@ #define CAPIOC_UNREGISTER _CAPIOC(11) +/* Command: CAPIOC_SET_EDGE + * Description: Set the capture edge polarity (handled by the lower half). + * Arguments: enum cap_edge_e value. + * Return: OK on success; ERROR on failure. + */ + +#define CAPIOC_SET_EDGE _CAPIOC(12) + +/* Command: CAPIOC_START_CAPTURE + * Description: Start capture on all channels. + * Arguments: None. + * Return: OK on success; ERROR on failure. + */ + +#define CAPIOC_START_CAPTURE _CAPIOC(13) + +/* Command: CAPIOC_STOP_CAPTURE + * Description: Stop capture on all channels. + * Arguments: None. + * Return: OK on success; ERROR on failure. + */ + +#define CAPIOC_STOP_CAPTURE _CAPIOC(14) + +/* Command: CAPIOC_GET_INPUT_STATE + * Description: Read the input pin state of each channel. + * Arguments: uint8_t array (one entry per channel) to return + * enum cap_input_state_e values. + * Return: OK on success; ERROR on failure. + */ + +#define CAPIOC_GET_INPUT_STATE _CAPIOC(15) + /**************************************************************************** * Public Types ****************************************************************************/ +/* Activation edge type */ + +enum cap_edge_e +{ + CAP_EDGE_RISING = 0, + CAP_EDGE_FALLING = 1, + CAP_EDGE_BOTH = 2, +}; + +/* Input state */ + +enum cap_input_state_e +{ + CAP_STATE_IDLE = 0, + CAP_STATE_ACTIVE = 1, +}; + struct cap_all_s { uint32_t freq; @@ -220,6 +268,19 @@ struct cap_ops_s CODE int (*unbind)(FAR struct cap_lowerhalf_s *lower); #endif + + /* Start capture on a channel */ + + CODE int (*start_capture)(FAR struct cap_lowerhalf_s *lower); + + /* Stop capture on a channel */ + + CODE int (*stop_capture)(FAR struct cap_lowerhalf_s *lower); + + /* Read current input pin state (ACTIVE/IDLE) */ + + CODE int (*get_input_state)(FAR struct cap_lowerhalf_s *lower, + FAR uint8_t *state); }; /* This structure provides the publicly visible representation of the
