drivers/timers_xeno/timers_xeno.c
author etisserant
Tue, 08 Aug 2006 22:34:32 +0200
changeset 41 e70439f50539
parent 35 88812de1d7cc
child 47 8a1047ab51f4
permissions -rw-r--r--
First working cygwin/pcan_light commit. can_peak_win32.c is obfuscated.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
#include <unistd.h> 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
#include <native/task.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
#include <native/timer.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
#include <native/mutex.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
#include <native/alarm.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
#include "applicfg.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
#include "can_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
#define TIMERLOOP_TASK_CREATED        1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
RT_MUTEX CanFestival_mutex;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
RT_TASK timerloop_task;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
RT_ALARM timerloop_alarm;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
RTIME last_time_read;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
RTIME last_occured_alarm;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
RTIME last_alarm_set;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
char stop_timer=0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
void cleanup_all(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
	rt_task_delete(&timerloop_task);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
	rt_alarm_delete(&timerloop_alarm);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
}
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    29
void StopTimerLoop(void)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
	stop_timer = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
	rt_task_unblock(&timerloop_task);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
void EnterMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
	rt_mutex_lock(&CanFestival_mutex, TM_INFINITE); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
void LeaveMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
	rt_mutex_unlock(&CanFestival_mutex);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
void timerloop_task_proc(void *arg)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
	int ret;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
	do{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
		do{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
			last_occured_alarm = last_alarm_set;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
			EnterMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
			TimeDispatch();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
			LeaveMutex();
33
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 32
diff changeset
    55
			while ((ret = rt_task_sleep_until(last_alarm_set)) == -EINTR);
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 32
diff changeset
    56
		}while (ret == 0);
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 32
diff changeset
    57
	}while (!stop_timer);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
	printf("End of TimerLoop, code %d\n",ret);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    61
void StartTimerLoop(TimerCallback_t init_callback)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
	int ret;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
	stop_timer = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
	ret = rt_alarm_create( &timerloop_alarm, NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
		printf("Failed to create timerloop_alarm, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
		goto error;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
	//create timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
	ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
		printf("Failed to create timerloop_task, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
		return;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
 	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
	getElapsedTime();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
	last_alarm_set = last_time_read;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
	last_occured_alarm = last_alarm_set;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
	SetAlarm(NULL, 0, init_callback, 0, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
	// start timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
	ret = rt_task_start(&timerloop_task,&timerloop_task_proc,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
		printf("Failed to start timerloop_task, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
		goto error;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
	
33
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 32
diff changeset
    90
	return;
c767eabbaaac Fixed some bugs with Xenomai and CAN/peak
etisserant
parents: 32
diff changeset
    91
	
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
error:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
	cleanup_all();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
void ReceiveLoop_task_proc(void* arg)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
	canReceiveLoop((CAN_HANDLE)arg);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE *ReceiveLoop_task)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
	int ret;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
	static int id = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
	char taskname[64];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
	sprintf(taskname,"canloop%d",id);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
	id++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
	//create timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
	ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
		printf("Failed to create ReceiveLoop_task number %d, code %d\n", id, errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
		return;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
	// start timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
	ret = rt_task_start(ReceiveLoop_task,&ReceiveLoop_task_proc,(void*)fd0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
		printf("Failed to start ReceiveLoop_task number %d, code %d\n", id, errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
		return;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 34
diff changeset
   123
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
{
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 34
diff changeset
   125
	rt_task_delete(Thread);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
}
34
3e24a4d68325 Fixed Xeno crash when no more alarm are set...
etisserant
parents: 33
diff changeset
   127
3e24a4d68325 Fixed Xeno crash when no more alarm are set...
etisserant
parents: 33
diff changeset
   128
//#define max(a,b) a>b?a:b
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
{	
34
3e24a4d68325 Fixed Xeno crash when no more alarm are set...
etisserant
parents: 33
diff changeset
   131
	last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
	rt_task_unblock(&timerloop_task);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
	last_time_read = rt_timer_ticks2ns(rt_timer_read());
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
	return last_time_read - last_occured_alarm;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
}