TEST(ComediTest,AnalogReadings ) { lsampl_t data; int ret; int subdev = 0; int chan = 0; int range = 0; int aref = AREF_GROUND; int num_scans = options.n_scan; int num_channels = options.n_chan; int readscans; std::string prop("/sys/class/comedi/comedi0/read_buffer_pos"); dev = comedi_open(options.filename); ASSERT_TRUE( dev ); subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice); RecordProperty("NumScans", num_scans); #if 0 fprintf(stderr, "command before testing:\n"); dump_cmd(stderr, cmd); #endif prepare_cmd_lib(dev, options.subdevice, num_scans, num_channels, 1e9 / options.freq, cmd); #if 0 fprintf(stderr, "command before testing:\n"); dump_cmd(stderr, cmd); #endif /** * Setup the Command structure */ cmd->chanlist_len = 2; cmd->stop_arg = num_scans; /* verify no problems */ ret = comedi_command_test(dev, cmd); ASSERT_GE( ret, 0 ) << "Comedi Testing of command for device was ok"; /* make sure our scan number hasn't changed */ ASSERT_EQ( cmd->stop_arg , num_scans ); ASSERT_EQ( cmd->chanlist_len , 2 ); ret = comedi_command(dev, cmd); sleep(1); ASSERT_GE( ret, 0 ) << "Able to Submit comedi command\n"; /* Verify that the buffer position has in fact moved to say num_scans scans remain in it */ FILE *fp = fopen(prop.c_str(),"r"); ASSERT_TRUE( fp ) << "Able to open sysfs property file"; fscanf(fp,"%d",&readscans ); ASSERT_EQ( readscans, 4*num_scans ) << "Able to read the fifo position 4*" << num_scans << "\n"; fclose(fp); comedi_close( dev ); }
int main(int argc, char *argv[]) { lsampl_t data; int ret; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); device=comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } data = options.value; if(options.verbose){ printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", data, options.filename, options.subdevice, options.channel, options.range, options.aref); } ret = comedi_apply_calibration(device, options.subdevice, options.channel, options.range, options.aref, NULL); if(ret < 0){ comedi_perror(options.filename); exit(0); } return 0; }
double readpin(int chan) { comedi_t *it; lsampl_t data, maxdata; comedi_range *rang; int readres; double outval; if((it=comedi_open("/dev/comedi0"))==NULL) { printf("fail open"); } comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); readres=comedi_data_read(it,subdev,chan,0,aref, & data); if(!readres) { comedi_perror("comedi_data_read: "); } rang = comedi_get_range(it, subdev, chan, 0); maxdata = comedi_get_maxdata(it, subdev, chan); outval = comedi_to_phys(data, rang, maxdata); printf("data=%d\noutval=%g\nreadres=%d\n",data, outval, readres); return outval; }
static int init_board(void) { dev = comedi_open("/dev/comedi0"); printf("Comedi device (6071) handle: %p.\n", dev); if (!dev){ printf("Unable to open (6071) %s.\n", "/dev/comedi0"); return 1; } subdevai = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AI, 0); if (subdevai < 0) { comedi_close(dev); printf("AI subdev (6071) %d not found.\n", COMEDI_SUBD_AI); return 1; } comedi_get_krange(dev, subdevai, 0, AI_RANGE, &krangeai); maxdatai = comedi_get_maxdata(dev, subdevai, 0); subdevao = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AO, 0); if (subdevao < 0) { comedi_close(dev); printf("AO subdev (6071) %d not found.\n", COMEDI_SUBD_AO); return 1; } comedi_get_krange(dev, subdevao, 0, AO_RANGE, &krangeao); maxdatao = comedi_get_maxdata(dev, subdevao, 0); return 0; }
int init_daq(double min_range, double max_range, int range_update) { int i = 0; if (!DEV_OPEN) { it = comedi_open("/dev/comedi0"); if (it == NULL) { comedi_perror("comedi_open"); ADC_OPEN = FALSE; DEV_OPEN = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_ai = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AI, subdev_ai); if (subdev_ai < 0) { return -2; ADC_OPEN = FALSE; } subdev_ao = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AO, subdev_ao); if (subdev_ao < 0) { HAS_AO = FALSE; } else { HAS_AO = TRUE; } printf("Subdev AI %i ", subdev_ai); channels_ai = comedi_get_n_channels(it, subdev_ai); printf("Analog Channels %i ", channels_ai); maxdata_ai = comedi_get_maxdata(it, subdev_ai, i); printf("Maxdata %i ", maxdata_ai); ranges_ai = comedi_get_n_ranges(it, subdev_ai, i); printf("Ranges %i ", ranges_ai); ad_range = comedi_get_range(it, subdev_ai, i, ranges_ai - 1); if (range_update) { ad_range->min = min_range; ad_range->max = max_range; } printf(": ad_range .min = %.3f, max = %.3f\n", ad_range->min, ad_range->max); if (HAS_AO) { printf("Subdev AO %i ", subdev_ao); channels_ao = comedi_get_n_channels(it, subdev_ao); printf("Analog Channels %i ", channels_ao); maxdata_ao = comedi_get_maxdata(it, subdev_ao, i); printf("Maxdata %i ", maxdata_ao); ranges_ao = comedi_get_n_ranges(it, subdev_ao, i); printf("Ranges %i ", ranges_ao); da_range = comedi_get_range(it, subdev_ao, i, ranges_ao - 1); printf(": da_range .min = %.3f, max = %.3f\n", da_range->min, da_range->max); } ADC_OPEN = TRUE; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); return 0; }
int io_init(ElevatorType type){ elevatorType = type; switch(elevatorType){ case ET_comedi: it_g = comedi_open("/dev/comedi0"); if (it_g == NULL) { return 0; } int status = 0; for (int i = 0; i < 8; i++) { status |= comedi_dio_config(it_g, PORT_1_SUBDEVICE, i + PORT_1_CHANNEL_OFFSET, PORT_1_DIRECTION); status |= comedi_dio_config(it_g, PORT_2_SUBDEVICE, i + PORT_2_CHANNEL_OFFSET, PORT_2_DIRECTION); status |= comedi_dio_config(it_g, PORT_3_SUBDEVICE, i + PORT_3_CHANNEL_OFFSET, PORT_3_DIRECTION); status |= comedi_dio_config(it_g, PORT_4_SUBDEVICE, i + PORT_4_CHANNEL_OFFSET, PORT_4_DIRECTION); } return (status == 0); case ET_simulation: //simulation_elevator_start(); return 1; default: return 0; } }
int io_init() { int i; int res; it_g = comedi_open("/dev/comedi0"); if (it_g == NULL) return -1; // init 3 inputs and 3 outputs for(i=1; i<4; i++) { res = comedi_dio_config(it_g, IO_DEV, DI(i), COMEDI_INPUT); if (res<0) return res; res = comedi_dio_config(it_g, IO_DEV, DO(i), COMEDI_OUTPUT); if (res<0) return res; res = comedi_dio_write(it_g, IO_DEV, DO(i), 0); if (res<0) return res; } // set outputs to high for(i=1; i<4; i++) { comedi_dio_write(it_g, IO_DEV, DO(i), 1); } return 1; }
int init_dio(void) { int i = 0; if (!DEV_OPEN) { it = comedi_open(comedi_dev); if (it == NULL) { comedi_perror("comedi_open"); DIO_OPEN = FALSE; DEV_OPEN = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_dio = comedi_find_subdevice_by_type(it, COMEDI_SUBD_DIO, subdev_dio); if (subdev_dio < 0) { return -1; DIO_OPEN = FALSE; } printf("Subdev %i ", subdev_dio); channels_dio = comedi_get_n_channels(it, subdev_dio); printf("Digital Channels %i ", channels_dio); maxdata_dio = comedi_get_maxdata(it, subdev_dio, i); printf("Maxdata %i ", maxdata_dio); ranges_dio = comedi_get_n_ranges(it, subdev_dio, i); printf("Ranges %i \n", ranges_dio); DIO_OPEN = TRUE; return 0; }
int main(int argc, char *argv[]) { comedi_cmd cmd; int i; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ perror(options.filename); exit(1); } fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK); for(i = 0; i < n_chans; i++){ chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref); } prepare_cmd(device, &cmd, options.subdevice); do_cmd(device, &cmd); return 0; }
bool ComediAnalogIO::openDevice() { Logger(Debug, "ComediAnalogIO::openDevice()\n"); m_device = comedi_open(m_deviceFile); if(m_device == NULL) { comedi_perror(m_deviceFile); return false; } return true; }
int main ( int argc, char *argb[] ) { comedi_t *it; lsampl_t data; it = comedi_open("/dev/comedi0"); int i = 0; for ( i = 0; i < 16; i++ ) { comedi_data_read(it, subdev, i, range, aref, &data); printf("%d\n", data); } }
int com_enable() /* Enable bus communication */ { int r = 0; char filestr[MEDSTR]; memset(filestr, 0, MEDSTR); sprintf(filestr, "/dev/comedi%d", Comedi_file); printf("%s\n", filestr); if ((it = comedi_open(filestr)) == NULL ) r = -1; // printf("io card enabled\n"); return r; }
int main(int argc, char *argv[]) { struct sigaction sa; int ret; sigset_t sigset; int flags; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ perror(options.filename); exit(1); } out_subd = 2; config_output(); fcntl(comedi_fileno(device), F_SETOWN, getpid()); flags = fcntl(comedi_fileno(device),F_GETFL); ret = fcntl(comedi_fileno(device),F_SETFL,flags|O_ASYNC); //ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC); if(ret<0)perror("fcntl"); memset(&sa,0,sizeof(sa)); sa.sa_handler = &sigio_handler; ret = sigaction(SIGIO,&sa,NULL); if(ret<0)perror("sigaction"); sigemptyset(&sigset); sigaddset(&sigset,SIGIO); ret = sigprocmask(SIG_UNBLOCK,&sigset,NULL); if(ret<0)perror("sigprocmask"); #if 0 { struct sched_param p; memset(&p,0,sizeof(p)); p.sched_priority = 1; ret = sched_setscheduler(0,SCHED_FIFO,&p); if(ret<0)perror("sched_setscheduler"); } #endif do_cmd_1(device, options.subdevice); return 0; }
static comedi_t *lookup_dev( unsigned dev ) { static char devname[] = "/dev/comedixx"; if( dev >= 16 ) { fprintf( stderr, "Comedi device number %d too large.\n", dev ); return NULL; } if( !devices[dev] ) { sprintf( devname, "/dev/comedi%u", dev ); devices[dev] = comedi_open( devname ); if( !devices[dev] ) comedi_perror( devname ); } return devices[dev]; }
extern uchar init(char* dev) { uint i, nchan; ni = comedi_open(dev); if (ni == NULL) { printf("Error opening device\n"); return -1; } nchan = comedi_get_n_channels(ni, 0); printf("found comedi system with %d channels\n", nchan); for (i=0; i < nchan; i++) { if (comedi_dio_config(ni, 0, i, COMEDI_OUTPUT) < 0) return -1; } return 0; }
bool ComediDigitalIO::openDevice() { Logger(Debug, "ComediDigitalIO::openDevice()\n"); m_device = comedi_open(m_deviceFile); if(m_device == NULL) { comedi_perror(m_deviceFile); return false; } if (comedi_get_subdevice_type(m_device, m_subdevice) != COMEDI_SUBD_DIO) { Logger(Critical, "ComediDigitalIO::openDevice() - Subdevice is not DIO.\n"); if (!closeDevice()) Logger(Critical, "ComediDigitalIO::openDevice() - Failed to close device.\n"); return false; } return true; }
int io_init(void) { it_g = comedi_open("/dev/comedi0"); if (it_g == NULL) { return 0; } int status = 0; for (int i = 0; i < 8; i++) { status |= comedi_dio_config(it_g, PORT_1_SUBDEVICE, i + PORT_1_CHANNEL_OFFSET, PORT_1_DIRECTION); status |= comedi_dio_config(it_g, PORT_2_SUBDEVICE, i + PORT_2_CHANNEL_OFFSET, PORT_2_DIRECTION); status |= comedi_dio_config(it_g, PORT_3_SUBDEVICE, i + PORT_3_CHANNEL_OFFSET, PORT_3_DIRECTION); status |= comedi_dio_config(it_g, PORT_4_SUBDEVICE, i + PORT_4_CHANNEL_OFFSET, PORT_4_DIRECTION); } return (status == 0); }
int io_init() { int i, status = 0; it_g = comedi_open("/dev/comedi0"); if (it_g == NULL) return 0; for (i = 0; i < 8; i++) { status |= comedi_dio_config(it_g, PORT1, i, COMEDI_INPUT); status |= comedi_dio_config(it_g, PORT2, i, COMEDI_OUTPUT); status |= comedi_dio_config(it_g, PORT3, i+8, COMEDI_OUTPUT); status |= comedi_dio_config(it_g, PORT4, i+16, COMEDI_INPUT); } return (status == 0); }
int main(int argc, char *argv[]) { int ret; int stype; int i; struct parsed_options options; init_parsed_options(&options); options.subdevice = -1; parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } if(options.subdevice < 0) { options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_DIO, 0); if(options.subdevice < 0){ fprintf(stderr,"No dio subdevice found.\n"); exit(-1); } } stype = comedi_get_subdevice_type(device, options.subdevice); if(stype != COMEDI_SUBD_DIO){ printf("%d is not a digital I/O subdevice\n", options.subdevice); exit(-1); } printf("configuring pin %d for output...\n", chan_dat); ret = comedi_dio_config(device, options.subdevice, chan_dat, COMEDI_OUTPUT); printf("configuring pin %d for output...\n", chan_clk); ret = comedi_dio_config(device, options.subdevice, chan_clk, COMEDI_OUTPUT); for(i = 0; i < 0x100; i++){ write_bits(options.subdevice, i); } //write_bits(0xa5); return 0; }
int init_daq(void) { int i = 0, range_index = 0; comedi_range *ad_range; if (!DEV_OPEN) { it = comedi_open(comedi_dev); if (it == NULL) { comedi_perror("comedi_open"); ADC_OPEN = FALSE; DEV_OPEN = FALSE; HAVE_DIO = FALSE; HAVE_AI = FALSE; return -1; } DEV_OPEN = TRUE; } subdev_ai = comedi_find_subdevice_by_type(it, COMEDI_SUBD_AI, subdev_ai); if (subdev_ai < 0) { return -1; ADC_OPEN = FALSE; } printf("Subdev %i ", subdev_ai); channels_ai = comedi_get_n_channels(it, subdev_ai); printf("Analog Channels %i ", channels_ai); maxdata_ai = comedi_get_maxdata(it, subdev_ai, i); printf("Maxdata %i ", maxdata_ai); ranges_ai = comedi_get_n_ranges(it, subdev_ai, i); printf("Ranges %i ", ranges_ai); for (range_index = 0; range_index < ranges_ai; range_index++) { ad_range = comedi_get_range(it, subdev_ai, i, range_index); printf(": range %i, min = %.1f, max = %.1f ", range_index, ad_range->min, ad_range->max); } printf("\n"); ADC_OPEN = TRUE; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); return 0; }
int main(int argc, char *argv[]) { int ret; comedi_cmd cmd; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ perror(options.filename); exit(1); } out_subd = 0; config_output(); ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC); if(ret<0)perror("fcntl"); #if 0 { struct sched_param p; memset(&p,0,sizeof(p)); p.sched_priority = 1; ret = sched_setscheduler(0,SCHED_FIFO,&p); if(ret<0)perror("sched_setscheduler"); } #endif prepare_cmd(device, &cmd, options.subdevice); do_cmd(device,&cmd); return 0; }
void __po_hi_c_driver_exarm_ni_6071e_digital_init (__po_hi_device_id device_id) { int ret; po_hi_driver_exarm_ni_6071e_digital_it = comedi_open("/dev/comedi0"); ret = comedi_dio_config(po_hi_driver_exarm_ni_6071e_digital_it, 2, 1, COMEDI_INPUT); if (ret == -1) { __DEBUGMSG ("Error when invoking comedi_dio_config()"); } ret = comedi_dio_config(po_hi_driver_exarm_ni_6071e_digital_it, 2, 0, COMEDI_INPUT); if (ret == -1) { __DEBUGMSG ("Error when invoking comedi_dio_config()"); } return; }
int main(int argc, char *argv[]) { int retval; lsampl_t routing; struct parsed_options options; init_parsed_options(&options); options.freq = 0.; parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } routing = options.value; printf("Selecting routing %d for channel %d on subdevice %d.\n", routing, options.channel, options.subdevice); retval = comedi_set_routing(device, options.subdevice, options.channel, routing); if(retval < 0) comedi_perror("comedi_do_insn"); return retval; }
int main() { it=comedi_open("/dev/comedi0"); int ret; if(cmd==NULL) { cmd=malloc(sizeof(*cmd)); if(cmd==NULL) return -1; } else { if(cmd->chanlist) free(cmd->chanlist); } comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); ret = prepare_cmd(COMEDI_SUBD_AI,cmd,freq); printf("B");fflush(stdout); //comedi_get_cmd_generic_timed(dev,subdevice,cmd,0,1e9/freq); if(ret<0){ printf("comedi_get_cmd_generic_timed failed\n"); return ret; } /* Modify parts of the command */ cmd->scan_end_arg = 0; cmd->stop_arg = 0; setup_read_wave(COMEDI_SUBD_AI, 0, 0, 100); ret = comedi_command(it,cmd); if(ret) printf("comedi_command (acq): %d\n", ret); internal_trigger(); comedi_cancel(it, 0); comedi_cancel(it, 1); }
int main(int argc, char *argv[]) { int ret; int stype; struct parsed_options options; init_parsed_options(&options); parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } stype = comedi_get_subdevice_type(device, options.subdevice); if(stype != COMEDI_SUBD_DIO){ printf("%d is not a digital I/O subdevice\n", options.subdevice); exit(-1); } printf("configuring pin %d on subdevice %d ", options.channel, options.subdevice); if(options.value) { printf("for output.\n"); ret = comedi_dio_config(device, options.subdevice, options.channel, COMEDI_OUTPUT); }else { printf("for input.\n"); ret = comedi_dio_config(device, options.subdevice, options.channel, COMEDI_INPUT); } if(ret < 0){ comedi_perror("comedi_dio_config"); exit(-1); } return 0; }
int cal() { int subdevai, subdevao; // printf("calibrating.."); fflush(stdout); devc = comedi_open("/dev/comedi0"); if (!devc) { printf("Unable to open (6071) %s.\n", "/dev/comedi0"); return 1; } subdevai = comedi_find_subdevice_by_type(devc, COMEDI_SUBD_AI, 0); subdevao = comedi_find_subdevice_by_type(devc, COMEDI_SUBD_AO, 0); comcal = comedi_parse_calibration_file("/usr/var/lib/comedi/calibrations/ni_pcimio_pci-6289_comedi0"); comedi_get_softcal_converter(subdevai,0, 0, COMEDI_TO_PHYSICAL, comcal, &conv_ai); comedi_get_softcal_converter(subdevao,0, 0, COMEDI_FROM_PHYSICAL, comcal, &conv_ao); //comedi_cleanup_calibration_file(cal); comedi_close(devc); //printf("done\n");fflush(stdout); return 0; }
int main(int argc, char *argv[]) { unsigned period_ns; int retval; lsampl_t clock_selection; struct parsed_options options; init_parsed_options(&options); options.freq = 0.; parse_options(&options, argc, argv); device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } if(options.freq > 0.) period_ns = 1e9 / options.freq; else period_ns = 0; clock_selection = options.value; printf("Selecting master clock %d for channel %d on subdevice %d.\n", clock_selection, options.channel, options.subdevice); if(period_ns) { printf("Clock period = %d nanoseconds.\n", period_ns); }else { printf("Clock period unspecified.\n"); } retval = comedi_set_clock_source(device, options.subdevice, options.channel, clock_selection, period_ns); if(retval < 0) comedi_perror("comedi_set_clock_source"); comedi_close(device); return retval; }
static long dsetInit_devDiSyncComedi (biRecord *pbi) { comediSupport *p_myDigitalInput; #ifdef DEBUG1 packageInfo(); printf( __FILE__ "[%d] -> %s \n", __LINE__, __func__ ); #endif #ifdef DEBUG2 printf( __FILE__ "[%d] Initializing %s\n", __LINE__, pbi->name); #endif p_myDigitalInput=(comediSupport *)calloc(1,sizeof(comediSupport)); /* Get the options from the db file */ sscanf(pbi->inp.value.instio.string, "d=%s s=%d c=%d", p_myDigitalInput->filename, &p_myDigitalInput->subdevice, &p_myDigitalInput->channel); #ifdef DEBUG2 printf(__FILE__ "[%d] Parsed options >%s<\n", __LINE__, pbi->inp.value.instio.string); printf(__FILE__ "[%d] filename = >%s<\n",__LINE__,p_myDigitalInput->filename); printf(__FILE__ "[%d] subdevice = %d\n",__LINE__, p_myDigitalInput->subdevice); printf(__FILE__ "[%d] channel = %d\n",__LINE__, p_myDigitalInput->channel); #endif p_myDigitalInput->device=comedi_open(p_myDigitalInput->filename); /* p_myDigitalInput->maxdata=comedi_get_maxdata( p_myDigitalInput->device, p_myDigitalInput->subdevice, p_myDigitalInput->channel); p_myDigitalInput->c_range=comedi_get_range( p_myDigitalInput->device, p_myDigitalInput->subdevice, p_myDigitalInput->channel, p_myDigitalInput->range); */ if ( initComediDI(p_myDigitalInput) <= ERROR ) { printf(__FILE__ "[%d] Error initializing digital input (%s)\n", __LINE__, pbi->name); sleep(SLEEPTIME_ERROR); return(ERROR); } pbi->dpvt=(void *)p_myDigitalInput; /* initialize the hardware to match the epics record value (probably 0) */ if (readComediDI((unsigned int *)&pbi->val,(comediSupport *)pbi->dpvt) <= ERROR) { printf(__FILE__ "[%d] Error: Couldn't read %s\n", __LINE__, pbi->name); sleep(SLEEPTIME_ERROR); return(ERROR); } #ifdef DEBUG1 printf( __FILE__ "[%d] <- %s\n", __LINE__, __func__); #endif return(SUCCESS); }
/** * Start the the ADC sampling, unless the external trigger command * is set it will begin sampling as soon as the usbdux device has been * initialized. * The final three arguments are very important to get right * as the recorder thread will use them to determine where to write it's samples and * when it's reached end of the buffer. It doesn't care about the c_buffer class * at all. * @param arg_device name of the comedi device e.g., 'comedi0' * @param arg_sample_rate the rate the usb_dux should sample with in hertz (max=3[Mhz]}) * @param arg_start_address the start address of the circle buffer sample should be loading into * @param arg_end_address the end address of the cicle buffer. * @param arg_buffer_size byte size of the buffer. */ int start_Sampling(char* arg_device, uint32_t arg_sample_rate, char* arg_start_address, char* arg_end_address, int arg_buffer_size){ _channel_amount = 16; _sample_rate = arg_sample_rate; unsigned int chanlist[_channel_amount]; unsigned int convert_arg = 1e9 / _sample_rate; int ret; comedi_cmd *cmd = (comedi_cmd *) calloc(1, sizeof(comedi_cmd)); _device = comedi_open(arg_device); int buffer = 1048576*40; buffer = (buffer * 4096)/4096; if(comedi_set_max_buffer_size(_device, 0, buffer) < 0 ){ printf("Failed to set max buffer size to %i bytes\n", buffer); return -1; } else printf("Maximum buffer size set to %i bytes\n", comedi_get_max_buffer_size(_device,0)); if(comedi_set_buffer_size(_device, 0, buffer) < 0){ printf("Failed to set buffer size to %iBytes\n", buffer); return -1; } else printf("Buffer size set to %iBytes\n", comedi_get_buffer_size(_device,0)); if(!_device){ errx(1, "unable to open device"); comedi_perror("/dev/comedi0"); return -1; } for(int i = 0; i < _channel_amount; i++) chanlist[i] = i; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); if((ret = comedi_get_cmd_generic_timed(_device, 0, cmd, _channel_amount,0)) < 0){ printf("comedi_get_cmd_generic_timed failed\n"); return ret; } cmd->chanlist = chanlist; cmd->stop_src = TRIG_NONE; cmd->stop_arg = 0; cmd->convert_arg = convert_arg; //setup the sampling to start from the trigger. //cmd->start_src = TRIG_EXT //cmd->start_arg = 1; /* call test twice because different things are tested? * if tests are successful run sampling command */ if((ret = comedi_command_test(_device, cmd)) != 0 || (ret = comedi_command_test(_device, cmd)) != 0 || (ret = comedi_command(_device, cmd)) < 0){ fprintf(stderr, "err: %d\n", ret); return -1; } FILE* dux_fp; if((dux_fp = fdopen(comedi_fileno(_device), "r")) <= 0) comedi_perror("fdopen"); char* write_address = arg_start_address; Utility::SNAP_SAMPLE = 0; Utility::LAST_SAMPLE = 0; uint64_t active_sample = 0; uint32_t tmp_block = 0; int samples_pr_block = 4096/sizeof(Type); while((ret = fread(write_address, 1, 4096,dux_fp)) >= 0){ write_address += 4096; tmp_block += 1; active_sample += samples_pr_block; Utility::LAST_SAMPLE += samples_pr_block; if(tmp_block >= Utility::SNAPSHOT_BLOCK_SIZE){ //printf("signalling serial snapshotter\n"); tmp_block = 0; Utility::SNAP_SAMPLE = active_sample; Utility::SNAP_READY = true; Utility::CV.notify_one(); } if(write_address == arg_end_address){ write_address -= arg_buffer_size; //printf("resetting to beginning of buffer\n"); } if(_stop == true) break; } if(ret < 0) perror("read"); comedi_cancel(_device, 0); return 0; }
int main(int argc, char *argv[]) { int ret; comedi_insn insn; lsampl_t d[5]; comedi_t *device; int freq; struct parsed_options options; init_parsed_options(&options); options.freq = -1; // we hijack this option to switch it on or off options.n_scan = -1; options.value = -1; parse_options(&options, argc, argv); if ((options.value==-1)&&(options.n_scan==-1)&&(options.freq==-1)) { fprintf(stderr, "Usage: %s OPTIONS duty_cycle\n" "options: \n" " -N 0 switches PWM off\n" " -N 1 switches PWM on\n" " -N 2 enquires the max value for the duty cycle\n" " -F FREQ sets the PWM frequency\n", argv[0]); } device = comedi_open(options.filename); if(!device){ comedi_perror(options.filename); exit(-1); } options.subdevice = comedi_find_subdevice_by_type(device,COMEDI_SUBD_PWM,0); if (options.verbose) printf("PWM subdevice autodetection gave subdevice number %d\n", options.subdevice); if(options.n_scan==2) { printf("%d\n",comedi_get_maxdata(device,options.subdevice,0)); comedi_close(device); exit(0); } insn.insn=INSN_CONFIG; insn.data=d; insn.subdev=options.subdevice; insn.chanspec=CR_PACK(0,0,0); if(options.n_scan==1) { d[0] = INSN_CONFIG_ARM; d[1] = 0; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not switch on:%d\n",ret); comedi_perror(options.filename); exit(-1); } } if(options.n_scan==0) { d[0] = INSN_CONFIG_DISARM; d[1] = 0; insn.n=1; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not switch off:%d\n",ret); comedi_perror(options.filename); exit(-1); } } if(options.freq>0) { freq = options.freq; d[0] = INSN_CONFIG_PWM_SET_PERIOD; d[1] = 1E9/freq; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could set frequ:%d\n",ret); comedi_perror(options.filename); exit(-1); } } d[0] = INSN_CONFIG_GET_PWM_STATUS; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could not get status:%d insn=%d\n", ret, d[0]); comedi_perror(options.filename); exit(-1); } if (options.verbose) { if (d[1]) fprintf(stderr, "PWM is on.\n"); else fprintf(stderr, "PWM is off.\n"); } d[0] = INSN_CONFIG_PWM_GET_PERIOD; insn.n=2; ret=comedi_do_insn(device,&insn); if(ret < 0){ fprintf(stderr,"Could get frequ:%d\n",ret); comedi_perror(options.filename); exit(-1); } freq = 1E9 / d[1]; if (options.verbose) fprintf(stderr,"PWM frequency is %d\n", freq); if (options.value>=0) if(comedi_data_write(device, options.subdevice, options.channel, 0, 0, options.value)<0) { fprintf(stderr,"error setting the pwm duty cycle on "); comedi_perror(options.filename); exit(1); } return 0; }