Convert to new Doxygen style.
[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.5  2006/07/19 12:56:26  bernie
18  *#* Convert to new Doxygen style.
19  *#*
20  *#* Revision 1.4  2006/05/28 12:17:57  bernie
21  *#* Drop almost all the Qt3 cruft.
22  *#*
23  *#* Revision 1.3  2006/02/21 21:28:02  bernie
24  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
25  *#*
26  *#* Revision 1.2  2006/02/20 02:01:35  bernie
27  *#* Port to Qt 4.1.
28  *#*
29  *#* Revision 1.1  2005/11/27 03:06:36  bernie
30  *#* Qt timer emulation.
31  *#*
32  *#*/
33
34 #include <cfg/compiler.h> /* hptime.t */
35
36 // Qt headers
37 #include <QtCore/QDateTime>
38 #include <QtCore/QTimer>
39
40
41 // The user interrupt server routine
42 void timer_isr(void);
43
44
45 /**
46  * Singleton class for Qt-based hardware timer emulation.
47  */
48 class EmulTimer : public QObject
49 {
50 private:
51         Q_OBJECT;
52
53         /// System timer (counts ms since application startup)
54         QTime system_time;
55
56         /// The 1ms "hardware" tick counter.
57         QTimer timer;
58
59         /**
60          * We deliberately don't use RAII because the real hardware
61          * we're simulating needs to be initialized manually.
62          */
63         bool initialized;
64
65         /// Private ctor (singleton)
66         EmulTimer() : initialized(false) { }
67
68 public:
69         /// Return singleton instance
70         static EmulTimer &instance()
71         {
72                 static EmulTimer et;
73                 return et;
74         }
75
76         /// Start timer emulator.
77         void init()
78         {
79                 // Timer initialized twice?
80                 ASSERT(!initialized);
81
82                 // Record initial time
83                 system_time.start();
84
85                 // Activate timer interrupt
86                 timer.connect(&timer, SIGNAL(timeout()), this, SLOT(timerInterrupt()));
87                 timer.start(1000 / TIMER_TICKS_PER_SEC);
88
89                 initialized = true;
90         }
91
92         /// Return current time in high-precision format.
93         hptime_t hpread()
94         {
95                 ASSERT(initialized);
96                 return system_time.elapsed();
97         }
98
99 public slots:
100         void timerInterrupt(void)
101         {
102                 // Just call user interrupt server, timer restarts automatically.
103                 timer_isr();
104         }
105
106 };
107
108 #include "timer_qt_moc.cpp"
109
110
111 /// HW dependent timer initialization.
112 extern "C" static void timer_hw_init(void)
113 {
114         // Kick EmulTimer initialization
115         EmulTimer::instance().init();
116 }
117
118 extern "C" INLINE hptime_t timer_hw_hpread(void)
119 {
120         return EmulTimer::instance().hpread();
121 }
122