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