31
31
#define DBG_LVL DBG_INFO
32
32
#include <rtdbg.h>
33
33
34
+ #ifndef RT_USING_TIMER_ALL_SOFT
34
35
/* hard timer list */
35
36
static rt_list_t _timer_list [RT_TIMER_SKIP_LIST_LEVEL ];
36
37
static struct rt_spinlock _htimer_lock ;
38
+ #endif
37
39
38
40
#ifdef RT_USING_TIMER_SOFT
39
41
@@ -93,6 +95,9 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
93
95
94
96
rt_inline struct rt_spinlock * _timerlock_idx (struct rt_timer * timer )
95
97
{
98
+ #ifdef RT_USING_TIMER_ALL_SOFT
99
+ return & _stimer_lock ;
100
+ #else
96
101
#ifdef RT_USING_TIMER_SOFT
97
102
if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
98
103
{
@@ -103,6 +108,7 @@ rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
103
108
{
104
109
return & _htimer_lock ;
105
110
}
111
+ #endif
106
112
}
107
113
108
114
/**
@@ -130,6 +136,10 @@ static void _timer_init(rt_timer_t timer,
130
136
{
131
137
int i ;
132
138
139
+ #ifdef RT_USING_TIMER_ALL_SOFT
140
+ flag |= RT_TIMER_FLAG_SOFT_TIMER ;
141
+ #endif
142
+
133
143
/* set flag */
134
144
timer -> parent .flag = flag ;
135
145
@@ -570,6 +580,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
570
580
RT_ASSERT (timer != RT_NULL );
571
581
RT_ASSERT (rt_object_get_type (& timer -> parent ) == RT_Object_Class_Timer );
572
582
583
+ #ifdef RT_USING_TIMER_ALL_SOFT
584
+ timer_list = _soft_timer_list ;
585
+ spinlock = & _stimer_lock ;
586
+ #else
573
587
#ifdef RT_USING_TIMER_SOFT
574
588
if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
575
589
{
@@ -582,6 +596,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
582
596
timer_list = _timer_list ;
583
597
spinlock = & _htimer_lock ;
584
598
}
599
+ #endif
585
600
586
601
if (timer -> parent .flag & RT_TIMER_FLAG_THREAD_TIMER )
587
602
{
@@ -598,13 +613,6 @@ rt_err_t rt_timer_start(rt_timer_t timer)
598
613
599
614
err = _timer_start (timer_list , timer );
600
615
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
-
608
616
rt_spin_unlock_irqrestore (spinlock , level );
609
617
610
618
if (is_thread_timer )
@@ -756,7 +764,20 @@ void rt_timer_check(void)
756
764
return ;
757
765
}
758
766
#endif
767
+
768
+ #ifdef RT_USING_TIMER_SOFT
769
+ rt_err_t ret = RT_ERROR ;
770
+ rt_tick_t next_timeout ;
771
+
772
+ ret = _timer_list_next_timeout (_soft_timer_list , & next_timeout );
773
+ if ((ret == RT_EOK ) && (next_timeout <= rt_tick_get ()))
774
+ {
775
+ rt_sem_release (& _soft_timer_sem );
776
+ }
777
+ #endif
778
+ #ifndef RT_USING_TIMER_ALL_SOFT
759
779
_timer_check (_timer_list , & _htimer_lock );
780
+ #endif
760
781
}
761
782
762
783
/**
@@ -767,13 +788,21 @@ void rt_timer_check(void)
767
788
rt_tick_t rt_timer_next_timeout_tick (void )
768
789
{
769
790
rt_base_t level ;
770
- rt_tick_t next_timeout = RT_TICK_MAX ;
791
+ rt_tick_t htimer_next_timeout = RT_TICK_MAX , stimer_next_timeout = RT_TICK_MAX ;
771
792
793
+ #ifndef RT_USING_TIMER_ALL_SOFT
772
794
level = rt_spin_lock_irqsave (& _htimer_lock );
773
- _timer_list_next_timeout (_timer_list , & next_timeout );
795
+ _timer_list_next_timeout (_timer_list , & htimer_next_timeout );
774
796
rt_spin_unlock_irqrestore (& _htimer_lock , level );
797
+ #endif
775
798
776
- return next_timeout ;
799
+ #ifdef RT_USING_TIMER_SOFT
800
+ level = rt_spin_lock_irqsave (& _stimer_lock );
801
+ _timer_list_next_timeout (_soft_timer_list , & stimer_next_timeout );
802
+ rt_spin_unlock_irqrestore (& _stimer_lock , level );
803
+ #endif
804
+
805
+ return htimer_next_timeout < stimer_next_timeout ? htimer_next_timeout : stimer_next_timeout ;
777
806
}
778
807
779
808
#ifdef RT_USING_TIMER_SOFT
@@ -784,41 +813,12 @@ rt_tick_t rt_timer_next_timeout_tick(void)
784
813
*/
785
814
static void _timer_thread_entry (void * parameter )
786
815
{
787
- rt_err_t ret = RT_ERROR ;
788
- rt_tick_t next_timeout ;
789
- rt_base_t level ;
790
-
791
816
RT_UNUSED (parameter );
792
817
793
- rt_sem_control (& _soft_timer_sem , RT_IPC_CMD_SET_VLIMIT , (void * )1 );
794
-
795
818
while (1 )
796
819
{
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
807
- {
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
- }
819
- }
820
-
821
820
_timer_check (_soft_timer_list , & _stimer_lock ); /* check software timer */
821
+ rt_sem_take (& _soft_timer_sem , RT_WAITING_FOREVER );
822
822
}
823
823
}
824
824
#endif /* RT_USING_TIMER_SOFT */
@@ -830,13 +830,16 @@ static void _timer_thread_entry(void *parameter)
830
830
*/
831
831
void rt_system_timer_init (void )
832
832
{
833
+ #ifndef RT_USING_TIMER_ALL_SOFT
833
834
rt_size_t i ;
834
835
835
836
for (i = 0 ; i < sizeof (_timer_list ) / sizeof (_timer_list [0 ]); i ++ )
836
837
{
837
838
rt_list_init (_timer_list + i );
838
839
}
840
+
839
841
rt_spin_lock_init (& _htimer_lock );
842
+ #endif
840
843
}
841
844
842
845
/**
@@ -857,6 +860,7 @@ void rt_system_timer_thread_init(void)
857
860
}
858
861
rt_spin_lock_init (& _stimer_lock );
859
862
rt_sem_init (& _soft_timer_sem , "stimer" , 0 , RT_IPC_FLAG_PRIO );
863
+ rt_sem_control (& _soft_timer_sem , RT_IPC_CMD_SET_VLIMIT , (void * )1 );
860
864
/* start software timer thread */
861
865
rt_thread_init (& _timer_thread ,
862
866
"timer" ,
0 commit comments