Tscope5
timer_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file timer_internal.c
11 /// Definitions of internal timer functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 #include "../include/tscope5/timer_internal.h"
15 #include "../include/tscope5/system_internal.h"
16 
17 #ifdef TS5_LINUX
18  #include <sys/ioctl.h>
19  #include <linux/rtc.h>
20 
21 #endif
22 
23 /// Is the timer subsystem installed?
25 
26 /// Is the realtime clock installed
28 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Do some checks at the start of each timer function.
32 ///
33 /// \param calling_function Name the function that calls
34 /// for this check or installation.
35 ///
36 /// Checks whether the timer is installed. If not, the timer is installed.
37 ////////////////////////////////////////////////////////////////////////////////
38 void ts5_check_timer(char *calling_function)
39 {
40  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_timer\n", calling_function);
42  ts5_install_timer(calling_function);
43  }
44 }
45 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Do some checks at the start of each timer function.
49 ///
50 /// \param calling_function Name the function that calls
51 /// for this check or installation.
52 ///
53 /// Checks whether there are active response buttons. Aborts if necessary.
54 ////////////////////////////////////////////////////////////////////////////////
55 void ts5_check_timer2(char *calling_function)
56 {
57  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_timer2\n", calling_function);
58  if (_ts5_status.timer.num_active_buttons<1) {
59  ts5_fatal("%s: there are no active response buttons\n",
60  calling_function);
61  }
62 }
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Do some checks at the start of each timer function.
67 ///
68 /// \param calling_function Name the function that calls
69 /// for this check or installation.
70 ///
71 /// Checks whether there are active trigger devices. Aborts if necessary.
72 ////////////////////////////////////////////////////////////////////////////////
73 void ts5_check_timer3(char *calling_function)
74 {
75  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_timer3\n", calling_function);
76 
77  int found=0;
78 
79  if (_ts5_status.timer.cedrusbox_is_trigger_device) {
80 
81  int i;
82  for (i=0; i<_ts5_status.timer.num_cedrusboxes; i++) {
83 
84  if (_ts5_status.timer.cedrusbox[i].is_trigger_input_device) {
85  found=1;
86  }
87  }
88  }
89 
90  if (_ts5_status.timer.parport_is_trigger_device) {
91 
92  int i;
93  for (i=0; i<_ts5_status.timer.num_parports; i++) {
94 
95  if (_ts5_status.timer.parport[i].is_trigger_input_device) {
96  found=1;
97  }
98  }
99  }
100 
101  if (_ts5_status.timer.serialport_is_trigger_device) {
102 
103  int i;
104  for (i=0; i<_ts5_status.timer.num_serialports; i++) {
105 
106  if (_ts5_status.timer.serialport[i].is_trigger_input_device) {
107  found=1;
108  }
109  }
110  }
111 
112  if (!found) {
113  ts5_fatal("%s: there are no trigger input devices\n", calling_function);
114  }
115 }
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Do some checks at the start of each timer function.
120 ///
121 /// \param calling_function Name the function that calls
122 /// for this check or installation.
123 ///
124 /// Checks whether the realtime clock is installed.
125 /// If not, the realtime clock is installed.
126 ////////////////////////////////////////////////////////////////////////////////
127 void ts5_check_timer4(char *calling_function)
128 {
129  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_timer4\n", calling_function);
130 
132  ts5_install_realtime_clock(calling_function);
133  }
134 }
135 
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Install the timer subsystem.
139 ///
140 /// \param calling_function Name the function that calls
141 /// for this check or installation.
142 ///
143 /// This function is called automatically if necessary.
144 ////////////////////////////////////////////////////////////////////////////////
145 void ts5_install_timer(char *calling_function)
146 {
148  ts5_install_tscope5(calling_function);
149  }
150 
152 
154  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 timer\n",
155  calling_function);
156 
157  _ts5_data.timer.response_queue = al_create_event_queue();
158 
159  if (_ts5_data.timer.response_queue==NULL) {
160  ts5_fatal("%s: could not install timer\n", calling_function);
161  }
162 
163  _ts5_data.timer.trigger_queue = al_create_event_queue();
164 
165  if (_ts5_data.timer.trigger_queue==NULL) {
166  ts5_fatal("%s: could not install timer\n", calling_function);
167  }
168 
169  _ts5_data.timer.trigger_log = al_create_event_queue();
170 
171  if (_ts5_data.timer.trigger_log==NULL) {
172  ts5_fatal("%s: could not install timer\n", calling_function);
173  }
174 
175  atexit(ts5_uninstall_timer);
176  }
177 }
178 
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Uninstall the timer subsystem.
182 ///
183 /// This function is called automatically at the end of the program.
184 ////////////////////////////////////////////////////////////////////////////////
186 {
188 
189  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 timer\n");
190 
191  al_flush_event_queue(_ts5_data.timer.response_queue);
192  al_flush_event_queue(_ts5_data.timer.trigger_queue);
193  al_flush_event_queue(_ts5_data.timer.trigger_log);
194 
195  al_destroy_event_queue(_ts5_data.timer.response_queue);
196  al_destroy_event_queue(_ts5_data.timer.trigger_queue);
197  al_destroy_event_queue(_ts5_data.timer.trigger_log);
198 
200  }
201 }
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Install the realtime clock.
206 ///
207 /// \param calling_function Name the function that calls
208 /// for this check or installation.
209 ///
210 /// This function is called automatically if necessary.
211 ////////////////////////////////////////////////////////////////////////////////
212 void ts5_install_realtime_clock(char *calling_function)
213 {
215  ts5_install_tscope5(calling_function);
216  }
217 
219  ts5_install_timer(calling_function);
220  }
221 
223 
225  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 realtime clock\n",
226  calling_function);
227 
228  #ifdef TS5_LINUX
229  if (geteuid() != 0) {
230  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
231  "only root can open the realtime clock");
232  }
233 
234 
235  if ((_ts5_data.timer.realtime_clock
236  = open("/dev/rtc", O_RDONLY)) < 0) {
237  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
238  "error opening realtime clock");
239  }
240 
241  if (ioctl(_ts5_data.timer.realtime_clock, RTC_IRQP_SET, 8192) < 0) {
242  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
243  "error setting realtime clock frequency");
244  }
245 
246  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_ON, 0) < 0) {
247  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
248  "error setting realtime clock interrupts");
249  }
250 
252 
253  #endif
254 
255  }
256 }
257 
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 /// Uninstall the realtime clock.
261 ///
262 /// This function is called automatically at the end of the program.
263 ////////////////////////////////////////////////////////////////////////////////
265 {
267 
268  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 realtime clock\n");
269 
270  #ifdef TS5_LINUX
271  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_OFF, 0) < 0) {
272  ts5_fatal("%s: %s\n", "ts5_uninstall_realtime_clock",
273  "error removing realtime clock interrupts");
274  }
275 
276  close(_ts5_data.timer.realtime_clock);
277 
278  #endif
279 
281  }
282 }
283 
284 
285 ////////////////////////////////////////////////////////////////////////////////
286 /// Give up processor time to allow other processes to run.
287 ////////////////////////////////////////////////////////////////////////////////
289 {
290  #ifdef TS5_WINDOWS
291  Sleep(0);
292 
293  #elif defined TS5_LINUX
295  unsigned long timedata;
296  if (read(_ts5_data.timer.realtime_clock, &timedata,
297  sizeof(unsigned long)) < 0) {
298  ts5_fatal("%s: %s\n", "ts5_realtime_clock_nap",
299  "error reading realtime clock");
300  }
301  }
302 
303  #endif
304 
305 }
int _ts5_is_tscope5_installed
Is Tscope5 installed?
int _ts5_is_timer_installed
Is the timer subsystem installed?
int _ts5_is_realtime_clock_installed
Is the realtime clock installed.
void ts5_check_timer3(char *calling_function)
Do some checks at the start of each timer function.
void ts5_realtime_clock_nap()
Give up processor time to allow other processes to run.
void ts5_check_timer4(char *calling_function)
Do some checks at the start of each timer function.
void ts5_uninstall_realtime_clock()
Uninstall the realtime clock.
void ts5_install_timer(char *calling_function)
Install the timer subsystem.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_check_timer2(char *calling_function)
Do some checks at the start of each timer function.
void ts5_install_tscope5(char *calling_function)
Install Tscope5.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533
void ts5_uninstall_timer()
Uninstall the timer subsystem.
void ts5_install_realtime_clock(char *calling_function)
Install the realtime clock.
void ts5_check_timer(char *calling_function)
Do some checks at the start of each timer function.