RT-Thread / RT-Thread/rt-thread

rt_timer_check函数全程关中断有可能影响中断函数响应时间,其函数的关中断时间是否能优化?

Open
#7,860 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

芯片:HC32F4A0
问题描述:定时器捕获时发现中断响应有时会延时20多us,单步调试发现是由于rt_timer_check函数长时间关中断造成的。
0通道:捕获信号;1通道:测试用的IO,在捕获中断(上升沿)函数中反转电平;2通道:rt_timer_check函数,在关中断和开中断时反转电平
image

测试二:
修改rt_timer_check函数(更改开关中断位置、更改反转电平位置)再次测试。
0通道:捕获信号;1通道:测试用的IO,在捕获中断(上升沿)函数中反转电平;2通道:rt_timer_check函数,在执行t->timeout_func(t->parameter);时反转电平

/**
 * @brief This function will check timer list, if a timeout event happens,
 *        the corresponding timeout function will be invoked.
 *
 * @note This function shall be invoked in operating system timer interrupt.
 */
void rt_timer_check(void)
{
    struct rt_timer *t;
    rt_tick_t current_tick;
    rt_base_t level;
    rt_list_t list;

    rt_list_init(&list);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));

    current_tick = rt_tick_get();

    while (!rt_list_isempty(&_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();

        t = rt_list_entry(_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                          struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
        {
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));

            /* remove timer from timer list firstly */
            _timer_remove(t);
            if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
            {
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
            /* add timer to temporary list  */
            rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));

            GPIO_TogglePins(GPIO_PORT_D, GPIO_PIN_14);

            /* call timeout function */
            t->timeout_func(t->parameter);

            GPIO_TogglePins(GPIO_PORT_D, GPIO_PIN_14);

            /* re-get tick */
            current_tick = rt_tick_get();

            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }
            rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
        else break;
    }

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
}

通过下图可以看出t->timeout_func(t->parameter);函数耗时1.37us~2.35us,且执行多个。更改后不再影响中断响应。修改后的代码只是为了验证题目所说的内容,在HC32F4A0上可以正常运行,但并没有严格测试是否会影响内核其他功能。
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with rt_timer_check in the timer implementation and reproduce the HC32F4A0 capture test, measuring the interval between the capture interrupt and the test GPIO toggle. Review the interrupt-disable region and the t->timeout_func(t->parameter) calls, then verify any change against timer behavior and the reported interrupt latency without affecting other kernel functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.