Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arch/tricore/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ config ARCH_CHIP_TC397
select ALARM_ARCH
select ONESHOT
select ONESHOT_COUNT
select SYSTEM_TIME64
---help---
AURIX TC39x family: TC397

Expand Down
4 changes: 4 additions & 0 deletions drivers/timers/arch_alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ static void ndelay_accurate(unsigned long nanoseconds)
static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
FAR void *arg)
{
#ifdef CONFIG_HRTIMER
nxsched_process_hrtimer();
#else
clock_t now;

ONESHOT_TICK_CURRENT(g_oneshot_lower, &now);
Expand All @@ -138,6 +141,7 @@ static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
}

ONESHOT_TICK_ABSOLUTE(g_oneshot_lower, now + 1);
# endif
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions include/nuttx/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,10 @@ void up_ndelay(unsigned long nanoseconds);
void nxsched_process_timer(void);
#endif

#ifdef CONFIG_HRTIMER
void nxsched_process_hrtimer(void);
#endif

/****************************************************************************
* Name: nxsched_timer_expiration
*
Expand Down
2 changes: 1 addition & 1 deletion sched/hrtimer/hrtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static inline_function
int hrtimer_starttimer(uint64_t ns)
Comment thread
anchao marked this conversation as resolved.
{
struct timespec ts;
int ret;
int ret = OK;

/* Convert nanoseconds to timespec */

Expand Down
4 changes: 4 additions & 0 deletions sched/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ if(CONFIG_SMP)
list(APPEND SRCS sched_smp.c)
endif()

if(CONFIG_HRTIMER)
list(APPEND SRCS sched_processhrtimer.c)
endif()

target_sources(sched PRIVATE ${SRCS})
4 changes: 4 additions & 0 deletions sched/sched/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ ifeq ($(CONFIG_SMP),y)
CSRCS += sched_smp.c
endif

ifeq ($(CONFIG_HRTIMER),y)
CSRCS += sched_processhrtimer.c
endif

# Include sched build support

DEPPATH += --dep-path sched
Expand Down
5 changes: 5 additions & 0 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <nuttx/queue.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spinlock.h>
#include "hrtimer/hrtimer.h"

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -307,6 +308,10 @@ extern volatile spinlock_t g_cpu_tasklistlock;
* Public Function Prototypes
****************************************************************************/

#if defined(CONFIG_HRTIMER) && defined(CONFIG_SCHED_TICKLESS)
int nxsched_hrtimer_start(clock_t ticks);
#endif

int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
FAR void *stack_addr, int stack_size, main_t entry,
FAR char * const argv[], FAR char * const envp[]);
Expand Down
149 changes: 149 additions & 0 deletions sched/sched/sched_processhrtimer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/****************************************************************************
* sched/sched/sched_processhrtimer.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <time.h>

#include "sched/sched.h"
#include "hrtimer/hrtimer.h"
#include "clock/clock.h"

/****************************************************************************
* Private Data
****************************************************************************/

/* Global high-resolution timer instance used by the scheduler */

static hrtimer_t g_nxsched_hrtimer;

/* Whether the scheduler high-resolution timer has been initialized */

static bool g_sched_hrtimer_inited = false;

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: nxsched_hrtimer_callback
*
* Description:
* High-resolution timer callback used for scheduler tick processing.
*
* This callback updates the scheduler state and arms the next timer
* event. The behavior differs depending on whether tickless mode is
* enabled:
*
* - In tickless mode, nxsched_tick_expiration() determines the next
* expiration point.
* - In non-tickless mode, a tick is generated unconditionally at
* every timer interval.
*
* Input Parameters:
* hrtimer - Pointer to the high-resolution timer instance that expired.
*
* Returned Value:
* None.
*
****************************************************************************/

static uint32_t nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer)
{
#ifdef CONFIG_SCHED_TICKLESS
uint64_t now = hrtimer_gettime();
nxsched_tick_expiration(NSEC2TICK(now));
#else

/* Process a single scheduler tick */

nxsched_process_timer();

/* Schedule the next tick relative to now */

return NSEC_PER_TICK;
#endif
}

/****************************************************************************
* Public Functions
****************************************************************************/

#ifdef CONFIG_SCHED_TICKLESS
int nxsched_hrtimer_start(clock_t ticks)
{
return hrtimer_start(&g_nxsched_hrtimer,
hrtimer->expired + ticks * NSEC_PER_TICK,
HRTIMER_MODE_ABS);
}
#endif

/****************************************************************************
* Name: nxsched_process_hrtimer
*
* Description:
* Process pending high-resolution timer events for the scheduler.
*
* This function performs lazy initialization of the scheduler's
* high-resolution timer and processes expired timer events. It is intended
* to be invoked periodically from architecture-specific high-resolution
* timer handling code.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/

void nxsched_process_hrtimer(void)
{
uint64_t now = hrtimer_gettime();

/* Perform one-time initialization */

if (g_sched_hrtimer_inited == false)
{
g_sched_hrtimer_inited = true;

/* Initialize the scheduler hrtimer */

hrtimer_init(&g_nxsched_hrtimer,
nxsched_hrtimer_callback,
NULL);

/* Start the first timer event */

hrtimer_start(&g_nxsched_hrtimer,
now + NSEC_PER_TICK,
HRTIMER_MODE_ABS);
}

/* Process any expired high-resolution timer events */

hrtimer_process(now);
}
11 changes: 6 additions & 5 deletions sched/sched/sched_timerexpiration.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,21 @@ static clock_t nxsched_timer_start(clock_t ticks, clock_t interval)
interval = interval <= (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK) ? 0 :
interval - (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK);

#ifdef CONFIG_SCHED_TICKLESS_ALARM
#ifdef CONFIG_HRTIMER
ret = nxsched_hrtimer_start(interval);
#else
# ifdef CONFIG_SCHED_TICKLESS_ALARM
/* Convert the delay to a time in the future (with respect
* to the time when last stopped the timer).
*/

ret = up_alarm_tick_start(ticks + interval);
#else
# else
/* [Re-]start the interval timer */

ret = up_timer_tick_start(interval);
# endif
#endif

if (ret < 0)
{
serr("ERROR: up_timer_start/up_alarm_start failed: %d\n", ret);
Expand Down Expand Up @@ -490,7 +493,6 @@ void nxsched_tick_expiration(clock_t ticks)
/* Process the timer ticks and set up the next interval (or not) */

nexttime = nxsched_timer_process(ticks, elapsed, false);

elapsed = nxsched_timer_start(ticks, nexttime);
atomic_set(&g_timer_interval, elapsed);
}
Expand Down Expand Up @@ -596,7 +598,6 @@ void nxsched_reassess_timer(void)
/* Process the timer ticks and start next timer */

nexttime = nxsched_timer_process(ticks, elapsed, true);

elapsed = nxsched_timer_start(ticks, nexttime);
atomic_set(&g_timer_interval, elapsed);
}
Expand Down
Loading