~ubuntu-branches/debian/jessie/rtai/jessie

« back to all changes in this revision

Viewing changes to rtai-lab/scilab/devices/rtai_comedi_dioout.c

  • Committer: Bazaar Package Importer
  • Author(s): Roland Stigge
  • Date: 2009-07-04 11:47:08 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090704114708-0ivbkccfaawz2pby
Tags: 3.7.1-1
* New upstream release
* debian/control: Standards-Version: 3.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
COPYRIGHT (C) 2006  Roberto Bucher (roberto.bucher@supsi.ch)
 
2
COPYRIGHT (C) 2006 Roberto Bucher (roberto.bucher@supsi.ch)
 
3
              2009 Guillaume Millet (millet@isir.fr)
3
4
 
4
5
This library is free software; you can redistribute it and/or
5
6
modify it under the terms of the GNU Lesser General Public
18
19
 
19
20
#include <machine.h>
20
21
#include <scicos_block4.h>
21
 
#include <stdio.h>
22
 
#include <unistd.h>
23
 
#include <sys/types.h>
24
 
#include <sys/mman.h>
25
 
#include <sys/stat.h>
26
 
#include <fcntl.h>
27
 
#include <math.h>
28
 
#include <rtai_lxrt.h>
 
22
 
29
23
#include <rtai_comedi.h>
30
 
#include <string.h>
31
 
#include <stdlib.h>
 
24
#include "rtmain.h"
32
25
 
33
26
extern void *ComediDev[];
34
27
extern int ComediDev_InUse[];
35
28
extern int ComediDev_DIOInUse[];
36
29
 
37
30
struct DOCOMDev{
38
 
  int channel;
39
 
  char devName[20];
40
31
  void * dev;
 
32
  unsigned int index;
 
33
  unsigned int channel;
41
34
  int subdev;
42
35
  int subdev_type;
43
36
  double threshold;
44
37
};
45
38
 
46
 
 
47
39
static void init(scicos_block *block)
48
40
{
49
41
  struct DOCOMDev * comdev = (struct DOCOMDev *) malloc(sizeof(struct DOCOMDev));
50
 
  comdev->subdev_type = -1;
 
42
  *block->work = (void *) comdev;
51
43
 
52
 
  int n_channels;
 
44
  char devName[15];
53
45
  char board[50];
54
 
  char sName[15];
55
 
  int len, index;
56
46
 
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];
61
 
 
62
 
  len=strlen(comdev->devName);
63
 
  index = comdev->devName[len-1]-'0';
64
 
 
65
 
  if (!ComediDev[index]) {
66
 
    comdev->dev = comedi_open(comdev->devName);
 
50
  comdev->subdev_type = -1;
 
51
 
 
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);
69
57
      exit_on_error();
 
58
      return;
70
59
    }
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;
74
 
 
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");
77
 
    }else {
78
 
      comdev->subdev_type = COMEDI_SUBD_DO;
79
 
    }
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);
84
 
        exit_on_error();
85
 
      }else{
86
 
        comdev->subdev_type = COMEDI_SUBD_DIO;
87
 
      }
88
 
    }
89
 
 
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;
 
63
  } else
 
64
    comdev->dev = ComediDev[comdev->index];
 
65
 
 
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);
93
70
      exit_on_error();
94
 
    }
95
 
  } else {
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;
101
 
  }
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);
 
71
      return;
 
72
    } else
 
73
      comdev->subdev_type = COMEDI_SUBD_DIO;
 
74
  } else
 
75
    comdev->subdev_type = COMEDI_SUBD_DO;
 
76
 
 
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);
106
80
    exit_on_error();
 
81
    return;
107
82
  }
108
 
  if (comdev->channel >= n_channels) {
 
83
 
 
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);
112
88
    exit_on_error();
 
89
    return;
113
90
  }
114
91
 
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);
120
 
      exit_on_error();
121
 
    }
 
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);
 
97
    exit_on_error();
 
98
    return;
122
99
  }
123
100
 
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);
127
 
 
128
 
  *block->work=(void *) comdev;
 
104
  printf("%s Channel %d\n\n", (comdev->subdev_type == COMEDI_SUBD_DIO)?"DIO Output":"DO", comdev->channel);
129
105
}
130
106
 
131
107
static void inout(scicos_block *block)
143
119
static void end(scicos_block *block)
144
120
{
145
121
  struct DOCOMDev * comdev = (struct DOCOMDev *) (*block->work);
146
 
  int len, index;
147
 
 
148
 
  len=strlen(comdev->devName);
149
 
  index = comdev->devName[len-1]-'0';
150
 
 
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);
156
 
  }
157
 
  if (!ComediDev_InUse[index]) {
158
 
    comedi_close(comdev->dev);
159
 
    printf("\nCOMEDI DO %s closed.\n\n", comdev->devName);
160
 
    ComediDev[index] = NULL;
 
122
 
 
123
  if (comdev->dev) {
 
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);
 
130
    }
 
131
    if (!ComediDev_InUse[index]) {
 
132
      comedi_close(comdev->dev);
 
133
      printf("\nCOMEDI /dev/comedi%d closed.\n\n", index);
 
134
      ComediDev[index] = NULL;
 
135
    }
161
136
  }
162
137
  free(comdev);
163
138
}
174
149
    init(block);
175
150
  }
176
151
}
177
 
 
178