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.parport_is_trigger_device) {
80 
81  int i;
82  for (i=0; i<_ts5_status.timer.num_parports; i++) {
83 
84  if (_ts5_status.timer.parport[i].is_trigger_input_device) {
85  found=1;
86  }
87  }
88  }
89 
90  if (_ts5_status.timer.serialport_is_trigger_device) {
91 
92  int i;
93  for (i=0; i<_ts5_status.timer.num_serialports; i++) {
94 
95  if (_ts5_status.timer.serialport[i].is_trigger_input_device) {
96  found=1;
97  }
98  }
99  }
100 
101  if (!found) {
102  ts5_fatal("%s: there are no trigger input devices\n", calling_function);
103  }
104 }
105 
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Do some checks at the start of each timer function.
109 ///
110 /// \param calling_function Name the function that calls
111 /// for this check or installation.
112 ///
113 /// Checks whether the realtime clock is installed.
114 /// If not, the realtime clock is installed.
115 ////////////////////////////////////////////////////////////////////////////////
116 void ts5_check_timer4(char *calling_function)
117 {
118  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_timer4\n", calling_function);
119 
121  ts5_install_realtime_clock(calling_function);
122  }
123 }
124 
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Install the timer subsystem.
128 ///
129 /// \param calling_function Name the function that calls
130 /// for this check or installation.
131 ///
132 /// This function is called automatically if necessary.
133 ////////////////////////////////////////////////////////////////////////////////
134 void ts5_install_timer(char *calling_function)
135 {
137  ts5_install_tscope5(calling_function);
138  }
139 
141 
143  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 timer\n",
144  calling_function);
145 
146  _ts5_data.timer.response_queue = al_create_event_queue();
147 
148  if (_ts5_data.timer.response_queue==NULL) {
149  ts5_fatal("%s: could not install timer\n", calling_function);
150  }
151 
152  _ts5_data.timer.trigger_queue = al_create_event_queue();
153 
154  if (_ts5_data.timer.trigger_queue==NULL) {
155  ts5_fatal("%s: could not install timer\n", calling_function);
156  }
157 
158  _ts5_data.timer.trigger_log = al_create_event_queue();
159 
160  if (_ts5_data.timer.trigger_log==NULL) {
161  ts5_fatal("%s: could not install timer\n", calling_function);
162  }
163 
164  #ifdef TS5_LINUX
165 
166 
167  #endif
168 
169  atexit(ts5_uninstall_timer);
170  }
171 }
172 
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Uninstall the timer subsystem.
176 ///
177 /// This function is called automatically at the end of the program.
178 ////////////////////////////////////////////////////////////////////////////////
180 {
182 
183  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 timer\n");
184 
185  al_flush_event_queue(_ts5_data.timer.response_queue);
186  al_flush_event_queue(_ts5_data.timer.trigger_queue);
187  al_flush_event_queue(_ts5_data.timer.trigger_log);
188 
189  al_destroy_event_queue(_ts5_data.timer.response_queue);
190  al_destroy_event_queue(_ts5_data.timer.trigger_queue);
191  al_destroy_event_queue(_ts5_data.timer.trigger_log);
192 
194  }
195 }
196 
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Install the realtime clock.
200 ///
201 /// \param calling_function Name the function that calls
202 /// for this check or installation.
203 ///
204 /// This function is called automatically if necessary.
205 ////////////////////////////////////////////////////////////////////////////////
206 void ts5_install_realtime_clock(char *calling_function)
207 {
209  ts5_install_tscope5(calling_function);
210  }
211 
213  ts5_install_timer(calling_function);
214  }
215 
217 
219  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 realtime clock\n",
220  calling_function);
221 
222  #ifdef TS5_LINUX
223  if (geteuid() != 0) {
224  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
225  "only root can open the realtime clock");
226  }
227 
228 
229  if ((_ts5_data.timer.realtime_clock
230  = open("/dev/rtc", O_RDONLY)) < 0) {
231  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
232  "error opening realtime clock");
233  }
234 
235  if (ioctl(_ts5_data.timer.realtime_clock, RTC_IRQP_SET, 8192) < 0) {
236  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
237  "error setting realtime clock frequency");
238  }
239 
240  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_ON, 0) < 0) {
241  ts5_fatal("%s: %s\n", "ts5_install_realtime_clock",
242  "error setting realtime clock interrupts");
243  }
244 
246 
247  #endif
248 
249  }
250 }
251 
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /// Uninstall the realtime clock.
255 ///
256 /// This function is called automatically at the end of the program.
257 ////////////////////////////////////////////////////////////////////////////////
259 {
261 
262  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 realtime clock\n");
263 
264  #ifdef TS5_LINUX
265  if (ioctl(_ts5_data.timer.realtime_clock, RTC_PIE_OFF, 0) < 0) {
266  ts5_fatal("%s: %s\n", "ts5_uninstall_realtime_clock",
267  "error removing realtime clock interrupts");
268  }
269 
270  close(_ts5_data.timer.realtime_clock);
271 
272  #endif
273 
275  }
276 }
277 
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Give up processor time to allow other processes to run.
281 ////////////////////////////////////////////////////////////////////////////////
283 {
284  #ifdef TS5_WINDOWS
285  Sleep(0);
286 
287  #elif defined TS5_LINUX
289  unsigned long timedata;
290  if (read(_ts5_data.timer.realtime_clock, &timedata,
291  sizeof(unsigned long)) < 0) {
292  ts5_fatal("%s: %s\n", "ts5_realtime_clock_nap",
293  "error reading realtime clock");
294  }
295  }
296 
297  #endif
298 
299 }