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