/* $Id: load.c,v 1.4 2004/02/16 10:51:34 jmuelmen Exp $ */ #include #include #ifndef DUMMY_INST #include #include "Decl-32.h" #endif static long _dev = -1; #undef FUCKFUCK #ifdef FUCKFUCK static pthread_mutex_t _mut; #endif #define LISTLEN 10000 #define STRLEN 10000 #define NCHN 2 static int _listlen = 0; static double _curr_list[NCHN][LISTLEN]; static double _curr_slew_list[NCHN][LISTLEN]; static double _curr_dwell_list[NCHN][LISTLEN]; static char _curr_list_s[NCHN][STRLEN]; static char _curr_slew_list_s[NCHN][STRLEN]; static char _curr_dwell_list_s[NCHN][STRLEN]; static char _fname[NCHN][STRLEN] = { "c1.data", "c2.data" }; static double _interval[NCHN] = { 1e-2, 1e-2 }; static int _npts[NCHN] = { 100, 100 }; static double _offset[NCHN] = { 0, 0 }; #ifdef FUCKFUCK #define LOAD_LOCK pthread_mutex_lock(&_mut) #define LOAD_UNLOCK pthread_mutex_unlock(&_mut) #else #define LOAD_LOCK #define LOAD_UNLOCK #endif int load_set_current (int chn, double); int load_set_current_list (int chn, const char *); int load_set_current_slew_list (int chn, const char *); int load_set_current_dwell_list (int chn, const char *); int load_set_output_file (int chn, const char *); static int _str2float (const char *, double *, int*); int _gpwr (char *s) { #ifndef DUMMY_INST /* printf("Writing %s\n", s); */ return ibwrt(_dev, s, strlen(s)); #else return 0; #endif } int _gprd (char *s) { #ifndef DUMMY_INST int ret = ibrd(_dev, s, STRLEN); if (ibcnt == STRLEN) s[STRLEN - 1] = 0; return ret; #else return 0; #endif } static int _str2float (const char *c, double *d, int *len) { int cc = 0; double value; *len = 0; while (sscanf(c, " %le,", &value) == 1) { c = strstr(c, ",") + 1; if (cc == LISTLEN - 1) return 1; d[cc++] = value; (*len)++; /* printf("%e... ", value); */ if (c == (char *)1) return 0; } return 1; } int load_init () { char s[STRLEN]; if (_dev >= 0) return 0; printf("Initializing load tester at device 29... "); #ifndef DUMMY_INST _dev = ibdev(0,29,0,T3s,1,0); _gpwr("*rst\r"); _gpwr("*idn?\r"); _gprd(s); printf("%s.\n", s); _gpwr("*rst\r"); _gpwr("inp on\r"); #else _dev = 0; printf("dummy.\n"); #endif #ifdef FUCKFUCK pthread_mutex_init(&_mut, NULL); #endif return 0; } int load_set_current (int chn, double i) { char s[STRLEN]; if (chn > NCHN || chn < 1) return -1; LOAD_LOCK; #ifndef DUMMY_INST sprintf(s, "chan %d; curr %f\r", chn, i); _gpwr(s); #else /* don't do anything */ #endif LOAD_UNLOCK; return 0; } double load_get_current (int chn) { char s[STRLEN]; double i = 0; if (chn > NCHN || chn < 1) return -1; LOAD_LOCK; #ifndef DUMMY_INST sprintf(s, "chan %d; curr?\r", chn); _gpwr(s); _gprd(s); i = atof(s); #else /* don't do anything */ #endif LOAD_UNLOCK; return i; } int load_set_current_list (int chn, const char *l) { int ret; if (chn > NCHN || chn < 1) return -1; if (strlen(l) >= STRLEN) return -1; LOAD_LOCK; strncpy(_curr_list_s[chn - 1], l, STRLEN); ret = _str2float(l, _curr_list[chn - 1], &_listlen); /* printf("str2float returned %d\n", ret); */ LOAD_UNLOCK; return ret; } int load_set_current_slew_list (int chn, const char *l) { int ret; if (chn > NCHN || chn < 1) return -1; if (strlen(l) >= STRLEN) return -1; LOAD_LOCK; strncpy(_curr_slew_list_s[chn - 1], l, STRLEN); ret = _str2float(l, _curr_slew_list[chn - 1], &_listlen); LOAD_UNLOCK; return 0; } int load_set_current_dwell_list (int chn, const char *l) { int ret; if (chn > NCHN || chn < 1) return -1; if (strlen(l) >= STRLEN) return -1; LOAD_LOCK; strncpy(_curr_dwell_list_s[chn - 1], l, STRLEN); ret = _str2float(l, _curr_dwell_list[chn - 1], &_listlen); LOAD_UNLOCK; return 0; } int load_set_output_file (int chn, const char *l) { if (chn > NCHN || chn < 1) return -1; if (strlen(l) >= STRLEN) return -1; LOAD_LOCK; strncpy(_fname[chn - 1], l, STRLEN); LOAD_UNLOCK; return 0; } #ifdef BUILD_LOAD int load_trigger (int chn) #else int load_trigger (int chn, double *time, double *volt, double *curr, int *nent) #endif { int i_d, idwell, ipt, n; char cmd[STRLEN]; char data[STRLEN]; double tdwell = 0; FILE *fout; #ifdef BUILD_LOAD double time[LISTLEN], volt[LISTLEN], curr[LISTLEN]; #endif if (chn > NCHN || chn < 1) return 1; LOAD_LOCK; #ifndef DUMMY_INST ibtmo(_dev, TNONE); sprintf(cmd, "%s", "ABOR; *RST\r"); _gpwr(cmd); sprintf(cmd, "%s %d \r", "CHAN ", chn); _gpwr(cmd); _gpwr("CURR:MODE LIST\r"); sprintf(cmd, "LIST:CURR %s\r", _curr_list_s[chn - 1]); _gpwr(cmd); sprintf(cmd, "LIST:CURR:SLEW %s\r", _curr_slew_list_s[chn - 1]); _gpwr(cmd); sprintf(cmd, "LIST:DWEL %s\r", _curr_dwell_list_s[chn - 1]); _gpwr(cmd); _gpwr("LIST:CURR:RANG 4.5\r"); _gpwr("LIST:CURR:TLEV 0\r"); sprintf(cmd, "TRIG:SEQ2:COUN %d\r", _listlen); _gpwr(cmd); _gpwr("LIST:COUN 1\r"); _gpwr("LIST:STEP AUTO\r"); sprintf(cmd, "SENS:SWE:TINT %e\r", _interval[chn - 1]); _gpwr(cmd); sprintf(cmd, "SENS:SWE:POIN %d\r", _npts[chn - 1]); _gpwr(cmd); sprintf(cmd, "SENS:SWE:OFFS %e\r", _offset[chn - 1]); _gpwr(cmd); sprintf(cmd, "INIT:SEQ1\r"); _gpwr(cmd); sprintf(cmd, "INIT:SEQ2\r"); _gpwr(cmd); sprintf(cmd, "TRIG:IMM\r"); _gpwr(cmd); sprintf(cmd, "*TRG\r"); _gpwr(cmd); sprintf(cmd, "FETC:ARR:CURR?\r"); _gpwr(cmd); _gprd(data); /* printf("%s\n", data); */ _str2float(data, curr, &n); sprintf(cmd, "FETC:ARR:VOLT?\r"); _gpwr(cmd); _gprd(data); _str2float(data, volt, &n); printf("read %d data points\n", n); i_d = 0; fout = fopen(_fname[chn - 1], "w"); for (idwell = 0; idwell < _listlen; ++idwell) { /* figure out how long we've cumulatively dwelt */ tdwell += idwell ? _curr_dwell_list[chn - 1][idwell - 1] : 0; for (ipt = 0; ipt < _npts[chn - 1]; ++ipt) { double t = ipt * _interval[chn - 1] + tdwell + _offset[chn - 1]; if (i_d == n) goto print_done; time[i_d] = t; #ifdef BUILD_LOAD fprintf(fout, "%e\t%e\t%e\n", t, volt[i_d], curr[i_d]); #endif i_d++; } } print_done: #else /* don't do anything */ #endif #ifndef BUILD_LOAD *nent = n; #endif fclose(fout); LOAD_UNLOCK; return 0; } int load_set_int (int chn, double interval) { if (chn > NCHN || chn < 1) return 1; LOAD_LOCK; _interval[chn - 1] = interval; LOAD_UNLOCK; } int load_set_npts (int chn, int npts) { if (chn > NCHN || chn < 1) return 1; LOAD_LOCK; _npts[chn - 1] = npts; LOAD_UNLOCK; } int load_set_offset (int chn, double offset) { if (chn > NCHN || chn < 1) return 1; LOAD_LOCK; _offset[chn - 1] = offset; LOAD_UNLOCK; }