Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Other > Operation System && Industrial > IoT/Edge Computing &&
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1.1K Star 5.5K Fork 2.3K

GVP RT-Thread/rt-thread

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (38)
Tags (43)
master
copilot/fix-format-string-injection
v5.3.0-beta
copilot/fix-gpt-partition-underflow-issue
copilot/fix-dlopen-name-mismatch
v5.2.2-beta
revert-11186-master
copilot/update-kconfig-help-descriptions
copilot/create-device-driver-model-docs
copilot/refactor-hwtimer-ktime-cputime-again
copilot/refactor-hwtimer-ktime-cputime
copilot/add-undef-kconfig-allowlist
copilot/update-virtio-to-modern-version
copilot/clean-up-finsh-function-code
v5.2.1-beta
v5.2.0-beta
copilot/organize-group-structure
copilot/fix-fpu-initialization-issue
revert-10878-fix_ci
lts-v5.2.x
v5.2.2
v5.2.1
v5.2.0
v5.1.0
v5.0.2
v5.0.1
v5.0.0
v4.1.1
v4.1.1-beta
v4.1.0
v4.1.0-beta
v4.0.5
v4.0.4
v3.1.5
v4.0.3
v3.1.4
v4.0.2
v3.1.3
v4.0.1
v3.1.2
master
Branches (38)
Tags (43)
master
copilot/fix-format-string-injection
v5.3.0-beta
copilot/fix-gpt-partition-underflow-issue
copilot/fix-dlopen-name-mismatch
v5.2.2-beta
revert-11186-master
copilot/update-kconfig-help-descriptions
copilot/create-device-driver-model-docs
copilot/refactor-hwtimer-ktime-cputime-again
copilot/refactor-hwtimer-ktime-cputime
copilot/add-undef-kconfig-allowlist
copilot/update-virtio-to-modern-version
copilot/clean-up-finsh-function-code
v5.2.1-beta
v5.2.0-beta
copilot/organize-group-structure
copilot/fix-fpu-initialization-issue
revert-10878-fix_ci
lts-v5.2.x
v5.2.2
v5.2.1
v5.2.0
v5.1.0
v5.0.2
v5.0.1
v5.0.0
v4.1.1
v4.1.1-beta
v4.1.0
v4.1.0-beta
v4.0.5
v4.0.4
v3.1.5
v4.0.3
v3.1.4
v4.0.2
v3.1.3
v4.0.1
v3.1.2
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (38)
Tags (43)
master
copilot/fix-format-string-injection
v5.3.0-beta
copilot/fix-gpt-partition-underflow-issue
copilot/fix-dlopen-name-mismatch
v5.2.2-beta
revert-11186-master
copilot/update-kconfig-help-descriptions
copilot/create-device-driver-model-docs
copilot/refactor-hwtimer-ktime-cputime-again
copilot/refactor-hwtimer-ktime-cputime
copilot/add-undef-kconfig-allowlist
copilot/update-virtio-to-modern-version
copilot/clean-up-finsh-function-code
v5.2.1-beta
v5.2.0-beta
copilot/organize-group-structure
copilot/fix-fpu-initialization-issue
revert-10878-fix_ci
lts-v5.2.x
v5.2.2
v5.2.1
v5.2.0
v5.1.0
v5.0.2
v5.0.1
v5.0.0
v4.1.1
v4.1.1-beta
v4.1.0
v4.1.0-beta
v4.0.5
v4.0.4
v3.1.5
v4.0.3
v3.1.4
v4.0.2
v3.1.3
v4.0.1
v3.1.2
rt-thread
/
src
/
timer.c
rt-thread
/
src
/
timer.c
timer.c 23.29 KB
Copy Edit Raw Blame History
unicornx authored 2025年06月24日 16:11 +08:00 . doxygen: group name all in lowcase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2006年03月12日 Bernard first version
* 2006年04月29日 Bernard implement thread timer
* 2006年06月04日 Bernard implement rt_timer_control
* 2006年08月10日 Bernard fix the periodic timer bug
* 2006年09月03日 Bernard implement rt_timer_detach
* 2009年11月11日 LiJin add soft timer
* 2010年05月12日 Bernard fix the timer check bug.
* 2010年11月02日 Charlie re-implement tick overflow issue
* 2012年12月15日 Bernard fix the next timeout issue in soft timer
* 2014年07月12日 Bernard does not lock scheduler when invoking soft-timer
* timeout function.
* 2021年08月15日 supperthomas add the comment
* 2022年01月07日 Gabriel Moving __on_rt_xxxxx_hook to timer.c
* 2022年04月19日 Stanley Correct descriptions
* 2023年09月15日 xqyjlj perf rt_hw_interrupt_disable/enable
* 2024年01月25日 Shell add RT_TIMER_FLAG_THREAD_TIMER for timer to sync with sched
* 2024年05月01日 wdfk-prog The rt_timer_check and _soft_timer_check functions are merged
*/
#include <rtthread.h>
#include <rthw.h>
#define DBG_TAG "kernel.timer"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifndef RT_USING_TIMER_ALL_SOFT
/* hard timer list */
static rt_list_t _timer_list[RT_TIMER_SKIP_LIST_LEVEL];
static struct rt_spinlock _htimer_lock;
#endif
#ifdef RT_USING_TIMER_SOFT
#ifndef RT_TIMER_THREAD_STACK_SIZE
#define RT_TIMER_THREAD_STACK_SIZE 512
#endif /* RT_TIMER_THREAD_STACK_SIZE */
#ifndef RT_TIMER_THREAD_PRIO
#define RT_TIMER_THREAD_PRIO 0
#endif /* RT_TIMER_THREAD_PRIO */
/* soft timer list */
static rt_list_t _soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
static struct rt_spinlock _stimer_lock;
static struct rt_thread _timer_thread;
static struct rt_semaphore _soft_timer_sem;
rt_align(RT_ALIGN_SIZE)
static rt_uint8_t _timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
#endif /* RT_USING_TIMER_SOFT */
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
static void (*rt_timer_enter_hook)(struct rt_timer *timer);
static void (*rt_timer_exit_hook)(struct rt_timer *timer);
/**
* @addtogroup group_hook
*/
/**@{*/
/**
* @brief This function will set a hook function on timer,
* which will be invoked when enter timer timeout callback function.
*
* @param hook is the function point of timer
*/
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
{
rt_timer_enter_hook = hook;
}
/**
* @brief This function will set a hook function, which will be
* invoked when exit timer timeout callback function.
*
* @param hook is the function point of timer
*/
void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
{
rt_timer_exit_hook = hook;
}
/**@}*/
#endif /* RT_USING_HOOK */
rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
{
#ifdef RT_USING_TIMER_ALL_SOFT
return &_stimer_lock;
#else
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
return &_stimer_lock;
}
else
#endif /* RT_USING_TIMER_SOFT */
{
return &_htimer_lock;
}
#endif
}
/**
* @brief [internal] The init funtion of timer
*
* The internal called function of rt_timer_init
*
* @see rt_timer_init
*
* @param timer is timer object
*
* @param timeout is the timeout function
*
* @param parameter is the parameter of timeout function
*
* @param time is the tick of timer
*
* @param flag the flag of timer
*/
static void _timer_init(rt_timer_t timer,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
int i;
#ifdef RT_USING_TIMER_ALL_SOFT
flag |= RT_TIMER_FLAG_SOFT_TIMER;
#endif
/* set flag */
timer->parent.flag = flag;
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_tick = 0;
timer->init_tick = time;
/* initialize timer list */
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
{
rt_list_init(&(timer->row[i]));
}
}
/**
* @brief Find the next emtpy timer ticks
*
* @param timer_list is the array of time list
*
* @param timeout_tick is the next timer's ticks
*
* @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
* If the return value is any other values, it means this operation failed.
*/
static rt_err_t _timer_list_next_timeout(rt_list_t timer_list[], rt_tick_t *timeout_tick)
{
struct rt_timer *timer;
if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
{
timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
*timeout_tick = timer->timeout_tick;
return RT_EOK;
}
return -RT_ERROR;
}
/**
* @brief Remove the timer
*
* @param timer the point of the timer
*/
rt_inline void _timer_remove(rt_timer_t timer)
{
int i;
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
{
rt_list_remove(&timer->row[i]);
}
}
#if (DBG_LVL == DBG_LOG)
/**
* @brief The number of timer
*
* @param timer the head of timer
*
* @return count of timer
*/
static int _timer_count_height(struct rt_timer *timer)
{
int i, cnt = 0;
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
{
if (!rt_list_isempty(&timer->row[i]))
cnt++;
}
return cnt;
}
/**
* @brief dump the all timer information
*
* @param timer_heads the head of timer
*/
void rt_timer_dump(rt_list_t timer_heads[])
{
rt_list_t *list;
for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
list = list->next)
{
struct rt_timer *timer = rt_list_entry(list,
struct rt_timer,
row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
rt_kprintf("%d", _timer_count_height(timer));
}
rt_kprintf("\n");
}
#endif /* (DBG_LVL == DBG_LOG) */
/**
* @addtogroup group_clock_management
*/
/**@{*/
/**
* @brief This function will initialize a timer
* normally this function is used to initialize a static timer object.
*
* @param timer is the point of timer
*
* @param name is a pointer to the name of the timer
*
* @param timeout is the callback of timer
*
* @param parameter is the param of the callback
*
* @param time is timeout ticks of timer
*
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
*
* @param flag is the flag of timer
*
*/
void rt_timer_init(rt_timer_t timer,
const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(timeout != RT_NULL);
RT_ASSERT(time < RT_TICK_MAX / 2);
/* timer object initialization */
rt_object_init(&(timer->parent), RT_Object_Class_Timer, name);
_timer_init(timer, timeout, parameter, time, flag);
}
RTM_EXPORT(rt_timer_init);
/**
* @brief This function will detach a timer from timer management.
*
* @param timer is the timer to be detached
*
* @return the status of detach
*/
rt_err_t rt_timer_detach(rt_timer_t timer)
{
rt_base_t level;
struct rt_spinlock *spinlock;
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
RT_ASSERT(rt_object_is_systemobject(&timer->parent));
spinlock = _timerlock_idx(timer);
level = rt_spin_lock_irqsave(spinlock);
_timer_remove(timer);
/* stop timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_spin_unlock_irqrestore(spinlock, level);
rt_object_detach(&(timer->parent));
return RT_EOK;
}
RTM_EXPORT(rt_timer_detach);
#ifdef RT_USING_HEAP
/**
* @brief This function will create a timer
*
* @param name is the name of timer
*
* @param timeout is the timeout function
*
* @param parameter is the parameter of timeout function
*
* @param time is timeout ticks of the timer
*
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
*
* @param flag is the flag of timer. Timer will invoke the timeout function according to the selected values of flag, if one or more of the following flags is set.
*
* RT_TIMER_FLAG_ONE_SHOT One shot timing
* RT_TIMER_FLAG_PERIODIC Periodic timing
*
* RT_TIMER_FLAG_HARD_TIMER Hardware timer
* RT_TIMER_FLAG_SOFT_TIMER Software timer
* RT_TIMER_FLAG_THREAD_TIMER Thread timer
*
* NOTE:
* You can use multiple values with "|" logical operator. By default, system will use the RT_TIME_FLAG_HARD_TIMER.
*
* @return the created timer object
*/
rt_timer_t rt_timer_create(const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
struct rt_timer *timer;
/* parameter check */
RT_ASSERT(timeout != RT_NULL);
RT_ASSERT(time < RT_TICK_MAX / 2);
/* allocate a object */
timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
if (timer == RT_NULL)
{
return RT_NULL;
}
_timer_init(timer, timeout, parameter, time, flag);
return timer;
}
RTM_EXPORT(rt_timer_create);
/**
* @brief This function will delete a timer and release timer memory
*
* @param timer the timer to be deleted
*
* @return the operation status, RT_EOK on OK; -RT_ERROR on error
*/
rt_err_t rt_timer_delete(rt_timer_t timer)
{
rt_base_t level;
struct rt_spinlock *spinlock;
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
spinlock = _timerlock_idx(timer);
level = rt_spin_lock_irqsave(spinlock);
_timer_remove(timer);
/* stop timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_spin_unlock_irqrestore(spinlock, level);
rt_object_delete(&(timer->parent));
return RT_EOK;
}
RTM_EXPORT(rt_timer_delete);
#endif /* RT_USING_HEAP */
/**
* @brief This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
static rt_err_t _timer_start(rt_list_t *timer_list, rt_timer_t timer)
{
unsigned int row_lvl;
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
unsigned int tst_nr;
static unsigned int random_nr;
/* remove timer from list */
_timer_remove(timer);
/* change status of timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
timer->timeout_tick = rt_tick_get() + timer->init_tick;
row_head[0] = &timer_list[0];
for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
for (; row_head[row_lvl] != timer_list[row_lvl].prev;
row_head[row_lvl] = row_head[row_lvl]->next)
{
struct rt_timer *t;
rt_list_t *p = row_head[row_lvl]->next;
/* fix up the entry pointer */
t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
/* If we have two timers that timeout at the same time, it's
* preferred that the timer inserted early get called early.
* So insert the new timer to the end the the some-timeout timer
* list.
*/
if ((t->timeout_tick - timer->timeout_tick) == 0)
{
continue;
}
else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
{
break;
}
}
if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
row_head[row_lvl + 1] = row_head[row_lvl] + 1;
}
/* Interestingly, this super simple timer insert counter works very very
* well on distributing the list height uniformly. By means of "very very
* well", I mean it beats the randomness of timer->timeout_tick very easily
* (actually, the timeout_tick is not random and easy to be attacked). */
random_nr++;
tst_nr = random_nr;
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
else
break;
/* Shift over the bits we have tested. Works well with 1 bit and 2
* bits. */
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
}
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
return RT_EOK;
}
/**
* @brief This function will check timer list, if a timeout event happens,
* the corresponding timeout function will be invoked.
*
* @param timer_list The timer list to check.
* @param lock The lock for the timer list.
*/
static void _timer_check(rt_list_t *timer_list, struct rt_spinlock *lock)
{
struct rt_timer *t;
rt_tick_t current_tick;
rt_base_t level;
rt_list_t list;
level = rt_spin_lock_irqsave(lock);
current_tick = rt_tick_get();
rt_list_init(&list);
while (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
{
t = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
/* re-get tick */
current_tick = rt_tick_get();
/*
* 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]));
rt_spin_unlock_irqrestore(lock, level);
/* call timeout function */
t->timeout_func(t->parameter);
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
level = rt_spin_lock_irqsave(lock);
/* 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;
_timer_start(timer_list, t);
}
}
else break;
}
rt_spin_unlock_irqrestore(lock, level);
}
/**
* @brief This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_start(rt_timer_t timer)
{
rt_sched_lock_level_t slvl;
int is_thread_timer = 0;
struct rt_spinlock *spinlock;
rt_list_t *timer_list;
rt_base_t level;
rt_err_t err;
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
#ifdef RT_USING_TIMER_ALL_SOFT
timer_list = _soft_timer_list;
spinlock = &_stimer_lock;
#else
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
timer_list = _soft_timer_list;
spinlock = &_stimer_lock;
}
else
#endif /* RT_USING_TIMER_SOFT */
{
timer_list = _timer_list;
spinlock = &_htimer_lock;
}
#endif
if (timer->parent.flag & RT_TIMER_FLAG_THREAD_TIMER)
{
rt_thread_t thread;
is_thread_timer = 1;
rt_sched_lock(&slvl);
thread = rt_container_of(timer, struct rt_thread, thread_timer);
RT_ASSERT(rt_object_get_type(&thread->parent) == RT_Object_Class_Thread);
rt_sched_thread_timer_start(thread);
}
level = rt_spin_lock_irqsave(spinlock);
err = _timer_start(timer_list, timer);
rt_spin_unlock_irqrestore(spinlock, level);
if (is_thread_timer)
{
rt_sched_unlock(slvl);
}
return err;
}
RTM_EXPORT(rt_timer_start);
/**
* @brief This function will stop the timer
*
* @param timer the timer to be stopped
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_stop(rt_timer_t timer)
{
rt_base_t level;
struct rt_spinlock *spinlock;
/* timer check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
spinlock = _timerlock_idx(timer);
level = rt_spin_lock_irqsave(spinlock);
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
{
rt_spin_unlock_irqrestore(spinlock, level);
return -RT_ERROR;
}
RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
_timer_remove(timer);
/* change status */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_spin_unlock_irqrestore(spinlock, level);
return RT_EOK;
}
RTM_EXPORT(rt_timer_stop);
/**
* @brief This function will get or set some options of the timer
*
* @param timer the timer to be get or set
* @param cmd the control command
* @param arg the argument
*
* @return the statu of control
*/
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
{
struct rt_spinlock *spinlock;
rt_base_t level;
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
spinlock = _timerlock_idx(timer);
level = rt_spin_lock_irqsave(spinlock);
switch (cmd)
{
case RT_TIMER_CTRL_GET_TIME:
*(rt_tick_t *)arg = timer->init_tick;
break;
case RT_TIMER_CTRL_SET_TIME:
RT_ASSERT((*(rt_tick_t *)arg) < RT_TICK_MAX / 2);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
_timer_remove(timer);
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
timer->init_tick = *(rt_tick_t *)arg;
break;
case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
break;
case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break;
case RT_TIMER_CTRL_GET_STATE:
if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
/*timer is start and run*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
}
else
{
/*timer is stop*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
}
break;
case RT_TIMER_CTRL_GET_REMAIN_TIME:
*(rt_tick_t *)arg = timer->timeout_tick;
break;
case RT_TIMER_CTRL_GET_FUNC:
*(void **)arg = (void *)timer->timeout_func;
break;
case RT_TIMER_CTRL_SET_FUNC:
timer->timeout_func = (void (*)(void*))arg;
break;
case RT_TIMER_CTRL_GET_PARM:
*(void **)arg = timer->parameter;
break;
case RT_TIMER_CTRL_SET_PARM:
timer->parameter = arg;
break;
default:
break;
}
rt_spin_unlock_irqrestore(spinlock, level);
return RT_EOK;
}
RTM_EXPORT(rt_timer_control);
/**
* @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)
{
RT_ASSERT(rt_interrupt_get_nest() > 0);
#ifdef RT_USING_SMP
/* Running on core 0 only */
if (rt_cpu_get_id() != 0)
{
return;
}
#endif
#ifdef RT_USING_TIMER_SOFT
rt_err_t ret = RT_ERROR;
rt_tick_t next_timeout;
ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout);
if ((ret == RT_EOK) && (next_timeout <= rt_tick_get()))
{
rt_sem_release(&_soft_timer_sem);
}
#endif
#ifndef RT_USING_TIMER_ALL_SOFT
_timer_check(_timer_list, &_htimer_lock);
#endif
}
/**
* @brief This function will return the next timeout tick in the system.
*
* @return the next timeout tick in the system
*/
rt_tick_t rt_timer_next_timeout_tick(void)
{
rt_base_t level;
rt_tick_t htimer_next_timeout = RT_TICK_MAX, stimer_next_timeout = RT_TICK_MAX;
#ifndef RT_USING_TIMER_ALL_SOFT
level = rt_spin_lock_irqsave(&_htimer_lock);
_timer_list_next_timeout(_timer_list, &htimer_next_timeout);
rt_spin_unlock_irqrestore(&_htimer_lock, level);
#endif
#ifdef RT_USING_TIMER_SOFT
level = rt_spin_lock_irqsave(&_stimer_lock);
_timer_list_next_timeout(_soft_timer_list, &stimer_next_timeout);
rt_spin_unlock_irqrestore(&_stimer_lock, level);
#endif
return htimer_next_timeout < stimer_next_timeout ? htimer_next_timeout : stimer_next_timeout;
}
#ifdef RT_USING_TIMER_SOFT
/**
* @brief System timer thread entry
*
* @param parameter is the arg of the thread
*/
static void _timer_thread_entry(void *parameter)
{
RT_UNUSED(parameter);
while (1)
{
_timer_check(_soft_timer_list, &_stimer_lock); /* check software timer */
rt_sem_take(&_soft_timer_sem, RT_WAITING_FOREVER);
}
}
#endif /* RT_USING_TIMER_SOFT */
/**
* @ingroup group_system_init
*
* @brief This function will initialize system timer
*/
void rt_system_timer_init(void)
{
#ifndef RT_USING_TIMER_ALL_SOFT
rt_size_t i;
for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
{
rt_list_init(_timer_list + i);
}
rt_spin_lock_init(&_htimer_lock);
#endif
}
/**
* @ingroup group_system_init
*
* @brief This function will initialize system timer thread
*/
void rt_system_timer_thread_init(void)
{
#ifdef RT_USING_TIMER_SOFT
int i;
for (i = 0;
i < sizeof(_soft_timer_list) / sizeof(_soft_timer_list[0]);
i++)
{
rt_list_init(_soft_timer_list + i);
}
rt_spin_lock_init(&_stimer_lock);
rt_sem_init(&_soft_timer_sem, "stimer", 0, RT_IPC_FLAG_PRIO);
rt_sem_control(&_soft_timer_sem, RT_IPC_CMD_SET_VLIMIT, (void*)1);
/* start software timer thread */
rt_thread_init(&_timer_thread,
"timer",
_timer_thread_entry,
RT_NULL,
&_timer_thread_stack[0],
sizeof(_timer_thread_stack),
RT_TIMER_THREAD_PRIO,
10);
/* startup */
rt_thread_startup(&_timer_thread);
#endif /* RT_USING_TIMER_SOFT */
}
/**@}*/
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

RT-Thread是一个来自中国的开源物联网操作系统,它提供了非常强的可伸缩能力:从一个可以运行在ARM Cortex-M0芯片上的极小内核,到中等的ARM Cortex-M3/4/7系统,甚至是多核,64位的ARM Cortex-A,MIPS32/64处理器的功能丰富系统
Cancel

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/rtthread/rt-thread.git
git@gitee.com:rtthread/rt-thread.git
rtthread
rt-thread
rt-thread
master
Going to Help Center

Search

Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /