drivers/timers_xeno/timers_xeno.c
author oremeq
Sat, 20 May 2006 00:16:29 +0200
changeset 21 8737e6224393
parent 0 4472ee7c6c3e
child 32 8afa33692372
permissions -rw-r--r--
Some documentation correction
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
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
void stopTimer(void)
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();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
		}while ((ret =  rt_task_sleep_until(last_alarm_set)) == 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
	}while (ret == -EINTR && !stop_timer );
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
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
void TimerLoop(TimerCallback_t init_callback)
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;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
	ret = rt_alarm_create( &timerloop_alarm, NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
		printf("Failed to create timerloop_alarm, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
		goto error;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
	//create timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
	ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
		printf("Failed to create timerloop_task, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
		return;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
 	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
	getElapsedTime();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
	last_alarm_set = last_time_read;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
	last_occured_alarm = last_alarm_set;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
	SetAlarm(NULL, 0, init_callback, 0, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
	// start timerloop_task
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
	ret = rt_task_start(&timerloop_task,&timerloop_task_proc,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
	if (ret) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
		printf("Failed to start timerloop_task, code %d\n",errno);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
		goto error;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
	// At first, TimeDispatch will call init_callback.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
	pause();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
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
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
void WaitReceiveTaskEnd(TASK_HANDLE Thread)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
	rt_task_delete(&Thread);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
#define max(a,b) a>b?a:b
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
{	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
	last_alarm_set = last_time_read + value;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
	rt_task_unblock(&timerloop_task);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
	last_time_read = rt_timer_ticks2ns(rt_timer_read());
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
	return last_time_read - last_occured_alarm;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
}