3131#define DBG_LVL DBG_INFO
3232#include <rtdbg.h>
3333
34+ #ifndef RT_USING_TIMER_ALL_SOFT
3435/* hard timer list */
3536static rt_list_t _timer_list [RT_TIMER_SKIP_LIST_LEVEL ];
3637static struct rt_spinlock _htimer_lock ;
38+ #endif
3739
3840#ifdef RT_USING_TIMER_SOFT
3941
@@ -49,7 +51,6 @@ static struct rt_spinlock _htimer_lock;
4951static rt_list_t _soft_timer_list [RT_TIMER_SKIP_LIST_LEVEL ];
5052static struct rt_spinlock _stimer_lock ;
5153static struct rt_thread _timer_thread ;
52- static struct rt_semaphore _soft_timer_sem ;
5354rt_align (RT_ALIGN_SIZE )
5455static rt_uint8_t _timer_thread_stack [RT_TIMER_THREAD_STACK_SIZE ];
5556#endif /* RT_USING_TIMER_SOFT */
@@ -93,6 +94,9 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
9394
9495rt_inline struct rt_spinlock * _timerlock_idx (struct rt_timer * timer )
9596{
97+ #ifdef RT_USING_TIMER_ALL_SOFT
98+ return & _stimer_lock ;
99+ #else
96100#ifdef RT_USING_TIMER_SOFT
97101 if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
98102 {
@@ -103,6 +107,7 @@ rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
103107 {
104108 return & _htimer_lock ;
105109 }
110+ #endif
106111}
107112
108113/**
@@ -130,6 +135,10 @@ static void _timer_init(rt_timer_t timer,
130135{
131136 int i ;
132137
138+ #ifdef RT_USING_TIMER_ALL_SOFT
139+ flag |= RT_TIMER_FLAG_SOFT_TIMER ;
140+ #endif
141+
133142 /* set flag */
134143 timer -> parent .flag = flag ;
135144
@@ -570,6 +579,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
570579 RT_ASSERT (timer != RT_NULL );
571580 RT_ASSERT (rt_object_get_type (& timer -> parent ) == RT_Object_Class_Timer );
572581
582+ #ifdef RT_USING_TIMER_ALL_SOFT
583+ timer_list = _soft_timer_list ;
584+ spinlock = & _stimer_lock ;
585+ #else
573586#ifdef RT_USING_TIMER_SOFT
574587 if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
575588 {
@@ -582,6 +595,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
582595 timer_list = _timer_list ;
583596 spinlock = & _htimer_lock ;
584597 }
598+ #endif
585599
586600 if (timer -> parent .flag & RT_TIMER_FLAG_THREAD_TIMER )
587601 {
@@ -598,13 +612,6 @@ rt_err_t rt_timer_start(rt_timer_t timer)
598612
599613 err = _timer_start (timer_list , timer );
600614
601- #ifdef RT_USING_TIMER_SOFT
602- if (err == RT_EOK && (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER ))
603- {
604- rt_sem_release (& _soft_timer_sem );
605- }
606- #endif /* RT_USING_TIMER_SOFT */
607-
608615 rt_spin_unlock_irqrestore (spinlock , level );
609616
610617 if (is_thread_timer )
@@ -747,6 +754,9 @@ RTM_EXPORT(rt_timer_control);
747754 */
748755void rt_timer_check (void )
749756{
757+ rt_err_t ret = RT_ERROR ;
758+ rt_tick_t next_timeout ;
759+
750760 RT_ASSERT (rt_interrupt_get_nest () > 0 );
751761
752762#ifdef RT_USING_SMP
@@ -756,7 +766,17 @@ void rt_timer_check(void)
756766 return ;
757767 }
758768#endif
769+
770+ #ifdef RT_USING_TIMER_SOFT
771+ ret = _timer_list_next_timeout (_soft_timer_list , & next_timeout );
772+ if ((ret == RT_EOK ) && (next_timeout <= rt_tick_get ()))
773+ {
774+ rt_thread_resume (& _timer_thread );
775+ }
776+ #endif
777+ #ifndef RT_USING_TIMER_ALL_SOFT
759778 _timer_check (_timer_list , & _htimer_lock );
779+ #endif
760780}
761781
762782/**
@@ -767,13 +787,21 @@ void rt_timer_check(void)
767787rt_tick_t rt_timer_next_timeout_tick (void )
768788{
769789 rt_base_t level ;
770- rt_tick_t next_timeout = RT_TICK_MAX ;
790+ rt_tick_t htimer_next_timeout = RT_TICK_MAX , stimer_next_timeout = RT_TICK_MAX ;
771791
792+ #ifndef RT_USING_TIMER_ALL_SOFT
772793 level = rt_spin_lock_irqsave (& _htimer_lock );
773- _timer_list_next_timeout (_timer_list , & next_timeout );
794+ _timer_list_next_timeout (_timer_list , & htimer_next_timeout );
774795 rt_spin_unlock_irqrestore (& _htimer_lock , level );
796+ #endif
775797
776- return next_timeout ;
798+ #ifdef RT_USING_TIMER_SOFT
799+ level = rt_spin_lock_irqsave (& _stimer_lock );
800+ _timer_list_next_timeout (_soft_timer_list , & stimer_next_timeout );
801+ rt_spin_unlock_irqrestore (& _stimer_lock , level );
802+ #endif
803+
804+ return htimer_next_timeout < stimer_next_timeout ? htimer_next_timeout : stimer_next_timeout ;
777805}
778806
779807#ifdef RT_USING_TIMER_SOFT
@@ -784,41 +812,15 @@ rt_tick_t rt_timer_next_timeout_tick(void)
784812 */
785813static void _timer_thread_entry (void * parameter )
786814{
787- rt_err_t ret = RT_ERROR ;
788- rt_tick_t next_timeout ;
789- rt_base_t level ;
790-
791815 RT_UNUSED (parameter );
792816
793- rt_sem_control (& _soft_timer_sem , RT_IPC_CMD_SET_VLIMIT , (void * )1 );
794-
795817 while (1 )
796818 {
797- /* get the next timeout tick */
798- level = rt_spin_lock_irqsave (& _stimer_lock );
799- ret = _timer_list_next_timeout (_soft_timer_list , & next_timeout );
800- rt_spin_unlock_irqrestore (& _stimer_lock , level );
801-
802- if (ret != RT_EOK )
803- {
804- rt_sem_take (& _soft_timer_sem , RT_WAITING_FOREVER );
805- }
806- else
819+ _timer_check (_soft_timer_list , & _stimer_lock ); /* check software timer */
820+ if (rt_thread_suspend (& _timer_thread ) == RT_EOK )
807821 {
808- rt_tick_t current_tick ;
809-
810- /* get current tick */
811- current_tick = rt_tick_get ();
812-
813- if ((next_timeout - current_tick ) < RT_TICK_MAX / 2 )
814- {
815- /* get the delta timeout tick */
816- next_timeout = next_timeout - current_tick ;
817- rt_sem_take (& _soft_timer_sem , next_timeout );
818- }
822+ rt_schedule ();
819823 }
820-
821- _timer_check (_soft_timer_list , & _stimer_lock ); /* check software timer */
822824 }
823825}
824826#endif /* RT_USING_TIMER_SOFT */
@@ -830,13 +832,16 @@ static void _timer_thread_entry(void *parameter)
830832 */
831833void rt_system_timer_init (void )
832834{
835+ #ifndef RT_USING_TIMER_ALL_SOFT
833836 rt_size_t i ;
834837
835838 for (i = 0 ; i < sizeof (_timer_list ) / sizeof (_timer_list [0 ]); i ++ )
836839 {
837840 rt_list_init (_timer_list + i );
838841 }
842+
839843 rt_spin_lock_init (& _htimer_lock );
844+ #endif
840845}
841846
842847/**
@@ -856,7 +861,6 @@ void rt_system_timer_thread_init(void)
856861 rt_list_init (_soft_timer_list + i );
857862 }
858863 rt_spin_lock_init (& _stimer_lock );
859- rt_sem_init (& _soft_timer_sem , "stimer" , 0 , RT_IPC_FLAG_PRIO );
860864 /* start software timer thread */
861865 rt_thread_init (& _timer_thread ,
862866 "timer" ,
0 commit comments