MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / timeout_calc

Function timeout_calc

components/drivers/hwtimer/hwtimer.c:36–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34#endif /* RT_USING_DM */
35
36rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
37{
38 float overflow;
39 float timeout;
40 rt_uint32_t counter;
41 int i, index = 0;
42 float tv_sec;
43 float devi_min = 1;
44 float devi;
45
46 /* changed to second */
47 overflow = timer->info->maxcnt/(float)timer->freq;
48 tv_sec = tv->sec + tv->usec/(float)1000000;
49
50 if (tv_sec < (1/(float)timer->freq))
51 {
52 /* little timeout */
53 i = 0;
54 timeout = 1/(float)timer->freq;
55 }
56 else
57 {
58 for (i = 1; i > 0; i ++)
59 {
60 timeout = tv_sec/i;
61
62 if (timeout <= overflow)
63 {
64 counter = (rt_uint32_t)(timeout * timer->freq);
65 devi = tv_sec - (counter / (float)timer->freq) * i;
66 /* Minimum calculation error */
67 if (devi > devi_min)
68 {
69 i = index;
70 timeout = tv_sec/i;
71 break;
72 }
73 else if (devi == 0)
74 {
75 break;
76 }
77 else if (devi < devi_min)
78 {
79 devi_min = devi;
80 index = i;
81 }
82 }
83 }
84 }
85
86 timer->cycles = i;
87 timer->reload = i;
88 timer->period_sec = timeout;
89 counter = (rt_uint32_t)(timeout * timer->freq);
90
91 return counter;
92}
93

Callers 1

rt_hwtimer_writeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected