drivers/timers_kernel/timers_kernel.c
author greg
Wed, 15 Jul 2009 09:36:36 +0200
changeset 556 8296acd119a9
parent 467 40efa79d27dd
child 629 b9274b595650
permissions -rw-r--r--
Rewrite win32 code (C only)
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     1
/*
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     3
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     5
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     7
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    12
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    17
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    21
*/
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    22
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    23
#include <linux/spinlock.h>
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    24
#include <linux/jiffies.h>
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    25
#include <linux/timer.h>
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    26
#include <linux/errno.h>
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    27
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    28
#include "timer.h"
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    29
#include "applicfg.h"
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    30
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    31
static spinlock_t lock = SPIN_LOCK_UNLOCKED;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    32
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    33
static struct timer_list timer;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    34
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    35
static TIMEVAL last_time_read,
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    36
	last_occured_alarm,
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    37
	last_alarm_set;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    38
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    39
void TimerInit(void)
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    40
{
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    41
	/* only used in realtime apps */
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    42
}
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    43
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    44
void TimerCleanup(void)
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    45
{
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    46
	/* only used in realtime apps */
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    47
}
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    48
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    49
void EnterMutex(void)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    50
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    51
	spin_lock (&lock);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    52
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    53
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    54
void LeaveMutex(void)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    55
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    56
	spin_unlock (&lock);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    57
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    58
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    59
void timer_notify(unsigned long data)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    60
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    61
	last_occured_alarm = last_alarm_set;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    62
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    63
	EnterMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    64
	TimeDispatch();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    65
	LeaveMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    66
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    67
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    68
void StartTimerLoop(TimerCallback_t init_callback)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    69
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    70
	getElapsedTime();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    71
	last_alarm_set = last_time_read;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    72
	last_occured_alarm = last_alarm_set;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    73
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    74
	init_timer(&timer);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    75
	timer.function = timer_notify;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    76
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    77
	EnterMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    78
	// At first, TimeDispatch will call init_callback.
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    79
	SetAlarm(NULL, 0, init_callback, 0, 0);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    80
	LeaveMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    81
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    82
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    83
void StopTimerLoop(TimerCallback_t exitfunction)
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    84
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    85
	EnterMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    86
	del_timer (&timer);
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
    87
	exitfunction(NULL,0);
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    88
	LeaveMutex();
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    89
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    90
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    91
void setTimer(TIMEVAL value)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    92
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    93
	if (value == TIMEVAL_MAX)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    94
		return;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    95
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    96
	last_alarm_set = last_time_read + value;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    97
	mod_timer (&timer, last_alarm_set);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    98
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
    99
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   100
TIMEVAL getElapsedTime(void)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   101
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   102
	last_time_read = jiffies;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   103
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   104
	return (long)last_time_read - (long)last_occured_alarm;
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   105
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   106
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   107
void CreateReceiveTask(CAN_PORT port, TASK_HANDLE *Thread, void* ReceiveLoopPtr)
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   108
{
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   109
	*Thread = kthread_run(ReceiveLoopPtr, port, "canReceiveLoop");
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   110
}
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   111
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
   112
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   113
{
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 391
diff changeset
   114
	force_sig (SIGTERM, *Thread);
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents:
diff changeset
   115
}