summaryrefslogtreecommitdiffstats
path: root/source/ct-Bot/timer.c
blob: c7c3208cc95b88f946b88789b70b7753f1dd995d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * c't-Bot
 * 
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your
 * option) any later version. 
 * This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 * PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307, USA.
 * 
 */

/*! @file 	timer.c
 * @brief 	Zeitmanagement
 * @author 	Benjamin Benz (bbe@heise.de)
 * @date 	26.12.05
*/

#include "ct-Bot.h"
#include "timer.h"

#ifdef PC
	#include <pthread.h>
	#include "sensor.h"
#endif	/* PC */

#ifdef PC
	/*! Schuetzt die Zeitsynchronisation */
	#define LOCK()		pthread_mutex_lock(&timer_mutex);
	/*! Hebt den Schutz fuer die Zeitsynchronisation wieder auf */
	#define UNLOCK()	pthread_mutex_unlock(&timer_mutex);
#else
	/*! Schuetzt die Zeitsynchronisation */
	#define LOCK()		/* ISR */
	/*! Hebt den Schutz fuer die Zeitsynchronisation wieder auf */
	#define UNLOCK()	/* ISR */
#endif	/* PC */

#ifdef PC
	/*! Schuetzt die Zeitvariablen */
	static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif	/* PC */

#ifdef MCU
	volatile tickCount_t tickCount;		/*!< ein Tick alle 176 us */
#else
	float tickCount=0;
#endif 

#ifdef PC
	inline uint16 timer_get_tickCount16(void){
		uint16 temp;
		LOCK();
		temp = tickCount;
		UNLOCK();
		return temp;
	}
	
	inline uint32 timer_get_tickCount32(void){
		uint32 temp;
		LOCK();
		temp = tickCount;	
		UNLOCK();
		return temp;
	}
#endif

#ifdef TIME_AVAILABLE
	/*!
	 * Diese Funktion liefert den Millisekundenanteil der Systemzeit zurueck.
	 * @return uint16
	 */
	uint16 timer_get_ms(void){
		return ((TIMER_GET_TICKCOUNT_32*(TIMER_STEPS/8))/(1000/8))%1000;
	}
	
	/*!
	 * Diese Funktion liefert den Sekundenanteil der Systemzeit zurueck.
	 * @return uint16
	 */
	uint16 timer_get_s(void){
		return TIMER_GET_TICKCOUNT_32*(TIMER_STEPS/16)/(1000000/16);
	}
	
	/*!
	 *  Liefert die Millisekunden zurueck, die seit old_s, old_ms verstrichen sind 
	 * @param old_s alter Sekundenstand
	 * @param old_ms alter Millisekundenstand
	 */
	uint16 timer_get_ms_since(uint16 old_s, uint16 old_ms){
		return (timer_get_s()-old_s)*1000 + timer_get_ms()-old_ms;
	}
#endif // TIME_AVAILABLE

#ifdef PC
	/*! 
	 * Funktion, die die TickCounts um die vergangene Simulzeit erhoeht
	 */
	void system_time_isr(void){
		LOCK();
		/* TickCounter [176 us] erhoehen */
		static uint16 last_simultime=0;
		if (simultime < last_simultime) last_simultime -= 10000;	// der Sim setzt simultime alle 10s zurueck auf 0
		tickCount += MS_TO_TICKS((float)(simultime - last_simultime));
		last_simultime = simultime;
		UNLOCK();
	}
#endif