/* $Id: iseg.c,v 1.6 2003/09/28 09:05:15 jmuelmen Exp $ */ /* #undef DUMMY_INST */ #include "iseg.h" #include "iseg_const.h" #include "isegcan.h" #include #define DEBUG #ifdef DEBUG #include #endif /* #define _iseg_lock */ #ifdef _iseg_lock #include static pthread_mutex_t _mut; #endif static unsigned short _id; static unsigned short _on_off = 0; static char _init = 0; char iseg_init () { #ifndef DUMMY_INST char init, logged_on; char str[128] = "iseg HV"; char timestamp[20]; unsigned char stat = 0, type = 0; #endif #ifdef _iseg_lock pthread_mutex_init(&_mut, NULL); #endif #ifndef DUMMY_INST /* initialize the CAN thing */ ReInitCanSystem(0); init = InitCanSystem(3, 0x378, 7, 125, 0, str); #ifdef DEBUG printf("Initialized %s? %s\n", str, init ? "yes" : "no"); #endif /* DEBUG */ /* log on to the CAN thing */ do { logged_on = CanLogOnOff(&_id, &stat, &type, timestamp); } while (!logged_on); #ifdef DEBUG printf("Logged on as id %d stat %d type %d? %s\n", _id, stat, type, logged_on ? "yes" : "no"); #endif /* DEBUG */ #endif /* DUMMY_INST */ return _init = 1; } /* If we don't have to be thread-safe we can do auto-initialization; otherwise * the initialization has to be done explicitly before threads get spawned. */ #ifndef _iseg_lock #define INIT if (!_init) _init = iseg_init() #else #define INIT if (!_init) return -1 #endif int iseg_lock () { #ifdef _iseg_lock return pthread_mutex_lock(&_mut); #endif return 0; } int iseg_unlock () { #ifdef _iseg_lock return pthread_mutex_unlock(&_mut); #endif return 0; } double iseg_set_v (int _chan_no, double v) { #ifndef DUMMY_INST long int dac = (long)(v / 700.0 * 1e7); uchar CanBuff[5]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); CanBuff[0] = 0x04; CanBuff[1] = DataID_SetVoltage | _chan_no; CanBuff[2] = dac >> 16; CanBuff[3] = dac >> 8; CanBuff[4] = dac; /* printf("writing DAC value %x %x %x, ", CanBuff[2], CanBuff[3], CanBuff[4]); */ strcpy(tstamp, SendTime); Write2Can(_id & 0xffe, CanBuff, tstamp); /* printf("tstamp is %s\n", tstamp); */ iseg_unlock(); #endif return v; } double iseg_get_v (int _chan_no) { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = DataID_SetVoltage | _chan_no; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_lock(); return 700 / 1e7 * ((CanBuff[2] << 16) + (CanBuff[3] << 8) + CanBuff[4]); } iseg_unlock(); #endif return -1; } double iseg_get_v_meas (int _chan_no) { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = DataID_ActualVoltage | _chan_no; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_unlock(); return 700 / 1e7 * ((CanBuff[2] << 16) + (CanBuff[3] << 8) + CanBuff[4]); } iseg_unlock(); #endif return -1; } double iseg_get_i_meas (int _chan_no) { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = DataID_ActualCurrent | _chan_no; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_unlock(); return 4 / 1e7 * ((CanBuff[2] << 16) + (CanBuff[3] << 8) + CanBuff[4]); } iseg_unlock(); #endif return -1; } void iseg_turn_on (int _chan_no) { #ifndef DUMMY_INST uchar CanBuff[4]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return; #ifndef DUMMY_INST /* serialize access to the module */ iseg_lock(); _on_off |= 1 << _chan_no; CanBuff[0] = 0x03; CanBuff[1] = DataID_OnOff; CanBuff[2] = _on_off >> 8; CanBuff[3] = _on_off; strcpy(tstamp, SendTime); Write2Can(_id & 0xffe, CanBuff, tstamp); iseg_unlock(); #endif } void iseg_turn_off (int _chan_no) { #ifndef DUMMY_INST uchar CanBuff[4]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return; #ifndef DUMMY_INST iseg_lock(); _on_off &= ~1 << _chan_no; CanBuff[0] = 0x03; CanBuff[1] = DataID_OnOff; CanBuff[2] = _on_off >> 8; CanBuff[3] = _on_off; strcpy(tstamp, SendTime); Write2Can(_id & 0xffe, CanBuff, tstamp); iseg_unlock(); #endif } double iseg_set_i_trip (int _chan_no, double i) { #ifndef DUMMY_INST long int dac = (long)(i / 4.0 * 1e7); uchar CanBuff[5]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); CanBuff[0] = 0x04; CanBuff[1] = 0x80 | _chan_no; CanBuff[2] = dac >> 16; CanBuff[3] = dac >> 8; CanBuff[4] = dac; strcpy(tstamp, SendTime); Write2Can((_id & 0xffe) | 2, CanBuff, tstamp); iseg_unlock(); #endif return i; } double iseg_get_i_trip (int _chan_no) { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = 0x80 | _chan_no; strcpy(tstamp, SendTime); if (WriteReadfCan(_id | 2, CanBuff, tstamp)) { iseg_unlock(); return 4 / 1e7 * ((CanBuff[2] << 16) + (CanBuff[3] << 8) + CanBuff[4]); } iseg_unlock(); #endif return -1; } unsigned int iseg_get_chan_stat (int _chan_no) { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; if (_chan_no > ISEG_MAX_CHAN) return -1; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = 0xb0 | _chan_no; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_unlock(); return (CanBuff[2] << 8) + CanBuff[3]; } iseg_unlock(); #endif return 0; } double iseg_get_ramp () { #ifndef DUMMY_INST uw16 id; uchar CanBuff[10]; char tstamp[20]; #endif INIT; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = 0xd0; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_unlock(); return 700 / 5e4 * ((CanBuff[2] << 8) + CanBuff[3]); } iseg_unlock(); #endif return -1; } double iseg_set_ramp (double r) { #ifndef DUMMY_INST long int dac = (long)(r / 700.0 * 5e4); uchar CanBuff[5]; char tstamp[20]; #endif INIT; #ifndef DUMMY_INST iseg_lock(); CanBuff[0] = 0x03; CanBuff[1] = 0xd0; CanBuff[2] = dac >> 8; CanBuff[3] = dac; strcpy(tstamp, SendTime); Write2Can(_id & 0xffe, CanBuff, tstamp); iseg_unlock(); #endif return r; } unsigned int iseg_reset () { #ifndef DUMMY_INST uchar CanBuff[5]; char tstamp[20]; #endif INIT; #ifndef DUMMY_INST iseg_lock(); while (ReadfCan(&_id, CanBuff, tstamp)); /* make a blank CAN buffer */ CanBuff[0] = 1; CanBuff[1] = 0xf8; strcpy(tstamp, SendTime); if (WriteReadfCan(_id, CanBuff, tstamp)) { iseg_unlock(); return (CanBuff[2] << 8) + CanBuff[3]; } iseg_unlock(); #endif return 0; }