pdv_initcam.c

Go to the documentation of this file.
00001 
00008 #include "edtinc.h"
00009 
00010 #include "libedt_timing.h"
00011 #include "pdv_irig.h"
00012 
00013 /* shorthand debug level */
00014 #define INITCAM_MSG_ALWAYS EDT_MSG_FATAL
00015 
00016 /*
00017 * static prototypes
00018 */
00019 int     is_hex_byte_command(char *str);
00020 void    check_terminator(char *str);
00021 void    dir_from_path(char *dirstr, const char *pathstr);
00022 void    fname_from_path(char *fname, char *pathstr);
00023 int     async_bitfile(EdtDev *edt_p);
00024 char   *grepit(char *buf, char *pat);
00025 int     is_hex_byte_command(char *str);
00026 int     findid(char *str, char *idstr);
00027 char   *strip_crlf(char *str);
00028 void    propeller_sleep(int n);
00029 char   *search_for_bitfile(char *rbtfile, const char *cfgfname, char *bitpath);
00030 int     init_serial(EdtDev * edt_p, Dependent * dd_p, Edtinfo *ei_p);
00031 int     init_serial_basler_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init);
00032 int     init_serial_duncan_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init);
00033 int     init_serial_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init);
00034 int     init_serial_ascii(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init);
00035 int     send_xilinx_commands(EdtDev * edt_p, char *str);
00036 int     check_roi(EdtDev * edt_p, Dependent * dd_p, int ena);
00037 void    check_camera_values(EdtDev * ed, Dependent * dd_p);
00038 int     kodak_query_serial(EdtDev * edt_p, char *cmd, int *val);
00039 int     specinst_download(EdtDev * edt_p, char *fname);
00040 int     specinst_setparams(EdtDev * edt_p, char *fname);
00041 void    setup_cl2_simulator(EdtDev *edt_p, Dependent *dd_p);
00042 char   *serial_tx_rx(PdvDev * pdv_p, char *command, int hexin);
00043 void    do_xregwrites(EdtDev *edt_p, Dependent *dd_p);
00044 static int set_rci = 0;
00045 static int rci_set_unit = 0;
00046 static long    isascii_str(u_char * buf, int n);
00047 
00048 void
00049 pdv_initcam_set_rci(EdtDev * edt_p, int rci_unit)
00050 {
00051     rci_set_unit = rci_unit;
00052     set_rci = 1;
00053 }
00054 
00055 /*
00056 * initcam internal function -- assumes dependent struct has already been loaded from
00057 * pdv_readcfg() or equivalent.
00058 */
00059 
00060 int
00061 pdv_channel_initialized(int unit, int channel)
00062 
00063 {
00064     PdvDev *pdv_p;
00065 
00066     pdv_p = pdv_open_channel(EDT_INTERFACE,unit, channel);
00067 
00068     if (pdv_p && pdv_get_width(pdv_p) && pdv_get_height(pdv_p))
00069     {
00070         return 1;
00071     }
00072 
00073     return 0;
00074 }
00075 
00134 int
00135 pdv_initcam(EdtDev * pdv_p, Dependent * dd_p, int unit, Edtinfo * ei_p,
00136             const char *cfgfname, char *bitdir, int pdv_debug)
00137 {
00138 
00139     char errmsg[256];
00140 
00141     /* make sure the device is open and valid */
00142     if ((pdv_p == NULL) || (dd_p == NULL) || (pdv_p->fd == 0))
00143     {
00144         edt_msg(EDT_MSG_FATAL, "ERROR: invalid dependent struct pointer\n");
00145         return -1;
00146     }
00147 
00148 #if 0 /* this doesn't work since is_dvcl2 includes PCIe8 and PCIe4 even if they're not configured as a simulator -- need a way to detect that!!! */
00149     if (edt_is_dvcl2(pdv_p))
00150     {
00151         edt_msg(EDT_MSG_FATAL, "ERROR: this is a simulator (output) board -- use clsimit to initialize\n");
00152         return -1;
00153     }
00154 #endif
00155 
00156     /*
00157     * reality check on some critical input variables
00158     */
00159     if ((dd_p->camera_class[0] == '\0')
00160         || (dd_p->width == 0)
00161         || (dd_p->height == 0)
00162         || (dd_p->depth < 8)
00163         || (dd_p->depth > 32)
00164         || (dd_p->extdepth < 8)
00165         || (dd_p->extdepth > 32)
00166         || (dd_p->depth & 1)
00167         || (dd_p->extdepth & 1))
00168     {
00169         edt_msg(PDVLIB_MSG_WARNING, "WARNING: one or more critical config values appear invalid\n");
00170         sprintf(errmsg, "check readcfg/dependent struct and camera_class, width, height, depth, extdepth\n");
00171         edt_msg(DEBUG2, errmsg);
00172     }
00173 
00174     /*
00175     * pdv_p->dd_p is not necessarily used here, and is normally set by
00176     * pdv_open, but initcam is a special case
00177     */
00178     if (pdv_p->dd_p == 0) /* must've been opened with edt_open (or separately freed?) */
00179         pdv_p->dd_p = dd_p;
00180     else if (pdv_p->dd_p != dd_p)
00181         memcpy(pdv_p->dd_p, dd_p, sizeof(Dependent));
00182 
00183     /* IMPORTANT: use pdv_p->dd_p not just dd_p from here on out in this function */
00184 
00185     if (pdv_p->devid == PDVFOI_ID)
00186     {
00187         edt_msg(EDT_MSG_FATAL, "FOI not supported after v4.1.5.9\n");
00188     }
00189     else
00190     {
00191         edt_reset_serial(pdv_p);
00192 
00193         if (pdv_p->dd_p->rbtfile[0])
00194         {
00195             /* no bitload if a single FPGA */
00196             if (edt_has_combined_fpga(pdv_p) )
00197             {
00198                 edt_msg(DEBUG1, "Combined FPGA, skipping xilinx load\n");
00199             }
00200             else if (strcmp(pdv_p->dd_p->rbtfile, "_SKIPPED_") == 0)
00201                 edt_msg(INITCAM_MSG_ALWAYS, "skipping bitload\n");
00202             else if (edt_is_dvfox(pdv_p) && pdv_p->channel_no > 0)
00203             {
00204                 edt_msg(DEBUG1, "DV FOX channel %d skipping bit load\n",
00205                     pdv_p->channel_no);
00206 
00207                 if (!pdv_channel_initialized(pdv_p->unit_no, 0))
00208                 {
00209                     edt_msg(EDT_MSG_FATAL, "You must initialize channel 0 before initializing any other channels on the DV-FOX\n");
00210                 }
00211             }
00212             else if (pdv_initcam_load_bitfile(pdv_p, pdv_p->dd_p, unit, bitdir, cfgfname))
00213             {
00214                 edt_msg(EDT_MSG_FATAL, "ERROR: Failed bitload\n");
00215                 return -1;
00216             }
00217         }
00218         else edt_msg(DEBUG1, "No bitfile specified, skipping xilinx load\n");
00219 
00220         if (pdv_initcam_reset_camera(pdv_p, pdv_p->dd_p, ei_p))
00221         {
00222             edt_msg(EDT_MSG_FATAL, "ERROR: Failed camera reset\n");
00223             return -1;
00224         }
00225     }
00226 
00227     return 0;
00228 }
00229 
00230 
00231 
00239 Dependent *
00240 pdv_alloc_dependent()
00241 {
00242     int     dsize = sizeof(Dependent);
00243 
00244     /*
00245     * check dependent size, not over limit allocated for it
00246     */
00247     if (dsize > EDT_DEPSIZE)
00248     {
00249         edt_msg(EDT_MSG_FATAL, "libpdv internal error: bad dependent struct size (%d) s/b %d\n", dsize, EDT_DEPSIZE);
00250         return NULL;
00251     }
00252 
00253     return (Dependent *) calloc(EDT_DEPSIZE, 1);
00254 }
00255 
00256 
00257 void
00258 dep_wait(EdtDev * edt_p)
00259 {
00260 }
00261 
00262 /*
00263 * isapromaddress -- must be XXXXX.mmmm-YYYYY.mmmmo, where
00264 * XXXXX and YYYYY are 5-digit (or more) hex addresses, and mmmm and nnnnn
00265 * are the respective sizes in decimal  to download
00266 */
00267 int
00268 get_prom_addrs(char *str, u_int *addr1, int *size1, u_int *addr2, int *size2)
00269 {
00270 
00271     if (str == NULL)
00272         return 0;
00273 
00274     if (sscanf(str, "%x.%d-%x.%d", addr1, size1, addr2, size2) != 4)
00275     {
00276         *addr1 = 0;
00277         *addr2 = 0;
00278         *size1 = 0;
00279         *size2 = 0;
00280         return -1;
00281     }
00282     return 0;
00283 }
00284 
00285 int
00286 pdv_initcam_load_bitfile(EdtDev * edt_p,
00287                          Dependent * dd_p,
00288                          int unit,
00289                          char *bitdir,
00290                          const char *cfgfname)
00291 {
00292     char    dir_arg[256];
00293     char    cfgdir[256];
00294     int     emb = 0;
00295     int     ret, flags=0;
00296 #ifdef NO_FS
00297     char bitname[128];
00298     int  len;
00299 #endif
00300 
00301     cfgdir[0] = '\0';
00302 
00303     edt_msg(DEBUG2, "pdv_initcam_load_bitfile('%s')\n", dd_p->rbtfile);
00304 
00305     if (strcmp(bitdir, "_NOFS_") == 0)
00306     {
00307         emb = 1;
00308         flags = BITLOAD_FLAGS_NOFS;
00309     }
00310 
00311 
00312     if (edt_p->devid == DMY_ID)
00313     {
00314 #if 0
00315         edt_flush_fifo(edt_p);
00316 #endif
00317         dd_p->serial_respcnt = 4;
00318     }
00319     else
00320     {
00321         u_int addr1, addr2;
00322         int size1, size2;
00323 
00324         dd_p->serial_respcnt = 2;
00325         /*
00326         * if -b flag specified, send "-b dirname". if not, strip off leading
00327         * dir from filename and if that exists, send -d dirname otheriwise
00328         * just send "-d camera_config" and hope there's a
00329         * camera_config/bitfiles
00330         */
00331 
00332         if (get_prom_addrs(dd_p->rbtfile, &addr1, &size1, &addr2, &size2) == 0)
00333         {
00334 
00335             edt_msg(DEBUG1, "loading camera xilinx from PROM @ %x %d / %x %d", addr1, size1, addr2, size2);
00336             ret = edt_bitload_from_prom(edt_p, addr1, size1, addr2, size2, flags);
00337             printf("\n");
00338         }
00339         else
00340         {
00341 #ifdef NO_FS
00342             if (emb)
00343             {
00344                 /* strip off .bit from name if nofs (embedded) xilinx */
00345                 strcpy(bitname, dd_p->rbtfile);
00346                 len = strlen(bitname);
00347                 if ((len >= 4) && (strcasecmp(&bitname[len-4], ".bit") == 0))
00348                     bitname[len-4] = '\0';
00349                 edt_msg(EDT_MSG_INFO_1, "loading embedded camera xilinx %s....\n", bitname);
00350                 ret = edt_bitload(edt_p, bitdir, bitname, 1, 0);
00351                 if (edt_is_dvfox(edt_p))
00352                     edt_msleep(500);
00353                 edt_flush_fifo(edt_p);
00354                 return ret;
00355             }
00356 #endif
00357             if (!emb)
00358             {
00359                 if (*bitdir)
00360                     strcpy(dir_arg, bitdir);
00361                 else
00362                 {
00363                     dir_from_path(cfgdir, cfgfname);
00364                     sprintf(bitdir, "%s/bitfiles", cfgdir);
00365 
00366                     if ((!(*cfgdir)) || (pdv_access(bitdir, 0) != 0))
00367                         strcpy(cfgdir, "camera_config");
00368 
00369                     strcpy(dir_arg, cfgdir);
00370                 }
00371             }
00372 
00373             edt_msg(DEBUG1, "loading camera xilinx %s....\n", dd_p->rbtfile);
00374 
00375             if ((ret = edt_bitload(edt_p, dir_arg, dd_p->rbtfile, 0, 0)) != 0)
00376                 return ret;
00377         }
00378 
00379         if (edt_is_dvfox(edt_p))
00380             edt_msleep(500);
00381 
00382         edt_flush_fifo(edt_p);
00383     }
00384 
00385     return 0;
00386 }
00387 
00388 
00389 
00394 int
00395 pdv_initcam_reset_camera(EdtDev * edt_p, Dependent * dd_p, Edtinfo * ei_p)
00396 {
00397     int     ret = 0;
00398     int     data_path = 0;
00399 
00400     if (dd_p->xilinx_rev == NOT_SET)
00401     {
00402         pdv_check_fpga_rev(edt_p);
00403 
00404         /*
00405         * need to let the driver know the rev in dep_set/get since register
00406         * offsets changed in rev 2
00407         */
00408         edt_set_dependent(edt_p, dd_p);
00409     }
00410 
00411     pdv_set_baud(edt_p, dd_p->serial_baud);
00412 
00413     /*
00414     * infer dual channel from other flags
00415     */
00416     if (pdv_is_cameralink(edt_p)
00417         && (dd_p->cl_data_path & 0x10)
00418         && (!(dd_p->cl_cfg & PDV_CL_CFG_RGB)))
00419         dd_p->dual_channel = 1;
00420 
00421     if (!(edt_is_dvcl2(edt_p) && dd_p->sim_enable))
00422     {
00423         int roi_enabled = (dd_p->cl_cfg & PDV_CL_CFG_ROIDIS)?0:1;
00424 
00425         /*
00426         * set width/height.  Width and height as set in config file should be
00427         * actual camera width and height, before any ROI changes. ROI changes
00428         * will change dd_p->width/height but not dd_p->cam_width/height.
00429         */
00430         edt_msg(DEBUG1, "setting device defaults....\n");
00431         pdv_set_cam_width(edt_p, dd_p->width);
00432         pdv_set_cam_height(edt_p, dd_p->height);
00433         pdv_set_depth_extdepth_dpath(edt_p, dd_p->depth, dd_p->extdepth, dd_p->cl_data_path);
00434 
00435         /* since setsize and set width/height also set ROI now... */
00436         if (roi_enabled)
00437         {
00438             if (dd_p->hactv > 0)
00439                 dd_p->width = dd_p->hactv;
00440             if (dd_p->vactv > 0)
00441                 dd_p->height = dd_p->vactv;
00442         }
00443 
00444         pdv_set_width(edt_p, dd_p->width);
00445         pdv_set_height(edt_p, dd_p->height);
00446 
00447         check_roi(edt_p, dd_p, roi_enabled);
00448 
00449         if (dd_p->sim_enable) /* old simulator */
00450         {
00451             u_int   roictl;
00452 
00453             roictl = edt_reg_read(edt_p, PDV_ROICTL);
00454             /* default to simulated not from camera */
00455             if (!dd_p->sim_ctl)
00456                 roictl |= (PDV_ROICTL_SIM_DAT | PDV_ROICTL_SIM_SYNC);
00457             else
00458                 roictl |= dd_p->sim_ctl | PDV_ROICTL_SIM_DAT;
00459             edt_msg(DEBUG1, "setting simulator bits in roictl %x\n", roictl);
00460             edt_reg_write(edt_p, PDV_ROICTL, roictl);
00461         }
00462     }
00463 
00464     edt_set_dependent(edt_p, dd_p);
00465 
00466     edt_msg(DEBUG1, "setting registers....\n");
00467     if (dd_p->genericsim)
00468     {
00469         edt_msg(DEBUG1, "setting up for simulator....\n");
00470         edt_reg_write(edt_p, SIM_LDELAY, dd_p->line_delay);
00471         edt_reg_write(edt_p, SIM_FDELAY, dd_p->frame_delay);
00472         edt_reg_write(edt_p, SIM_WIDTH, dd_p->sim_width - 1);
00473         edt_reg_write(edt_p, SIM_HEIGHT, dd_p->sim_height - 1);
00474         edt_reg_write(edt_p, SIM_CFG, dd_p->genericsim);
00475 
00476 
00477         edt_msg(DEBUG2, "SIM_CFG %x\n", dd_p->genericsim);
00478         switch (dd_p->sim_speed)
00479         {
00480         case 0:
00481             edt_msg(DEBUG2, "starting pixel clock at 5Mhz\n");
00482             edt_reg_write(edt_p, SIM_SPEED, 0);
00483             break;
00484         case 1:
00485             edt_msg(DEBUG2, "starting pixel clock at 10Mhz\n");
00486             edt_reg_write(edt_p, SIM_SPEED, 1);
00487             break;
00488         case 2:
00489             edt_msg(DEBUG2, "starting pixel clock at 20Mhz\n");
00490             edt_reg_write(edt_p, SIM_SPEED, 2);
00491             break;
00492         }
00493     }
00494 
00495 
00496     /* Configuration register settings (except CL2 simulator */
00497     if (!(edt_is_dvcl2(edt_p) && dd_p->sim_enable))
00498     {
00499         int     configuration = 0;
00500 
00501         if (dd_p->inv_shutter)
00502             configuration |= PDV_INV_SHUTTER;
00503         else
00504             configuration &= ~PDV_INV_SHUTTER;
00505 
00506         /*
00507         * if MODE_CNTL_NORM is not set, set default based on value
00508         * of camera_shutter timing
00509         */
00510         if (dd_p->mode_cntl_norm == NOT_SET)
00511         {
00512             if ((dd_p->camera_shutter_timing == AIA_MCL)
00513                 || (dd_p->camera_shutter_timing == AIA_MCL_100US)
00514                 || (dd_p->camera_shutter_timing == AIA_TRIG))
00515                 dd_p->mode_cntl_norm = 0x10;
00516             else dd_p->mode_cntl_norm = 0;
00517         }
00518 
00519         /*
00520         * set default values for shutter_speed_min/max if using board shutter
00521         * timer and no min/max specified
00522         */
00523         if ((dd_p->camera_shutter_timing == AIA_MCL)
00524             || (dd_p->camera_shutter_timing == AIA_MCL_100US))
00525         {
00526             int exp;
00527 
00528             if ((dd_p->shutter_speed_min == 0) && (dd_p->shutter_speed_max == 0))
00529             {
00530                 dd_p->shutter_speed_min = 0;
00531                 dd_p->shutter_speed_max = 25500;
00532             }
00533 
00534             exp = pdv_get_exposure(edt_p);
00535             if (exp < dd_p->shutter_speed_min || exp > dd_p->shutter_speed_max)
00536                 pdv_set_exposure(edt_p, dd_p->shutter_speed_min);
00537         }
00538 
00539         if (dd_p->camera_shutter_timing == AIA_SERIAL)
00540             dd_p->trig_pulse = 1;
00541 
00542         if (dd_p->trig_pulse || dd_p->camera_shutter_timing == AIA_SERIAL)
00543             configuration |= PDV_TRIG;
00544         else
00545             configuration &= ~PDV_TRIG;
00546 
00547         if (dd_p->dis_shutter)
00548             configuration |= PDV_DIS_SHUTTER;
00549         else
00550             configuration &= ~PDV_DIS_SHUTTER;
00551 
00552         if (dd_p->enable_dalsa)
00553             configuration |= PDV_EN_DALSA;
00554         else
00555             configuration &= ~PDV_EN_DALSA;
00556 
00557         edt_msg(DEBUG2, "CONFIG %x\n", configuration);
00558         dep_wait(edt_p);
00559         edt_reg_write(edt_p, PDV_CFG, configuration);
00560     }
00561 
00562     /* util3 register settings */
00563     {
00564         int     util3 = 0;
00565 
00566         if (dd_p->inv_ptrig)
00567             util3 |= PDV_LV_INVERT;
00568         else
00569             util3 &= ~PDV_LV_INVERT;
00570 
00571         if (dd_p->inv_fvalid)
00572             util3 |= PDV_FV_INVERT;
00573         else
00574             util3 &= ~PDV_FV_INVERT;
00575 
00576         /*
00577         * set dvfox mode16 automatically (ON if 8 bits single or dual channel
00578         * or 8-16 bits single channel, otherwise off), unless specifically set
00579         * in the config file
00580         */
00581         if (edt_is_dvfox(edt_p))
00582         {
00583             if (dd_p->mode16 == NOT_SET)
00584             {
00585                 if ((!(dd_p->cl_cfg & PDV_CL_CFG_RGB))
00586                     && (((dd_p->cl_data_path & 0x0f) == 0x07)
00587                     || (dd_p->cl_data_path <= 0x0f)))
00588                     util3 |= PDV_MODE16;
00589             }
00590             else if (dd_p->mode16)
00591                 util3 |= PDV_MODE16;
00592         }
00593 
00594 
00595         edt_msg(DEBUG2, "UTIL3 %x\n", util3);
00596         dep_wait(edt_p);
00597 
00598         edt_reg_write(edt_p, PDV_UTIL3, util3);
00599     }
00600 
00601     pdv_set_fval_done(edt_p, dd_p->fval_done);
00602 
00603     /* old initcam for irc was this */
00604     if (dd_p->camera_download == IRC_160)
00605     {
00606         int     tmp;
00607 
00608         edt_msg(DEBUG1, "doing old initcam method for IRC160");
00609 #if 0
00610         tmp = edt_reg_read(edt_p, PDV_CFG);     /* inverse shutter, not
00611                                             * trigger */
00612         tmp &= ~(PDV_INV_SHUTTER | PDV_TRIG);
00613 #endif
00614         tmp = 0;
00615         if (dd_p->inv_shutter)
00616             tmp |= PDV_INV_SHUTTER;
00617         if (dd_p->camera_shutter_timing != AIA_MCL)
00618             tmp |= PDV_TRIG;
00619         dep_wait(edt_p);
00620         edt_reg_write(edt_p, PDV_CFG, tmp);
00621         edt_msg(DEBUG2, "CONFIG %x", tmp);
00622     }
00623 
00624     /* Data Path register settings (and camera link config/cntl) */
00625     if (pdv_is_cameralink(edt_p))
00626     {
00627         edt_msg(DEBUG2, "camera link cfg register %x\n", dd_p->cl_cfg);
00628         edt_reg_write(edt_p, PDV_CL_CFG, dd_p->cl_cfg);
00629 
00630         edt_msg(DEBUG2, "camera link cfg2 register %x\n", dd_p->cl_cfg2);
00631         edt_reg_write(edt_p, PDV_CL_CFG2, dd_p->cl_cfg2);
00632 
00633         edt_msg(DEBUG2, "camera link data_path register %x\n", dd_p->cl_data_path);
00634         edt_reg_write(edt_p, PDV_CL_DATA_PATH, dd_p->cl_data_path);
00635 
00636         if (dd_p->cl_hmax != 0x0)
00637         {
00638             edt_msg(DEBUG2, "camera link hmax register %x\n", dd_p->cl_hmax);
00639             edt_reg_write(edt_p, PDV_CL_HMAX, dd_p->cl_hmax);
00640         }
00641 
00642         if (dd_p->swinterlace == PDV_BGGR
00643             || dd_p->swinterlace == PDV_ILLUNIS_BGGR)
00644         {
00645             /* kludge for now */
00646         }
00647         else if (dd_p->depth > 8)
00648             data_path |= PDV_EXT_DEPTH;
00649 
00650         if (dd_p->camera_shutter_timing == AIA_MCL_100US)
00651             data_path |= PDV_MULTIPLIER_100US;
00652 
00653         if (dd_p->fv_once)
00654             data_path |= PDV_CONTINUOUS;
00655 
00656         if (dd_p->trigdiv)
00657             edt_reg_write(edt_p, PDV_TRIGDIV, dd_p->trigdiv);
00658 
00659         /* used hwpad to set 30-bit RGB in PDV and maybe PCIe4 medium mode FW but
00660          * not PCIe8 or any future boards (better not!) */
00661         if (dd_p->rgb30)
00662             if (edt_p->devid < PE8DVCL_ID)
00663                 dd_p->hwpad = dd_p->rgb30;
00664 
00665         dep_wait(edt_p);
00666         edt_reg_write(edt_p, PDV_DATA_PATH, data_path);
00667         dd_p->datapath_reg = data_path;
00668 
00669         /* set up framesync */
00670         pdv_enable_framesync(edt_p, edt_p->dd_p->framesync_mode);
00671 
00672         if (ei_p->cl_mgtspeed != NOT_SET)
00673             edt_reg_write(edt_p, PDV_CL_MGTSPEED, ei_p->cl_mgtspeed);
00674 
00675         edt_set_dependent(edt_p, dd_p);
00676     }
00677     else
00678     {
00679         if (dd_p->camera_shutter_timing == DALSA_CONTINUOUS)
00680             dd_p->continuous = 1;
00681 #if 0
00682         /* don't turn on until in driver */
00683         if (dd_p->continuous)
00684             data_path |= PDV_CONTINUOUS;
00685         else
00686             data_path &= ~PDV_CONTINUOUS;
00687 #endif
00688 
00689         if (dd_p->fv_once)
00690             data_path |= PDV_CONTINUOUS;
00691 
00692         if (dd_p->interlace)
00693             data_path |= PDV_INTERLACED;
00694         else
00695             data_path &= ~PDV_INTERLACED;
00696 
00697         edt_msg(DEBUG2, "data_path register %x\n", data_path);
00698 
00699         /*
00700         * if async we set the board generated pixel clock speed in
00701         * roictl register (added async check so we can use pclock_speed
00702         * for auto timeout and other stuff 08/06 rwh)
00703         */
00704         if (async_bitfile(edt_p))
00705         {
00706             u_int bits = 0;
00707             u_int roictl = edt_reg_read(edt_p, PDV_ROICTL);
00708 
00709             if (dd_p->pclock_speed < 5)
00710                 dd_p->pclock_speed = 5;
00711 
00712             switch(dd_p->pclock_speed)
00713             {
00714             case 20: bits = 0x04; break;
00715             case 10: bits = 0x05; break;
00716             case 5: bits = 0x06; break;
00717             default: edt_msg(PDVLIB_MSG_WARNING, "invalid pclock_speed value for async\n");
00718             }
00719 
00720             if (bits)
00721                 edt_reg_write(edt_p, PDV_ROICTL, roictl | bits);
00722 
00723         }
00724         else if (dd_p->pclock_speed < 1)
00725         {
00726             if (pdv_is_cameralink(edt_p))
00727                 dd_p->pclock_speed = 20;
00728             else dd_p->pclock_speed = 5;
00729         }
00730 
00731         if (dd_p->double_rate)
00732         {
00733             u_int   roictl = edt_reg_read(edt_p, PDV_ROICTL) | PDV_RIOCTL_PCLKSEL_DBL_CAM;
00734 
00735             edt_reg_write(edt_p, PDV_ROICTL,
00736                 roictl);
00737 
00738         }
00739 
00740         if (dd_p->camera_shutter_timing == PDV_DALSA_LS)
00741         {
00742             u_int   roictl = edt_reg_read(edt_p, PDV_ROICTL);
00743 
00744             roictl |= PDV_ROICTL_DALSA_LS;
00745 
00746             edt_reg_write(edt_p, PDV_ROICTL,
00747                 roictl);
00748 
00749         }
00750 
00751         /*
00752         * 'different' dual channel
00753         */
00754         if (dd_p->dual_channel)
00755         {
00756             edt_msg(DEBUG1, "setting dual channel\n");
00757             data_path &= ~PDV_RES0;
00758             data_path |= PDV_RES1;
00759 #if 0                           /* wha...? this seems to BREAK Dalsa
00760             * (ca-d4-1024A anyway), so skip it! */
00761             if (dd_p->enable_dalsa)
00762             {
00763                 if (dd_p->extdepth > 8)
00764                     data_path |= PDV_EXT_DEPTH;
00765             }
00766             else
00767 #endif
00768                 data_path |= PDV_EXT_DEPTH;
00769         }
00770         else if (dd_p->swinterlace == PDV_BGGR)
00771         {
00772             /* set for 10 bit ext - 8 bit first pass kludge */
00773             data_path |= PDV_RES0;
00774             data_path &= ~PDV_RES1;
00775         }
00776         else if (dd_p->swinterlace == PDV_BGGR_WORD
00777             || dd_p->swinterlace == PDV_BGGR_DUAL)
00778         {
00779             /* set for 10 bit ext */
00780             data_path |= PDV_EXT_DEPTH;
00781             data_path |= PDV_RES0;
00782             data_path &= ~PDV_RES1;
00783         }
00784         else
00785         {
00786             if (dd_p->depth > 8)
00787                 data_path |= PDV_EXT_DEPTH;
00788             switch (dd_p->extdepth)
00789             {
00790             case 8:
00791                 data_path &= ~PDV_EXT_DEPTH;
00792                 break;
00793             case 10:
00794                 data_path |= PDV_RES0;
00795                 data_path &= ~PDV_RES1;
00796                 break;
00797             case 12:
00798                 data_path &= ~PDV_RES0;
00799                 data_path &= ~PDV_RES1;
00800                 break;
00801             case 14:
00802                 data_path |= PDV_RES0;
00803                 data_path |= PDV_RES1;
00804                 break;
00805             case 16:
00806                 data_path &= ~PDV_RES0;
00807                 data_path |= PDV_RES1;
00808                 break;
00809             }
00810         }
00811 
00812         if (dd_p->camera_shutter_timing == AIA_MCL_100US)
00813             data_path |= PDV_MULTIPLIER_100US;
00814 
00815         edt_msg(DEBUG2, "DATA_PATH %x\n", data_path);
00816         dep_wait(edt_p);
00817         edt_reg_write(edt_p, PDV_DATA_PATH, data_path);
00818         dd_p->datapath_reg = data_path;
00819         edt_set_dependent(edt_p, dd_p);
00820     }
00821 
00822     /* new aia */
00823     if (dd_p->xilinx_rev >= 1 && dd_p->xilinx_rev <= 32)
00824     {
00825         int util2 = dd_p->util2;
00826 
00827         /*
00828         * set shift/mask (except n/a on cameralink)
00829         */
00830         if (!pdv_is_cameralink(edt_p))
00831         {
00832             if (dd_p->shift == NOT_SET)
00833             {
00834                 int     tmpdepth;
00835 
00836                 if (dd_p->depth == 24)
00837                     tmpdepth = 8;
00838                 else
00839                     tmpdepth = dd_p->depth;
00840 
00841                 /*
00842                 * shift gets a bit weird for DVC -- the DVK and DV44 cables are
00843                 * wired differently, and the older DVK is yet a third case --
00844                 * punt on that one and try to handle the first two as best we we
00845                 * can. Worst case will be that someone will need to use explicit
00846                 * shift in the in the config file if they have a specific cable.
00847                 */
00848                 if (pdv_is_dvc(edt_p))
00849                 {
00850                     edt_msg(DEBUG1, "auto-setting shift for dvc\n");
00851 
00852                     if (edt_p->devid == PDV44_ID)
00853                         dd_p->shift = (16 - dd_p->extdepth) | PDV_AIA_SWAP;
00854                     else if (dd_p->extdepth == 12)      /* dvk 12-bit cable */
00855                     {
00856                         dd_p->shift = 2;
00857                     }
00858                     else
00859                         dd_p->shift = 0;
00860                 }
00861                 /*
00862                 * not set in config so calculate default for non-cameralink
00863                 */
00864                 else
00865                 {
00866                     dd_p->shift = 16 - dd_p->extdepth;
00867                     dd_p->shift |= PDV_AIA_SWAP;
00868                 }
00869 
00870                 /*
00871                 * since shift was not set, check mask as well to see if it
00872                 * appears not to have been set either (0xffff) and set it too
00873                 */
00874                 if (dd_p->mask == 0xffff)
00875                 {
00876                     dd_p->mask = (u_int) (0x0000ffff >> (16 - tmpdepth));
00877                     if (pdv_is_dvc(edt_p))
00878                         edt_msg(DEBUG1, "auto-setting mask for dvc\n");
00879                     else
00880                         edt_msg(DEBUG1, "auto-set shift/mask to %02x/%02x (AIA/swap)\n", dd_p->shift, dd_p->mask);
00881                 }
00882                 else
00883                     edt_msg(DEBUG1, "auto-set shift to %02x (AIA/swap)\n", dd_p->shift);
00884             }
00885 
00886             edt_msg(DEBUG2, "PDV_SHIFT %x\n", dd_p->shift);
00887             dep_wait(edt_p);
00888             edt_reg_write(edt_p, PDV_SHIFT, dd_p->shift);
00889             edt_msg(DEBUG2, "PDV_MASK %x\n", dd_p->mask);
00890             dep_wait(edt_p);
00891             edt_reg_write(edt_p, PDV_MASK, dd_p->mask);
00892         }
00893 #if 0
00894         edt_reg_write(edt_p, PDV_LINERATE, dd_p->linerate);
00895         /* Note dd_p->prin has been retired from dependent struct */
00896         edt_reg_write(edt_p, PDV_PRIN, dd_p->prin);
00897 #endif
00898         if (dd_p->photo_trig)
00899             util2 |= PDV_PHOTO_TRIGGER;
00900         if (dd_p->fieldid_trig)
00901             util2 |= PDV_FLDID_TRIGGER;
00902         if (dd_p->acquire_mult)
00903             util2 |= PDV_AQUIRE_MULTIPLE;
00904 
00905         /*
00906         * RS-232 bit -- overloaded on MC4, only applicable to PCI DVa or DVFox
00907         */
00908         if ((dd_p->serial_mode == PDV_SERIAL_RS232)
00909             && ((edt_p->devid == PDVA_ID) || (edt_is_dvfox(edt_p))))
00910             util2 |= PDV_RX232;
00911 
00912         else if (dd_p->sel_mc4)
00913             util2 |= PDV_SEL_MC4;
00914 
00915         /*
00916         * mc4 is EXPEXSYNC on camera link, so do it whether or not (the nearly obsolete)
00917         * SEL_MC4 is specified
00918         */
00919         if (dd_p->mc4) /* alias EXPEXSYNC on pcidv c-link */
00920             util2 |= PDV_MC4;
00921 
00922         if (dd_p->dbl_trig)
00923             util2 |= PDV_DBL_TRIG;
00924         if (dd_p->pulnix)
00925             util2 |= PDV_PULNIX;
00926 
00927         dd_p->util2 = util2;
00928         edt_msg(DEBUG2, "PDV_UTIL2 %x\n", util2);
00929         dep_wait(edt_p);
00930         edt_reg_write(edt_p, PDV_UTIL2, util2);
00931     }
00932 
00933     if (dd_p->byteswap == NOT_SET)
00934     {
00935         if (edt_little_endian())
00936             dd_p->byteswap = 0;
00937         else
00938             dd_p->byteswap = 1;
00939     }
00940     else edt_msg(DEBUG2, "byteswap %d, forcing %s\n",
00941         dd_p->byteswap? "ON":"OFF");
00942 
00943     /* set Byteswap/ Hwpad */
00944     {
00945         int     padword = 0;
00946 
00947         padword = dd_p->hwpad << 1;
00948 
00949         if (dd_p->byteswap)
00950             padword |= PDV_BSWAP;
00951         if (dd_p->shortswap)
00952             padword |= PDV_SSWAP;
00953         if (dd_p->disable_mdout)
00954             padword |= PDV_DISABLEMD;
00955         if (dd_p->gendata)
00956             padword |= PDV_GENDATA;
00957         if (dd_p->skip)
00958             padword |= PDV_SKIP;
00959 
00960         edt_msg(DEBUG2, "PAD_SWAP %x\n", padword);
00961         dep_wait(edt_p);
00962         edt_reg_write(edt_p, PDV_BYTESWAP, padword);
00963     }
00964 
00965 
00966     /* xybion only (for now) uses fixedlen */
00967     if (dd_p->fixedlen)
00968     {
00969         /* #define SWAPEM 1 */
00970 #if SWAPEM
00971         unsigned short val = dd_p->fixedlen;
00972         unsigned short tmp = ((val & 0xff) << 8) | ((val & 0xff00) >> 8);
00973 
00974         edt_msg(DEBUG2, "FIXEDLEN %x\n", tmp);
00975         dep_wait(edt_p);
00976         edt_reg_write(edt_p, PDV_FIXEDLEN, tmp);
00977 #else
00978 
00979         edt_msg(DEBUG2, "FIXEDLEN %x\n", dd_p->fixedlen);
00980         dep_wait(edt_p);
00981         edt_reg_write(edt_p, PDV_FIXEDLEN, dd_p->fixedlen);
00982 #endif
00983     }
00984 
00985 
00986     edt_msg(DEBUG2, "MODE_CNTL %x\n", dd_p->mode_cntl_norm);
00987     dep_wait(edt_p);
00988     edt_reg_write(edt_p, PDV_MODE_CNTL, dd_p->mode_cntl_norm);
00989     dep_wait(edt_p);
00990 
00991 
00992     edt_reset_serial(edt_p);
00993 
00994     /* set waitc */
00995     if (dd_p->serial_waitc == NOT_SET)
00996         pdv_set_waitchar(edt_p, 0, 0);
00997     else
00998         pdv_set_waitchar(edt_p, 1, (u_char)dd_p->serial_waitc);
00999 
01000     if (dd_p->user_timeout == NOT_SET)
01001     {
01002         dd_p->user_timeout = 0; /* user_set will be the real flag */
01003         pdv_auto_set_timeout(edt_p);
01004     }
01005     else
01006         pdv_set_timeout(edt_p, dd_p->user_timeout);
01007 
01008     if (dd_p->timeout != NOT_SET)
01009         edt_set_rtimeout(edt_p, dd_p->timeout);
01010 
01011     pdv_set_defaults(edt_p);
01012     check_camera_values(edt_p, dd_p);
01013 
01014     do_xregwrites(edt_p, dd_p);
01015 
01016     /*
01017     * final stuff (none applicable to CL2 sim) 
01018     */
01019     if (!(edt_is_dvcl2(edt_p) && dd_p->sim_enable))
01020     {
01021         check_terminator(dd_p->serial_term);
01022 
01023         if ((dd_p->camera_shutter_timing == SPECINST_SERIAL)
01024             || (dd_p->camera_shutter_speed == SPECINST_SERIAL)
01025             || (dd_p->camera_download == SPECINST_SERIAL))
01026             dd_p->force_single = 1;
01027 
01028         if (dd_p->camera_download == SPECINST_SERIAL)
01029         {
01030             if ((DD_P_CAMERA_DOWNLOAD_FILE[0])
01031                 && ((ret = specinst_download(edt_p, DD_P_CAMERA_DOWNLOAD_FILE)) == 0))
01032                 if (DD_P_CAMERA_COMMAND_FILE[0])
01033                     ret = specinst_setparams(edt_p, DD_P_CAMERA_COMMAND_FILE);
01034             if (ret != 0)
01035                 return ret;
01036         }
01037 
01038         if (ei_p->serial_init)
01039             init_serial(edt_p, dd_p, ei_p);
01040 
01041         if (strlen(dd_p->xilinx_init) > 0)
01042             send_xilinx_commands(edt_p, dd_p->xilinx_init);
01043 
01044         if (dd_p->frame_timing != 0)
01045             pdv_set_frame_period(edt_p, dd_p->frame_period, dd_p->frame_timing);
01046 
01047         if (ei_p->startdma != NOT_SET)
01048             edt_startdma_action(edt_p, ei_p->startdma);
01049         else
01050             edt_startdma_action(edt_p, EDT_ACT_ALWAYS);
01051         if (ei_p->enddma != NOT_SET)
01052             edt_enddma_action(edt_p, ei_p->enddma);
01053         if (ei_p->flushdma != NOT_SET)
01054             edt_set_firstflush(edt_p, ei_p->flushdma);
01055         else
01056             edt_set_firstflush(edt_p, EDT_ACT_ONCE);
01057     }
01058 
01059     if (dd_p->header_type)
01060         pdv_set_header_type(edt_p, dd_p->header_type, dd_p->irig_slave, dd_p->irig_offset, dd_p->irig_raw);
01061 
01062     return 0;
01063 }
01064 
01065 
01066 
01067 #ifdef _NT_
01068 #define popen(cmd, mode) _popen(cmd, mode)
01069 #define pclose(cmd) _pclose(cmd)
01070 #define BITLOAD "bitload"
01071 #else
01072 #define BITLOAD "./bitload"
01073 #endif
01074 
01075 /* find id string, starting with "id:". If found return 1, else 0 */
01076 int
01077 findid(char *buf, char *idstr)
01078 {
01079     if (strncmp(buf, "id:", 3) == 0)
01080     {
01081 #if 0
01082         char   *dp = idstr;
01083         char   *p = strchr(buf, '"');
01084         char   *pp = strrchr(p, '"');
01085 
01086         for (; p != pp; p++)
01087             *dp++ = *p;
01088 #endif
01089         return (sscanf(&buf[4], " \"%[^\"]\"", idstr));
01090     }
01091     return 0;
01092 }
01093 
01094 void
01095 dir_from_path(char *dirstr, const char *pathstr)
01096 {
01097     char   *bsp = strrchr(pathstr, '\\');
01098     char   *fsp = strrchr(pathstr, '/');
01099     char   *sp;
01100 
01101     if (bsp > fsp)
01102         sp = bsp;
01103     else
01104         sp = fsp;
01105 
01106     if (sp != NULL)
01107     {
01108         strncpy(dirstr, pathstr, sp - pathstr);
01109         dirstr[sp - pathstr] = '\0';
01110     }
01111     else
01112         *dirstr = '\0';
01113 }
01114 
01115 /*
01116 * return 1 if async bitfile (from name); so that we set roictl
01117 * from pclock speed ONLY if async bitfile
01118 */
01119 int
01120 async_bitfile(EdtDev *edt_p)
01121 {
01122     edt_bitpath  pathbuf;
01123 
01124     edt_get_bitname(edt_p, pathbuf, sizeof(edt_bitpath)) ;
01125     if (pathbuf[0] && (strstr(pathbuf, "async") != NULL))
01126         return 1;
01127     return 0;
01128 }
01129 
01130 void
01131 fname_from_path(char *fname, char *pathstr)
01132 {
01133     char   *bsp = strrchr(pathstr, '\\');
01134     char   *fsp = strrchr(pathstr, '/');
01135     char   *sp;
01136 
01137     if (bsp > fsp)
01138         sp = bsp;
01139     else
01140         sp = fsp;
01141 
01142     if (sp == NULL)
01143         strcpy(fname, pathstr);
01144     else
01145         strcpy(fname, sp + 1);
01146 }
01147 
01148 /*
01149 * check serial_exe to see if it's  Kodak 'i' type, if found then flush the
01150 * serial with a TRM command, and get the idn. Then send serial init string,
01151 * one command at a time.
01152 */
01153 int
01154 init_serial(EdtDev * edt_p, Dependent * dd_p, Edtinfo *ei_p)
01155 {
01156     SerialInitNode *lp = ei_p->serial_init;
01157 
01158     edt_msg(DEBUG1, "sending serial init....\n");
01159 
01160 
01161     while (lp)
01162     {
01163         if (lp->tag == serial_tag_binary)
01164             init_serial_binary(edt_p, dd_p, ei_p->serial_init_delay, lp->data);
01165 
01166         else if (lp->tag == serial_tag_baslerf)
01167             init_serial_basler_binary(edt_p, dd_p, ei_p->serial_init_delay, lp->data);
01168 
01169         else if (lp->tag == serial_tag_duncanf)
01170             init_serial_duncan_binary(edt_p, dd_p, ei_p->serial_init_delay, lp->data);
01171 
01172         else init_serial_ascii(edt_p, dd_p, ei_p->serial_init_delay, lp->data);
01173 
01174         lp = lp->next;
01175     }
01176     return 0;
01177 
01178 }
01179 
01180 int
01181 init_serial_ascii(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init)
01182 {
01183     char    resp[257];
01184     int     skip_init = 0;
01185     int     hamamatsu = 0;
01186     int     ret = 0;
01187 
01188     if (grepit(dd_p->cameratype, "Hamamatsu") != NULL)
01189         hamamatsu = 1;
01190 
01191     /* first flush any pending/garbage serial */
01192     resp[0] = '\0';
01193     ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 256);
01194     pdv_serial_read(edt_p, resp, 256);
01195 
01196     if (edt_p->dd_p->startup_delay)
01197         edt_msleep(edt_p->dd_p->startup_delay);
01198 
01199     edt_msg(DEBUG1, "sending serial init commands to camera....\n");
01200 
01201     /*
01202     * if kodak 'i' camera, send a couple of "TRM?"s just to flush out the
01203     * serial, and send the IDN string to get the camera firmware ID
01204     */
01205     if (pdv_is_kodak_i(edt_p))
01206     {
01207         /* first send a couple of TRM?s to flush and sync */
01208         pdv_serial_command(edt_p, "TRM?");
01209         pdv_serial_wait(edt_p, 500, 40);
01210         pdv_serial_command(edt_p, "TRM?");
01211         pdv_serial_wait(edt_p, 500, 40);
01212         ret = pdv_serial_read(edt_p, resp, 256);
01213 
01214         pdv_serial_command(edt_p, "IDN?");
01215         edt_msg(DEBUG1, "IDN? ");
01216         pdv_serial_wait(edt_p, 1000, 60);
01217         ret = pdv_serial_read(edt_p, resp, 256);
01218         if (ret > 20)
01219             edt_msg(DEBUG1, "%s\n", resp);
01220         else if (ret > 0)
01221             edt_msg(DEBUG1, "%s (unexpected response!)\n", resp);
01222         else
01223         {
01224             edt_msg(DEBUG1, "<no response from camera -- check cables and connections>\n");
01225             skip_init = 1;
01226         }
01227     }
01228 
01229     if (!skip_init)
01230     {
01231         int    i = 0, ms;
01232         char   *lastp = NULL;
01233         char   *p = serial_init;
01234         char    cmdstr[32];
01235 
01236         if (delay == NOT_SET)
01237             ms = 500;
01238         else ms = delay;
01239 
01240         /*
01241         * send serial init string
01242         */
01243         cmdstr[0] = '\0';
01244         while (*p)
01245         {
01246             if (i > 31)
01247             {
01248                 edt_msg(EDT_MSG_FATAL, "ERROR: serial command too long\n");
01249                 return -1;
01250             }
01251             if (*p == ':' && lastp && *lastp != '\\')
01252             {
01253                 cmdstr[i] = '\0';
01254                 i = 0;
01255 
01256                 memset(resp, '\0', 257);
01257 
01258                 if (cmdstr[0])
01259                 {
01260                     if (is_hex_byte_command(cmdstr))
01261                     {
01262                         if (delay == NOT_SET)
01263                             ms = 10;
01264                         edt_msg(DEBUG2, "%s\n", cmdstr);
01265                         pdv_serial_command_hex(edt_p, cmdstr, 1);
01266                         ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 128);
01267                         pdv_serial_read(edt_p, resp, ret);
01268                     }
01269                     else if (hamamatsu)
01270                     {
01271                         char   *resp_str;
01272 
01273                         edt_msg(DEBUG2, "%s", strip_crlf(cmdstr));
01274                         fflush(stdout);
01275                         resp_str = serial_tx_rx(edt_p, cmdstr, 0);
01276                         edt_msg(DEBUG2, " <%s>\n", strip_crlf(resp_str));
01277                     }
01278                     else                        /* FUTURE: expand and test serial_tx_rx and replace
01279                                      * pdv_serial_command/wait/read with that for all (?)
01280                                      */
01281                     {
01282                         /* edt_msg(DEBUG1, ".", cmdstr); */
01283                         edt_msg(DEBUG2, "%s ", cmdstr);
01284                         pdv_serial_command(edt_p, cmdstr);
01285                         ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 16);
01286                         pdv_serial_read(edt_p, resp, 256);
01287                         edt_msg(DEBUG2, " <%s>", strip_crlf(resp));
01288                         edt_msg(DEBUG2, "\n");
01289                     }
01290                     /* edt_msg(DEBUG1, ".", cmdstr); */
01291                     edt_msleep(ms);
01292                 }
01293             }
01294             else if (*p != '\\')
01295                 cmdstr[i++] = *p;
01296             lastp = p;
01297             ++p;
01298         }
01299     }
01300 
01301     return 0;
01302 }
01303 
01304 u_char
01305 atoxdigit(char ch)
01306 {
01307     if (ch >= '0' && ch <= '9')
01308         return (ch - '0');
01309 
01310     if (ch >= 'a' && ch <= 'f')
01311         return (ch - 'a') + 10;
01312 
01313     if (ch >= 'A' && ch <= 'F')
01314         return (ch - 'A') + 10;
01315 
01316     return 0;
01317 }
01318 
01319 /*
01320 * binary initialization: serial_binit specified, interpret as string of
01321 * hex bytes only
01322 * update 7/10/07: now sends adjacent bytes as a single command (string of
01323 * bytes); does a * wait/read on whitespace only
01324 */
01325 int
01326 init_serial_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init)
01327 {
01328     int ret;
01329     u_int i;
01330     size_t buflen = strlen(serial_init);
01331     int cmdlen = 0, nibble_index = 0;
01332     u_char hbuf[1024];
01333     char    resp[1025];
01334     u_char ch;
01335 
01336 
01337     /* flush junk */
01338     ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1024);
01339 
01340     hbuf[0] = 0;
01341     nibble_index = 0;
01342     for (i = 0; i < buflen+1; i++)
01343     {
01344         if (i < buflen)
01345             ch = serial_init[i];
01346         else ch = '\0';
01347 
01348         if ((i == buflen) || (ch == '\t' || ch == ' '))
01349         {
01350             cmdlen = (nibble_index+1) / 2;
01351 
01352             if (cmdlen)
01353             {
01354                 /* send command, wait for response */
01355                 pdv_serial_binary_command(edt_p, (char *) hbuf, cmdlen);
01356                 if ((ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1024)))
01357                     pdv_serial_read(edt_p, resp, ret);
01358             }
01359 
01360             nibble_index = 0;
01361             hbuf[0] = 0;
01362         }
01363         else if (isxdigit(ch))
01364         {
01365             hbuf[nibble_index/2] = (hbuf[nibble_index/2] << 4) | atoxdigit(ch);
01366             if ((nibble_index++) % 2)
01367                 hbuf[nibble_index/2] = 0;
01368         }
01369         else
01370         {
01371             edt_msg(PDVLIB_MSG_WARNING, "serial_binit: hex string format error on buf[%d] ('%c')\n", i, serial_init[i]);
01372             return -1;
01373         }
01374     }
01375 
01376     edt_set_dependent(edt_p, dd_p);
01377 
01378     return 0;
01379 }
01380 
01381 /*
01382 * basler A202k binary only initialization -- expects serial_init to contain
01383 * colon-separated strings of hex bytes WITHOUT framing information; add
01384 * basler A202k style framing and send the commands to the camera
01385 */
01386 int
01387 init_serial_basler_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init)
01388 {
01389     u_int  i, j;
01390     char   *p;
01391     int    ms = 50;
01392     char   *nextp;
01393     size_t len;
01394     int     ret;
01395     char    cmdstr[32];
01396     char    bytestr[3];
01397     char    resp[1024];
01398 
01399     if (delay != NOT_SET)
01400         ms = delay;
01401 
01402     /* first flush any pending/garbage serial */
01403     resp[0] = '\0';
01404     ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1023);
01405     pdv_serial_read(edt_p, resp, 1023);
01406 
01407     edt_msg(DEBUG1, "sending Basler A202k framed commands to camera:\n");
01408 
01409     /*
01410      * send serial init string (first stick on a trailing ':' for parser)
01411      */
01412     len = strlen(serial_init);
01413     if (serial_init[len-1] != ':')
01414     {
01415         serial_init[len] = ':';    /* safe because data is */
01416         serial_init[len+1] = '\0'; /* malloc'd to arg len + 2 */
01417     }
01418 
01419     p = serial_init;
01420 
01421     while ((nextp = strchr(p, ':')))
01422     {
01423         int     ms = 50;
01424         u_char bytebuf[32];
01425         u_char *bp = bytebuf;
01426 
01427         len = (size_t)(nextp - p);
01428         if (len > 31)
01429         {
01430             edt_msg(EDT_MSG_FATAL, "ERROR: serial command too long\n");
01431             return -1;
01432         }
01433 
01434         strncpy(cmdstr, p, len);
01435         cmdstr[len] = 0;
01436         memset(resp, '\0', 1023);
01437 
01438         if (len % 2)
01439         {
01440             edt_msg(EDT_MSG_FATAL, "serial_binary format string error: odd nibble count\n");
01441             return -1;
01442         }
01443 
01444         for (i=0; i<len; i++)
01445         {
01446             if (!isxdigit(cmdstr[i]))
01447             {
01448                 edt_msg(EDT_MSG_FATAL, "serial_binrary format string error: odd nibble count\n");
01449                 return -1;
01450             }
01451         }
01452 
01453         for (i=0, j=0; i<len; i+=2, j++)
01454         {
01455             u_int bint;
01456             bytestr[0] = cmdstr[i];
01457             bytestr[1] = cmdstr[i+1];
01458             bytestr[2] = '\0';
01459             sscanf(bytestr, "%x", &bint);
01460             *(bp++) = (u_char)(bint & 0xff);
01461         }
01462         pdv_send_basler_frame(edt_p, bytebuf, (int)len/2);
01463 
01464         ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1023);
01465         pdv_serial_read(edt_p, resp, ret);
01466 
01467         p = nextp + 1;
01468         edt_msleep(ms);
01469     }
01470 
01471     edt_set_dependent(edt_p, dd_p);
01472 
01473     return 0;
01474 }
01475 
01476 /*
01477 * duncantech MS2100, 2150, 3100, 4100 binary only initialization -- expects
01478 * serial_init to contain colon-separated strings of hex bytes WITHOUT
01479 * framing information; add duncan framing and send the commands to the
01480 * camera
01481 */
01482 int
01483 init_serial_duncan_binary(EdtDev * edt_p, Dependent * dd_p, int delay, char *serial_init)
01484 {
01485     int    i, j;
01486     char   *p;
01487     int    ms = 50;
01488     char   *nextp;
01489     int     len;
01490     int     ret;
01491     char    cmdstr[32];
01492     char    bytestr[3];
01493     char    resp[1024];
01494 
01495     if (delay != NOT_SET)
01496         ms = delay;
01497 
01498     /* first flush any pending/garbage serial */
01499     resp[0] = '\0';
01500     ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1023);
01501     pdv_serial_read(edt_p, resp, 1023);
01502 
01503     edt_msg(DEBUG1, "sending DuncanTech framed commands to camera:\n");
01504 
01505     /*
01506      * send serial init string (first stick on a trailing ':' for parser)
01507      */
01508     len = strlen(serial_init);
01509     if (serial_init[len-1] != ':')
01510     {
01511         serial_init[len] = ':';    /* safe because data is */
01512         serial_init[len+1] = '\0'; /* malloc'd to arg len + 2 */
01513     }
01514 
01515     p = serial_init;
01516 
01517     while ((nextp = strchr(p, ':')))
01518     {
01519         int     ms = 50;
01520         u_char bytebuf[32];
01521         u_char *bp = bytebuf;
01522 
01523         len = (int)(nextp - p);
01524         if (len > 31)
01525         {
01526             edt_msg(EDT_MSG_FATAL, "ERROR: serial command too long\n");
01527             return -1;
01528         }
01529 
01530         strncpy(cmdstr, p, len);
01531         cmdstr[len] = 0;
01532         memset(resp, '\0', 1024);
01533 
01534         if (len % 2)
01535         {
01536             edt_msg(EDT_MSG_FATAL, "serial_binrary format string error: odd nibble count\n");
01537             return -1;
01538         }
01539 
01540         for (i=0; i<len; i++)
01541         {
01542             if (!isxdigit(cmdstr[i]))
01543             {
01544                 edt_msg(EDT_MSG_FATAL, "serial_binrary format string error: odd nibble count\n");
01545                 return -1;
01546             }
01547         }
01548 
01549         for (i=0, j=0; i<len; i+=2, j++)
01550         {
01551             u_int bint;
01552             bytestr[0] = cmdstr[i];
01553             bytestr[1] = cmdstr[i+1];
01554             bytestr[2] = '\0';
01555             sscanf(bytestr, "%x", &bint);
01556             *(bp++) = (u_char)(bint & 0xff);
01557         }
01558         pdv_send_duncan_frame(edt_p, bytebuf, len/2);
01559 
01560         /* flush out junk */
01561         ret = pdv_serial_wait(edt_p, dd_p->serial_timeout, 1023);
01562         pdv_serial_read(edt_p, resp, ret);
01563 
01564         p = nextp + 1;
01565         edt_msleep(ms);
01566     }
01567 
01568 
01569     edt_set_dependent(edt_p, dd_p);
01570 
01571     return 0;
01572 }
01573 
01574 /*
01575 * multi-purpose ROI check/set. Cases include:
01576 * 
01577 * - xilinx rev doesn't support ROI: return without setting
01578 * - dalsa line scan: check hskip/hactv for valid values (no need to enable tho)
01579 * - hactv/vactv set: set and enable ROI
01580 * - camera link board: auto set to width (or 4 byte aligned), full height - otherwise disable
01581 */
01582 int
01583 check_roi(EdtDev * edt_p, Dependent * dd_p, int ena)
01584 {
01585     int     ret = 0;
01586 
01587     if ((dd_p->xilinx_rev < 2) || (dd_p->xilinx_rev > 32))
01588         return 0;
01589 
01590     /*
01591     * NEW 10/30/2008: htaps / vtaps now get set automatically (htaps = #taps as set by
01592     * CL_DATA_PATH_NORM left nibble) unless set explicitly via htaps / vtaps directives
01593     */
01594     if (dd_p->htaps == NOT_SET)
01595     {
01596         if (dd_p->vtaps == NOT_SET)
01597         {
01598             if ((dd_p->cl_data_path == 0)               /* special case, datapath is 00 */
01599                 || ((dd_p->cl_data_path & 0xf0) == 0x20))  /* special case RGB */
01600                 dd_p->htaps = 1;
01601             else dd_p->htaps = ((dd_p->cl_data_path & 0xf0) >> 4) + 1;
01602             dd_p->vtaps = 1;
01603         }
01604         else dd_p->htaps = 1;
01605     }
01606     else if (dd_p->vtaps == NOT_SET)
01607         dd_p->vtaps = 1;
01608 
01609     /* set ROI */
01610     if (dd_p->hactv && dd_p->camera_shutter_timing == PDV_DALSA_LS)
01611     {
01612         if (pdv_dalsa_ls_set_expose(edt_p, dd_p->hskip, dd_p->hactv) != 0)
01613         {
01614             edt_msg(EDT_MSG_FATAL, "Error setting DALSA LS parameters!\n");
01615             ret = -1;
01616         }
01617     }
01618     else if (dd_p->hactv || dd_p->vactv)
01619     {
01620         if (dd_p->hactv == 0)   /* only vactv given; set hactv to a mult. of
01621                                 * 4 */
01622                                 dd_p->hactv = ((pdv_get_width(edt_p) / 4) * 4);
01623         else if (dd_p->vactv == 0)      /* only hactv given; set hactv to
01624                                     * height */
01625                                     dd_p->vactv = pdv_get_height(edt_p);
01626 
01627         if ((ret = (pdv_set_roi(edt_p, dd_p->hskip, dd_p->hactv, dd_p->vskip, dd_p->vactv) == 0)))
01628             ret = pdv_enable_roi(edt_p, ena);
01629 
01630         if (ret != 0)
01631             edt_msg(EDT_MSG_FATAL, "Error setting or enabling ROI!\n");
01632     }
01633     else if (pdv_is_cameralink(edt_p) && ena)
01634         ret = pdv_auto_set_roi(edt_p);  /* also enables */
01635     else
01636         ret = pdv_enable_roi(edt_p, 0); /* DISable */
01637 
01638 
01639     /* new 6/20/06 -- set up pingpong stuff in initcam */
01640     if (edt_p->dd_p->pingpong_varsize)
01641     {
01642         int util3;
01643 
01644         pdv_enable_roi(edt_p, 0); /* disable ROI regardless */
01645 
01646         /* 
01647         * set frame valid done flag and enable ping pong bit (0x02) in
01648         * utility 3 register (NOTE: special FW required, might want to
01649         * check for that here)
01650         */
01651         pdv_set_fval_done(edt_p, 0);
01652         util3 = edt_reg_read(edt_p, PDV_UTIL3);
01653         edt_reg_write(edt_p, PDV_UTIL3, util3 & ~PDV_PPENB);
01654         edt_reg_write(edt_p, PDV_UTIL3, util3 | PDV_PPENB);
01655 
01656         /* reset pingpong to channel 0 */
01657         util3 = edt_reg_read(edt_p, PDV_UTIL3);
01658         edt_reg_write(edt_p, PDV_UTIL3, util3 | PDV_PPRST);
01659         edt_reg_write(edt_p, PDV_UTIL3, util3 & ~PDV_PPRST);
01660         pdv_set_fval_done(edt_p, 1);
01661     }
01662 
01663     return ret;
01664 }
01665 
01666 
01667 void
01668 check_camera_values(EdtDev * ed, Dependent * dd_p)
01669 {
01670     if ((dd_p->shutter_speed_min != NOT_SET)
01671         && ((dd_p->shutter_speed < dd_p->shutter_speed_min)
01672         || (dd_p->shutter_speed > dd_p->shutter_speed_max)))
01673         dd_p->shutter_speed = dd_p->shutter_speed_min;
01674 
01675     if ((dd_p->gain_min != NOT_SET)
01676         && ((dd_p->gain < dd_p->gain_min)
01677         || (dd_p->gain > dd_p->gain_max)))
01678         dd_p->gain = dd_p->gain_min;
01679 
01680     if ((dd_p->offset_min != NOT_SET)
01681         && ((dd_p->level < dd_p->offset_min)
01682         || (dd_p->level > dd_p->offset_max)))
01683         dd_p->level = dd_p->offset_min;
01684 
01685     edt_set_dependent(ed, dd_p);
01686 }
01687 
01688 
01689 int
01690 kodak_query_serial(EdtDev * ed, char *cmd, int *val)
01691 {
01692     char    cmdstr[32];
01693     char    resp[256];
01694     char   *p, *pp;
01695     int     ret;
01696 
01697     if (!(*cmd))
01698         return 0;
01699 
01700     sprintf(cmdstr, "%s?", cmd);
01701     pdv_serial_command(ed, cmdstr);
01702     ret = pdv_read_response(ed, resp);
01703 
01704     if ((ret < 5) || (ret > 15))
01705         return 0;
01706 
01707     p = strchr(resp, ' ');
01708     pp = p + 1;
01709 
01710     if ((p == NULL) || (*pp == '\0') || ((*pp != '-') && !isdigit(*pp)))
01711         return 0;
01712 
01713     /* check for Kodak ES (and ES 4.0) format */
01714     if (strchr(pp, '.') != NULL)
01715     {
01716         /*
01717         * ES 4.0 -- uses fractions for both in and out, we punt and stick
01718         * with positive integers only
01719         */
01720         if (ed->dd_p->camera_shutter_timing == AIA_SERIAL_ES40)
01721         {
01722             *val = (int) (atof(pp) + 0.5);
01723             if (*val < 1)
01724                 *val = 1;
01725         }
01726         /* ES 1.0 -- uses fractions out, ints in */
01727         else
01728             *val = (int) ((atof(pp) / .0636) + 0.5);
01729     }
01730     else
01731         *val = atoi(pp);
01732     return (ret);
01733 }
01734 
01735 
01736 /*
01737 * looking for "0xNN". If so, return 1, else return 0
01738 */
01739 int
01740 is_hex_byte_command(char *str)
01741 {
01742     int     i;
01743 
01744     if (strlen(str) != 4)
01745         return 0;
01746 
01747     if ((strncmp(str, "0x", 2) != 0)
01748         && (strncmp(str, "0X", 2) != 0))
01749         return 0;
01750 
01751     for (i = 2; i < (int)strlen(str); i++)
01752     {
01753         if ((str[i] < '0') && (str[i] > '9')
01754             && (str[i] < 'a') && (str[i] > 'z')
01755             && (str[i] < 'A') && (str[i] > 'Z'))
01756             return 0;
01757     }
01758     return 1;
01759 }
01760 
01761 
01762 /*
01763 * takes a colon separated string of xilinx commands, parses them into
01764 * individual commands, and sends one at a time to the interface xilinx
01765 */
01766 int
01767 send_xilinx_commands(EdtDev * edt_p, char *str)
01768 {
01769     char   *p;
01770     char   *nextp;
01771     int     len;
01772     char    cmdstr[32];
01773     char    cmd[32];
01774     u_int   addr;
01775     unsigned long lvalue;
01776     u_char  value;
01777 
01778     edt_msg(DEBUG1, "sending xilinx commands....\n");
01779 
01780     p = str;
01781 
01782     while ((nextp = strchr(p, ':')))
01783     {
01784         len = (int)(nextp - p);
01785         if (len > 31)
01786         {
01787             edt_msg(EDT_MSG_FATAL, "ERROR: xilinx cmd too long\n");
01788             return -1;
01789         }
01790 
01791         strncpy(cmdstr, p, len);
01792         cmdstr[len] = 0;
01793 
01794         sscanf(cmdstr, "%s %x %lx", cmd, &addr, &lvalue);
01795         if (addr < 0xffff)
01796             addr |= 0x01010000;
01797         value = (unsigned char) (lvalue & 0xff);
01798         edt_msg(DEBUG1, "%s %08x %02x\n", cmd, addr, value);
01799 
01800         if (strcmp(cmd, "w") == 0)
01801         {
01802 #if 0
01803             edt_intfc_write(edt_p, addr, value);
01804 #else
01805             edt_msg(PDVLIB_MSG_WARNING, "ALERT: edt_intfc_write commented out for testing only\n");
01806 #endif
01807         }
01808         /* else other commands here */
01809         else
01810         {
01811             edt_msg(PDVLIB_MSG_WARNING, "unknown xilinx command %s\n", cmd);
01812         }
01813 
01814         p = nextp + 1;
01815     }
01816     return 1;
01817 }
01818 
01819 /*
01820 * search for a pattern in a char buffer, return pointer to next char if
01821 * found, NULL if not
01822 */
01823 char   *
01824 grepit(char *buf, char *pat)
01825 {
01826     int     i;
01827 
01828     for (i = 0; i < (int)strlen(buf); i++)
01829     {
01830         if (buf[i] == pat[0])
01831         {
01832             if (strncmp(&buf[i], pat, strlen(pat)) == 0)
01833                 return &buf[i + strlen(pat)];
01834         }
01835     }
01836     return NULL;
01837 }
01838 
01839 /*
01840 * config file takes "\r" and "\n" which need to be converted from strings to
01841 * chars
01842 */
01843 void
01844 check_terminator(char *str)
01845 {
01846     int     i, j = 0;
01847     char    tmpstr[MAXSER];
01848 
01849     for (i = 0; i < (int) strlen(str); i += 2)
01850     {
01851         if (str[i] == '\\')
01852         {
01853             if (str[i + 1] == 'r')
01854                 tmpstr[j++] = '\r';
01855             else if (str[i + 1] == 'n')
01856                 tmpstr[j++] = '\n';
01857             else if (str[i + 1] == '0')
01858                 tmpstr[j++] = '\0';
01859         }
01860     }
01861     tmpstr[j] = '\0';
01862     if (*tmpstr)
01863         strcpy(str, tmpstr);
01864 }
01865 
01866 int
01867 specinst_download(EdtDev * edt_p, char *fname)
01868 {
01869     int     i, extras = 0, ena;
01870     size_t  n, nb = 0;
01871     char    dmy[32];
01872     int     ret, resp = 0;
01873     u_char  buf[1024];
01874     u_char  savechar;
01875     FILE   *fd;
01876 
01877     edt_msg(DEBUG1, "SpecInst download <%s>", fname);
01878     fflush(stdout);
01879 
01880     if (pdv_access(fname, 0) != 0)
01881     {
01882         edt_msg(EDT_MSG_FATAL, "ERROR: Failed camera download (%s)\n", fname);
01883         return -1;
01884     }
01885 
01886     if ((fd = fopen(fname, "rb")) == NULL)
01887     {
01888         edt_perror(fname);
01889         edt_msg(EDT_MSG_FATAL, "\nERROR: couldn't open camera download file <%s>", fname);
01890         return -1;
01891     }
01892 
01893     pdv_send_break(edt_p);
01894     edt_msleep(500);
01895 
01896     /* flush any pending/garbage serial */
01897     ret = pdv_serial_wait_next(edt_p, edt_p->dd_p->serial_timeout, 256);
01898     if (ret == 0)
01899         ret = 3;
01900     if (ret)
01901         pdv_serial_read(edt_p, (char *) buf, ret);
01902 
01903     ena = pdv_get_waitchar(edt_p, &savechar);
01904     while ((n = (int)fread(buf, 1, 1, fd)))
01905     {
01906         buf[n] = '\0';
01907         nb += n;
01908         if (!(nb % 200))
01909         {
01910             edt_msg(DEBUG1, ".");
01911             fflush(stdout);
01912         }
01913         pdv_set_waitchar(edt_p, 1, buf[0]);
01914         pdv_serial_binary_command(edt_p, (char *) buf, (int)n);
01915 
01916         if ((ret = pdv_serial_wait_next(edt_p, 50, (int)n)))
01917         {
01918             pdv_serial_read(edt_p, dmy, ret);
01919             /* DEBUG */ dmy[ret] = '\0';
01920             if (ret > 1)
01921             {
01922                 edt_msg(PDVLIB_MSG_WARNING, "specinst_download wrote %x read %x ret %d! \n", buf[0], dmy[0], ret);
01923             }
01924         }
01925 
01926         if (ret > 1)
01927         {
01928             ++extras;
01929 
01930             edt_msg(PDVLIB_MSG_WARNING, "specinst_download: ret %d s/b 1, read <", ret);
01931             for (i = 0; i < ret; i++);
01932             edt_msg(PDVLIB_MSG_WARNING, "%c", dmy[i]);
01933             edt_msg(PDVLIB_MSG_WARNING, ">\n");
01934             edt_msleep(2000);
01935         }
01936         resp += ret;
01937     }
01938     edt_msg(DEBUG1, "done\n");
01939 
01940     /*
01941     * restore old waitchar (if any)
01942     */
01943     pdv_set_waitchar(edt_p, ena, savechar);
01944 
01945     if (extras)
01946         edt_msg(DEBUG1, "Spectral Instruments program download got extra bytes...???\n");
01947     else if (nb > (size_t)resp)
01948     {
01949         edt_msg(EDT_MSG_FATAL, "Spectral Instruments program download apparently FAILED\n");
01950         edt_msg(EDT_MSG_FATAL, "Wrote %d bytes, got %d responses (continuing anyway)\n", nb, resp);
01951     }
01952 
01953     fclose(fd);
01954     edt_msleep(500);
01955     return 0;
01956 }
01957 
01958 int
01959 specinst_setparams(EdtDev * edt_p, char *fname)
01960 {
01961     char    cmd;
01962     char    resp[256];
01963     u_char  savechar;
01964     int     ret, ena;
01965     char    buf[1024];
01966     u_long  offset;
01967     u_long  param;
01968     u_char  cmdbuf[8];
01969     u_char  offsetbuf[8];
01970     u_char  parambuf[8];
01971     FILE   *fd;
01972     int     si_wait = 150;
01973 
01974 
01975     edt_msg(DEBUG1, "SpecInst setparams <%s>", fname);
01976     fflush(stdout);
01977 
01978     pdv_serial_wait_next(edt_p, si_wait, 2);
01979 
01980     ret = pdv_serial_read(edt_p, buf, 64);
01981 
01982 
01983     resp[0] = resp[1] = resp[2] = 0;
01984 
01985     if (pdv_access(fname, 0) != 0)
01986     {
01987         edt_msg(EDT_MSG_FATAL, "\nERROR: Failed camera setparams (%s) - aborting\n", fname);
01988         return -1;
01989     }
01990 
01991     if ((fd = fopen(fname, "rb")) == NULL)
01992     {
01993         edt_perror(fname);
01994         edt_msg(EDT_MSG_FATAL, "\ncouldn't open camera parameter file <%s> - aborting", fname);
01995         return -1;
01996     }
01997 
01998     pdv_serial_wait_next(edt_p, 1000, 64);
01999     ret = pdv_serial_read(edt_p, buf, 64);
02000 
02001     ena = pdv_get_waitchar(edt_p, &savechar);
02002     while (fgets(buf, 1024, fd))
02003     {
02004         if ((buf[0] == '#') || (strlen(buf) < 1))
02005             continue;
02006 
02007         if (sscanf(buf, "%c %lu %lu", &cmd, &offset, &param) != 3)
02008         {
02009             edt_msg(PDVLIB_MSG_WARNING, "\ninvalid format in parameter file <%s> '%s' -- ignored\n", fname, buf);
02010             return -1;
02011         }
02012 
02013         edt_msg(DEBUG1, ".");
02014         fflush(stdout);
02015 
02016         /* edt_msg(DEBUG1, "cmd %c %04x %04x\n",cmd,offset,param) ; */
02017         dvu_long_to_charbuf(offset, offsetbuf);
02018         dvu_long_to_charbuf(param, parambuf);
02019 
02020 
02021         /*
02022         * specinst echoes the the command
02023         */
02024         cmdbuf[0] = cmd;
02025         pdv_set_waitchar(edt_p, 1, cmd);
02026 
02027         pdv_serial_binary_command(edt_p, (char *) cmdbuf, 1);
02028         /* pdv_serial_binary_command(edt_p, &cmd, 1); */
02029         pdv_serial_wait_next(edt_p, 500, 2);
02030         ret = pdv_serial_read(edt_p, resp, 2);
02031         if ((ret != 1) || (resp[0] != cmd))
02032         {
02033             edt_msg(EDT_MSG_FATAL, "specinst_setparams: invalid or missing serial response (sent %c, ret %d resp %02x), aborting\n", cmd, ret, (ret > 0) ? cmd : 0);
02034             return -1;
02035         }
02036 
02037         pdv_set_waitchar(edt_p, 1, 'Y');
02038 
02039         pdv_serial_binary_command(edt_p, (char *) offsetbuf, 4);
02040 #if 0
02041         pdv_serial_wait_next(edt_p, si_wait, 2);
02042         ret = pdv_serial_read(edt_p, buf, 2);
02043 #endif
02044         pdv_serial_binary_command(edt_p, (char *) parambuf, 4);
02045         pdv_serial_wait_next(edt_p, si_wait, 2);
02046         ret = pdv_serial_read(edt_p, resp, 2);
02047 
02048         if ((ret != 1) || (resp[0] != 'Y'))
02049         {
02050             edt_msg(EDT_MSG_FATAL, "invalid or missing serial response (sent %04x %04x ret %d resp %02x), aborting\n", offset, param, ret, (ret > 0) ? cmd : 0);
02051             return -1;
02052         }
02053     }
02054 
02055     /*
02056     * restore old waitchar (if any)
02057     */
02058     pdv_set_waitchar(edt_p, ena, savechar);
02059 
02060     edt_msg(DEBUG1, "done\n");
02061     fclose(fd);
02062     return 0;
02063 }
02064 
02065 
02066 /*
02067 * serial send AND recieve -- send a command and wait for the response
02068 */
02069 static char stRetstr[256];
02070 char   *
02071 serial_tx_rx(PdvDev * pdv_p, char *getbuf, int hexin)
02072 {
02073     int     i;
02074     int     ret;
02075     int     nbytes;
02076     int     length;
02077     u_char  hbuf[2], waitc, lastbyte;
02078     char    tmpbuf[256];
02079     char   *buf_p, *ibuf_p;
02080     char    buf[2048];
02081     char    bs[32][3];
02082     long    stx_etx_str(u_char * buf, int n);
02083 
02084     ibuf_p = getbuf;
02085     stRetstr[0] = '\0';
02086 
02087     /* flush any junk */
02088     (void) pdv_serial_read(pdv_p, buf, 2048);
02089 
02090     if (hexin)
02091     {
02092         nbytes = sscanf(ibuf_p, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
02093             bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7],
02094             bs[8], bs[9], bs[10], bs[11], bs[12], bs[13], bs[14], bs[15]);
02095 
02096         /*
02097         * change 5/28/99 one serial_binary_command for the whole thing --
02098         * before it did one write per byte which was dumb 
02099         */
02100         for (i = 0; i < nbytes; i++)
02101         {
02102             if (strlen(bs[i]) > 2)
02103             {
02104                 edt_msg(EDT_MSG_FATAL, "hex string format error\n");
02105                 break;
02106             }
02107             hbuf[i] = (u_char) (strtoul(bs[i], NULL, 16) & 0xff);
02108 
02109         }
02110 
02111         /*
02112          * using pdv_serial_binary_command instead of pdv_serial_write
02113          * because it prepends a 'c' if FOI [but FOI is gone anyhow...]
02114          */
02115         pdv_serial_binary_command(pdv_p, (char *) hbuf, nbytes);
02116         /* edt_msleep(10000); */
02117     }
02118     else
02119     {
02120         sprintf(tmpbuf, "%s\r", ibuf_p);
02121         edt_msg(DEBUG2, "writing <%s>\n", ibuf_p);
02122         pdv_serial_command(pdv_p, tmpbuf);
02123     }
02124 
02125     /*
02126     * serial_timeout comes from the config file (or -t override flag in this
02127     * app), or if not present defaults to 500
02128     */
02129     pdv_serial_wait(pdv_p, pdv_p->dd_p->serial_timeout, 64);
02130 
02131     /*
02132     * get the return string. How it gets printed depends on whether (1
02133     * ASCII, 2) HEX, or 3) Pulnix STX/ETX format
02134     */
02135     buf_p = buf;
02136     length = 0;
02137     do
02138     {
02139         ret = pdv_serial_read(pdv_p, buf_p, 2048 - length);
02140         edt_msg(DEBUG2, "\nread returned %d\n", ret);
02141 
02142         if (*buf_p)
02143             lastbyte = (u_char)buf_p[strlen(buf_p)-1];
02144 
02145         if (ret != 0)
02146         {
02147             buf_p[ret + 1] = 0;
02148             if (!hexin && (stx_etx_str((u_char *) buf, ret)))
02149                 /* PULNIX TM-1010 fmt */
02150             {
02151                 for (i = 0; i < ret; i++)
02152                 {
02153                     switch (buf_p[i])
02154                     {
02155                     case 0x02:
02156                         sprintf(&stRetstr[strlen(stRetstr)], "[STX]");
02157                         break;
02158 
02159                     case 0x03:
02160                         sprintf(&stRetstr[strlen(stRetstr)], "[ETX]");
02161                         break;
02162 
02163                     case 0x06:
02164                         sprintf(&stRetstr[strlen(stRetstr)], "[ACK]");
02165                         break;
02166                     case 0x15:
02167                         sprintf(&stRetstr[strlen(stRetstr)], "[NAK]");
02168                         break;
02169                     default:
02170                         sprintf(&stRetstr[strlen(stRetstr)], "%c", buf_p[i]);
02171                     }
02172                     edt_msg(DEBUG2, "");
02173                 }
02174             }
02175             /* Hex (or other non-ASCII */
02176             else if (hexin || (!isascii_str((u_char *) buf_p, ret)))
02177             {
02178                 int     i;
02179 
02180                 if (ret)
02181                 {
02182                     for (i = 0; i < ret; i++)
02183                         sprintf(&stRetstr[strlen(stRetstr)], "%s%02x", i ? " " : "", (u_char) buf_p[i]);
02184                 }
02185             }
02186             else                /* simple ASCII */
02187             {
02188                 sprintf(&stRetstr[strlen(stRetstr)], "%s", strip_crlf(buf_p));
02189             }
02190             buf_p += ret;
02191             length += ret;
02192         }
02193         if (pdv_get_waitchar(pdv_p, &waitc) && (lastbyte == waitc))
02194             ret = 0; /* jump out if waitchar is enabled/received */
02195         else pdv_serial_wait(pdv_p, pdv_p->dd_p->serial_timeout, 64);
02196     } while (ret > 0);
02197 
02198     return stRetstr;
02199 }
02200 
02201 static long
02202 isascii_str(u_char * buf, int n)
02203 {
02204     int     i;
02205 
02206     for (i = 0; i < n; i++)
02207         if ((buf[i] < ' ' || buf[i] > '~')
02208             && (buf[i] != '\n')
02209             && (buf[i] != '\r'))
02210             return 0;
02211     return 1;
02212 }
02213 
02214 long
02215 stx_etx_str(u_char * buf, int n)
02216 {
02217     int     i;
02218 
02219     if ((buf[0] != 0x02) || (buf[n - 1] != 0x03))
02220         return 0;
02221 
02222     for (i = 1; i < n - 1; i++)
02223         if ((buf[i] < ' ' || buf[i] > '~')      /* any ASCII */
02224             && (buf[i] != 0x6)  /* ACK */
02225             && (buf[i] != 0x15))/* NAK */
02226             return 0;
02227     return 1;
02228 }
02229 
02230 void
02231 propeller_sleep(int n)
02232 {
02233     int     i;
02234     char    prop_position[5] = "-\\|/";
02235 
02236     for (i = 0; i < n * 2; i++)
02237     {
02238         edt_msg(DEBUG1, "%c\r", prop_position[i % 4]);
02239         fflush(stdout);
02240         edt_msleep(500);
02241     }
02242 }
02243 
02244 char    scRetStr[256];
02245 char   *
02246 strip_crlf(char *str)
02247 {
02248     char   *p = str;
02249 
02250     scRetStr[0] = '\0';
02251 
02252     while (*p)
02253     {
02254         if (*p == '\r')
02255             sprintf(&scRetStr[strlen(scRetStr)], "\\r");
02256         else if (*p == '\n')
02257             sprintf(&scRetStr[strlen(scRetStr)], "\\n");
02258         else
02259             sprintf(&scRetStr[strlen(scRetStr)], "%c", *p);
02260         ++p;
02261     }
02262 
02263     return scRetStr;
02264 }
02265 
02266 
02267 void
02268 do_xregwrites(EdtDev *edt_p, Dependent *dd_p)
02269 {
02270     int i;
02271 
02272     /*
02273     * set any registers specifically called out with xregwrite
02274     * xilinx_flag used to be just a flag 0 or 1 to write, now 
02275     * holds the actual address of the register (unless 0xff)
02276     */
02277     for (i = 0; i < 32; i++)
02278     {
02279         if (dd_p->xilinx_flag[i] == 0xff)
02280             break;
02281 
02282         edt_intfc_write(edt_p, dd_p->xilinx_flag[i], dd_p->xilinx_value[i]);
02283         edt_msg(DEBUG2, "xregwrite_%d writing reg 0x%02x value 0x%02x\n",
02284             dd_p->xilinx_flag[i], dd_p->xilinx_flag[i],
02285             dd_p->xilinx_value[i]);
02286         dep_wait(edt_p);
02287     }
02288 }
02289 
02290 /*
02291 * special setup for CL2 Camera Link simulator
02292 */
02293 void
02294 setup_cl2_simulator(EdtDev *edt_p, Dependent *dd_p)
02295 {
02296     edt_msg(DEBUG1, "STUB setting up camera link simulator...\n");
02297 }
02298 

Generated on 19 Jun 2015 by  doxygen 1.4.7