drivers/timers_xeno/timers_xeno.c
author etisserant
Mon, 02 Jul 2007 18:22:58 +0200
changeset 236 905677ed00f3
parent 170 d069631e9ca9
child 454 bc000083297a
permissions -rw-r--r--
Full preliminary implementation of TPDO transmit type:
- SYNC (N) (1-240)
- RTR only + SYNC (252)
- RTR only (253)
- EVENT, with timer and inhibit time (254 and 255)

User app have to call sendPDOevent(d) to eventually signal mapped data changes.
Callbacks added to 0x140N, TPDO comm parameters for on the fly timers values change.
TestMasterSlave updated.
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
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    93
void CreateReceiveTask(CAN_PORT fd0, TASK_HANDLE *ReceiveLoop_task, void* ReceiveLoop_task_proc)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
	int ret;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
	static int id = 0;
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents: 35
diff changeset
    97
	char taskname[32];
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents: 35
diff changeset
    98
	snprintf(taskname, sizeof(taskname), "canloop%d-%d", id, getpid());
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
	id++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents: 35
diff changeset
   101
	mlockall(MCL_CURRENT | MCL_FUTURE);
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents: 35
diff changeset
   102
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
	//create timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
	ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
		printf("Failed to create ReceiveLoop_task number %d, code %d\n", id, errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
		return;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
	// start timerloop_task
170
d069631e9ca9 Fixed segfault with Xenomai timer_xeno.c
etisserant
parents: 145
diff changeset
   110
	ret = rt_task_start(ReceiveLoop_task,ReceiveLoop_task_proc,(void*)fd0);
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 start 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
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 34
diff changeset
   117
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
{
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 34
diff changeset
   119
	rt_task_delete(Thread);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
}
34
3e24a4d68325 Fixed Xeno crash when no more alarm are set...
etisserant
parents: 33
diff changeset
   121
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
void setTimer(TIMEVAL value)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents: 35
diff changeset
   123
{
34
3e24a4d68325 Fixed Xeno crash when no more alarm are set...
etisserant
parents: 33
diff changeset
   124
	last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
	rt_task_unblock(&timerloop_task);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
	last_time_read = rt_timer_ticks2ns(rt_timer_read());
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
	return last_time_read - last_occured_alarm;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
}