0
|
1 |
#include <stdlib.h>
|
|
2 |
#include <unistd.h>
|
|
3 |
|
|
4 |
#include <native/task.h>
|
|
5 |
#include <native/timer.h>
|
|
6 |
#include <native/mutex.h>
|
|
7 |
#include <native/alarm.h>
|
|
8 |
|
|
9 |
#include "applicfg.h"
|
|
10 |
#include "can_driver.h"
|
|
11 |
#include "timer.h"
|
|
12 |
|
|
13 |
#define TIMERLOOP_TASK_CREATED 1
|
|
14 |
|
|
15 |
RT_MUTEX CanFestival_mutex;
|
|
16 |
RT_TASK timerloop_task;
|
|
17 |
RT_ALARM timerloop_alarm;
|
|
18 |
RTIME last_time_read;
|
|
19 |
RTIME last_occured_alarm;
|
|
20 |
RTIME last_alarm_set;
|
|
21 |
|
|
22 |
char stop_timer=0;
|
|
23 |
|
|
24 |
void cleanup_all(void)
|
|
25 |
{
|
|
26 |
rt_task_delete(&timerloop_task);
|
|
27 |
rt_alarm_delete(&timerloop_alarm);
|
|
28 |
}
|
|
29 |
void stopTimer(void)
|
|
30 |
{
|
|
31 |
stop_timer = 1;
|
|
32 |
rt_task_unblock(&timerloop_task);
|
|
33 |
}
|
|
34 |
|
|
35 |
|
|
36 |
void EnterMutex(void)
|
|
37 |
{
|
|
38 |
rt_mutex_lock(&CanFestival_mutex, TM_INFINITE);
|
|
39 |
}
|
|
40 |
|
|
41 |
void LeaveMutex(void)
|
|
42 |
{
|
|
43 |
rt_mutex_unlock(&CanFestival_mutex);
|
|
44 |
}
|
|
45 |
|
|
46 |
void timerloop_task_proc(void *arg)
|
|
47 |
{
|
|
48 |
int ret;
|
|
49 |
do{
|
|
50 |
do{
|
|
51 |
last_occured_alarm = last_alarm_set;
|
|
52 |
EnterMutex();
|
|
53 |
TimeDispatch();
|
|
54 |
LeaveMutex();
|
|
55 |
}while ((ret = rt_task_sleep_until(last_alarm_set)) == 0);
|
|
56 |
}while (ret == -EINTR && !stop_timer );
|
|
57 |
printf("End of TimerLoop, code %d\n",ret);
|
|
58 |
}
|
|
59 |
|
|
60 |
void TimerLoop(TimerCallback_t init_callback)
|
|
61 |
{
|
|
62 |
int ret;
|
|
63 |
stop_timer = 0;
|
|
64 |
|
|
65 |
ret = rt_alarm_create( &timerloop_alarm, NULL);
|
|
66 |
if (ret) {
|
|
67 |
printf("Failed to create timerloop_alarm, code %d\n",errno);
|
|
68 |
goto error;
|
|
69 |
}
|
|
70 |
|
|
71 |
//create timerloop_task
|
|
72 |
ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
|
|
73 |
if (ret) {
|
|
74 |
printf("Failed to create timerloop_task, code %d\n",errno);
|
|
75 |
return;
|
|
76 |
}
|
|
77 |
|
|
78 |
getElapsedTime();
|
|
79 |
last_alarm_set = last_time_read;
|
|
80 |
last_occured_alarm = last_alarm_set;
|
|
81 |
SetAlarm(NULL, 0, init_callback, 0, 0);
|
|
82 |
// start timerloop_task
|
|
83 |
ret = rt_task_start(&timerloop_task,&timerloop_task_proc,NULL);
|
|
84 |
if (ret) {
|
|
85 |
printf("Failed to start timerloop_task, code %d\n",errno);
|
|
86 |
goto error;
|
|
87 |
}
|
|
88 |
|
|
89 |
// At first, TimeDispatch will call init_callback.
|
|
90 |
pause();
|
|
91 |
|
|
92 |
error:
|
|
93 |
cleanup_all();
|
|
94 |
}
|
|
95 |
|
|
96 |
void ReceiveLoop_task_proc(void* arg)
|
|
97 |
{
|
|
98 |
canReceiveLoop((CAN_HANDLE)arg);
|
|
99 |
}
|
|
100 |
|
|
101 |
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE *ReceiveLoop_task)
|
|
102 |
{
|
|
103 |
int ret;
|
|
104 |
static int id = 0;
|
|
105 |
char taskname[64];
|
|
106 |
sprintf(taskname,"canloop%d",id);
|
|
107 |
id++;
|
|
108 |
|
|
109 |
//create timerloop_task
|
|
110 |
ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
|
|
111 |
if (ret) {
|
|
112 |
printf("Failed to create ReceiveLoop_task number %d, code %d\n", id, errno);
|
|
113 |
return;
|
|
114 |
}
|
|
115 |
// start timerloop_task
|
|
116 |
ret = rt_task_start(ReceiveLoop_task,&ReceiveLoop_task_proc,(void*)fd0);
|
|
117 |
if (ret) {
|
|
118 |
printf("Failed to start ReceiveLoop_task number %d, code %d\n", id, errno);
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
void WaitReceiveTaskEnd(TASK_HANDLE Thread)
|
|
124 |
{
|
|
125 |
rt_task_delete(&Thread);
|
|
126 |
}
|
|
127 |
#define max(a,b) a>b?a:b
|
|
128 |
void setTimer(TIMEVAL value)
|
|
129 |
{
|
|
130 |
last_alarm_set = last_time_read + value;
|
|
131 |
rt_task_unblock(&timerloop_task);
|
|
132 |
}
|
|
133 |
|
|
134 |
TIMEVAL getElapsedTime(void)
|
|
135 |
{
|
|
136 |
last_time_read = rt_timer_ticks2ns(rt_timer_read());
|
|
137 |
return last_time_read - last_occured_alarm;
|
|
138 |
}
|