Commit d3160321 authored by Pavel Vainerman's avatar Pavel Vainerman

Merge branch 'mutabor'

Conflicts: Utilities/codegen/tests/TestGen.cc
parents ff446726 ed82ce62
#noinst_PROGRAMS = test
#test_LDADD = $(top_builddir)/lib/libUniSet.la
#test_SOURCES = TestGen_SK.cc TestGen.cc TestGen-main.cc
noinst_PROGRAMS = test
test_LDADD = $(top_builddir)/lib/libUniSet.la
test_SOURCES = TestGen_SK.cc TestGen.cc TestGen-main.cc
TestGen_SK.cc: @PACKAGE@-codegen testgen.src.xml
chmod 'a+x' @PACKAGE@-codegen
./uniset-codegen --local-include -l --ask -n TestGen testgen.src.xml
TestGen_SK.cc TestGen-main.cc: ../@PACKAGE@-codegen testgen.src.xml ../*.xsl
chmod 'a+x' ../@PACKAGE@-codegen
./testgen.sh
clean-local:
rm -rf *_SK.cc *_SK.h *-main.cc
\ No newline at end of file
......@@ -3,6 +3,7 @@
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <alloca.h>
#include "ComediInterface.h"
// -----------------------------------------------------------------------------
using namespace UniSetTypes;
......@@ -43,6 +44,37 @@ int ComediInterface::getAnalogChannel( int subdev, int channel, int range, int a
return data;
}
// -----------------------------------------------------------------------------
std::vector<lsampl_t> ComediInterface::getAnalogPacket( int subdev, int channel, int range, int aref )
throw(UniSetTypes::Exception)
{
lsampl_t* data = (lsampl_t *)alloca(1024); /* FIFO size, maximum possible samples */
comedi_insn insn;
memset(&insn, 0, sizeof(insn));
insn.insn = INSN_READ;
insn.n = 1024;
insn.data = data;
insn.subdev = subdev;
insn.chanspec = CR_PACK(channel, range, aref);
int ret = comedi_do_insn(card, &insn);
if( ret < 0 )
{
ostringstream err;
err << "(ComediInterface:getAnalogPacket): can`t read data from subdev=" << subdev
<< " channel=" << channel << " range=" << range <<" aref="<< aref
<< " err: " << ret << " (" << strerror(ret) << ")";
throw Exception(err.str());
return std::vector<lsampl_t>(0);
}
std::vector<lsampl_t> result(ret);
if(ret > 0)
memcpy(&result[0], data, ret * sizeof(lsampl_t));
return result;
}
// -----------------------------------------------------------------------------
void ComediInterface::setAnalogChannel( int subdev, int channel, int data, int range, int aref )
throw(UniSetTypes::Exception)
{
......@@ -82,6 +114,42 @@ void ComediInterface::setDigitalChannel( int subdev, int channel, bool bit )
throw Exception(err.str());
}
}
// -----------------------------------------------------------------------------
void ComediInterface::instrChannel( int subdev, int channel, const std::string instr,
std::vector<lsampl_t> args, int range, int aref )
throw(UniSetTypes::Exception)
{
lsampl_t ins = instr2type(instr);
comedi_insn insn;
if(ins < 0)
{
ostringstream err;
err << "(ComediInterface:instrChannel): unknown instruction "
<< " subdev=" << subdev << " channel=" << channel << " instruction=" << instr;
throw Exception(err.str());
return;
}
args.insert(args.begin(), ins);
memset(&insn,0,sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = args.size();
insn.data = &args[0];
insn.subdev = subdev;
insn.chanspec = CR_PACK(channel,range,aref);
if( comedi_do_insn(card,&insn) < 0 )
{
ostringstream err;
err << "(ComediInterface:instrChannel): can`t execute the instruction "
<< " subdev=" << subdev << " channel=" << channel << " instruction=" << instr;
throw Exception(err.str());
}
return;
}
// -----------------------------------------------------------------------------
void ComediInterface::configureChannel( int subdev, int channel, ChannelType t,
int range, int aref )
......@@ -139,21 +207,39 @@ void ComediInterface::configureChannel( int subdev, int channel, ChannelType t,
void ComediInterface::configureSubdev( int subdev, SubdevType t )
throw(UniSetTypes::Exception)
{
lsampl_t cmd = 102;
static const unsigned char chans[4] = {0, 8, 16, 20}; /* We can configure only one channel per 8-bit port (4-bit for CL and CH). */
lsampl_t cmd[4]; /* Ports A, B, CL, CH */
comedi_insn insn;
memset(&insn,0,sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = 1;
insn.data = &cmd;
insn.unused[0] = t;
insn.subdev = subdev;
insn.chanspec = 0;
if( comedi_do_insn(card,&insn) < 0 )
switch(t)
{
ostringstream err;
err << "(ComediInterface:configureSubdev): can`t configure subdev "
<< " subdev=" << subdev << " type=" << t;
throw Exception(err.str());
case TBI24_0:
cmd[0] = cmd[1] = cmd[2] = cmd[3] = INSN_CONFIG_DIO_INPUT;
break;
case TBI0_24:
default:
cmd[0] = cmd[1] = cmd[2] = cmd[3] = INSN_CONFIG_DIO_OUTPUT;
break;
case TBI16_8:
cmd[0] = cmd[1] = INSN_CONFIG_DIO_INPUT;
cmd[2] = cmd[3] = INSN_CONFIG_DIO_OUTPUT;
break;
}
for(int i = 0; i < 4; i++) {
memset(&insn,0,sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = 1;
insn.data = &cmd[i];
insn.subdev = subdev;
insn.chanspec = CR_PACK(chans[i], 0, 0);
if( comedi_do_insn(card,&insn) < 0 )
{
ostringstream err;
err << "(ComediInterface:configureSubdev): can`t configure subdev "
<< " subdev=" << subdev << " type=" << t;
throw Exception(err.str());
}
}
}
// -----------------------------------------------------------------------------
......@@ -191,3 +277,46 @@ ComediInterface::SubdevType ComediInterface::str2type( const std::string s )
return Unknown;
}
// -----------------------------------------------------------------------------
ComediInterface::EventType ComediInterface::event2type( const std::string s )
{
if( s == "Front" )
return Front;
if( s == "Rear" )
return Rear;
if( s == "FrontThenRear" )
return FrontThenRear;
return No;
}
// -----------------------------------------------------------------------------
lsampl_t ComediInterface::instr2type( const std::string s )
{
if( s == "AVERAGING" || s == "BOUNCE_SUPPRESSION" ) /* This are the same instructions, one for AI, another for DI */
return INSN_CONFIG_AVERAGING;
if( s == "TIMER" )
return INSN_CONFIG_TIMER_1;
if( s == "INPUT_MASK" )
return INSN_CONFIG_INPUT_MASK;
if( s == "FILTER" )
return INSN_CONFIG_FILTER;
if( s == "0mA" )
return INSN_CONFIG_0MA;
if( s == "COMPL" )
return INSN_CONFIG_COMPL;
if( s == "COUNTER" )
return INSN_CONFIG_COUNTER;
if( s == "DI_MODE" )
return INSN_CONFIG_DI_MODE;
return -1;
}
// -----------------------------------------------------------------------------
......@@ -4,7 +4,9 @@
#define ComediInterface_H_
// -----------------------------------------------------------------------------
#include <string>
#include <vector>
#include <comedilib.h>
#include <fastwel.h>
#include "Exceptions.h"
// -----------------------------------------------------------------------------
/*! / */
......@@ -17,6 +19,9 @@ class ComediInterface
int getAnalogChannel( int subdev, int channel, int range=0, int aref=AREF_GROUND )
throw(UniSetTypes::Exception);
std::vector<lsampl_t> getAnalogPacket( int subdev, int channel, int range=0, int aref=AREF_GROUND )
throw(UniSetTypes::Exception);
void setAnalogChannel( int subdev, int channel, int data, int range=0, int aref=AREF_GROUND )
throw(UniSetTypes::Exception);
......@@ -32,10 +37,10 @@ class ComediInterface
{
DI = INSN_CONFIG_DIO_INPUT,
DO = INSN_CONFIG_DIO_OUTPUT,
AI = 100, // INSN_CONFIG_AIO_INPUT,
AO = 101 // INSN_CONFIG_AIO_OUTPUT
AI = INSN_CONFIG_AIO_INPUT,
AO = INSN_CONFIG_AIO_OUTPUT
};
enum SubdevType
{
Unknown = 0,
......@@ -43,18 +48,31 @@ class ComediInterface
TBI0_24 = 2,
TBI16_8 = 3
};
enum EventType
{
No = 0,
Front = 1,
Rear = 2,
FrontThenRear = 3
};
static std::string type2str( SubdevType t );
static SubdevType str2type( const std::string s );
static EventType event2type( const std::string s );
static lsampl_t instr2type( const std::string s );
void configureSubdev( int subdev, SubdevType type ) throw(UniSetTypes::Exception);
void configureChannel( int subdev, int channel, ChannelType type, int range=0, int aref=0 )
throw(UniSetTypes::Exception);
/* Perform extended instruction at the channel. Arguments are being taken as C++ vector. */
void instrChannel( int subdev, int channel, const std::string instr, std::vector<lsampl_t> args, int range=0, int aref=0 )
throw(UniSetTypes::Exception);
inline const std::string devname(){ return dname; }
protected:
comedi_t* card; /*!< / */
......
......@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <comedilib.h>
#include <fastwel.h>
#include <getopt.h>
int subdev = 0;
......@@ -16,6 +17,7 @@ static struct option longopts[] = {
{ "read", required_argument, 0, 'r' },
{ "write", required_argument, 0, 'w' },
{ "aread", required_argument, 0, 'i' },
{ "anread", required_argument, 0, 'n' },
{ "awrite", required_argument, 0, 'o' },
{ "subdev", required_argument, 0, 's' },
{ "device", required_argument, 0, 'd' },
......@@ -23,6 +25,9 @@ static struct option longopts[] = {
{ "aref", required_argument, 0, 'z' },
{ "range", required_argument, 0, 'x' },
{ "config", required_argument, 0, 'c' },
{ "extconfig", required_argument, 0, 'e' },
{ "timer", required_argument, 0, 't' },
{ "autoscan", no_argument, 0, 'u' },
{ "autoconf", no_argument, 0, 'a' },
{ "plus", required_argument, 0, 'p' },
{ "blink", no_argument, 0, 'b' },
......@@ -39,18 +44,22 @@ enum Command
cmdARead,
cmdAWrite,
cmdConfig,
cmdSubConfig
cmdSubConfig,
cmdAnRead,
cmdExtConfig,
cmdTimer
} cmd;
// --------------------------------------------------------------------------
static void insn_config( comedi_t* card, int subdev, int channel, lsampl_t iotype, int range, int aref );
static void insn_subdev_config( comedi_t* card, int subdev, lsampl_t type );
static void insn_config_extra( comedi_t* card, int subdev, int channel, lsampl_t data, int range, int aref, int val );
// --------------------------------------------------------------------------
int main(int argc, char* argv[])
{
comedi_t* card;
lsampl_t data = 0;
lsampl_t darr[20];
int optindex = 0;
int opt = 0;
char* dev = "/dev/comedi0";
......@@ -61,10 +70,14 @@ int main(int argc, char* argv[])
int cnum = 0;
int blink = 0;
int exret = EXIT_SUCCESS;
int instruction;
int autoscan = 0;
int n_data;
memset(chan,-1,sizeof(chan));
while( (opt = getopt_long(argc, argv, "habr:w:i:o:s:d:c:p:m:q:",longopts,&optindex)) != -1 )
while( (opt = getopt_long(argc, argv, "habur:w:i:o:s:d:c:p:m:q:e:t:n:",longopts,&optindex)) != -1 )
{
switch (opt)
{
......@@ -74,6 +87,7 @@ int main(int argc, char* argv[])
printf("[-r|--read] chan - read from digital channel\n");
printf("[-o|--awrite] chan val - write to analog channel\n");
printf("[-i|--aread] chan - read from analog channel\n");
printf("[-n|--anread] chan - read N values (n = 20) from analog channel\n");
printf("[-s|--subdev] sub - use subdev number sub. (Default: 0)\n");
printf("[-d|--device] dev - use device dev. (Default: /dev/comedi0)\n");
printf("[--aref] val - AREF (Default: %d)\n",aref);
......@@ -94,11 +108,14 @@ int main(int argc, char* argv[])
printf(" 1 - -5 - 5\n");
printf(" 2 - -2.5 - 2.5\n");
printf(" 3 - -1.25 - 1.25\n");
printf("[-e|--extconfig] channel instruction [parameters] - perform configuring instruction at given channel\n");
printf("[-t|--timer] channel interval - start acquisition by timer at given channel or stop it (if interval = 0)\n");
printf("[-u|--autoscan] - (together with -t) set autoscan mode for acquisition by timer\n");
printf("[--blink] - (blink output): ONLY FOR 'write': 0-1-0-1-0-...\n");
printf("[--blink-msec] val - Blink pause [msec]. Default: 300 msec\n");
return 0;
case 'r':
case 'r':
chan[0] = atoi(optarg);
cmd = cmdDRead;
break;
......@@ -110,7 +127,16 @@ int main(int argc, char* argv[])
val = atoi(argv[optind]);
break;
case 'i':
case 'n':
chan[0] = atoi(optarg);
cmd = cmdAnRead;
break;
case 'u':
autoscan = 1;
break;
case 'i':
chan[0] = atoi(optarg);
cmd = cmdARead;
break;
......@@ -122,7 +148,23 @@ int main(int argc, char* argv[])
val = atoi(argv[optind]);
break;
case 'd':
case 'e':
chan[0] = atoi(optarg);
cmd = cmdExtConfig;
n_data = 0;
for(; optind < argc && (argv[optind])[0]!='-'; optind++, n_data++) {
darr[n_data] = atoi(argv[optind]);
}
break;
case 't':
chan[0] = atoi(optarg);
cmd = cmdTimer;
if( optind < argc && (argv[optind])[0]!='-' )
val = atoi(argv[optind++]);
break;
case 'd':
dev = optarg;
break;
......@@ -197,12 +239,6 @@ int main(int argc, char* argv[])
{
for( int k=0; chan[k]!=-1; k++ )
{
if( comedi_dio_config(card, subdev, chan[k],INSN_CONFIG_DIO_INPUT) < 0)
{
comedi_perror("can't configure DI channels");
exret = EXIT_FAILURE;
}
if( comedi_dio_read(card, subdev, chan[k],&data) < 0)
{
fprintf(stderr, "can't read from channel %d\n",chan[k]);
......@@ -216,14 +252,6 @@ int main(int argc, char* argv[])
case cmdDWrite:
{
for( int k=0; chan[k]!=-1; k++ )
{
if( comedi_dio_config(card, subdev, chan[k],INSN_CONFIG_DIO_OUTPUT) < 0 )
{
comedi_perror("can't configure DO channels");
exret = EXIT_FAILURE;
}
}
//
while(1)
{
......@@ -267,6 +295,35 @@ int main(int argc, char* argv[])
}
break;
case cmdAnRead:
{
comedi_insn insn;
memset(&insn, 0, sizeof(insn));
insn.insn = INSN_READ;
insn.n = 20;
insn.data = darr;
insn.subdev = subdev;
for( int k=0; chan[k]!=-1; k++ )
{
if( autoconf )
insn_config(card, subdev,chan[k],100,range,aref);
insn.chanspec = CR_PACK(chan[k], range, aref);
int ret = comedi_do_insn(card, &insn);
if( ret < 0)
{
fprintf(stderr, "can't read from channel %d: (%d) %s\n",chan,ret,strerror(ret));
exret = EXIT_FAILURE;
}
printf("Readed from channel %d: expected 20 samples, got %d samples\n", chan[k], ret);
for(int i = 0; i < ret; i++)
printf("Data[%d] = %d\n", i, darr[i]);
}
}
break;
case cmdAWrite:
{
for( int k=0; chan[k]!=-1; k++ )
......@@ -289,10 +346,10 @@ int main(int argc, char* argv[])
for( int k=0; chan[k]!=-1; k++ )
{
if( val != INSN_CONFIG_DIO_INPUT
&& val != INSN_CONFIG_DIO_OUTPUT
&& val != 100 /* INSN_CONFIG_AIO_INPUT */
&& val != 101 /* INSN_CONFIG_AIO_OUTPUT */
if( val != INSN_CONFIG_DIO_INPUT
&& val != INSN_CONFIG_DIO_OUTPUT
&& val != INSN_CONFIG_AIO_INPUT
&& val != INSN_CONFIG_AIO_OUTPUT
)
{
fprintf(stderr, "can't config channel %d for type = %d (val=[%d,%d,%d,%d])\n"
......@@ -301,6 +358,45 @@ int main(int argc, char* argv[])
}
}
}
case cmdExtConfig:
{
for( int k=0; chan[k]!=-1; k++ )
{
comedi_insn insn;
memset(&insn, 0, sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = n_data;
insn.data = darr;
insn.subdev = subdev;
insn.chanspec = CR_PACK(chan[k], 0, 0);
comedi_do_insn(card, &insn);
}
break;
}
case cmdTimer:
{
for( int k=0; chan[k]!=-1; k++ )
{
comedi_insn insn;
lsampl_t data[3];
data[0] = INSN_CONFIG_TIMER_1;
data[1] = val;
data[2] = autoscan;
memset(&insn, 0, sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = 3;
insn.data = data;
insn.subdev = subdev;
insn.chanspec = CR_PACK(chan[k], 0, 0);
comedi_do_insn(card, &insn);
}
break;
}
case cmdSubConfig:
insn_subdev_config(card, subdev, val);
......@@ -333,38 +429,38 @@ void insn_config( comedi_t* card, int subdev, int channel, lsampl_t iotype, int
void insn_subdev_config( comedi_t* card, int subdev, lsampl_t type )
{
lsampl_t cmd = 102;
static const unsigned char chans[4] = {0, 8, 16, 20}; /* We can configure only one channel per 8-bit port (4-bit for CL and CH). */
lsampl_t cmd[4]; /* Ports A, B, CL, CH */
comedi_insn insn;
memset(&insn,0,sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = 1;
insn.data = &cmd;
insn.unused[0] = type;
insn.subdev = subdev;
insn.chanspec = 0;
switch(type)
{
case 1:
case 1: /* TBI 24_0 */
cmd[0] = cmd[1] = cmd[2] = cmd[3] = INSN_CONFIG_DIO_INPUT;
printf("set subdev %d type: 'TBI 24/0'\n",subdev);
break;
case 2:
break;
case 2: /* TBI 0_24 */
default:
cmd[0] = cmd[1] = cmd[2] = cmd[3] = INSN_CONFIG_DIO_OUTPUT;
printf("set subdev %d type: 'TBI 0/24'\n",subdev);
break;
case 3:
break;
case 3: /* TBI 16_8 */
cmd[0] = cmd[1] = INSN_CONFIG_DIO_INPUT;
cmd[2] = cmd[3] = INSN_CONFIG_DIO_OUTPUT;
printf("set subdev %d type: 'TBI 16/8'\n",subdev);
break;
default:
printf("set subdev %d type: UNKNOWN\n",subdev);
break;
break;
}
if( comedi_do_insn(card,&insn) < 0 )
{
fprintf(stderr, "can`t configure subdev subdev=%d type=%d",subdev,type);
exit(EXIT_FAILURE);
for(int i = 0; i < 4; i++) {
memset(&insn,0,sizeof(insn));
insn.insn = INSN_CONFIG;
insn.n = 1;
insn.data = &cmd[i];
insn.subdev = subdev;
insn.chanspec = CR_PACK(chans[i], 0, 0);
if( comedi_do_insn(card,&insn) < 0 ) {
fprintf(stderr, "can`t configure subdev subdev=%d type=%d",subdev,type);
exit(EXIT_FAILURE);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment