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  #ifdef TS5_LINUX
176 
177 
178  #endif
179 
180  atexit(ts5_uninstall_timer);
181  }
182 }
183 
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Uninstall the timer subsystem.
187 ///
188 /// This function is called automatically at the end of the program.
189 ////////////////////////////////////////////////////////////////////////////////
191 {
193 
194  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 timer\n");
195 
196  al_flush_event_queue(_ts5_data.timer.response_queue);
197  al_flush_event_queue(_ts5_data.timer.trigger_queue);
198  al_flush_event_queue(_ts5_data.timer.trigger_log);
199 
200  al_destroy_event_queue(_ts5_data.timer.response_queue);
201  al_destroy_event_queue(_ts5_data.timer.trigger_queue);
202  al_destroy_event_queue(_ts5_data.timer.trigger_log);
203 
205  }
206 }
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Install the realtime clock.
211 ///
212 /// \param calling_function Name the function that calls
213 /// for this check or installation.
214 ///
215 /// This function is called automatically if necessary.
216 ////////////////////////////////////////////////////////////////////////////////
217 void ts5_install_realtime_clock(char *calling_function)
218 {
220  ts5_install_tscope5(calling_function);
221  }
222 
224  ts5_install_timer(calling_function);
225  }
226 
228 
230  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 realtime clock\n",
231  calling_function);
232 
233  #ifdef TS5_LINUX
234  if (geteuid() != 0) {
235  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
236  "only root can open the realtime clock");
237  }
238 
239 
240  if ((_ts5_data.timer.realtime_clock
241  = open("/dev/rtc", O_RDONLY)) < 0) {
242  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
243  "error opening realtime clock");
244  }
245 
246  if (ioctl(_ts5_data.timer.realtime_clock, RTC_IRQP_SET, 8192) < 0) {
247  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
248  "error setting realtime clock frequency");
249  }
250 
251  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_ON, 0) < 0) {
252  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
253  "error setting realtime clock interrupts");
254  }
255 
257 
258  #endif
259 
260  }
261 }
262 
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Uninstall the realtime clock.
266 ///
267 /// This function is called automatically at the end of the program.
268 ////////////////////////////////////////////////////////////////////////////////
270 {
272 
273  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 realtime clock\n");
274 
275  #ifdef TS5_LINUX
276  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_OFF, 0) < 0) {
277  ts5_fatal("%s: %s\n", "ts5_uninstall_realtime_clock",
278  "error removing realtime clock interrupts");
279  }
280 
281  close(_ts5_data.timer.realtime_clock);
282 
283  #endif
284 
286  }
287 }
288 
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// Give up processor time to allow other processes to run.
292 ////////////////////////////////////////////////////////////////////////////////
294 {
295  #ifdef TS5_WINDOWS
296  Sleep(0);
297 
298  #elif defined TS5_LINUX
300  unsigned long timedata;
301  if (read(_ts5_data.timer.realtime_clock, &timedata,
302  sizeof(unsigned long)) < 0) {
303  ts5_fatal("%s: %s\n", "ts5_realtime_clock_nap",
304  "error reading realtime clock");
305  }
306  }
307 
308  #endif
309 
310 }
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:529
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.