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