rename nxsched_timer_expiration to nxsched_tick_expiration
to align with nxsched_process_tick()
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Add basic timer operations to the kernel, including registration,
start/stop, and callback handling. This provides a consistent timer
interface for use by kernel components and drivers.
Signed-off-by: zhaohaiyang1 <zhaohaiyang1@xiaomi.com>
The reference count (upper->crefs) is incremented in timer_open()
only after ensuring it will not overflow, timer_close() is only
called after a successful open. Therefore, crefs should never be
zero when timer_close() is called. The check for crefs > 0 before
decrementing is unnecessary and has been removed to simplify the code.
Signed-off-by: fangpeina <fangpeina@xiaomi.com>
This commit introduces a software-based fake capture driver that simulates
a 10Hz square wave with 50% duty cycle, enabling development and testing
of capture-related applications without requiring real hardware.
Background:
The capture driver subsystem requires hardware support (timers with input
capture functionality) to measure external signal frequency and duty cycle.
During development, testing, or on platforms without capture hardware, it's
difficult to develop and test applications that use the capture API.
Problem:
Without a fake/simulator driver:
- Developers cannot test capture applications without specific hardware
- Continuous Integration (CI) systems cannot run capture-related tests
- Applications cannot be developed on platforms lacking capture hardware
- Testing edge notification features requires complex hardware setup
- Difficult to reproduce specific timing scenarios for debugging
Solution:
This commit provides a software-simulated capture driver that generates
predictable capture events using a watchdog timer. The fake driver
produces a square wave signal at 10Hz frequency with 50% duty cycle,
allowing applications to test the full capture API without hardware.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit adds comprehensive signal notification support to the capture
driver, enabling applications to be asynchronously notified when capture
edge events occur on any channel, rather than having to poll for events.
Background:
The capture driver is used to measure frequency and duty cycle of external
signals by detecting edges (rising/falling). Previously, applications could
only retrieve captured values through periodic polling via CAPIOC_GETDUTY
and CAPIOC_GETFREQ ioctl commands, which is inefficient for event-driven
applications.
Problem:
Without event notification, applications must:
- Continuously poll the capture device to check for new events
- Waste CPU cycles in polling loops
- Experience higher latency in responding to capture events
- Cannot efficiently handle multiple capture channels simultaneously
Solution:
This commit adds a signal-based notification mechanism that allows
applications to register callbacks for specific capture channels and
edge types (rising, falling, or both). When the hardware detects the
configured edge, a signal is sent to the registered task.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
using Desired frequency offset from nominal frequency in
parts per billion(ppb) to adjust frequency
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This patch adds a dummy PTP clock driver implementation that provides
a software-based PTP clock for testing and development purposes.
Key changes include:
1. Dummy PTP clock driver implementation:
- Added drivers/timers/ptp_clock_dummy.c
- Provides software-based PTP clock using system monotonic clock
- Implements all required lower-half driver operations
2. Lower-half driver operations:
- adjfine: Frequency adjustment (stores adjustment value)
- adjtime: Time offset adjustment (applies delta to base time)
- gettime: Returns current PTP clock time
- settime: Sets PTP clock time
- getcaps: Reports clock capabilities (1 billion PPB max adjustment)
- getcrosststamp: Provides system/device cross-timestamp
3. Driver initialization:
- Added ptp_clock_dummy_init() in drivers/drivers_initialize.c
- Auto-registration under CONFIG_PTP_CLOCK_DUMMY configuration
- Creates /dev/ptp0 character device node
4. Build system integration:
- Added CONFIG_PTP_CLOCK_DUMMY Kconfig option (depends on PTP_CLOCK)
- Updated CMakeLists.txt and Make.defs
- Added include/nuttx/timers/ptp_clock_dummy.h header
5. Implementation details:
- Uses clock_gettime(CLOCK_MONOTONIC) as time base
- Tracks time offset and frequency adjustment internally
- Suitable for testing PTP clock applications without hardware
This dummy driver enables development and testing of PTP clock applications
on platforms without dedicated PTP hardware support.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This patch introduces the foundational PTP (Precision Time Protocol) clock
driver framework for NuttX, enabling precise time synchronization support
based on IEEE 1588 standard.
Key changes include:
1. New PTP clock driver infrastructure:
- Added drivers/timers/ptp_clock.c implementing upper-half driver
- Created include/nuttx/timers/ptp_clock.h with PTP clock interfaces
- Implemented upper/lower half driver architecture for hardware abstraction
2. Core functionality:
- Clock time get/set operations (gettime, settime)
- Frequency adjustment support (adjtime, adjfine)
- Phase adjustment capabilities
- System-device cross-timestamping for precise synchronization
3. IOCTL commands:
- PTP_CLOCK_SETTIME/GETTIME for time manipulation
- PTP_CLOCK_GETRES for resolution queries
- PTP_CLOCK_ADJTIME for time adjustment
- PTP_CLOCK_GETCAPS for capability queries
- PTP_SYS_OFFSET* for system offset measurements
4. Supporting structures:
- struct ptp_lowerhalf_s: lower-half driver interface
- struct ptp_clock_caps: clock capabilities descriptor
- struct ptp_sys_offset: system time offset measurement
- Added timex structures in include/sys/timex.h for ADJ_* operations
5. Build system integration:
- Added CONFIG_PTP_CLOCK Kconfig option
- Updated CMakeLists.txt and Make.defs
- Added PTPCLK debug macros in include/debug.h
This framework provides the base for PTP clock implementations, allowing
hardware-specific drivers to register and provide precise time services
through a standardized interface.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This reverts the removal of ndelay_accurate from #14450, since as
mentioned in #17011, this fails to consider the `sim` architecture
where CONFIG_BOARD_LOOPSPERMSEC was set to 0 because of reliance on the
accurate implementations of the up_delay functions. All the commit did
was remove a more accurate implementation in favour of a less accurate
one.
Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
This commit fixed oneshot callback for non-tickless using tick-aligned
absolute oneshot interfaces.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit introduce new clockdevice interfaces for oneshot.
- It can represent both `timer` and `alarm`.
- It simplifies the implementation of timer driver. Timer driver do not need considering the time conversion between clock count, tick and timespec.
- It sets timer align to the tick boundary to improve timer accuracy.
- It can avoid using 64-bit division during timer expiration interrupt, improves performance and reduces interrupt latency.
- It considers almost all multiplication overflow problems in time conversion without processing at the driver.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Introduce a new function nxsched_tick_expiration() to replace
nxsched_alarm_tick_expiration(). The new function provides a
common implementation that can be shared by both the alarm
and timer architectures.
This change reduces code duplication and provides a unified
function that can be directly reused.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Nuttx currently has 2 types of sleep interfaces:
1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
Weaknesses:
a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.
2. Busy sleep: up_mdelay() / up_udelay()
Weaknesses:
a. Does not actively trigger scheduling, occupy the CPU loading.
3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
Strengths:
a. Does not depend on the signal framework.
b. Tick-based, without additional computational overhead.
Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.
(This PR also aims to make signals optional, further reducing the code size of Nuttx.)
Signed-off-by: chao an <anchao.archer@bytedance.com>
Many different architectures re-implemented the exact same code for
`up_*delay` because it was originally written as architecture dependent
code. Busy-waiting can be done regardless of architecture, so this
commit moves that duplicated implementation to a common file with weak
definitions so that individual architectures (see tc32) are still able
to override the definition if needed/desired.
Default implementation is not included if ARCH_TIMER is enabled, since
it is more accurate and provides its own weak definitions to override.
Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
This patch fixes an incorrect call to stm32_cap_initialize() in
stm32_bringup.c: the call was made without the channel parameter.
Instead of adding the channel in the call, the channel is selected by
stm32_cap_gpio() (first available channel).
This patch also fixes incorrect driver registration in
drivers/timers/capture.c: the driver was registered with the wrong
name (/dev/cap -> /dev/capture). Also added more error checking in
cap_register_multiple().
Signed-off-by: Côme VINCENT <44554692+comejv@users.noreply.github.com>
This commit introduces a timer capture driver for the STM32H7 series
ported from the STM32 F series.
The main changes include:
- A new generic timer capture driver for STM32H7.
- A lower-half driver to integrate with the NuttX capture subsystem.
- Kconfig options to enable and configure capture for various timers.
- Pin definitions for TIM1-4 capture inputs on the nucleo-h743zi.
- An update to `cap_register_multiple` to handle multiple device registration.
- An update to `stm32_bringup` to register the capture drivers.
The current implementation is based on a driver originally for PWM input,
and as such, it calculates duty cycle and frequency. It is also limited
to a single capture channel per timer.
The original implementation's `stm32_cap_init` in
`arch/arm/src/stm32h7/stm32_capture.c` has been modified to accept a
channel number instead of using a hardcoded 0 through
`STM32_CAP_CHANNEL_COUNTER`.
This serves as a foundation for future development of more comprehensive
input capture capabilities on STM32H7 platforms.
Tested by polling and reading `/dev/cap0-4` with
`ioctl(fds[i], CAPIOC_FREQUENCE, freq)` while sending a square wave signal to
appropriate pins and checking frequency.
Also tested by bypassing upper half driver and setting up capture on
TIM4 channels 1-4 as explained in #16762.
Signed-off-by: Côme VINCENT <44554692+comejv@users.noreply.github.com>
Use the flag CONFIG_ARCH_HAVE_PERF_EVENTS to detect whether the architecture specific code
provides the up_perf_* functions. Now it is mixed with CONFIG_ARCH_PERF_EVENTS, which should
select just whether the perf events (perf_*) are enabled for the configuration.
- drivers/timers/arch_alarm.c: Don't compile the up_perf_* functions here if the
CONFIG_ARCH_HAVE_PERF_EVENTS is defined
- arch/*/*_perf.c: Change CONFIG_ARCH_PERF_EVENTS -> CONFIG_ARCH_HAVE_PERF_EVENTS to
select whether architecture specific up_perf_* functions are provided
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
Add support for watchdog timer notifer chain so that users
can customize the callback function when the watchdog timer
times out which enabled by Auto-monitor
Signed-off-by: yaojiaqi <yaojiaqi@lixiang.com>
In some areas with high security requirements such as vehicle control, in order to meet functional safety requirements, the timeout and feeding interval of the watchdog need to be configured in milliseconds
Signed-off-by: yaojiaqi <yaojiaqi@lixiang.com>
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.
Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
This patch fixed userspace headers conflict. Architecture-related definition and API should not be exposed to users.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
currently, nuttx implements readv/writev on the top of read/write.
while it might work for the simplest cases, it's broken by design.
for example, it's impossible to make it work correctly for files
which need to preserve data boundaries without allocating a single
contiguous buffer. (udp socket, some character devices, etc)
this change is a start of the migration to a better design.
that is, implement read/write on the top of readv/writev.
to avoid a single huge change, following things will NOT be done in
this commit:
* fix actual bugs caused by the original readv-based-on-read design.
(cf. https://github.com/apache/nuttx/pull/12674)
* adapt filesystems/drivers to actually benefit from the new interface.
(except a few trivial examples)
* eventually retire the old interface.
* retire read/write syscalls. implement them in libc instead.
* pread/pwrite/preadv/pwritev (except the introduction of struct uio,
which is a preparation to back these variations with the new
interface.)