drivers/timers_xeno/timers_xeno.c
changeset 47 8a1047ab51f4
parent 35 88812de1d7cc
child 145 e747d2e26af0
equal deleted inserted replaced
46:a3b169324368 47:8a1047ab51f4
     1 #include <stdlib.h>
     1 #include <stdlib.h>
     2 #include <unistd.h> 
     2 #include <unistd.h>
       
     3 #include <sys/mman.h>
     3 
     4 
     4 #include <native/task.h>
     5 #include <native/task.h>
     5 #include <native/timer.h>
     6 #include <native/timer.h>
     6 #include <native/mutex.h>
     7 #include <native/mutex.h>
     7 #include <native/alarm.h>
     8 #include <native/alarm.h>
    12 
    13 
    13 #define TIMERLOOP_TASK_CREATED        1
    14 #define TIMERLOOP_TASK_CREATED        1
    14 
    15 
    15 RT_MUTEX CanFestival_mutex;
    16 RT_MUTEX CanFestival_mutex;
    16 RT_TASK timerloop_task;
    17 RT_TASK timerloop_task;
    17 RT_ALARM timerloop_alarm;
       
    18 RTIME last_time_read;
    18 RTIME last_time_read;
    19 RTIME last_occured_alarm;
    19 RTIME last_occured_alarm;
    20 RTIME last_alarm_set;
    20 RTIME last_alarm_set;
    21 
    21 
    22 char stop_timer=0;
    22 char stop_timer=0;
    23 
    23 
    24 void cleanup_all(void)
    24 void cleanup_all(void)
    25 {
    25 {
    26 	rt_task_delete(&timerloop_task);
    26 	rt_task_delete(&timerloop_task);
    27 	rt_alarm_delete(&timerloop_alarm);
       
    28 }
    27 }
    29 void StopTimerLoop(void)
    28 void StopTimerLoop(void)
    30 {
    29 {
    31 	stop_timer = 1;
    30 	stop_timer = 1;
    32 	rt_task_unblock(&timerloop_task);
    31 	rt_task_unblock(&timerloop_task);
    60 
    59 
    61 void StartTimerLoop(TimerCallback_t init_callback)
    60 void StartTimerLoop(TimerCallback_t init_callback)
    62 {
    61 {
    63 	int ret;
    62 	int ret;
    64 	stop_timer = 0;
    63 	stop_timer = 0;
       
    64 	char taskname[32];
       
    65 	snprintf(taskname, sizeof(taskname), "timerloop-%d", getpid());
    65 
    66 
    66 	ret = rt_alarm_create( &timerloop_alarm, NULL);
    67 	mlockall(MCL_CURRENT | MCL_FUTURE);
    67 	if (ret) {
    68 
    68 		printf("Failed to create timerloop_alarm, code %d\n",errno);
       
    69 		goto error;
       
    70 	}
       
    71 	
       
    72 	//create timerloop_task
    69 	//create timerloop_task
    73 	ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
    70 	ret = rt_task_create(&timerloop_task, taskname, 0, 50, 0);
    74 	if (ret) {
    71 	if (ret) {
    75 		printf("Failed to create timerloop_task, code %d\n",errno);
    72 		printf("Failed to create timerloop_task, code %d\n",errno);
    76 		return;
    73 		return;
    77 	}
    74 	}
    78  	
    75  	
   100 
    97 
   101 void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE *ReceiveLoop_task)
    98 void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE *ReceiveLoop_task)
   102 {
    99 {
   103 	int ret;
   100 	int ret;
   104 	static int id = 0;
   101 	static int id = 0;
   105 	char taskname[64];
   102 	char taskname[32];
   106 	sprintf(taskname,"canloop%d",id);
   103 	snprintf(taskname, sizeof(taskname), "canloop%d-%d", id, getpid());
   107 	id++;
   104 	id++;
       
   105 
       
   106 	mlockall(MCL_CURRENT | MCL_FUTURE);
   108 
   107 
   109 	//create timerloop_task
   108 	//create timerloop_task
   110 	ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
   109 	ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
   111 	if (ret) {
   110 	if (ret) {
   112 		printf("Failed to create ReceiveLoop_task number %d, code %d\n", id, errno);
   111 		printf("Failed to create ReceiveLoop_task number %d, code %d\n", id, errno);
   123 void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
   122 void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
   124 {
   123 {
   125 	rt_task_delete(Thread);
   124 	rt_task_delete(Thread);
   126 }
   125 }
   127 
   126 
   128 //#define max(a,b) a>b?a:b
       
   129 void setTimer(TIMEVAL value)
   127 void setTimer(TIMEVAL value)
   130 {	
   128 {
   131 	last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
   129 	last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
   132 	rt_task_unblock(&timerloop_task);
   130 	rt_task_unblock(&timerloop_task);
   133 }
   131 }
   134 
   132 
   135 TIMEVAL getElapsedTime(void)
   133 TIMEVAL getElapsedTime(void)