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
@@ -49,7 +51,6 @@ static struct rt_spinlock _htimer_lock;
49
51
static rt_list_t _soft_timer_list [RT_TIMER_SKIP_LIST_LEVEL ];
50
52
static struct rt_spinlock _stimer_lock ;
51
53
static struct rt_thread _timer_thread ;
52
- static struct rt_semaphore _soft_timer_sem ;
53
54
rt_align (RT_ALIGN_SIZE )
54
55
static rt_uint8_t _timer_thread_stack [RT_TIMER_THREAD_STACK_SIZE ];
55
56
#endif /* RT_USING_TIMER_SOFT */
@@ -93,6 +94,9 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
93
94
94
95
rt_inline struct rt_spinlock * _timerlock_idx (struct rt_timer * timer )
95
96
{
97
+ #ifdef RT_USING_TIMER_ALL_SOFT
98
+ return & _stimer_lock ;
99
+ #else
96
100
#ifdef RT_USING_TIMER_SOFT
97
101
if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
98
102
{
@@ -103,6 +107,7 @@ rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
103
107
{
104
108
return & _htimer_lock ;
105
109
}
110
+ #endif
106
111
}
107
112
108
113
/**
@@ -130,6 +135,10 @@ static void _timer_init(rt_timer_t timer,
130
135
{
131
136
int i ;
132
137
138
+ #ifdef RT_USING_TIMER_ALL_SOFT
139
+ flag |= RT_TIMER_FLAG_SOFT_TIMER ;
140
+ #endif
141
+
133
142
/* set flag */
134
143
timer -> parent .flag = flag ;
135
144
@@ -570,6 +579,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
570
579
RT_ASSERT (timer != RT_NULL );
571
580
RT_ASSERT (rt_object_get_type (& timer -> parent ) == RT_Object_Class_Timer );
572
581
582
+ #ifdef RT_USING_TIMER_ALL_SOFT
583
+ timer_list = _soft_timer_list ;
584
+ spinlock = & _stimer_lock ;
585
+ #else
573
586
#ifdef RT_USING_TIMER_SOFT
574
587
if (timer -> parent .flag & RT_TIMER_FLAG_SOFT_TIMER )
575
588
{
@@ -582,6 +595,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
582
595
timer_list = _timer_list ;
583
596
spinlock = & _htimer_lock ;
584
597
}
598
+ #endif
585
599
586
600
if (timer -> parent .flag & RT_TIMER_FLAG_THREAD_TIMER )
587
601
{
@@ -598,13 +612,6 @@ rt_err_t rt_timer_start(rt_timer_t timer)
598
612
599
613
err = _timer_start (timer_list , timer );
600
614
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
615
rt_spin_unlock_irqrestore (spinlock , level );
609
616
610
617
if (is_thread_timer )
@@ -747,6 +754,9 @@ RTM_EXPORT(rt_timer_control);
747
754
*/
748
755
void rt_timer_check (void )
749
756
{
757
+ rt_err_t ret = RT_ERROR ;
758
+ rt_tick_t next_timeout ;
759
+
750
760
RT_ASSERT (rt_interrupt_get_nest () > 0 );
751
761
752
762
#ifdef RT_USING_SMP
@@ -756,7 +766,17 @@ void rt_timer_check(void)
756
766
return ;
757
767
}
758
768
#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
759
778
_timer_check (_timer_list , & _htimer_lock );
779
+ #endif
760
780
}
761
781
762
782
/**
@@ -767,13 +787,21 @@ void rt_timer_check(void)
767
787
rt_tick_t rt_timer_next_timeout_tick (void )
768
788
{
769
789
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 ;
771
791
792
+ #ifndef RT_USING_TIMER_ALL_SOFT
772
793
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 );
774
795
rt_spin_unlock_irqrestore (& _htimer_lock , level );
796
+ #endif
775
797
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 ;
777
805
}
778
806
779
807
#ifdef RT_USING_TIMER_SOFT
@@ -784,41 +812,15 @@ rt_tick_t rt_timer_next_timeout_tick(void)
784
812
*/
785
813
static void _timer_thread_entry (void * parameter )
786
814
{
787
- rt_err_t ret = RT_ERROR ;
788
- rt_tick_t next_timeout ;
789
- rt_base_t level ;
790
-
791
815
RT_UNUSED (parameter );
792
816
793
- rt_sem_control (& _soft_timer_sem , RT_IPC_CMD_SET_VLIMIT , (void * )1 );
794
-
795
817
while (1 )
796
818
{
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 )
807
821
{
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 ();
819
823
}
820
-
821
- _timer_check (_soft_timer_list , & _stimer_lock ); /* check software timer */
822
824
}
823
825
}
824
826
#endif /* RT_USING_TIMER_SOFT */
@@ -830,13 +832,16 @@ static void _timer_thread_entry(void *parameter)
830
832
*/
831
833
void rt_system_timer_init (void )
832
834
{
835
+ #ifndef RT_USING_TIMER_ALL_SOFT
833
836
rt_size_t i ;
834
837
835
838
for (i = 0 ; i < sizeof (_timer_list ) / sizeof (_timer_list [0 ]); i ++ )
836
839
{
837
840
rt_list_init (_timer_list + i );
838
841
}
842
+
839
843
rt_spin_lock_init (& _htimer_lock );
844
+ #endif
840
845
}
841
846
842
847
/**
@@ -856,7 +861,6 @@ void rt_system_timer_thread_init(void)
856
861
rt_list_init (_soft_timer_list + i );
857
862
}
858
863
rt_spin_lock_init (& _stimer_lock );
859
- rt_sem_init (& _soft_timer_sem , "stimer" , 0 , RT_IPC_FLAG_PRIO );
860
864
/* start software timer thread */
861
865
rt_thread_init (& _timer_thread ,
862
866
"timer" ,
0 commit comments