summaryrefslogtreecommitdiffstats
path: root/source/ct-Bot/include/timer.h
blob: 446f51ef1136a9b767633b4ee43d24610e5a067e (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
/*
 * c't-Sim - Robotersimulator fuer den 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.h
 * @brief 	Timer und Zaehler
 * @author 	Benjamin Benz (bbe@heise.de)
 * @date 	26.12.05
*/

#ifndef TIMER_H_
#define TIMER_H_

#include "ct-Bot.h"

/*!
 * Makros zur Umrechnung von Ticks in ms und zurueck
 * (ms / ticks evtl. nach uint32 casten, fuer grosse Werte)
 */
#define TICKS_TO_MS(ticks)	((ticks)*(TIMER_STEPS/8)/(1000/8))
#define MS_TO_TICKS(ms)		((ms)*(1000/8)/(TIMER_STEPS/8))

#ifdef TIME_AVAILABLE		
	/*!
	 * Diese Funktion liefert den Millisekundenanteil der Systemzeit zurueck.
	 * @return uint16
	 */
	uint16 timer_get_ms(void);
	
	/*!
	 * Diese Funktion liefert den Sekundenanteil der Systemzeit zurueck.
	 * @return uint16
	 */
	uint16 timer_get_s(void);

	/*!
	 *  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);
#endif // TIME_AVAILABLE
	
#ifdef PC
	/*! 
	 *  Funktion, die die TickCounts um die vergangene Simulzeit erhoeht
	 */
	void system_time_isr(void);

	/*!< liefert Ticks seit Systemstart [176 us] */
	inline uint16 timer_get_tickCount16(void);
	inline uint32 timer_get_tickCount32(void);	
	
	#define TIMER_GET_TICKCOUNT_8  (uint8)timer_get_tickCount16()
	#define TIMER_GET_TICKCOUNT_16 timer_get_tickCount16()
	#define TIMER_GET_TICKCOUNT_32 timer_get_tickCount32()	
#else
	typedef union{
		uint32 u32;
		uint16 u16;
		uint8 u8;
	} tickCount_t;
	extern volatile tickCount_t tickCount;
	
	#define TIMER_GET_TICKCOUNT_8  tickCount.u8
	#define TIMER_GET_TICKCOUNT_16 tickCount.u16
	#define TIMER_GET_TICKCOUNT_32 tickCount.u32	
#endif

// Die Werte fuer TIMER_X_CLOCK sind Angaben in Hz

/*!
 * Frequenz von Timer 2 in Hz 
 */
#define TIMER_2_CLOCK	5619	// Derzeit genutzt fuer RC5-Dekodierung

/*!
 * Mikrosekunden, die zwischen zwei Timer-Aufrufen liegen 
 */
//	#define TIMER_STEPS	(1 000 000/TIMER_2_CLOCK)
	#define TIMER_STEPS 176

/*!
 * Initialisiert Timer 2 und startet ihn 
 */
void timer_2_init(void);
#endif