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 |
}
|
32
|
29 |
void StopTimerLoop(void)
|
0
|
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();
|
33
|
55 |
while ((ret = rt_task_sleep_until(last_alarm_set)) == -EINTR);
|
|
56 |
}while (ret == 0);
|
|
57 |
}while (!stop_timer);
|
0
|
58 |
printf("End of TimerLoop, code %d\n",ret);
|
|
59 |
}
|
|
60 |
|
32
|
61 |
void StartTimerLoop(TimerCallback_t init_callback)
|
0
|
62 |
{
|
|
63 |
int ret;
|
|
64 |
stop_timer = 0;
|
|
65 |
|
|
66 |
ret = rt_alarm_create( &timerloop_alarm, NULL);
|
|
67 |
if (ret) {
|
|
68 |
printf("Failed to create timerloop_alarm, code %d\n",errno);
|
|
69 |
goto error;
|
|
70 |
}
|
|
71 |
|
|
72 |
//create timerloop_task
|
|
73 |
ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
|
|
74 |
if (ret) {
|
|
75 |
printf("Failed to create timerloop_task, code %d\n",errno);
|
|
76 |
return;
|
|
77 |
}
|
|
78 |
|
|
79 |
getElapsedTime();
|
|
80 |
last_alarm_set = last_time_read;
|
|
81 |
last_occured_alarm = last_alarm_set;
|
|
82 |
SetAlarm(NULL, 0, init_callback, 0, 0);
|
|
83 |
// start timerloop_task
|
|
84 |
ret = rt_task_start(&timerloop_task,&timerloop_task_proc,NULL);
|
|
85 |
if (ret) {
|
|
86 |
printf("Failed to start timerloop_task, code %d\n",errno);
|
|
87 |
goto error;
|
|
88 |
}
|
|
89 |
|
33
|
90 |
return;
|
|
91 |
|
0
|
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 |
|
35
|
123 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
|
0
|
124 |
{
|
35
|
125 |
rt_task_delete(Thread);
|
0
|
126 |
}
|
34
|
127 |
|
|
128 |
//#define max(a,b) a>b?a:b
|
0
|
129 |
void setTimer(TIMEVAL value)
|
|
130 |
{
|
34
|
131 |
last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
|
0
|
132 |
rt_task_unblock(&timerloop_task);
|
|
133 |
}
|
|
134 |
|
|
135 |
TIMEVAL getElapsedTime(void)
|
|
136 |
{
|
|
137 |
last_time_read = rt_timer_ticks2ns(rt_timer_read());
|
|
138 |
return last_time_read - last_occured_alarm;
|
|
139 |
}
|