2001-03-01 21:57:00 +00:00
|
|
|
/*
|
|
|
|
* Digital I/O example
|
|
|
|
* Part of Comedilib
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
|
|
|
|
*
|
|
|
|
* This file may be freely modified, distributed, and combined with
|
|
|
|
* other software, as long as proper attribution is given in the
|
|
|
|
* source code.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Requirements: A board with a digital I/O subdevice. Not just
|
|
|
|
* a 'digital input' or 'digital output' subdevice, but one in
|
|
|
|
* which the channels can be configured between input and output.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <comedilib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2003-01-01 23:07:02 +00:00
|
|
|
#include <stdlib.h>
|
2001-03-01 21:57:00 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "examples.h"
|
|
|
|
|
|
|
|
int pin_clk = 2;
|
|
|
|
int pin_data = 10;
|
|
|
|
|
|
|
|
comedi_t *device;
|
|
|
|
|
|
|
|
#define BUFSZ 1024
|
|
|
|
sampl_t buf[BUFSZ];
|
|
|
|
|
2007-01-03 21:14:53 +00:00
|
|
|
void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice);
|
2001-03-01 21:57:00 +00:00
|
|
|
void do_cmd(comedi_t *dev,comedi_cmd *cmd);
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
comedi_t *dev;
|
|
|
|
comedi_cmd cmd;
|
|
|
|
int ret;
|
2007-01-03 21:14:53 +00:00
|
|
|
struct parsed_options options;
|
2001-03-01 21:57:00 +00:00
|
|
|
|
2007-01-03 21:14:53 +00:00
|
|
|
init_parsed_options(&options);
|
|
|
|
options.channel = -1;
|
|
|
|
parse_options(&options, argc, argv);
|
2001-03-01 21:57:00 +00:00
|
|
|
|
2007-01-03 21:14:53 +00:00
|
|
|
dev = comedi_open(options.filename);
|
2001-03-01 21:57:00 +00:00
|
|
|
if(!dev){
|
2007-01-03 21:14:53 +00:00
|
|
|
perror(options.filename);
|
2001-03-01 21:57:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
device = dev;
|
|
|
|
|
2007-01-03 21:14:53 +00:00
|
|
|
if(options.channel >= 0) pin_data = options.channel;
|
2001-03-01 21:57:00 +00:00
|
|
|
|
|
|
|
ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
|
|
|
|
if(ret<0)perror("fcntl");
|
|
|
|
|
2007-01-03 21:14:53 +00:00
|
|
|
prepare_cmd(dev, &cmd, options.subdevice);
|
2001-03-01 21:57:00 +00:00
|
|
|
|
|
|
|
do_cmd(dev,&cmd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int c=0;
|
|
|
|
static unsigned int bits =0;
|
|
|
|
|
|
|
|
void do_cmd(comedi_t *dev,comedi_cmd *cmd)
|
|
|
|
{
|
|
|
|
unsigned int *chanlist;
|
|
|
|
int n_chans;
|
|
|
|
int total=0;
|
|
|
|
int ret;
|
|
|
|
int go;
|
|
|
|
struct timeval timeout;
|
|
|
|
fd_set rdset;
|
|
|
|
|
|
|
|
chanlist = cmd->chanlist;
|
|
|
|
n_chans = cmd->chanlist_len;
|
|
|
|
|
|
|
|
ret=comedi_command_test(dev,cmd);
|
|
|
|
|
|
|
|
//printf("test ret=%d\n",ret);
|
|
|
|
if(ret<0){
|
|
|
|
printf("errno=%d\n",errno);
|
|
|
|
comedi_perror("comedi_command_test");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-07-14 00:50:52 +00:00
|
|
|
dump_cmd(stdout,cmd);
|
2001-03-01 21:57:00 +00:00
|
|
|
|
|
|
|
cmd->chanlist = chanlist;
|
|
|
|
cmd->chanlist_len = n_chans;
|
|
|
|
|
|
|
|
ret=comedi_command_test(dev,cmd);
|
|
|
|
|
|
|
|
printf("test ret=%d\n",ret);
|
|
|
|
if(ret<0){
|
|
|
|
printf("errno=%d\n",errno);
|
|
|
|
comedi_perror("comedi_command_test");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-07-14 00:50:52 +00:00
|
|
|
dump_cmd(stdout,cmd);
|
2001-03-01 21:57:00 +00:00
|
|
|
|
2016-05-13 17:19:10 +01:00
|
|
|
comedi_set_read_subdevice(dev, cmd->subdev);
|
|
|
|
ret = comedi_get_read_subdevice(dev);
|
|
|
|
if (ret < 0 || ret != cmd->subdev) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"failed to change 'read' subdevice from %d to %d\n",
|
|
|
|
ret, cmd->subdev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-03-01 21:57:00 +00:00
|
|
|
cmd->chanlist = chanlist;
|
|
|
|
cmd->chanlist_len = n_chans;
|
|
|
|
|
|
|
|
ret=comedi_command(dev,cmd);
|
|
|
|
|
|
|
|
printf("ret=%d\n",ret);
|
|
|
|
if(ret<0){
|
|
|
|
printf("errno=%d\n",errno);
|
|
|
|
comedi_perror("comedi_command");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
go=1;
|
|
|
|
while(go){
|
|
|
|
FD_ZERO(&rdset);
|
|
|
|
FD_SET(comedi_fileno(dev),&rdset);
|
|
|
|
timeout.tv_sec=0;
|
|
|
|
timeout.tv_usec=50000;
|
|
|
|
ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
|
|
|
|
if(ret<0){
|
|
|
|
perror("select");
|
|
|
|
}else if(ret==0){
|
|
|
|
if(c){
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
c=0;
|
|
|
|
bits=0;
|
|
|
|
}
|
|
|
|
}else if(FD_ISSET(comedi_fileno(dev),&rdset)){
|
|
|
|
ret=read(comedi_fileno(dev),buf,BUFSZ);
|
|
|
|
if(ret<0){
|
|
|
|
if(errno==EAGAIN){
|
|
|
|
go = 0;
|
|
|
|
perror("read");
|
|
|
|
}
|
|
|
|
}else if(ret==0){
|
|
|
|
go = 0;
|
|
|
|
}else{
|
|
|
|
int i;
|
2007-01-03 21:14:53 +00:00
|
|
|
|
2001-03-01 21:57:00 +00:00
|
|
|
total+=ret;
|
|
|
|
for(i=0;i<ret/sizeof(sampl_t);i++){
|
|
|
|
fprintf(stderr,"%d",buf[i]>0xa000);
|
|
|
|
c++;
|
|
|
|
if(c>=32){
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
c=0;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
//printf("%d %d\n",buf[i],buf[i]>0xa000);
|
|
|
|
//printf("%d",buf[i]>0xa000);
|
|
|
|
bits<<=1;
|
|
|
|
bits|=(buf[i]>0xa000);
|
|
|
|
c++;
|
|
|
|
if(c>=33){
|
|
|
|
#if 0
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday(&now,NULL);
|
|
|
|
printf(" %08x %ld.%06ld\n",bits,now.tv_sec,now.tv_usec);
|
|
|
|
c=0;
|
|
|
|
bits=0;
|
|
|
|
#else
|
|
|
|
printf(" %08x\n",bits);
|
|
|
|
c=0;
|
|
|
|
bits=0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if(!bits)c=0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int chanlist[16];
|
|
|
|
/*
|
|
|
|
* This part of the demo measures channels 1, 2, 3, 4 at a rate of
|
|
|
|
* 10 khz, with the inter-sample time at 10 us (100 khz). The number
|
|
|
|
* of scans measured is 10. This is analogous to the old mode2
|
|
|
|
* acquisition.
|
|
|
|
*/
|
2007-01-03 21:14:53 +00:00
|
|
|
void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice)
|
2001-03-01 21:57:00 +00:00
|
|
|
{
|
|
|
|
memset(cmd,0,sizeof(comedi_cmd));
|
|
|
|
|
|
|
|
/* the subdevice that the command is sent to */
|
|
|
|
cmd->subdev = subdevice;
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
cmd->flags = TRIG_WAKE_EOS;
|
|
|
|
|
|
|
|
cmd->start_src = TRIG_NOW;
|
|
|
|
cmd->start_arg = 0;
|
|
|
|
|
|
|
|
cmd->scan_begin_src = TRIG_EXT;
|
2003-05-24 21:15:06 +00:00
|
|
|
cmd->scan_begin_arg = CR_EDGE | CR_INVERT | pin_clk;
|
2001-03-01 21:57:00 +00:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
cmd->convert_src = TRIG_TIMER;
|
|
|
|
cmd->convert_arg = 1;
|
|
|
|
#else
|
|
|
|
cmd->convert_src = TRIG_ANY;
|
|
|
|
cmd->convert_arg = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cmd->scan_end_src = TRIG_COUNT;
|
|
|
|
cmd->scan_end_arg = 1;
|
|
|
|
|
|
|
|
cmd->stop_src = TRIG_NONE;
|
|
|
|
cmd->stop_arg = 0;
|
|
|
|
|
|
|
|
cmd->chanlist = chanlist;
|
|
|
|
cmd->chanlist_len = 1;
|
|
|
|
|
|
|
|
chanlist[0]=CR_PACK(pin_data,0,0);
|
|
|
|
}
|
|
|
|
|