1 | /* mio_io.c |
---|
2 | * WinSystems support module file for the PCM-MIO Linux driver |
---|
3 | * |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * |
---|
8 | * This file implements all of the supported 'C' language functions. |
---|
9 | * Where necessary ioctl calls are made into the kernel driver to access |
---|
10 | * the actual hardware. |
---|
11 | */ |
---|
12 | |
---|
13 | /* #define DEBUG 1 */ |
---|
14 | |
---|
15 | #include "mio_io.h" |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <fcntl.h> /* open */ |
---|
19 | #include <unistd.h> /* exit */ |
---|
20 | #include <sys/ioctl.h> /* ioctl */ |
---|
21 | |
---|
22 | /************************************************************************** |
---|
23 | * |
---|
24 | * USER LIBRARY FUNCTIONS |
---|
25 | * |
---|
26 | *************************************************************************** |
---|
27 | */ |
---|
28 | |
---|
29 | int handle = 0; |
---|
30 | char *device_id ="pcmmio"; |
---|
31 | char *dev_id = "/dev/pcmmio"; |
---|
32 | |
---|
33 | /////////////////////////////////////////////////////////////////////////////// |
---|
34 | // |
---|
35 | // MIO_READ_IRQ_ASSIGNED |
---|
36 | // |
---|
37 | ////////////////////////////////////////////////////////////////////////////// |
---|
38 | |
---|
39 | int mio_read_irq_assigned(void) |
---|
40 | { |
---|
41 | unsigned char val; |
---|
42 | |
---|
43 | mio_error_code = MIO_SUCCESS; |
---|
44 | |
---|
45 | if(check_handle()) /* Check for chip available */ |
---|
46 | return -1; |
---|
47 | |
---|
48 | |
---|
49 | /* All of our programming of the hardware is handled at this level so that |
---|
50 | all of the routines that need to shove and IRQ value into hardware will |
---|
51 | use this call. |
---|
52 | */ |
---|
53 | |
---|
54 | val = ioctl(handle,READ_IRQ_ASSIGNED,NULL); |
---|
55 | |
---|
56 | return val & 0xff; |
---|
57 | } |
---|
58 | |
---|
59 | /////////////////////////////////////////////////////////////////////////////// |
---|
60 | // |
---|
61 | // READ_DIO_BYTE |
---|
62 | // |
---|
63 | ////////////////////////////////////////////////////////////////////////////// |
---|
64 | |
---|
65 | unsigned char read_dio_byte(int offset) |
---|
66 | { |
---|
67 | int val; |
---|
68 | |
---|
69 | mio_error_code = MIO_SUCCESS; |
---|
70 | |
---|
71 | if(check_handle()) /* Check for chip available */ |
---|
72 | return -1; |
---|
73 | |
---|
74 | /* All bit operations are handled at this level so we need only |
---|
75 | read and write bytes from the actual hardware using the driver |
---|
76 | to handle our ioctl call for it. |
---|
77 | */ |
---|
78 | |
---|
79 | val = ioctl(handle,READ_DIO_BYTE,offset); |
---|
80 | |
---|
81 | return (unsigned char) (val & 0xff);; |
---|
82 | } |
---|
83 | |
---|
84 | /////////////////////////////////////////////////////////////////////////////// |
---|
85 | // |
---|
86 | // MIO_READ_REG |
---|
87 | // |
---|
88 | ////////////////////////////////////////////////////////////////////////////// |
---|
89 | |
---|
90 | unsigned char mio_read_reg(int offset) |
---|
91 | { |
---|
92 | int val; |
---|
93 | |
---|
94 | mio_error_code = MIO_SUCCESS; |
---|
95 | |
---|
96 | if(check_handle()) /* Check for chip available */ |
---|
97 | return -1; |
---|
98 | |
---|
99 | |
---|
100 | /* This is a catchall register read routine that allows reading of |
---|
101 | ANY of the registers on the PCM-MIO. It is used primarily for |
---|
102 | retreiving control and access values in the hardware. |
---|
103 | */ |
---|
104 | |
---|
105 | val = ioctl(handle,MIO_READ_REG,offset); |
---|
106 | |
---|
107 | return (unsigned char) (val & 0xff); |
---|
108 | } |
---|
109 | |
---|
110 | /////////////////////////////////////////////////////////////////////////////// |
---|
111 | // |
---|
112 | // MIO_WRITE_REG |
---|
113 | // |
---|
114 | ////////////////////////////////////////////////////////////////////////////// |
---|
115 | |
---|
116 | int mio_write_reg(int offset, unsigned char value) |
---|
117 | { |
---|
118 | unsigned short param; |
---|
119 | int val; |
---|
120 | |
---|
121 | mio_error_code = MIO_SUCCESS; |
---|
122 | |
---|
123 | if(check_handle()) /* Check for chip available */ |
---|
124 | return -1; |
---|
125 | |
---|
126 | param = offset & 0xff; |
---|
127 | |
---|
128 | param = param | (value << 8); |
---|
129 | |
---|
130 | /* This function like the previous allow unlimited |
---|
131 | write access to ALL of the registers on the PCM-MIO |
---|
132 | */ |
---|
133 | |
---|
134 | val = ioctl(handle,MIO_WRITE_REG,param); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /////////////////////////////////////////////////////////////////////////////// |
---|
141 | // |
---|
142 | // WRITE_DIO_BYTE |
---|
143 | // |
---|
144 | ////////////////////////////////////////////////////////////////////////////// |
---|
145 | |
---|
146 | int write_dio_byte(int offset, unsigned char value) |
---|
147 | { |
---|
148 | unsigned short param; |
---|
149 | int val; |
---|
150 | |
---|
151 | mio_error_code = MIO_SUCCESS; |
---|
152 | |
---|
153 | if(check_handle()) /* Check for chip available */ |
---|
154 | return -1; |
---|
155 | |
---|
156 | param = offset & 0xff; |
---|
157 | |
---|
158 | param = param | (value << 8); |
---|
159 | |
---|
160 | /* All bit operations for the DIO are handled at this level |
---|
161 | and we need the driver to allow access to the actual |
---|
162 | DIO registers to update the value. |
---|
163 | */ |
---|
164 | |
---|
165 | val = ioctl(handle,WRITE_DIO_BYTE,param); |
---|
166 | |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /////////////////////////////////////////////////////////////////////////////// |
---|
172 | // |
---|
173 | // WRITE_DAC_COMMAND |
---|
174 | // |
---|
175 | ////////////////////////////////////////////////////////////////////////////// |
---|
176 | |
---|
177 | int write_dac_command(int dac_num,unsigned char value) |
---|
178 | { |
---|
179 | unsigned short param; |
---|
180 | int val; |
---|
181 | |
---|
182 | mio_error_code = MIO_SUCCESS; |
---|
183 | |
---|
184 | if(check_handle()) /* Check for chip available */ |
---|
185 | return -1; |
---|
186 | |
---|
187 | param = dac_num & 0xff; |
---|
188 | |
---|
189 | param = param | (value << 8); |
---|
190 | |
---|
191 | val = ioctl(handle,WRITE_DAC_COMMAND,param); |
---|
192 | |
---|
193 | return 0; |
---|
194 | } |
---|
195 | |
---|
196 | /////////////////////////////////////////////////////////////////////////////// |
---|
197 | // |
---|
198 | // WRITE_ADC_COMMAND |
---|
199 | // |
---|
200 | ////////////////////////////////////////////////////////////////////////////// |
---|
201 | |
---|
202 | int write_adc_command(int adc_num,unsigned char value) |
---|
203 | { |
---|
204 | unsigned short param; |
---|
205 | int ret_val; |
---|
206 | |
---|
207 | mio_error_code = MIO_SUCCESS; |
---|
208 | |
---|
209 | if(check_handle()) /* Check for chip available */ |
---|
210 | return -1; |
---|
211 | |
---|
212 | param = adc_num & 0xff; |
---|
213 | |
---|
214 | param = param | (value << 8); |
---|
215 | |
---|
216 | ret_val = ioctl(handle,WRITE_ADC_COMMAND,param); |
---|
217 | |
---|
218 | return 0; |
---|
219 | } |
---|
220 | |
---|
221 | /////////////////////////////////////////////////////////////////////////////// |
---|
222 | // |
---|
223 | // WRITE_DAC_DATA |
---|
224 | // |
---|
225 | ////////////////////////////////////////////////////////////////////////////// |
---|
226 | |
---|
227 | int write_dac_data(int dac_num, unsigned short value) |
---|
228 | { |
---|
229 | int ret_val; |
---|
230 | unsigned char buffer[3]; |
---|
231 | |
---|
232 | mio_error_code = MIO_SUCCESS; |
---|
233 | |
---|
234 | if(check_handle()) /* Check for chip available */ |
---|
235 | return -1; |
---|
236 | |
---|
237 | buffer[0] = dac_num; |
---|
238 | buffer[1] = value & 0xff; |
---|
239 | buffer[2] = value >> 8; |
---|
240 | |
---|
241 | ret_val = ioctl(handle,WRITE_DAC_DATA,buffer); |
---|
242 | |
---|
243 | return 0; |
---|
244 | } |
---|
245 | |
---|
246 | /////////////////////////////////////////////////////////////////////////////// |
---|
247 | // |
---|
248 | // DAC_READ_STATUS |
---|
249 | // |
---|
250 | ////////////////////////////////////////////////////////////////////////////// |
---|
251 | |
---|
252 | unsigned char dac_read_status(int dac_num) |
---|
253 | { |
---|
254 | int ret_val; |
---|
255 | |
---|
256 | mio_error_code = MIO_SUCCESS; |
---|
257 | |
---|
258 | if(check_handle()) /* Check for chip available */ |
---|
259 | return -1; |
---|
260 | |
---|
261 | ret_val = ioctl(handle,READ_DAC_STATUS,dac_num); |
---|
262 | |
---|
263 | return ret_val & 0xff; |
---|
264 | } |
---|
265 | |
---|
266 | /////////////////////////////////////////////////////////////////////////////// |
---|
267 | // |
---|
268 | // ADC_READ_STATUS |
---|
269 | // |
---|
270 | ////////////////////////////////////////////////////////////////////////////// |
---|
271 | |
---|
272 | unsigned char adc_read_status(int adc_num) |
---|
273 | { |
---|
274 | int ret_val; |
---|
275 | |
---|
276 | mio_error_code = MIO_SUCCESS; |
---|
277 | |
---|
278 | if(check_handle()) /* Check for chip available */ |
---|
279 | return -1; |
---|
280 | |
---|
281 | ret_val = ioctl(handle,READ_ADC_STATUS,adc_num); |
---|
282 | |
---|
283 | return (ret_val & 0xff); |
---|
284 | } |
---|
285 | |
---|
286 | /////////////////////////////////////////////////////////////////////////////// |
---|
287 | // |
---|
288 | // ADC_READ_CONVERSION_DATA |
---|
289 | // |
---|
290 | ////////////////////////////////////////////////////////////////////////////// |
---|
291 | |
---|
292 | unsigned short adc_read_conversion_data(int channel) |
---|
293 | { |
---|
294 | int ret_val; |
---|
295 | int adc_num; |
---|
296 | |
---|
297 | mio_error_code = MIO_SUCCESS; |
---|
298 | |
---|
299 | if(check_handle()) /* Check for chip available */ |
---|
300 | return -1; |
---|
301 | |
---|
302 | if(channel > 7) |
---|
303 | adc_num = 1; |
---|
304 | else |
---|
305 | adc_num = 0; |
---|
306 | |
---|
307 | ret_val = ioctl(handle,READ_ADC_DATA,adc_num); |
---|
308 | |
---|
309 | return (ret_val & 0xffff); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | int dio_get_int(void) |
---|
314 | { |
---|
315 | int c; |
---|
316 | |
---|
317 | c=ioctl(handle,DIO_GET_INT,NULL); |
---|
318 | |
---|
319 | return (c & 0xff); |
---|
320 | |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | int wait_adc_int(int adc_num) |
---|
325 | { |
---|
326 | int c; |
---|
327 | |
---|
328 | |
---|
329 | if(check_handle()) /* Check for chip available */ |
---|
330 | return -1; |
---|
331 | |
---|
332 | if(adc_num) |
---|
333 | c=ioctl(handle,WAIT_A2D_INT_1,NULL); |
---|
334 | else |
---|
335 | c=ioctl(handle,WAIT_A2D_INT_2,NULL); |
---|
336 | |
---|
337 | |
---|
338 | return (c & 0xff); |
---|
339 | |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | int wait_dac_int(int dac_num) |
---|
344 | { |
---|
345 | int c; |
---|
346 | |
---|
347 | if(check_handle()) /* Check for chip available */ |
---|
348 | return -1; |
---|
349 | |
---|
350 | if(dac_num) |
---|
351 | c=ioctl(handle,WAIT_DAC_INT_1,NULL); |
---|
352 | else |
---|
353 | c=ioctl(handle,WAIT_DAC_INT_2,NULL); |
---|
354 | |
---|
355 | return (c & 0xff); |
---|
356 | |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | int wait_dio_int(void) |
---|
361 | { |
---|
362 | int c; |
---|
363 | |
---|
364 | |
---|
365 | if(check_handle()) /* Check for chip available */ |
---|
366 | return -1; |
---|
367 | |
---|
368 | c=ioctl(handle,WAIT_DIO_INT,NULL); |
---|
369 | |
---|
370 | return (c & 0xff); |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | int check_handle(void) |
---|
377 | { |
---|
378 | if(handle > 0) /* If it's already a valid handle */ |
---|
379 | return 0; |
---|
380 | |
---|
381 | if(handle == -1) /* If it's already been tried */ |
---|
382 | { |
---|
383 | mio_error_code = MIO_OPEN_ERROR; |
---|
384 | sprintf(mio_error_string,"MIO - Unable to open device PCMMIO"); |
---|
385 | return -1; |
---|
386 | } |
---|
387 | |
---|
388 | /* Try opening the device file, in case it hasn't been opened yet */ |
---|
389 | |
---|
390 | handle = open(device_id,0); |
---|
391 | |
---|
392 | /* Try an alternate open at /dev */ |
---|
393 | |
---|
394 | if(handle < 0) |
---|
395 | handle = open(dev_id,0); |
---|
396 | |
---|
397 | if(handle > 0) /* If it's now a valid handle */ |
---|
398 | return 0; |
---|
399 | |
---|
400 | mio_error_code = MIO_OPEN_ERROR; |
---|
401 | sprintf(mio_error_string,"MIO - Unable to open device PCMMIO"); |
---|
402 | handle = -1; |
---|
403 | return -1; |
---|
404 | } |
---|
405 | |
---|