File: timer.c

package info (click to toggle)
comedilib 0.7.22-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,348 kB
  • ctags: 2,465
  • sloc: ansic: 16,316; sh: 8,492; ruby: 1,360; makefile: 325; yacc: 262; perl: 257; lex: 79; python: 59
file content (160 lines) | stat: -rw-r--r-- 3,407 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
    lib/timer.c
    legacy timer crap

    COMEDILIB - Linux Control and Measurement Device Interface Library
    Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation, version 2.1
    of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
    USA.
*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>

#include "libinternal.h"



/* dt282x timer */

static int dt282x_timer(double freq,unsigned int *trigvar,double *actual_freq)
{
	int divider,prescaler;
	double basefreq=4e6;
	
	divider=floor(4e6/(freq));
	prescaler=0;
	while(divider>255){
		prescaler++;
		divider>>=1;
		basefreq/=2;
	}
	if(prescaler==1){
		prescaler++;
		divider>>=1;
		basefreq/=2;
	}
	if(prescaler>=16)return -1;
	*trigvar=(prescaler<<8)|(255-divider);
	*actual_freq=basefreq/divider;
	
	return 0;
}


/* dt2814 timer */
static int dt2814_timer(double freq,unsigned int *trigvar,double *actual_freq)
{
	double f;
	int  i;
	
	f=1e5;
	for(i=0;i<8;i++){
		if(f-freq<freq-f/10){
			*trigvar=i;
			*actual_freq=f;
			return 0;
		}
		f/=10;
	}
	*trigvar=i;
	*actual_freq=f;
	
	return 0;
}

/* atmio/pcimio timer */
static int atmio_timer(double freq,unsigned int *trigvar,double *actual_freq)
{
	unsigned int divider;
	
	divider=floor(20e6/(freq));
	*actual_freq=20e6/divider;
	*trigvar=divider-1;
	
	return 0;
}

/* acl8112 timer */
static int acl8112_timer(double freq,unsigned int *trigvar,double *actual_freq)
{
	int divider,prescaler;
	double basefreq=2e6;

	/* XXX my notes say that the prescaler and divider cannot
	   be 1.  This needs to be checked.  --ds */
	
	/* Force at least one division to get something in CTR2. */
	prescaler=1;
	divider = basefreq/(freq);

	while(divider>32767){
		prescaler*=2;
		divider>>=1;
	}

	*trigvar = (prescaler<<16) | divider;
	*actual_freq=basefreq/(divider*prescaler);
	
	return 0;
}

/* nanosec timer */
static int nanosec_timer(double freq,unsigned int *trigvar,double *actual_freq)
{
	*trigvar=(1e9/freq);
	*actual_freq=1e9/(*trigvar);

	return 0;
}

typedef int (*timerfunc)(double freq,unsigned int *trigvar,double *actual_freq);

static timerfunc timer_functions[]={
	NULL,
	dt282x_timer,
	dt2814_timer,
	atmio_timer,
	acl8112_timer,
	nanosec_timer,
};
#define N_TIMERTYPES 6

EXPORT_ALIAS_DEFAULT(_comedi_get_timer,comedi_get_timer,0.7.18);
int _comedi_get_timer(comedi_t *it,unsigned int subdev,double freq,
	unsigned int *trigvar,double *actual_freq)
{
	int timer_type;
	
	if(!it || !trigvar || !actual_freq)
		return -1;

	timer_type=it->subdevices[subdev].timer_type;
	
	if(timer_type==0 || timer_type>=N_TIMERTYPES)
		return -1;
	
	return (timer_functions[timer_type])(freq,trigvar,actual_freq);
}