1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include "../comedidev.h"
54
55#include <asm/div64.h>
56
57#include "comedi_fc.h"
58#include <linux/timer.h>
59
60
61struct waveform_board {
62 const char *name;
63 int ai_chans;
64 int ai_bits;
65 int have_dio;
66};
67
68#define N_CHANS 8
69
70static const struct waveform_board waveform_boards[] = {
71 {
72 .name = "comedi_test",
73 .ai_chans = N_CHANS,
74 .ai_bits = 16,
75 .have_dio = 0,
76 },
77};
78
79#define thisboard ((const struct waveform_board *)dev->board_ptr)
80
81
82struct waveform_private {
83 struct timer_list timer;
84 struct timeval last;
85 unsigned int uvolt_amplitude;
86 unsigned long usec_period;
87 unsigned long usec_current;
88 unsigned long usec_remainder;
89 unsigned long ai_count;
90 unsigned int scan_period;
91 unsigned int convert_period;
92 unsigned timer_running:1;
93 unsigned int ao_loopbacks[N_CHANS];
94};
95#define devpriv ((struct waveform_private *)dev->private)
96
97static int waveform_attach(struct comedi_device *dev,
98 struct comedi_devconfig *it);
99static int waveform_detach(struct comedi_device *dev);
100static struct comedi_driver driver_waveform = {
101 .driver_name = "comedi_test",
102 .module = THIS_MODULE,
103 .attach = waveform_attach,
104 .detach = waveform_detach,
105 .board_name = &waveform_boards[0].name,
106 .offset = sizeof(struct waveform_board),
107 .num_names = ARRAY_SIZE(waveform_boards),
108};
109
110static int __init driver_waveform_init_module(void)
111{
112 return comedi_driver_register(&driver_waveform);
113}
114
115static void __exit driver_waveform_cleanup_module(void)
116{
117 comedi_driver_unregister(&driver_waveform);
118}
119
120module_init(driver_waveform_init_module);
121module_exit(driver_waveform_cleanup_module);
122
123static int waveform_ai_cmdtest(struct comedi_device *dev,
124 struct comedi_subdevice *s,
125 struct comedi_cmd *cmd);
126static int waveform_ai_cmd(struct comedi_device *dev,
127 struct comedi_subdevice *s);
128static int waveform_ai_cancel(struct comedi_device *dev,
129 struct comedi_subdevice *s);
130static int waveform_ai_insn_read(struct comedi_device *dev,
131 struct comedi_subdevice *s,
132 struct comedi_insn *insn, unsigned int *data);
133static int waveform_ao_insn_write(struct comedi_device *dev,
134 struct comedi_subdevice *s,
135 struct comedi_insn *insn, unsigned int *data);
136static short fake_sawtooth(struct comedi_device *dev, unsigned int range,
137 unsigned long current_time);
138static short fake_squarewave(struct comedi_device *dev, unsigned int range,
139 unsigned long current_time);
140static short fake_flatline(struct comedi_device *dev, unsigned int range,
141 unsigned long current_time);
142static short fake_waveform(struct comedi_device *dev, unsigned int channel,
143 unsigned int range, unsigned long current_time);
144
145
146static const int nano_per_micro = 1000;
147
148
149static const struct comedi_lrange waveform_ai_ranges = {
150 2,
151 {
152 BIP_RANGE(10),
153 BIP_RANGE(5),
154 }
155};
156
157
158
159
160
161
162static void waveform_ai_interrupt(unsigned long arg)
163{
164 struct comedi_device *dev = (struct comedi_device *)arg;
165 struct comedi_async *async = dev->read_subdev->async;
166 struct comedi_cmd *cmd = &async->cmd;
167 unsigned int i, j;
168
169 unsigned long elapsed_time;
170 unsigned int num_scans;
171 struct timeval now;
172
173 do_gettimeofday(&now);
174
175 elapsed_time =
176 1000000 * (now.tv_sec - devpriv->last.tv_sec) + now.tv_usec -
177 devpriv->last.tv_usec;
178 devpriv->last = now;
179 num_scans =
180 (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
181 devpriv->usec_remainder =
182 (devpriv->usec_remainder + elapsed_time) % devpriv->scan_period;
183 async->events = 0;
184
185 for (i = 0; i < num_scans; i++) {
186 for (j = 0; j < cmd->chanlist_len; j++) {
187 cfc_write_to_buffer(dev->read_subdev,
188 fake_waveform(dev,
189 CR_CHAN(cmd->
190 chanlist[j]),
191 CR_RANGE(cmd->
192 chanlist[j]),
193 devpriv->
194 usec_current +
195 i *
196 devpriv->scan_period +
197 j *
198 devpriv->
199 convert_period));
200 }
201 devpriv->ai_count++;
202 if (cmd->stop_src == TRIG_COUNT
203 && devpriv->ai_count >= cmd->stop_arg) {
204 async->events |= COMEDI_CB_EOA;
205 break;
206 }
207 }
208
209 devpriv->usec_current += elapsed_time;
210 devpriv->usec_current %= devpriv->usec_period;
211
212 if ((async->events & COMEDI_CB_EOA) == 0 && devpriv->timer_running)
213 mod_timer(&devpriv->timer, jiffies + 1);
214 else
215 del_timer(&devpriv->timer);
216
217 comedi_event(dev, dev->read_subdev);
218}
219
220static int waveform_attach(struct comedi_device *dev,
221 struct comedi_devconfig *it)
222{
223 struct comedi_subdevice *s;
224 int amplitude = it->options[0];
225 int period = it->options[1];
226 int i;
227
228 dev->board_name = thisboard->name;
229
230 if (alloc_private(dev, sizeof(struct waveform_private)) < 0)
231 return -ENOMEM;
232
233
234 if (amplitude <= 0)
235 amplitude = 1000000;
236 if (period <= 0)
237 period = 100000;
238
239 devpriv->uvolt_amplitude = amplitude;
240 devpriv->usec_period = period;
241
242 dev->n_subdevices = 2;
243 if (alloc_subdevices(dev, dev->n_subdevices) < 0)
244 return -ENOMEM;
245
246 s = dev->subdevices + 0;
247 dev->read_subdev = s;
248
249 s->type = COMEDI_SUBD_AI;
250 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
251 s->n_chan = thisboard->ai_chans;
252 s->maxdata = (1 << thisboard->ai_bits) - 1;
253 s->range_table = &waveform_ai_ranges;
254 s->len_chanlist = s->n_chan * 2;
255 s->insn_read = waveform_ai_insn_read;
256 s->do_cmd = waveform_ai_cmd;
257 s->do_cmdtest = waveform_ai_cmdtest;
258 s->cancel = waveform_ai_cancel;
259
260 s = dev->subdevices + 1;
261 dev->write_subdev = s;
262
263 s->type = COMEDI_SUBD_AO;
264 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND;
265 s->n_chan = thisboard->ai_chans;
266 s->maxdata = (1 << thisboard->ai_bits) - 1;
267 s->range_table = &waveform_ai_ranges;
268 s->len_chanlist = s->n_chan * 2;
269 s->insn_write = waveform_ao_insn_write;
270 s->do_cmd = NULL;
271 s->do_cmdtest = NULL;
272 s->cancel = NULL;
273
274
275 for (i = 0; i < s->n_chan; i++)
276 devpriv->ao_loopbacks[i] = s->maxdata / 2;
277
278 init_timer(&(devpriv->timer));
279 devpriv->timer.function = waveform_ai_interrupt;
280 devpriv->timer.data = (unsigned long)dev;
281
282 printk(KERN_INFO "comedi%d: comedi_test: "
283 "%i microvolt, %li microsecond waveform attached\n", dev->minor,
284 devpriv->uvolt_amplitude, devpriv->usec_period);
285 return 1;
286}
287
288static int waveform_detach(struct comedi_device *dev)
289{
290 printk("comedi%d: comedi_test: remove\n", dev->minor);
291
292 if (dev->private)
293 waveform_ai_cancel(dev, dev->read_subdev);
294
295 return 0;
296}
297
298static int waveform_ai_cmdtest(struct comedi_device *dev,
299 struct comedi_subdevice *s,
300 struct comedi_cmd *cmd)
301{
302 int err = 0;
303 int tmp;
304
305
306
307 tmp = cmd->start_src;
308 cmd->start_src &= TRIG_NOW;
309 if (!cmd->start_src || tmp != cmd->start_src)
310 err++;
311
312 tmp = cmd->scan_begin_src;
313 cmd->scan_begin_src &= TRIG_TIMER;
314 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
315 err++;
316
317 tmp = cmd->convert_src;
318 cmd->convert_src &= TRIG_NOW | TRIG_TIMER;
319 if (!cmd->convert_src || tmp != cmd->convert_src)
320 err++;
321
322 tmp = cmd->scan_end_src;
323 cmd->scan_end_src &= TRIG_COUNT;
324 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
325 err++;
326
327 tmp = cmd->stop_src;
328 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
329 if (!cmd->stop_src || tmp != cmd->stop_src)
330 err++;
331
332 if (err)
333 return 1;
334
335
336
337
338
339 if (cmd->convert_src != TRIG_NOW && cmd->convert_src != TRIG_TIMER)
340 err++;
341 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
342 err++;
343
344 if (err)
345 return 2;
346
347
348
349 if (cmd->start_arg != 0) {
350 cmd->start_arg = 0;
351 err++;
352 }
353 if (cmd->convert_src == TRIG_NOW) {
354 if (cmd->convert_arg != 0) {
355 cmd->convert_arg = 0;
356 err++;
357 }
358 }
359 if (cmd->scan_begin_src == TRIG_TIMER) {
360 if (cmd->scan_begin_arg < nano_per_micro) {
361 cmd->scan_begin_arg = nano_per_micro;
362 err++;
363 }
364 if (cmd->convert_src == TRIG_TIMER &&
365 cmd->scan_begin_arg <
366 cmd->convert_arg * cmd->chanlist_len) {
367 cmd->scan_begin_arg =
368 cmd->convert_arg * cmd->chanlist_len;
369 err++;
370 }
371 }
372
373
374
375
376 if (!cmd->chanlist_len) {
377 cmd->chanlist_len = 1;
378 err++;
379 }
380 if (cmd->scan_end_arg != cmd->chanlist_len) {
381 cmd->scan_end_arg = cmd->chanlist_len;
382 err++;
383 }
384
385 if (cmd->stop_src == TRIG_COUNT) {
386 if (!cmd->stop_arg) {
387 cmd->stop_arg = 1;
388 err++;
389 }
390 } else {
391 if (cmd->stop_arg != 0) {
392 cmd->stop_arg = 0;
393 err++;
394 }
395 }
396
397 if (err)
398 return 3;
399
400
401
402 if (cmd->scan_begin_src == TRIG_TIMER) {
403 tmp = cmd->scan_begin_arg;
404
405 cmd->scan_begin_arg =
406 nano_per_micro * ((tmp +
407 (nano_per_micro / 2)) / nano_per_micro);
408 if (tmp != cmd->scan_begin_arg)
409 err++;
410 }
411 if (cmd->convert_src == TRIG_TIMER) {
412 tmp = cmd->convert_arg;
413
414 cmd->convert_arg =
415 nano_per_micro * ((tmp +
416 (nano_per_micro / 2)) / nano_per_micro);
417 if (tmp != cmd->convert_arg)
418 err++;
419 }
420
421 if (err)
422 return 4;
423
424 return 0;
425}
426
427static int waveform_ai_cmd(struct comedi_device *dev,
428 struct comedi_subdevice *s)
429{
430 struct comedi_cmd *cmd = &s->async->cmd;
431
432 if (cmd->flags & TRIG_RT) {
433 comedi_error(dev,
434 "commands at RT priority not supported in this driver");
435 return -1;
436 }
437
438 devpriv->timer_running = 1;
439 devpriv->ai_count = 0;
440 devpriv->scan_period = cmd->scan_begin_arg / nano_per_micro;
441
442 if (cmd->convert_src == TRIG_NOW)
443 devpriv->convert_period = 0;
444 else if (cmd->convert_src == TRIG_TIMER)
445 devpriv->convert_period = cmd->convert_arg / nano_per_micro;
446 else {
447 comedi_error(dev, "bug setting conversion period");
448 return -1;
449 }
450
451 do_gettimeofday(&devpriv->last);
452 devpriv->usec_current = devpriv->last.tv_usec % devpriv->usec_period;
453 devpriv->usec_remainder = 0;
454
455 devpriv->timer.expires = jiffies + 1;
456 add_timer(&devpriv->timer);
457 return 0;
458}
459
460static int waveform_ai_cancel(struct comedi_device *dev,
461 struct comedi_subdevice *s)
462{
463 devpriv->timer_running = 0;
464 del_timer(&devpriv->timer);
465 return 0;
466}
467
468static short fake_sawtooth(struct comedi_device *dev, unsigned int range_index,
469 unsigned long current_time)
470{
471 struct comedi_subdevice *s = dev->read_subdev;
472 unsigned int offset = s->maxdata / 2;
473 u64 value;
474 const struct comedi_krange *krange =
475 &s->range_table->range[range_index];
476 u64 binary_amplitude;
477
478 binary_amplitude = s->maxdata;
479 binary_amplitude *= devpriv->uvolt_amplitude;
480 do_div(binary_amplitude, krange->max - krange->min);
481
482 current_time %= devpriv->usec_period;
483 value = current_time;
484 value *= binary_amplitude * 2;
485 do_div(value, devpriv->usec_period);
486 value -= binary_amplitude;
487
488 return offset + value;
489}
490
491static short fake_squarewave(struct comedi_device *dev,
492 unsigned int range_index,
493 unsigned long current_time)
494{
495 struct comedi_subdevice *s = dev->read_subdev;
496 unsigned int offset = s->maxdata / 2;
497 u64 value;
498 const struct comedi_krange *krange =
499 &s->range_table->range[range_index];
500 current_time %= devpriv->usec_period;
501
502 value = s->maxdata;
503 value *= devpriv->uvolt_amplitude;
504 do_div(value, krange->max - krange->min);
505
506 if (current_time < devpriv->usec_period / 2)
507 value *= -1;
508
509 return offset + value;
510}
511
512static short fake_flatline(struct comedi_device *dev, unsigned int range_index,
513 unsigned long current_time)
514{
515 return dev->read_subdev->maxdata / 2;
516}
517
518
519static short fake_waveform(struct comedi_device *dev, unsigned int channel,
520 unsigned int range, unsigned long current_time)
521{
522 enum {
523 SAWTOOTH_CHAN,
524 SQUARE_CHAN,
525 };
526 switch (channel) {
527 case SAWTOOTH_CHAN:
528 return fake_sawtooth(dev, range, current_time);
529 break;
530 case SQUARE_CHAN:
531 return fake_squarewave(dev, range, current_time);
532 break;
533 default:
534 break;
535 }
536
537 return fake_flatline(dev, range, current_time);
538}
539
540static int waveform_ai_insn_read(struct comedi_device *dev,
541 struct comedi_subdevice *s,
542 struct comedi_insn *insn, unsigned int *data)
543{
544 int i, chan = CR_CHAN(insn->chanspec);
545
546 for (i = 0; i < insn->n; i++)
547 data[i] = devpriv->ao_loopbacks[chan];
548
549 return insn->n;
550}
551
552static int waveform_ao_insn_write(struct comedi_device *dev,
553 struct comedi_subdevice *s,
554 struct comedi_insn *insn, unsigned int *data)
555{
556 int i, chan = CR_CHAN(insn->chanspec);
557
558 for (i = 0; i < insn->n; i++)
559 devpriv->ao_loopbacks[chan] = data[i];
560
561 return insn->n;
562}
563
564MODULE_AUTHOR("Comedi http://www.comedi.org");
565MODULE_DESCRIPTION("Comedi low-level driver");
566MODULE_LICENSE("GPL");
567