Sistema l'errore da me commesso in fase di conversione...
[bertos.git] / drv / timer_qt.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Low-level timer module for Qt emulator (implementation).
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.6  2006/09/13 18:25:22  bernie
18  *#* Fix GCC error.
19  *#*
20  *#* Revision 1.5  2006/07/19 12:56:26  bernie
21  *#* Convert to new Doxygen style.
22  *#*
23  *#* Revision 1.4  2006/05/28 12:17:57  bernie
24  *#* Drop almost all the Qt3 cruft.
25  *#*
26  *#* Revision 1.3  2006/02/21 21:28:02  bernie
27  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
28  *#*
29  *#* Revision 1.2  2006/02/20 02:01:35  bernie
30  *#* Port to Qt 4.1.
31  *#*
32  *#* Revision 1.1  2005/11/27 03:06:36  bernie
33  *#* Qt timer emulation.
34  *#*
35  *#*/
36
37 #include <cfg/compiler.h> /* hptime.t */
38
39 // Qt headers
40 #include <QtCore/QDateTime>
41 #include <QtCore/QTimer>
42
43
44 // The user interrupt server routine
45 void timer_isr(void);
46
47
48 /**
49  * Singleton class for Qt-based hardware timer emulation.
50  */
51 class EmulTimer : public QObject
52 {
53 private:
54         Q_OBJECT;
55
56         /// System timer (counts ms since application startup)
57         QTime system_time;
58
59         /// The 1ms "hardware" tick counter.
60         QTimer timer;
61
62         /**
63          * We deliberately don't use RAII because the real hardware
64          * we're simulating needs to be initialized manually.
65          */
66         bool initialized;
67
68         /// Private ctor (singleton)
69         EmulTimer() : initialized(false) { }
70
71 public:
72         /// Return singleton instance
73         static EmulTimer &instance()
74         {
75                 static EmulTimer et;
76                 return et;
77         }
78
79         /// Start timer emulator.
80         void init()
81         {
82                 // Timer initialized twice?
83                 ASSERT(!initialized);
84
85                 // Record initial time
86                 system_time.start();
87
88                 // Activate timer interrupt
89                 timer.connect(&timer, SIGNAL(timeout()), this, SLOT(timerInterrupt()));
90                 timer.start(1000 / TIMER_TICKS_PER_SEC);
91
92                 initialized = true;
93         }
94
95         /// Return current time in high-precision format.
96         hptime_t hpread()
97         {
98                 ASSERT(initialized);
99                 return system_time.elapsed();
100         }
101
102 public slots:
103         void timerInterrupt(void)
104         {
105                 // Just call user interrupt server, timer restarts automatically.
106                 timer_isr();
107         }
108
109 };
110
111 #include "timer_qt_moc.cpp"
112
113
114 /// HW dependent timer initialization.
115 static void timer_hw_init(void)
116 {
117         // Kick EmulTimer initialization
118         EmulTimer::instance().init();
119 }
120
121 INLINE hptime_t timer_hw_hpread(void)
122 {
123         return EmulTimer::instance().hpread();
124 }
125