19
20
#include <machine.h>
20
21
#include <scicos_block4.h>
23
#include <sys/types.h>
28
#include <rtai_lxrt.h>
29
23
#include <rtai_comedi.h>
33
26
extern void *ComediDev[];
34
27
extern int ComediDev_InUse[];
35
28
extern int ComediDev_DIOInUse[];
47
39
static void init(scicos_block *block)
49
41
struct DOCOMDev * comdev = (struct DOCOMDev *) malloc(sizeof(struct DOCOMDev));
50
comdev->subdev_type = -1;
42
*block->work = (void *) comdev;
57
47
comdev->channel=block->ipar[0];
58
par_getstr(sName,block->ipar,2,block->ipar[1]);
59
sprintf(comdev->devName,"/dev/%s",sName);
48
comdev->index = block->ipar[1];
60
49
comdev->threshold=block->rpar[0];
62
len=strlen(comdev->devName);
63
index = comdev->devName[len-1]-'0';
65
if (!ComediDev[index]) {
66
comdev->dev = comedi_open(comdev->devName);
50
comdev->subdev_type = -1;
52
sprintf(devName,"/dev/comedi%d", comdev->index);
53
if (!ComediDev[comdev->index]) {
54
comdev->dev = comedi_open(devName);
67
55
if (!(comdev->dev)) {
68
fprintf(stderr, "Comedi open failed\n");
56
fprintf(stderr, "COMEDI %s open failed\n", devName);
71
60
rt_comedi_get_board_name(comdev->dev, board);
72
printf("COMEDI %s (%s) opened.\n\n", comdev->devName, board);
73
ComediDev[index] = comdev->dev;
75
if ((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DO, 0)) < 0) {
76
// fprintf(stderr, "Comedi find_subdevice failed (No digital I/O)\n");
78
comdev->subdev_type = COMEDI_SUBD_DO;
80
if(comdev->subdev == -1){
81
if ((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DIO, 0)) < 0) {
82
fprintf(stderr, "Comedi find_subdevice failed (No digital Output)\n");
83
comedi_close(comdev->dev);
86
comdev->subdev_type = COMEDI_SUBD_DIO;
90
if ((comedi_lock(comdev->dev, comdev->subdev)) < 0) {
91
fprintf(stderr, "Comedi lock failed for subdevice %d\n",comdev-> subdev);
61
printf("COMEDI %s (%s) opened.\n\n", devName, board);
62
ComediDev[comdev->index] = comdev->dev;
64
comdev->dev = ComediDev[comdev->index];
66
if ((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DO, 0)) < 0) {
67
if ((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DIO, 0)) < 0) {
68
fprintf(stderr, "Comedi find_subdevice failed (No digital Output or I/O)\n");
92
69
comedi_close(comdev->dev);
96
comdev->dev = ComediDev[index];
97
if((comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DO, 0)) < 0){
98
comdev->subdev = comedi_find_subdevice_by_type(comdev->dev, COMEDI_SUBD_DIO, 0);
99
comdev->subdev_type =COMEDI_SUBD_DIO;
100
}else comdev->subdev_type =COMEDI_SUBD_DO;
102
if ((n_channels = comedi_get_n_channels(comdev->dev, comdev->subdev)) < 0) {
103
fprintf(stderr, "Comedi get_n_channels failed for subdevice %d\n", comdev->subdev);
104
comedi_unlock(comdev->dev, comdev->subdev);
73
comdev->subdev_type = COMEDI_SUBD_DIO;
75
comdev->subdev_type = COMEDI_SUBD_DO;
77
if (!ComediDev_DIOInUse[comdev->index] && comedi_lock(comdev->dev, comdev->subdev) < 0) {
78
fprintf(stderr, "Comedi lock failed for subdevice %d\n",comdev-> subdev);
105
79
comedi_close(comdev->dev);
108
if (comdev->channel >= n_channels) {
84
if (comdev->channel >= comedi_get_n_channels(comdev->dev, comdev->subdev)) {
109
85
fprintf(stderr, "Comedi channel not available for subdevice %d\n",comdev-> subdev);
110
86
comedi_unlock(comdev->dev, comdev->subdev);
111
87
comedi_close(comdev->dev);
115
if(comdev->subdev_type == COMEDI_SUBD_DIO){
116
if ((comedi_dio_config(comdev->dev,comdev->subdev, comdev->channel, COMEDI_OUTPUT)) < 0) {
117
fprintf(stderr, "Comedi DIO config failed for subdevice %d\n", comdev->subdev);
118
comedi_unlock(comdev->dev, comdev->subdev);
119
comedi_close(comdev->dev);
92
if (comdev->subdev_type == COMEDI_SUBD_DIO && comedi_dio_config(comdev->dev,
93
comdev->subdev, comdev->channel, COMEDI_OUTPUT) < 0) {
94
fprintf(stderr, "Comedi DIO config failed for subdevice %d\n", comdev->subdev);
95
comedi_unlock(comdev->dev, comdev->subdev);
96
comedi_close(comdev->dev);
124
ComediDev_InUse[index]++;
125
ComediDev_DIOInUse[index]++;
101
ComediDev_InUse[comdev->index]++;
102
ComediDev_DIOInUse[comdev->index]++;
126
103
comedi_dio_write(comdev->dev, comdev->subdev, comdev->channel, 0);
128
*block->work=(void *) comdev;
104
printf("%s Channel %d\n\n", (comdev->subdev_type == COMEDI_SUBD_DIO)?"DIO Output":"DO", comdev->channel);
131
107
static void inout(scicos_block *block)
143
119
static void end(scicos_block *block)
145
121
struct DOCOMDev * comdev = (struct DOCOMDev *) (*block->work);
148
len=strlen(comdev->devName);
149
index = comdev->devName[len-1]-'0';
151
comedi_dio_write(comdev->dev, comdev->subdev, comdev->channel, 0);
152
ComediDev_InUse[index]--;
153
ComediDev_DIOInUse[index]--;
154
if (!ComediDev_DIOInUse[index]) {
155
comedi_unlock(comdev->dev, comdev->subdev);
157
if (!ComediDev_InUse[index]) {
158
comedi_close(comdev->dev);
159
printf("\nCOMEDI DO %s closed.\n\n", comdev->devName);
160
ComediDev[index] = NULL;
124
int index = comdev->index;
125
comedi_dio_write(comdev->dev, comdev->subdev, comdev->channel, 0);
126
ComediDev_InUse[index]--;
127
ComediDev_DIOInUse[index]--;
128
if (!ComediDev_DIOInUse[index]) {
129
comedi_unlock(comdev->dev, comdev->subdev);
131
if (!ComediDev_InUse[index]) {
132
comedi_close(comdev->dev);
133
printf("\nCOMEDI /dev/comedi%d closed.\n\n", index);
134
ComediDev[index] = NULL;