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