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