Repository navigation
Expand file tree
/
Copy pathmio_io.c
More file actions
2532 lines (2072 loc) · 70.7 KB
/
Copy pathmio_io.c
File metadata and controls
2532 lines (2072 loc) · 70.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//****************************************************************************
//
// Copyright 2010-18 by WinSystems Inc.
//
// Permission is hereby granted to the purchaser of WinSystems GPIO cards
// and CPU products incorporating a GPIO device, to distribute any binary
// file or files compiled using this source code directly or in any work
// derived by the user from this file. In no case may the source code,
// original or derived from this file, be distributed to any third party
// except by explicit permission of WinSystems. This file is distributed
// on an "As-is" basis and no warranty as to performance or fitness of pur-
// poses is expressed or implied. In no case shall WinSystems be liable for
// any direct or indirect loss or damage, real or consequential resulting
// from the usage of this source code. It is the user's sole responsibility
// to determine fitness for any considered purpose.
//
//****************************************************************************
//
// Name : mio_io.c
//
// Project : PCMMIO Linux Device Driver
//
// Author : Paul DeMetrotion
//
//****************************************************************************
//
// Date Revision Description
// -------- -------- ---------------------------------------------
// 11/11/10 1.0 Original Release
// 08/30/11 2.1 Fixed bug in write_dio_byte function
// 10/09/12 3.0 Added comments for all functions
// 10/09/12 3.1 Renamed file to pcmmio_ws
// 11/07/18 4.0 Changed some function names
// Minor code clean-up
//
//****************************************************************************
#define LIB_DEFINED
#include "mio_io.h"
#include <stdio.h>
#include <fcntl.h> // open
#include <unistd.h> // exit
#include <sys/ioctl.h> // ioctl
// These image variable help out where a register is not
// capable of a read/modify/write operation
unsigned char dio_port_images[MAX_DEV][6];
unsigned char adc1_port_image[MAX_DEV] = {0, 0, 0, 0};
unsigned char adc2_port_image[MAX_DEV] = {0, 0, 0, 0};
unsigned char dac1_port_image[MAX_DEV] = {0x10, 0x10, 0x10, 0x10};
unsigned char dac2_port_image[MAX_DEV] = {0x30, 0x30, 0x30, 0x30};
// The channel selects on the ADC are non contigous. In order to avoid shifting and such
// with each channel select, we simple bild an array for selection.
unsigned char adc_channel_select[MAX_DEV][16] = {
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT };
// Mode selection can also be time consuming and we'd also like the mode to "Stick" from
// call to call. This array will eventually undefined reference to `main'hold the actual command byte to send to the
// ADC controller for each channel according to the mode set with adc_set_channel_mode
unsigned char adc_channel_mode[MAX_DEV][16] = {
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT,
ADC_CH0_SELECT, ADC_CH1_SELECT, ADC_CH2_SELECT, ADC_CH3_SELECT,
ADC_CH4_SELECT, ADC_CH5_SELECT, ADC_CH6_SELECT, ADC_CH7_SELECT };
// This array and the index value are used internally for the adc_convert_all_channels
// and for the adc_buffered_conversion routines.
unsigned char adc_channel_buff[18] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,0xff,0xff};
unsigned char *adc_input_buffer;
unsigned short *adc_user_buffer;
// Becaues of the nature of the ADC beast. It's necessary to keep track of the last
// channel and the previous channel in order to retrieve the data amd know who it belongs to
int adc_last_channel[MAX_DEV], adc_current_channel[MAX_DEV];
// Various index pointers for the above arrays and misc globals
int adc_ch_index[MAX_DEV] = {0, 0, 0, 0};
int adc_out_index[MAX_DEV] = {0, 0, 0, 0};
int adc_repeat_channel[MAX_DEV];
int adc_repeat_count[MAX_DEV];
//------------------------------------------------------------------------
// USER LIBRARY FUNCTIONS
//------------------------------------------------------------------------
int handle[MAX_DEV] = {0, 0, 0, 0};
char *device_id[MAX_DEV] ={"/dev/pcmmio_wsa",
"/dev/pcmmio_wsb",
"/dev/pcmmio_wsc",
"/dev/pcmmio_wsd"};
//------------------------------------------------------------------------
//
// check_handle
//
// Arguments:
// dev_num The index of the chip
//
// Returns:
// 0 The function completes successfully
// -1 Error, check error code
//
//------------------------------------------------------------------------
int check_handle(int dev_num)
{
if (handle[dev_num] > 0) // If it's already a valid handle
return 0;
if (handle[dev_num] == -1) // If it's already been tried
{
mio_error_code = MIO_OPEN_ERROR;
sprintf(mio_error_string, "MIO - Unable to open device PCMMIO\n");
return -1;
}
// Try opening the device file, in case it hasn't been opened yet
handle[dev_num] = open(device_id[dev_num], O_RDWR);
if (handle[dev_num] > 0) // If it's now a valid handle
return 0;
mio_error_code = MIO_OPEN_ERROR;
sprintf(mio_error_string, "MIO - Unable to open device PCMMIO\n");
handle[dev_num] = -1;
return -1;
}
//------------------------------------------------------------------------
//
// adc_set_channel_mode
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
// input_mode Desired channel mode
// duplex Desired channel duplex
// range Desired channel range
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_set_channel_mode(int dev_num, int channel, int input_mode, int duplex, int range)
{
unsigned char command_byte;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if (channel < 0 || channel > 15)
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad Channel Number %d\n", channel);
return;
}
// Check for illegal modes
if ((input_mode != ADC_SINGLE_ENDED) && (input_mode != ADC_DIFFERENTIAL))
{
mio_error_code = MIO_BAD_MODE_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad Mode Number\n");
return;
}
if ((duplex != ADC_UNIPOLAR) && (duplex != ADC_BIPOLAR))
{
mio_error_code = MIO_BAD_MODE_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad Mode Number\n");
return;
}
if ((range != ADC_TOP_5V) && (range != ADC_TOP_10V))
{
mio_error_code = MIO_BAD_RANGE;
sprintf(mio_error_string, "MIO (ADC) : Bad Range Value\n");
return;
}
command_byte = adc_channel_select[dev_num][channel];
command_byte = command_byte | input_mode | duplex | range;
// Building these four arrays at mode set time is critical for speed
// as we don't need to calculate anything when we want to start an ADC
// conversion. WE simply retrieve the command byte from the array
// and send it to the controller.
// Likewise, when doing conversion from raw 16-bit values to a voltage
// the mode controls the worth of each individual bit as well as binary
// bias and offset values.
adc_channel_mode[dev_num][channel] = command_byte;
// Calculate bit values, offset, and adjustment values
if ((range == ADC_TOP_5V) && (duplex == ADC_UNIPOLAR))
{
adc_bitval[dev_num][channel] = 5.00 / 65536.0;
adc_adjust[dev_num][channel] = 0;
adc_offset[dev_num][channel] = 0.0;
}
if ((range == ADC_TOP_5V) && (duplex == ADC_BIPOLAR))
{
adc_bitval[dev_num][channel] = 10.0 / 65536.0;
adc_adjust[dev_num][channel] = 0x8000;
adc_offset[dev_num][channel] = -5.000;
}
if ((range == ADC_TOP_10V) && (duplex == ADC_UNIPOLAR))
{
adc_bitval[dev_num][channel] = 10.0 / 65536.0;
adc_adjust[dev_num][channel] = 0;
adc_offset[dev_num][channel] = 0.0;
}
if ((range == ADC_TOP_10V) && (duplex == ADC_BIPOLAR))
{
adc_bitval[dev_num][channel] = 20.0 / 65536.0;
adc_adjust[dev_num][channel] = 0x8000;
adc_offset[dev_num][channel] = -10.0;
}
}
//------------------------------------------------------------------------
//
// adc_start_conversion
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_start_conversion(int dev_num, int channel)
{
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if ((channel < 0) || (channel > 15))
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad channel number %d\n", channel);
return;
}
adc_last_channel[dev_num] = adc_current_channel[dev_num];
adc_current_channel[dev_num] = channel;
adc_write_command(dev_num, channel / 8, adc_channel_mode[dev_num][channel]);
}
//------------------------------------------------------------------------
//
// adc_get_channel_voltage
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
//
// Returns:
// value returned is the voltage
// mio_error_code must be MIO_SUCCESS
// for return value to be valid
//
//------------------------------------------------------------------------
float adc_get_channel_voltage(int dev_num, int channel)
{
unsigned short value;
float result;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Set Channel Mode - Bad Device Number %d\n", dev_num);
return -1;
}
if ((channel < 0) || (channel > 15))
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Start conversion bad channel number %d\n", channel);
return -1;
}
if (check_handle(dev_num)) // Check for chip available
return -1;
// Start two conversions so that we can have current data
adc_start_conversion(dev_num, channel);
if (mio_error_code)
return 0.0;
adc_wait_ready(dev_num, channel);
if (mio_error_code)
return 0.0;
adc_start_conversion(dev_num, channel);
if (mio_error_code)
return 0.0;
adc_wait_ready(dev_num, channel);
if (mio_error_code)
return 0.0;
// Read out the conversion's raw data
value = adc_read_conversion_data(dev_num, channel);
if (mio_error_code)
return 0.0;
// Convert the raw data to a voltage
value = value + adc_adjust[dev_num][channel];
result = value * adc_bitval[dev_num][channel];
result = result + adc_offset[dev_num][channel];
return result;
}
//------------------------------------------------------------------------
//
// adc_convert_all_channels
//
// Arguments:
// dev_num The index of the chip
// buffer Storage of channel data
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_convert_all_channels(int dev_num, unsigned short *buffer)
{
int i;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if (buffer == NULL)
{
mio_error_code = MIO_NULL_POINTER;
sprintf(mio_error_string, "MIO (ADC) : Null buffer pointer\n");
return;
}
if (check_handle(dev_num)) // Check for chip available
return;
// Initialize global variables including transferring the
// address of the user's ouput buffer to an internal buffer pointer
adc_user_buffer = buffer;
adc_input_buffer = adc_channel_buff;
adc_ch_index[dev_num] = 0;
adc_out_index[dev_num] = 0;
adc_start_conversion(dev_num, 0);
if (mio_error_code)
return;
adc_wait_ready(dev_num, 0);
if (mio_error_code)
return;
// This is old data throw it out
adc_read_conversion_data(dev_num, 0);
if (mio_error_code)
return;
// Finish the rest of the channels
for (i = 1; i < 8; i++)
{
adc_start_conversion(dev_num, i);
if (mio_error_code)
return;
adc_wait_ready(dev_num, i);
if (mio_error_code)
return;
// Store the results in the user's buffer
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, i);
if (mio_error_code)
return;
}
// A final dummy conversion is required to get out the last data
adc_start_conversion(dev_num, 7);
if (mio_error_code)
return;
adc_wait_ready(dev_num, 7);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, 7);
if (mio_error_code)
return;
// Now start on the second controller
adc_start_conversion(dev_num, 8);
if (mio_error_code)
return;
adc_wait_ready(dev_num, 8);
if (mio_error_code)
return;
// This data is old - Throw it out
adc_read_conversion_data(dev_num, 8);
if (mio_error_code)
return;
for (i = 9; i < 16; i++)
{
adc_start_conversion(dev_num, i);
if (mio_error_code)
return;
adc_wait_ready(dev_num, i);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, i);
if (mio_error_code)
return;
}
// A final dummy conversion is required to get the last data
adc_start_conversion(dev_num, 15);
if (mio_error_code)
return;
adc_wait_ready(dev_num, 15);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, 15);
}
//------------------------------------------------------------------------
//
// adc_convert_to_volts
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
// value Value to be converted to volts
//
// Returns:
// value returned is the voltage
// mio_error_code must be MIO_SUCCESS
// for return value to be valid
//
//------------------------------------------------------------------------
float adc_convert_to_volts(int dev_num, int channel, unsigned short value)
{
float result;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return -1;
}
if ((channel < 0) || (channel > 15))
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad channel number %d\n", channel);
return -1;
}
value = value + adc_adjust[dev_num][channel];
result = value * adc_bitval[dev_num][channel];
result = result + adc_offset[dev_num][channel];
return result;
}
//------------------------------------------------------------------------
//
// adc_convert_single_repeated
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
// count Number of conversions
// buffer Storage of channel data
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_convert_single_repeated(int dev_num, int channel, unsigned short count, unsigned short *buffer)
{
int i;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if ((channel < 0) || (channel > 15))
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad channel number %d\n", channel);
return;
}
if (buffer == NULL)
{
mio_error_code = MIO_NULL_POINTER;
sprintf(mio_error_string, "MIO (ADC) : Null buffer pointer\n");
return;
}
if (check_handle(dev_num)) // Check for chip available
return;
// Setup global variables including transferring the address of the
// user's output buffer to a global variable the ISR knows about.
adc_user_buffer = buffer;
adc_out_index[dev_num] = 0;
adc_repeat_channel[dev_num] = channel;
adc_repeat_count[dev_num] = count;
adc_start_conversion(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
// This data is old, we don't want it
adc_read_conversion_data(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
// Perform the requested number of conversions. Place the results into
// the user's buffer.
for (i = 0; i < adc_repeat_count[dev_num]; i++)
{
adc_start_conversion(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
}
// One last dummy conversion to retrieve our last data
adc_start_conversion(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_repeat_channel[dev_num]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, adc_repeat_channel[dev_num]);
}
//------------------------------------------------------------------------
//
// adc_buffered_channel_conversions
//
// Arguments:
// dev_num The index of the chip
// input_channel_buffer
// buffer Storage of channel data
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_buffered_channel_conversions(int dev_num, unsigned char *input_channel_buffer, unsigned short *buffer)
{
int adc_next_channel;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if (input_channel_buffer == NULL || buffer == NULL)
{
mio_error_code = MIO_NULL_POINTER;
sprintf(mio_error_string, "MIO (ADC) : Null buffer pointer\n");
return;
}
if (check_handle(dev_num)) // Check for chip available
return;
// Reset all of the array index pointers
adc_ch_index[dev_num] = 0;
adc_out_index[dev_num] = 0;
adc_user_buffer = buffer;
adc_input_buffer = input_channel_buffer;
adc_start_conversion(dev_num, adc_input_buffer[adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_input_buffer[adc_ch_index[dev_num]++]);
if (mio_error_code)
return;
// While there are channel numbers in the buffer (1= 0xff)
// convert the requested channel and place the result in the
// user's output buffer
do {
adc_next_channel = adc_input_buffer[adc_ch_index[dev_num]];
// This function is particularly tricky because of the
// fact that the data is delayed by one conversion and if
// we switch back and forth between the two controllers
// we'll need to run an extra conversion in order to get the
// last data offering from the previous controller. The
// conditional code in the next several lines handles the
// switches from one controller to the other.
if (adc_current_channel[dev_num] < 8 && (adc_next_channel > 7 && adc_next_channel < 16))
{
adc_start_conversion(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_start_conversion(dev_num, adc_input_buffer[adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_input_buffer[adc_ch_index[dev_num]++]);
if (mio_error_code)
return;
}
else if ((adc_current_channel[dev_num] > 7 && adc_next_channel < 15) && adc_next_channel < 8)
{
adc_start_conversion(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
adc_start_conversion(dev_num, adc_input_buffer[adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_input_buffer[adc_ch_index[dev_num]++]);
if (mio_error_code)
return;
}
else if (adc_next_channel < 15)
{
adc_start_conversion(dev_num, adc_input_buffer[adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_input_buffer[adc_ch_index[dev_num]++]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]++] = adc_read_conversion_data(dev_num, adc_current_channel[dev_num]);
if (mio_error_code)
return;
}
} while (adc_next_channel != 0xff);
// One last conversion allows us to retrieve our real last data
adc_start_conversion(dev_num, adc_input_buffer[--adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_wait_ready(dev_num, adc_input_buffer[adc_ch_index[dev_num]]);
if (mio_error_code)
return;
adc_user_buffer[adc_out_index[dev_num]] = adc_read_conversion_data(dev_num, adc_current_channel[dev_num]);
}
//------------------------------------------------------------------------
//
// adc_wait_ready
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_wait_ready(int dev_num, int channel)
{
long retry;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n", dev_num);
return;
}
if ((channel < 0) || (channel > 15))
{
mio_error_code = MIO_BAD_CHANNEL_NUMBER;
sprintf(mio_error_string, "MIO (ADC) : Bad channel number %d\n", channel);
return;
}
if (check_handle(dev_num)) // Check for chip available
return;
retry = 100000l;
// Like with the DAC timeout routine, under normal circumstances we'll
// barely make it through the loop one time beacuse the hadrware is plenty
// fast. We have the delay for the rare occasion and when the hadrware is not
// responding properly.
while (retry--)
{
if (adc_read_status(dev_num, channel / 8) & 0x80)
return;
}
mio_error_code = MIO_TIMEOUT_ERROR;
sprintf(mio_error_string, "MIO (ADC) : Wait ready - Device timeout error\n");
}
//------------------------------------------------------------------------
//
// adc_write_command
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
// value ADC command
//
// Return value in mio_error_code:
// 0 The function completes successfully
// any other return value indicates function failed
//
//------------------------------------------------------------------------
void adc_write_command(int dev_num, int adc_num, unsigned char value)
{
// strip value for parameters
int channel = (adc_num * 8) + (((value >> 3) & 0x6) | ((value >> 6) & 0x1));
int duplex = value & ADC_UNIPOLAR;
int range = value & ADC_TOP_10V;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n",dev_num);
return;
}
if ((adc_num < 0) || (adc_num > 1))
{
mio_error_code = MIO_BAD_CHIP_NUM;
sprintf(mio_error_string, "MIO (ADC) : Bad ADC Number %d\n",dev_num);
return;
}
if (check_handle(dev_num)) // Check for chip available
return;
ioctl(handle[dev_num], ADC_WRITE_COMMAND, (value << 8) | adc_num);
// need to update the arrays also
adc_channel_mode[dev_num][channel] = value;
// Calculate bit values, offset, and adjustment values
if ((range == ADC_TOP_5V) && (duplex == ADC_UNIPOLAR))
{
adc_bitval[dev_num][channel] = 5.00 / 65536.0;
adc_adjust[dev_num][channel] = 0;
adc_offset[dev_num][channel] = 0.0;
}
if ((range == ADC_TOP_5V) && (duplex == ADC_BIPOLAR))
{
adc_bitval[dev_num][channel] = 10.0 / 65536.0;
adc_adjust[dev_num][channel] = 0x8000;
adc_offset[dev_num][channel] = -5.000;
}
if ((range == ADC_TOP_10V) && (duplex == ADC_UNIPOLAR))
{
adc_bitval[dev_num][channel] = 10.0 / 65536.0;
adc_adjust[dev_num][channel] = 0;
adc_offset[dev_num][channel] = 0.0;
}
if ((range == ADC_TOP_10V) && (duplex == ADC_BIPOLAR))
{
adc_bitval[dev_num][channel] = 20.0 / 65536.0;
adc_adjust[dev_num][channel] = 0x8000;
adc_offset[dev_num][channel] = -10.0;
}
}
//------------------------------------------------------------------------
//
// adc_read_status
//
// Arguments:
// dev_num The index of the chip
// adc_num ADC device number
//
// Returns:
// value returned is the status register contents
// mio_error_code must be MIO_SUCCESS
// for return value to be valid
//
//------------------------------------------------------------------------
unsigned char adc_read_status(int dev_num, int adc_num)
{
int ret_val;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{
mio_error_code = MIO_BAD_DEVICE;
sprintf(mio_error_string, "MIO (ADC) : Bad Device Number %d\n",dev_num);
return -1;
}
if ((adc_num < 0) || (adc_num > 1))
{
mio_error_code = MIO_BAD_CHIP_NUM;
sprintf(mio_error_string, "MIO (ADC) : Bad ADC Number %d\n",dev_num);
return -1;
}
if (check_handle(dev_num)) // Check for chip available
return -1;
ret_val = ioctl(handle[dev_num], ADC_READ_STATUS, adc_num);
return (ret_val & 0xff);
}
//------------------------------------------------------------------------
//
// adc_read_conversion_data
//
// Arguments:
// dev_num The index of the chip
// channel ADC channel
//
// Returns:
// value returned is the binary value of the conversion
// mio_error_code must be MIO_SUCCESS
// for return value to be valid
//
//------------------------------------------------------------------------
unsigned short adc_read_conversion_data(int dev_num, int channel)
{
int ret_val;
int adc_num;
mio_error_code = MIO_SUCCESS;
if (dev_num < 0 || dev_num > MAX_DEV - 1)
{