diff --git a/slsDetectorServers/eigerDetectorServer/FebControl.c b/slsDetectorServers/eigerDetectorServer/FebControl.c index 9dc53e9de..403e0355f 100644 --- a/slsDetectorServers/eigerDetectorServer/FebControl.c +++ b/slsDetectorServers/eigerDetectorServer/FebControl.c @@ -48,7 +48,6 @@ unsigned int Feb_Control_trimbit_size; unsigned int *Feb_Control_last_downloaded_trimbits; int Feb_Control_module_number; -int Feb_Control_current_index; int Feb_Control_counter_bit = 1; int Feb_control_master = 0; @@ -61,21 +60,21 @@ double ratemax = -1; int Feb_Control_activated = 1; int Feb_Control_hv_fd = -1; -void Module_Module(struct Module *mod, unsigned int number, - unsigned int address_top) { +// module +void Module_Module(struct Module *mod, unsigned int number) { mod->module_number = number; - mod->left_address = 0x100 | (0xff & address_top); - mod->right_address = (0x200 | (0xff & address_top)); + mod->left_address = 0x100; + mod->right_address = (0x200; + if (number == 0u) { + mod->left_address |= 0xff; + mod->right_address |= 0xff; + } mod->high_voltage = -1; mod->dac = malloc(Module_ndacs * sizeof(int)); for (unsigned int i = 0; i < Module_ndacs; i++) mod->dac[i] = 0; } -unsigned int Module_GetModuleNumber(struct Module *mod) { - return mod->module_number; -} - unsigned int Module_GetBaseAddress(struct Module *mod) { return (mod->left_address & 0xff); } @@ -112,6 +111,7 @@ int Module_GetDACValue(struct Module *mod, unsigned int i) { return (i < Module_ndacs) ? mod->dac[i] : -1; } +// setup void Feb_Control_activate(int activate) { Feb_Control_activated = activate; } int Feb_Control_GetModuleNumber() { return Feb_Control_module_number; } @@ -127,37 +127,29 @@ void Feb_Control_FebControl() { } int Feb_Control_Init(int master, int normal, int module_num) { - Feb_Control_module_number = 0; - Feb_Control_current_index = 0; Feb_control_master = master; Feb_control_normal = normal; - // global send - Feb_Control_AddModule(0, 0xff); + // global send (module number 0) + Feb_Control_AddModule(0); Feb_Control_PrintModuleList(); // Add the half module Feb_Control_module_number = (module_num & 0xFF); - Feb_Control_current_index = 1; - Feb_Control_AddModule(Feb_Control_module_number, 0); + Feb_Control_AddModule(Feb_Control_module_number); Feb_Control_PrintModuleList(); - unsigned int nfebs = 0; - unsigned int *feb_list = malloc(moduleSize * 4 * sizeof(unsigned int)); - feb_list[nfebs++] = Module_GetRightAddress(&modules[1]); - feb_list[nfebs++] = Module_GetLeftAddress(&modules[1]); - - Feb_Interface_SendCompleteList(nfebs, feb_list); - free(feb_list); - if (Feb_Control_activated) - Feb_Interface_SetByteOrder(); - + // interface setup + Feb_Interface_SetAddress(Module_GetRightAddress(&modules[1]), + Module_GetLeftAddress(&modules[1])); + if (Feb_Control_activated) { + return Feb_Interface_SetByteOrder(); + } return 1; } int Feb_Control_OpenSerialCommunication() { LOG(logINFO, ("opening serial communication of hv\n")); - // if (Feb_Control_hv_fd != -1) close(Feb_Control_hv_fd); Feb_Control_hv_fd = open(SPECIAL9M_HIGHVOLTAGE_PORT, O_RDWR | O_NOCTTY | O_SYNC); @@ -219,176 +211,101 @@ void Feb_Control_PrintModuleList() { LOG(logDEBUG1, ("Module list:\n")); for (unsigned int i = 0; i < moduleSize; i++) { LOG(logDEBUG1, - ("\t%d) %s modules: %d 0x%x %s\n", i, - ((i == 0) ? "All " : ((i == 1) ? "Master" : " ")), - Module_GetModuleNumber(&modules[i]), - (Module_TopAddressIsValid(&modules[i]) - ? Module_GetTopBaseAddress(&modules[i]) - : Module_GetBottomBaseAddress(&modules[i])), - (Module_TopAddressIsValid(&modules[i]) ? "(top) " - : "(bottom) "))); + ("\t%d) %s\t: [Module Number:%d, Base Address: 0x%x]\n", i, + ((i == 0) ? "Global" : "Half Module"), modules[i]->module_number, + Module_GetBaseAddress(&modules[i]))); } } -int Feb_Control_GetModuleIndex(unsigned int module_number, - unsigned int *module_index) { - for (unsigned int i = 0; i < moduleSize; i++) { - if (Module_GetModuleNumber(&modules[i]) == module_number) { - *module_index = i; - return 1; - } - } - - return 0; -} - -int Feb_Control_CheckModuleAddresses(struct Module *m) { - int found_t = 0; - int found_b = 0; - for (unsigned int i = 0; i < moduleSize; i++) { - if ((Module_TopAddressIsValid(m) && - Module_GetTopBaseAddress(&modules[i]) && - Module_GetTopBaseAddress(m) == - Module_GetTopBaseAddress(&modules[i])) || - (Module_TopAddressIsValid(m) && - Module_GetBottomBaseAddress(&modules[i]) && - Module_GetTopBaseAddress(m) == - Module_GetBottomBaseAddress(&modules[i]))) - found_t = 1; - if ((Module_BottomAddressIsValid(m) && - Module_GetTopBaseAddress(&modules[i]) && - Module_GetBottomBaseAddress(m) == - Module_GetTopBaseAddress(&modules[i])) || - (Module_BottomAddressIsValid(m) && - Module_GetBottomBaseAddress(&modules[i]) && - Module_GetBottomBaseAddress(m) == - Module_GetBottomBaseAddress(&modules[i]))) - found_b = 1; - } - - if (found_t) { - LOG(logERROR, - ("top address %d already used.\n", Module_GetTopBaseAddress(m))); - } - if (found_b) { - LOG(logERROR, ("bottom address %d already used.\n", - Module_GetBottomBaseAddress(m))); - } - - int top_bottom_same = - Module_TopAddressIsValid(m) && Module_BottomAddressIsValid(m) && - Module_GetTopBaseAddress(m) == Module_GetBottomBaseAddress(m); - if (top_bottom_same) { - LOG(logERROR, ("top and bottom address are the same %d.\n", - Module_GetTopBaseAddress(m))); - } - - return !(top_bottom_same || found_t || found_b); -} - -int Feb_Control_AddModule(unsigned int module_number, - unsigned int top_address) { - int parameters_ok = 1; +void Feb_Control_AddModule(unsigned int module_number) { struct Module mod, *m; m = &mod; - Module_Module(m, module_number, top_address); - parameters_ok &= Feb_Control_CheckModuleAddresses(m); + Module_Module(m, module_number); - if (Module_TopAddressIsValid(m) && Module_BottomAddressIsValid(m)) { - LOG(logDEBUG1, ("\tAdding full module number %d with top and bottom " - "base addresses: %d %d\n", - Module_GetModuleNumber(m), Module_GetTopBaseAddress(m), - Module_GetBottomBaseAddress(m))); - modules[moduleSize] = mod; - moduleSize++; - } else if (Module_TopAddressIsValid(m)) { - LOG(logDEBUG1, - ("\tAdding half module number %d with " - "top base address: %d\n", - Module_GetModuleNumber(m), Module_GetTopBaseAddress(m))); - modules[moduleSize] = mod; - moduleSize++; - } else if (Module_BottomAddressIsValid(m)) { - LOG(logDEBUG1, - ("\tAdding half module number %d with " - "bottom base address: %d\n", - Module_GetModuleNumber(m), Module_GetBottomBaseAddress(m))); - modules[moduleSize] = mod; - moduleSize++; - } else { - // free(m); - } - - return parameters_ok; + LOG(logDEBUG1, ("\tAdding half module number %d with " + "top base address: %d\n", + m->module_number, Module_GetBaseAddress(m))); + modules[moduleSize] = mod; + moduleSize++; } int Feb_Control_CheckSetup(int master) { LOG(logDEBUG1, ("Checking Set up\n")); - unsigned int i; - int ok = 1; - - i = Feb_Control_current_index; for (unsigned int j = 0; j < 4; j++) { - if (Module_GetTopIDelay(&modules[i], j) < 0) { - LOG(logERROR, ("module %d's idelay top number %d not set.\n", - Module_GetModuleNumber(&modules[i]), j)); - ok = 0; - } - if (Module_GetBottomIDelay(&modules[i], j) < 0) { - LOG(logERROR, ("module %d's idelay bottom number %d not set.\n", - Module_GetModuleNumber(&modules[i]), j)); - ok = 0; + if (Module_GetIDelay(&modules[1], j) < 0) { + LOG(logERROR, ("idelay chip %d not set.\n", j)); + return 0; } } int value = 0; if ((Feb_control_master) && (!Feb_Control_GetHighVoltage(&value))) { - LOG(logERROR, ("module %d's high voltage not set.\n", - Module_GetModuleNumber(&modules[i]))); - ok = 0; + LOG(logERROR, ("high voltage not set.\n")); + return 0; } for (unsigned int j = 0; j < Module_ndacs; j++) { - if (Module_GetTopDACValue(&modules[i], j) < 0) { - LOG(logERROR, - ("module %d's top \"%s\" dac is not set.\n", - Module_GetModuleNumber(&modules[i]), Module_dac_names[i])); - ok = 0; - } - if (Module_GetBottomDACValue(&modules[i], j) < 0) { - LOG(logERROR, - ("module %d's bottom \"%s\" dac is not set.\n", - Module_GetModuleNumber(&modules[i]), Module_dac_names[i])); - ok = 0; + if (Module_GetDACValue(&modules[1], j) < 0) { + LOG(logERROR, ("\"%s\" dac is not set.\n", Module_dac_names[j])); + return 0; } } - /* }*/ LOG(logDEBUG1, ("Done Checking Set up\n")); - return ok; + return 1; } -unsigned int Feb_Control_GetNModules() { - if (moduleSize <= 0) - return 0; - return moduleSize - 1; +unsigned int Feb_Control_AddressToAll() { + return Module_GetLeftAddress(&modules[1]) | + Module_GetRightAddress(&modules[1]); } -unsigned int Feb_Control_GetNHalfModules() { - unsigned int n_half_modules = 0; - for (unsigned int i = 1; i < moduleSize; i++) { - if (Module_TopAddressIsValid(&modules[i])) - n_half_modules++; - if (Module_BottomAddressIsValid(&modules[i])) - n_half_modules++; +int Feb_Control_SetCommandRegister(unsigned int cmd) { + if (Feb_Control_activated) + return Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_CHIP_CMDS, cmd, 0, 0); + else + return 1; +} + +int Feb_Control_SetStaticBits() { + if (Feb_Control_activated) { + // program=1,m4=2,m8=4,test=8,rotest=16,cs_bar_left=32,cs_bar_right=64 + if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_STATIC_BITS, + Feb_Control_staticBits, 0, 0) || + !Feb_Control_SetCommandRegister(DAQ_SET_STATIC_BIT) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { + LOG(logERROR, ("Could not set static bits\n")); + return 0; + } } - - return n_half_modules; + return 1; } -int Feb_Control_SetIDelays(unsigned int module_num, unsigned int ndelay_units) { - int ret = Feb_Control_SetIDelays1(module_num, 0, ndelay_units) && - Feb_Control_SetIDelays1(module_num, 1, ndelay_units) && - Feb_Control_SetIDelays1(module_num, 2, ndelay_units) && - Feb_Control_SetIDelays1(module_num, 3, ndelay_units); +int Feb_Control_SetStaticBits1(unsigned int the_static_bits) { + Feb_Control_staticBits = the_static_bits; + return Feb_Control_SetStaticBits(); +} + +int Feb_Control_SetInTestModeVariable(int on) { + if (on) + Feb_Control_staticBits |= + DAQ_STATIC_BIT_CHIP_TEST; // setting test bit to high + else + Feb_Control_staticBits &= + (~DAQ_STATIC_BIT_CHIP_TEST); // setting test bit to low + return 1; +} + +int Feb_Control_GetTestModeVariable() { + return Feb_Control_staticBits & DAQ_STATIC_BIT_CHIP_TEST; +} + +// idelay +int Feb_Control_SetIDelays(unsigned int ndelay_units) { + int ret = Feb_Control_SetIDelays1(0, ndelay_units) && + Feb_Control_SetIDelays1(1, ndelay_units) && + Feb_Control_SetIDelays1(2, ndelay_units) && + Feb_Control_SetIDelays1(3, ndelay_units); if (ret) { LOG(logINFO, ("IODelay set to %d\n", ndelay_units)); } @@ -396,107 +313,33 @@ int Feb_Control_SetIDelays(unsigned int module_num, unsigned int ndelay_units) { } int Feb_Control_SetIDelays1( - unsigned int module_num, unsigned int chip_pos, + unsigned int chip_pos, unsigned int ndelay_units) { // chip_pos 0=ll,1=lr,0=rl,1=rr - // currently set same for top and bottom if (chip_pos > 3) { LOG(logERROR, ("SetIDelay chip_pos %d doesn't exist.\n", chip_pos)); return 0; } - unsigned int module_index = 0; - if (!Feb_Control_GetModuleIndex(module_num, &module_index)) { - LOG(logERROR, - ("could not set i delay module number %d invalid.\n", module_num)); - return 0; - } - - int ok = 1; if (chip_pos / 2 == 0) { // left fpga - if (Module_TopAddressIsValid(&modules[module_index])) { - if (Feb_Control_SendIDelays( - Module_GetTopLeftAddress(&modules[module_index]), - chip_pos % 2 == 0, 0xffffffff, ndelay_units)) { - if (module_index != 0) - Module_SetTopIDelay(&modules[module_index], chip_pos, - ndelay_units); - else { - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetTopIDelay(&modules[i], chip_pos, - ndelay_units); - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetBottomIDelay(&modules[i], chip_pos, - ndelay_units); - } - } else { - LOG(logERROR, - ("could not set idelay module number %d (top_left).\n", - module_num)); - ok = 0; - } - } - if (Module_BottomAddressIsValid(&modules[module_index])) { - if (Feb_Control_SendIDelays( - Module_GetBottomLeftAddress(&modules[module_index]), - chip_pos % 2 == 0, 0xffffffff, ndelay_units)) { - if (module_index != 0) - Module_SetBottomIDelay(&modules[module_index], chip_pos, - ndelay_units); - else { - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetTopIDelay(&modules[i], chip_pos, - ndelay_units); - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetBottomIDelay(&modules[i], chip_pos, - ndelay_units); - } - } else { - LOG(logERROR, - ("could not set idelay module number %d (bottom_left).\n", - module_num)); - ok = 0; - } + if (Feb_Control_SendIDelays(Module_GetLeftAddress(&modules[1]), + chip_pos % 2 == 0, 0xffffffff, + ndelay_units)) { + Module_SetIDelay(&modules[1], chip_pos, ndelay_units); + } else { + LOG(logERROR, ("could not set idelay (left).\n")); + return 0; } } else { - if (Module_TopAddressIsValid(&modules[module_index])) { - if (Feb_Control_SendIDelays( - Module_GetTopRightAddress(&modules[module_index]), - chip_pos % 2 == 0, 0xffffffff, ndelay_units)) { - if (module_index != 0) - Module_SetTopIDelay(&modules[module_index], chip_pos, - ndelay_units); - else - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetTopIDelay(&modules[i], chip_pos, - ndelay_units); - } else { - LOG(logERROR, - ("could not set idelay module number %d (top_right).\n", - module_num)); - ok = 0; - } - } - if (Module_BottomAddressIsValid(&modules[module_index])) { - if (Feb_Control_SendIDelays( - Module_GetBottomRightAddress(&modules[module_index]), - chip_pos % 2 == 0, 0xffffffff, ndelay_units)) { - if (module_index != 0) - Module_SetBottomIDelay(&modules[module_index], chip_pos, - ndelay_units); - else - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetBottomIDelay(&modules[i], chip_pos, - ndelay_units); - } else { - LOG(logERROR, - ("could not set idelay module number %d (bottom_right).\n", - module_num)); - ok = 0; - } + if (Feb_Control_SendIDelays(Module_GetRightAddress(&modules[1]), + chip_pos % 2 == 0, 0xffffffff, + ndelay_units)) { + Module_SetIDelay(&modules[1], chip_pos, ndelay_units); + } else { + LOG(logERROR, ("could not set idelay (right).\n")); + return 0; } } - - return ok; + return 1; } int Feb_Control_SendIDelays(unsigned int dst_num, int chip_lr, @@ -534,23 +377,10 @@ int Feb_Control_SendIDelays(unsigned int dst_num, int chip_lr, return 0; } } - return 1; } -int Feb_Control_VoltageToDAC(float value, unsigned int *digital, - unsigned int nsteps, float vmin, float vmax) { - if (value < vmin || value > vmax) - return 0; - *digital = (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); - return 1; -} - -float Feb_Control_DACToVoltage(unsigned int digital, unsigned int nsteps, - float vmin, float vmax) { - return vmin + (vmax - vmin) * digital / (nsteps - 1); -} - +// highvoltage // only master gets to call this function int Feb_Control_SetHighVoltage(int value) { LOG(logDEBUG1, (" Setting High Voltage:\t")); @@ -769,32 +599,32 @@ int Feb_Control_ReceiveHighVoltage(unsigned int *value) { return 1; } -int Feb_Control_DecodeDACString(char *dac_str, unsigned int *module_index, - int *top, int *bottom, unsigned int *dac_ch) { - char *local_s = dac_str; - *module_index = Feb_Control_current_index; - *top = 1; // make them both 1 instead of this - *bottom = 1; +// dacs +int Feb_Control_VoltageToDAC(float value, unsigned int *digital, + unsigned int nsteps, float vmin, float vmax) { + if (value < vmin || value > vmax) + return 0; + *digital = (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); + return 1; +} - if (Module_BottomAddressIsValid(&modules[*module_index])) - *top = 0; - else - *bottom = 0; +float Feb_Control_DACToVoltage(unsigned int digital, unsigned int nsteps, + float vmin, float vmax) { + return vmin + (vmax - vmin) * digital / (nsteps - 1); +} +int Feb_Control_DecodeDACString(char *dac_str, unsigned int *dac_ch) { *dac_ch = 0; - if (!Feb_Control_GetDACNumber(local_s, dac_ch)) { - LOG(logERROR, ("invalid dac_name: %s (%s)\n", dac_str, local_s)); + if (!Feb_Control_GetDACNumber(dac_str, dac_ch)) { + LOG(logERROR, ("invalid dac_name: %s \n", dac_str)); return 0; } - return 1; } int Feb_Control_SetDAC(char *dac_str, int value, int is_a_voltage_mv) { - unsigned int module_index, dac_ch; - int top, bottom; - if (!Feb_Control_DecodeDACString(dac_str, &module_index, &top, &bottom, - &dac_ch)) + unsigned int dac_ch; + if (!Feb_Control_DecodeDACString(dac_str, &dac_ch)) return 0; unsigned int v = value; @@ -808,48 +638,22 @@ int Feb_Control_SetDAC(char *dac_str, int value, int is_a_voltage_mv) { LOG(logERROR, ("SetDac bad value, %d. The range is 0 to 4095.\n", v)); return 0; } + if (!Feb_Control_SendDACValue(Module_GetRightAddress(&modules[1]), dac_ch, + &v)) + return 0; - if (top && Module_TopAddressIsValid(&modules[module_index])) { - - if (!Feb_Control_SendDACValue( - Module_GetTopRightAddress(&modules[module_index]), dac_ch, &v)) - return 0; - - if (module_index != 0) - Module_SetTopDACValue(&modules[module_index], dac_ch, v); - else - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetTopDACValue(&modules[i], dac_ch, v); - } - - if (bottom && Module_BottomAddressIsValid(&modules[module_index])) { - if (!Feb_Control_SendDACValue( - Module_GetBottomRightAddress(&modules[module_index]), dac_ch, - &v)) - return 0; - if (module_index != 0) - Module_SetBottomDACValue(&modules[module_index], dac_ch, v); - else - for (unsigned int i = 0; i < moduleSize; i++) - Module_SetBottomDACValue(&modules[i], dac_ch, v); - } - + Module_SetDACValue(&modules[1], dac_ch, v); return 1; } int Feb_Control_GetDAC(char *s, int *ret_value, int voltage_mv) { - - unsigned int module_index, dac_ch; - int top, bottom; - if (!Feb_Control_DecodeDACString(s, &module_index, &top, &bottom, &dac_ch)) + unsigned int dac_ch; + if (!Feb_Control_DecodeDACString(s, &dac_ch)) return 0; - *ret_value = top ? Module_GetTopDACValue(&modules[module_index], dac_ch) - : Module_GetBottomDACValue(&modules[module_index], dac_ch); - + *ret_value = Module_GetDACValue(&modules[1], dac_ch); if (voltage_mv) *ret_value = Feb_Control_DACToVoltage(*ret_value, 4096, 0, 2048); - return 1; } @@ -870,7 +674,6 @@ int Feb_Control_GetDACNumber(char *s, unsigned int *n) { return 1; } } - return 0; } @@ -882,8 +685,6 @@ int Feb_Control_SendDACValue(unsigned int dst_num, unsigned int ch, return 0; } - // if (voltage<0) return PowerDownDAC(socket_num,ch); - *value &= 0xfff; unsigned int dac_ic = (ch < 8) ? 1 : 2; unsigned int dac_ch = ch % 8; @@ -906,22 +707,15 @@ int Feb_Control_SendDACValue(unsigned int dst_num, unsigned int ch, return 1; } -int Feb_Control_SetTrimbits(unsigned int module_num, unsigned int *trimbits, - int top) { +int Feb_Control_SetTrimbits(unsigned int *trimbits, int top) { LOG(logINFO, ("Setting Trimbits\n")); - unsigned int trimbits_to_load_l[1024]; unsigned int trimbits_to_load_r[1024]; - unsigned int module_index = 0; - if (!Feb_Control_GetModuleIndex(module_num, &module_index)) { - LOG(logERROR, ("could not set trimbits, bad module number.\n")); - return 0; - } - if (Feb_Control_Reset() == STATUS_ERROR) { LOG(logERROR, ("could not reset DAQ.\n")); } + for (int l_r = 0; l_r < 2; l_r++) { // l_r loop unsigned int disable_chip_mask = l_r ? DAQ_CS_BAR_LEFT : DAQ_CS_BAR_RIGHT; @@ -1037,43 +831,19 @@ int Feb_Control_SetTrimbits(unsigned int module_num, unsigned int *trimbits, } // end supercolumn loop sc } // end row loop - if (Module_TopAddressIsValid(&modules[1])) { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteMemoryInLoops( - Module_GetTopLeftAddress( - &modules[Feb_Control_current_index]), - 0, 0, 1024, trimbits_to_load_l) || - !Feb_Interface_WriteMemoryInLoops( - Module_GetTopRightAddress( - &modules[Feb_Control_current_index]), - 0, 0, 1024, trimbits_to_load_r) || - // if - // (!Feb_Interface_WriteMemory(Module_GetTopLeftAddress(&modules[0]),0,0,1023,trimbits_to_load_r)|| - // !Feb_Interface_WriteMemory(Module_GetTopRightAddress(&modules[0]),0,0,1023,trimbits_to_load_l)|| - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != - STATUS_IDLE)) { - LOG(logERROR, (" some errror!\n")); - return 0; - } - } - } else { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteMemoryInLoops( - Module_GetBottomLeftAddress( - &modules[Feb_Control_current_index]), - 0, 0, 1024, trimbits_to_load_l) || - !Feb_Interface_WriteMemoryInLoops( - Module_GetBottomRightAddress( - &modules[Feb_Control_current_index]), - 0, 0, 1024, trimbits_to_load_r) || - // if - // (!Feb_Interface_WriteMemory(Module_GetTopLeftAddress(&modules[0]),0,0,1023,trimbits_to_load_r)|| - // !Feb_Interface_WriteMemory(Module_GetTopRightAddress(&modules[0]),0,0,1023,trimbits_to_load_l)|| - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != - STATUS_IDLE)) { - LOG(logERROR, (" some errror!\n")); - return 0; - } + if (Feb_Control_activated) { + if (!Feb_Interface_WriteMemoryInLoops( + Module_GetLeftAddress( + &modules[Feb_Control_current_index]), + 0, 0, 1024, trimbits_to_load_l) || + !Feb_Interface_WriteMemoryInLoops( + Module_GetRightAddress( + &modules[Feb_Control_current_index]), + 0, 0, 1024, trimbits_to_load_r) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != + STATUS_IDLE)) { + LOG(logERROR, (" some errror in setting trimbits!\n")); + return 0; } } @@ -1086,107 +856,36 @@ int Feb_Control_SetTrimbits(unsigned int module_num, unsigned int *trimbits, return Feb_Control_SetStaticBits(); // send the static bits } +int Feb_Control_SaveAllTrimbitsTo(int value, int top) { + unsigned int chanregs[Feb_Control_trimbit_size]; + for (int i = 0; i < Feb_Control_trimbit_size; i++) + chanregs[i] = value; + return Feb_Control_SetTrimbits(0, chanregs, top); +} + unsigned int *Feb_Control_GetTrimbits() { return Feb_Control_last_downloaded_trimbits; } -unsigned int Feb_Control_AddressToAll() { - LOG(logDEBUG1, ("in Feb_Control_AddressToAll()\n")); - - if (moduleSize == 0) - return 0; - - if (Module_BottomAddressIsValid(&modules[1])) { - // if (Feb_Control_am_i_master) - return Module_GetBottomLeftAddress(&modules[1]) | - Module_GetBottomRightAddress(&modules[1]); - // else return 0; - } - - return Module_GetTopLeftAddress(&modules[1]) | - Module_GetTopRightAddress(&modules[1]); - // return - // Module_GetTopLeftAddress(&modules[0])|Module_GetTopRightAddress(&modules[0]); -} - -int Feb_Control_SetCommandRegister(unsigned int cmd) { - if (Feb_Control_activated) - return Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_CHIP_CMDS, cmd, 0, 0); - else - return 1; -} - -int Feb_Control_GetDAQStatusRegister(unsigned int dst_address, - unsigned int *ret_status) { - // if deactivated, should be handled earlier and should not get into this - // function - if (Feb_Control_activated) { - if (!Feb_Interface_ReadRegister(dst_address, DAQ_REG_STATUS, - ret_status)) { - LOG(logERROR, ("Error: reading status register.\n")); - return 0; - } - } - - *ret_status = (0x02FF0000 & *ret_status) >> 16; - return 1; -} - -int Feb_Control_StartDAQOnlyNWaitForFinish(int sleep_time_us) { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_CTRL, 0, 0, 0) || - !Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_CTRL, DAQ_CTRL_START, 0, 0)) { - LOG(logERROR, ("could not start.\n")); - return 0; - } - } - return Feb_Control_WaitForFinishedFlag(sleep_time_us); -} - int Feb_Control_AcquisitionInProgress() { unsigned int status_reg_r = 0, status_reg_l = 0; - // deactivated should return end of acquisition if (!Feb_Control_activated) return STATUS_IDLE; - int ind = Feb_Control_current_index; - if (Module_BottomAddressIsValid(&modules[ind])) { - - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetBottomRightAddress(&modules[ind]), &status_reg_r))) { - LOG(logERROR, ("Error: Trouble reading Status register. bottom " - "right address\n")); - return STATUS_ERROR; - } - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetBottomLeftAddress(&modules[ind]), &status_reg_l))) { - LOG(logERROR, ("Error: Trouble reading Status register. bottom " - "left address\n")); - return STATUS_ERROR; - } - - } else { - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetTopRightAddress(&modules[ind]), &status_reg_r))) { - LOG(logERROR, ("Error: Trouble reading Status register. top right " - "address\n")); - return STATUS_ERROR; - } - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetTopLeftAddress(&modules[ind]), &status_reg_l))) { - LOG(logERROR, - ("Error: Trouble reading Status register. top left address\n")); - return STATUS_ERROR; - } + if (!(Feb_Control_GetDAQStatusRegister(Module_GetRightAddress(&modules[1]), + &status_reg_r))) { + LOG(logERROR, ("Error: Trouble reading Status register (right)" + "address\n")); + return STATUS_ERROR; + } + if (!(Feb_Control_GetDAQStatusRegister(Module_GetLeftAddress(&modules[1]), + &status_reg_l))) { + LOG(logERROR, ("Error: Trouble reading Status register (left)\n")); + return STATUS_ERROR; } - // running if ((status_reg_r | status_reg_l) & DAQ_STATUS_DAQ_RUNNING) { - LOG(logDEBUG1, ("**runningggg\n")); return STATUS_RUNNING; } // idle @@ -1195,49 +894,48 @@ int Feb_Control_AcquisitionInProgress() { int Feb_Control_AcquisitionStartedBit() { unsigned int status_reg_r = 0, status_reg_l = 0; - // deactivated should return acquisition started/ready if (!Feb_Control_activated) return 1; - int ind = Feb_Control_current_index; - if (Module_BottomAddressIsValid(&modules[ind])) { - - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetBottomRightAddress(&modules[ind]), &status_reg_r))) { - LOG(logERROR, ("Error: Trouble reading Status register. bottom " - "right address\n")); - return -1; - } - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetBottomLeftAddress(&modules[ind]), &status_reg_l))) { - LOG(logERROR, ("Error: Trouble reading Status register. bottom " - "left address\n")); - return -1; - } - - } else { - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetTopRightAddress(&modules[ind]), &status_reg_r))) { - LOG(logERROR, ("Error: Trouble reading Status register. top right " - "address\n")); - return -1; - } - if (!(Feb_Control_GetDAQStatusRegister( - Module_GetTopLeftAddress(&modules[ind]), &status_reg_l))) { - LOG(logERROR, - ("Error: Trouble reading Status register. top left address\n")); - return -1; - } + if (!(Feb_Control_GetDAQStatusRegister(Module_GetRightAddress(&modules[1]), + &status_reg_r))) { + LOG(logERROR, ("Error: Trouble reading Status register (right)\n")); + return -1; + } + if (!(Feb_Control_GetDAQStatusRegister(Module_GetLeftAddress(&modules[1]), + &status_reg_l))) { + LOG(logERROR, ("Error: Trouble reading Status register (left)\n")); + return -1; } - // doesnt mean it started, just the bit if ((status_reg_r | status_reg_l) & DAQ_STATUS_DAQ_RUN_TOGGLE) return 1; - return 0; } +int Feb_Control_WaitForStartedFlag(int sleep_time_us, int prev_flag) { + // deactivated dont wait (otherwise give a toggle value back) + if (!Feb_Control_activated) + return 1; + + // did not start + if (prev_flag == -1) + return 0; + + int value = prev_flag; + while (value == prev_flag) { + usleep(sleep_time_us); + value = Feb_Control_AcquisitionStartedBit(); + } + + // did not start + if (value == -1) + return 0; + + return 1; +} + int Feb_Control_WaitForFinishedFlag(int sleep_time_us) { int is_running = Feb_Control_AcquisitionInProgress(); @@ -1258,33 +956,37 @@ int Feb_Control_WaitForFinishedFlag(int sleep_time_us) { else check_error = 0; } - return is_running; } -int Feb_Control_WaitForStartedFlag(int sleep_time_us, int prev_flag) { - - // deactivated dont wait (otherwise give a toggle value back) - if (!Feb_Control_activated) - return 1; - - // did not start - if (prev_flag == -1) - return 0; - - int value = prev_flag; - while (value == prev_flag) { - usleep(sleep_time_us); - value = Feb_Control_AcquisitionStartedBit(); +int Feb_Control_GetDAQStatusRegister(unsigned int dst_address, + unsigned int *ret_status) { + // if deactivated, should be handled earlier and should not get into this + // function + if (Feb_Control_activated) { + if (!Feb_Interface_ReadRegister(dst_address, DAQ_REG_STATUS, + ret_status)) { + LOG(logERROR, ("Error: reading status register.\n")); + return 0; + } } - - // did not start - if (value == -1) - return 0; - + *ret_status = (0x02FF0000 & *ret_status) >> 16; return 1; } +int Feb_Control_StartDAQOnlyNWaitForFinish(int sleep_time_us) { + if (Feb_Control_activated) { + if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_CTRL, 0, 0, 0) || + !Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_CTRL, DAQ_CTRL_START, 0, 0)) { + LOG(logERROR, ("could not start.\n")); + return 0; + } + } + return Feb_Control_WaitForFinishedFlag(sleep_time_us); +} + int Feb_Control_Reset() { LOG(logINFO, ("Reset daq\n")); if (Feb_Control_activated) { @@ -1298,44 +1000,201 @@ int Feb_Control_Reset() { return 0; } } - return Feb_Control_WaitForFinishedFlag(5000); } -int Feb_Control_SetStaticBits() { +int Feb_Control_ResetChipCompletely() { + if (!Feb_Control_SetCommandRegister(DAQ_RESET_COMPLETELY) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { + LOG(logERROR, ("could not ResetChipCompletely() with 0x%x.\n", + DAQ_RESET_COMPLETELY)); + return 0; + } + LOG(logINFO, ("Chip reset completely\n")); + return 1; +} + +int Feb_Control_ResetChipPartially() { + if (!Feb_Control_SetCommandRegister(DAQ_RESET_PERIPHERY) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { + LOG(logERROR, ("could not ResetChipPartially with periphery\n")); + return 0; + } + LOG(logINFO, ("Chip reset periphery 0x%x\n", DAQ_RESET_PERIPHERY)); + + if (!Feb_Control_SetCommandRegister(DAQ_RESET_COLUMN_SELECT) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { + LOG(logERROR, ("could not ResetChipPartially with column select\n")); + return 0; + } + LOG(logINFO, ("Chip reset column select 0x%x\n", DAQ_RESET_COLUMN_SELECT)); + return 1; +} + +int Feb_Control_SendBitModeToBebServer() { + unsigned int just_bit_mode = + (DAQ_STATIC_BIT_M4 | DAQ_STATIC_BIT_M8) & Feb_Control_staticBits; + unsigned int bit_mode = 16; // default + if (just_bit_mode == DAQ_STATIC_BIT_M4) + bit_mode = 4; + else if (just_bit_mode == DAQ_STATIC_BIT_M8) + bit_mode = 8; + else if (Feb_Control_subFrameMode & + DAQ_NEXPOSURERS_ACTIVATE_AUTO_SUBIMAGING) + bit_mode = 32; + + if (!Beb_SetUpTransferParameters(bit_mode)) { + LOG(logERROR, ("Error: sending bit mode to beb\n")); + return 0; + } + return 1; +} + +unsigned int Feb_Control_ConvertTimeToRegister(float time_in_sec) { + float n_clk_cycles = + round(time_in_sec / 10e-9); // 200 MHz ctb clk or 100 MHz feb clk + + unsigned int decoded_time; + if (n_clk_cycles > (pow(2, 29) - 1) * pow(10, 7)) { + float max_time = 10e-9 * (pow(2, 28) - 1) * pow(10, 7); + LOG(logERROR, ("time exceeds (%f) maximum exposure time of %f sec.\n", + time_in_sec, max_time)); + LOG(logINFO, ("\t Setting to maximum %f us.\n", max_time)); + decoded_time = 0xffffffff; + } else { + int power_of_ten = 0; + while (n_clk_cycles > pow(2, 29) - 1) { + power_of_ten++; + n_clk_cycles = round(n_clk_cycles / 10.0); + } + decoded_time = (int)(n_clk_cycles) << 3 | (int)(power_of_ten); + } + return decoded_time; +} + +int Feb_Control_PrepareForAcquisition() { + LOG(logINFOBLUE, ("Preparing for Acquisition\n")); + Feb_Control_PrintAcquisitionSetup(); + + if (Feb_Control_Reset() == STATUS_ERROR) { + LOG(logERROR, ("Trouble reseting daq or data stream\n")); + return 0; + } + + if (!Feb_Control_SetStaticBits1(Feb_Control_staticBits & + (DAQ_STATIC_BIT_M4 | DAQ_STATIC_BIT_M8))) { + LOG(logERROR, ("Trouble setting static bits\n")); + return 0; + } + + if (!Feb_Control_SendBitModeToBebServer()) { + LOG(logERROR, ("Trouble sending static bits to server\n")); + return 0; + } + + int ret = 0; + if (Feb_Control_counter_bit) + ret = Feb_Control_ResetChipCompletely(); + else + ret = Feb_Control_ResetChipPartially(); + if (!ret) { + LOG(logERROR, ("Trouble resetting chips\n")); + return 0; + } + + unsigned int reg_nums[7]; + unsigned int reg_vals[7]; + reg_nums[0] = DAQ_REG_CTRL; + reg_vals[0] = 0; + reg_nums[1] = DAQ_REG_NEXPOSURES; + reg_vals[1] = Feb_Control_nimages; + reg_nums[2] = DAQ_REG_EXPOSURE_TIMER; + reg_vals[2] = + Feb_Control_ConvertTimeToRegister(Feb_Control_exposure_time_in_sec); + reg_nums[3] = DAQ_REG_EXPOSURE_REPEAT_TIMER; + reg_vals[3] = + Feb_Control_ConvertTimeToRegister(Feb_Control_exposure_period_in_sec); + reg_nums[4] = DAQ_REG_CHIP_CMDS; + reg_vals[4] = (Feb_Control_acquireNReadoutMode | Feb_Control_triggerMode | + Feb_Control_externalEnableMode | Feb_Control_subFrameMode); + reg_nums[5] = DAQ_REG_SUBFRAME_EXPOSURES; + reg_vals[5] = Feb_Control_subframe_exposure_time_in_10nsec; + reg_nums[6] = DAQ_REG_SUBFRAME_PERIOD; + reg_vals[6] = Feb_Control_subframe_period_in_10nsec; if (Feb_Control_activated) { - // program=1,m4=2,m8=4,test=8,rotest=16,cs_bar_left=32,cs_bar_right=64 - if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_STATIC_BITS, - Feb_Control_staticBits, 0, 0) || - !Feb_Control_SetCommandRegister(DAQ_SET_STATIC_BIT) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("Could not set static bits\n")); + if (!Feb_Interface_WriteRegisters(Feb_Control_AddressToAll(), 7, + reg_nums, reg_vals, 0, 0)) { + LOG(logERROR, ("Trouble starting acquisition\n")); return 0; } } - - return 1; -} -int Feb_Control_SetStaticBits1(unsigned int the_static_bits) { - Feb_Control_staticBits = the_static_bits; - return Feb_Control_SetStaticBits(); -} - -int Feb_Control_SetInTestModeVariable(int on) { - if (on) - Feb_Control_staticBits |= - DAQ_STATIC_BIT_CHIP_TEST; // setting test bit to high - else - Feb_Control_staticBits &= - (~DAQ_STATIC_BIT_CHIP_TEST); // setting test bit to low return 1; } -int Feb_Control_GetTestModeVariable() { - return Feb_Control_staticBits & DAQ_STATIC_BIT_CHIP_TEST; +void Feb_Control_PrintAcquisitionSetup() { + time_t rawtime; + time(&rawtime); + struct tm *timeinfo = localtime(&rawtime); + LOG(logINFO, + ("Starting an exposure: (%s)" + "\t Dynamic range nbits: %d\n" + "\t Trigger mode: 0x%x\n" + "\t Number of exposures: %d\n" + "\t Exsposure time (if used): %f seconds.\n" + "\t Exsposure period (if used): %f seconds.\n\n", + asctime(timeinfo), Feb_Control_GetDynamicRange(), + Feb_Control_triggerMode, Feb_Control_GetNExposures(), + Feb_Control_exposure_time_in_sec, Feb_Control_exposure_period_in_sec)); } +int Feb_Control_StartAcquisition() { + LOG(logINFOBLUE, ("Starting Acquisition\n")); + unsigned int reg_nums[15]; + unsigned int reg_vals[15]; + for (int i = 0; i < 14; i++) { + reg_nums[i] = DAQ_REG_CTRL; + reg_vals[i] = 0; + } + reg_nums[14] = DAQ_REG_CTRL; + reg_vals[14] = ACQ_CTRL_START; + if (Feb_Control_activated) { + if (!Feb_Interface_WriteRegisters(Feb_Control_AddressToAll(), 15, + reg_nums, reg_vals, 0, 0)) { + LOG(logERROR, ("Trouble starting acquisition\n")); + return 0; + } + } + return 1; +} + +int Feb_Control_StopAcquisition() { return Feb_Control_Reset(); } + +int Feb_Control_SoftwareTrigger() { + unsigned int orig_value = 0; + Feb_Interface_ReadRegister(Feb_Control_AddressToAll(), DAQ_REG_CHIP_CMDS, + &orig_value); + unsigned int cmd = orig_value | DAQ_REG_CHIP_CMDS_INT_TRIGGER; + if (Feb_Control_activated) { + // set trigger bit + LOG(logDEBUG1, ("Setting Trigger, Register:0x%x\n", cmd)); + if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_CHIP_CMDS, cmd, 0, 0)) { + LOG(logERROR, ("Could not give software trigger\n")); + return 0; + } + // unset trigger bit + LOG(logDEBUG1, ("Unsetting Trigger, Register:0x%x\n", orig_value)); + if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_CHIP_CMDS, orig_value, 0, 0)) { + LOG(logERROR, ("Could not give software trigger\n")); + return 0; + } + LOG(logINFO, ("Software Internal Trigger Sent!\n")); + } + return 1; +} + +// parameters int Feb_Control_SetDynamicRange(unsigned int four_eight_sixteen_or_thirtytwo) { static unsigned int everything_but_bit_mode = DAQ_STATIC_BIT_PROGRAM | DAQ_STATIC_BIT_CHIP_TEST | @@ -1381,8 +1240,8 @@ unsigned int Feb_Control_GetDynamicRange() { return 16; } -int Feb_Control_SetReadoutSpeed( - unsigned int readout_speed) { // 0->full,1->half,2->quarter or 3->super_slow +int Feb_Control_SetReadoutSpeed(unsigned int readout_speed) { + // 0->full,1->half,2->quarter or 3->super_slow Feb_Control_acquireNReadoutMode &= (~DAQ_CHIP_CONTROLLER_SUPER_SLOW_SPEED); if (readout_speed == 1) { Feb_Control_acquireNReadoutMode |= DAQ_CHIP_CONTROLLER_HALF_SPEED; @@ -1400,12 +1259,11 @@ int Feb_Control_SetReadoutSpeed( } LOG(logINFO, ("Speed set to full speed (100 MHz)\n")); } - return 1; } -int Feb_Control_SetReadoutMode( - unsigned int readout_mode) { // 0->parallel,1->non-parallel,2-> safe_mode +int Feb_Control_SetReadoutMode(unsigned int readout_mode) { + // 0->parallel,1->non-parallel,2-> safe_mode Feb_Control_acquireNReadoutMode &= (~DAQ_NEXPOSURERS_PARALLEL_MODE); if (readout_mode == 1) { Feb_Control_acquireNReadoutMode |= @@ -1431,7 +1289,7 @@ int Feb_Control_SetReadoutMode( return 1; } -int Feb_Control_SetTriggerMode(unsigned int trigger_mode, int polarity) { +int Feb_Control_SetTriggerMode(unsigned int trigger_mode) { //"00"-> internal exposure time and period, //"01"-> external acquistion start and internal exposure time and period, //"10"-> external start trigger and internal exposure time, @@ -1457,7 +1315,8 @@ int Feb_Control_SetTriggerMode(unsigned int trigger_mode, int polarity) { LOG(logINFO, ("Trigger mode set to Auto\n")); return trigger_mode == 0; } - + // polarity + int polarity = 1; // hard coded (can be configured in future) if (polarity) { Feb_Control_triggerMode |= DAQ_NEXPOSURERS_EXTERNAL_TRIGGER_POLARITY; LOG(logINFO, ("External trigger polarity set to positive\n")); @@ -1465,7 +1324,6 @@ int Feb_Control_SetTriggerMode(unsigned int trigger_mode, int polarity) { Feb_Control_triggerMode &= (~DAQ_NEXPOSURERS_EXTERNAL_TRIGGER_POLARITY); LOG(logINFO, ("External trigger polarity set to negitive\n")); } - return 1; } @@ -1483,12 +1341,9 @@ int Feb_Control_SetExternalEnableMode(int use_external_enable, int polarity) { (polarity ? "positive" : "negative"))); } else { - Feb_Control_externalEnableMode = - 0; /* changed by Dhanya according to old code &= - (~DAQ_NEXPOSURERS_EXTERNAL_ENABLING);*/ + Feb_Control_externalEnableMode = 0; LOG(logINFO, ("External enabling disabled\n")); } - return 1; } @@ -1497,7 +1352,6 @@ int Feb_Control_SetNExposures(unsigned int n_images) { LOG(logERROR, ("nimages must be greater than zero.%d\n", n_images)); return 0; } - Feb_Control_nimages = n_images; LOG(logDEBUG1, ("Number of images set to %d\n", Feb_Control_nimages)); return 1; @@ -1510,9 +1364,11 @@ int Feb_Control_SetExposureTime(double the_exposure_time_in_sec) { ("Exposure time set to %fs\n", Feb_Control_exposure_time_in_sec)); return 1; } + double Feb_Control_GetExposureTime() { return Feb_Control_exposure_time_in_sec; } + int64_t Feb_Control_GetExposureTime_in_nsec() { return (int64_t)(Feb_Control_exposure_time_in_sec * (1E9)); } @@ -1526,6 +1382,7 @@ int Feb_Control_SetSubFrameExposureTime( (long long int)Feb_Control_subframe_exposure_time_in_10nsec * 10)); return 1; } + int64_t Feb_Control_GetSubFrameExposureTime() { return Feb_Control_subframe_exposure_time_in_10nsec * 10; } @@ -1550,205 +1407,280 @@ double Feb_Control_GetExposurePeriod() { return Feb_Control_exposure_period_in_sec; } -unsigned int Feb_Control_ConvertTimeToRegister(float time_in_sec) { - float n_clk_cycles = - round(time_in_sec / 10e-9); // 200 MHz ctb clk or 100 MHz feb clk - - unsigned int decoded_time; - if (n_clk_cycles > (pow(2, 29) - 1) * pow(10, 7)) { - float max_time = 10e-9 * (pow(2, 28) - 1) * pow(10, 7); - LOG(logERROR, ("time exceeds (%f) maximum exposure time of %f sec.\n", - time_in_sec, max_time)); - LOG(logINFO, ("\t Setting to maximum %f us.\n", max_time)); - decoded_time = 0xffffffff; - } else { - int power_of_ten = 0; - while (n_clk_cycles > pow(2, 29) - 1) { - power_of_ten++; - n_clk_cycles = round(n_clk_cycles / 10.0); - } - decoded_time = (int)(n_clk_cycles) << 3 | (int)(power_of_ten); - } - - return decoded_time; -} - -int Feb_Control_ResetChipCompletely() { - if (!Feb_Control_SetCommandRegister(DAQ_RESET_COMPLETELY) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("could not ResetChipCompletely() with 0x%x.\n", - DAQ_RESET_COMPLETELY)); - return 0; - } - LOG(logINFO, ("Chip reset completely\n")); - return 1; -} - -int Feb_Control_ResetChipPartially() { - if (!Feb_Control_SetCommandRegister(DAQ_RESET_PERIPHERY) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("could not ResetChipPartially with periphery\n")); - return 0; - } - LOG(logINFO, ("Chip reset periphery 0x%x\n", DAQ_RESET_PERIPHERY)); - - if (!Feb_Control_SetCommandRegister(DAQ_RESET_COLUMN_SELECT) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("could not ResetChipPartially with column select\n")); - return 0; - } - LOG(logINFO, ("Chip reset column select 0x%x\n", DAQ_RESET_COLUMN_SELECT)); - - return 1; -} - -void Feb_Control_PrintAcquisitionSetup() { - - time_t rawtime; - time(&rawtime); - struct tm *timeinfo = localtime(&rawtime); - - LOG(logINFO, - ("Starting an exposure: (%s)" - "\t Dynamic range nbits: %d\n" - "\t Trigger mode: 0x%x\n" - "\t Number of exposures: %d\n" - "\t Exsposure time (if used): %f seconds.\n" - "\t Exsposure period (if used): %f seconds.\n\n", - asctime(timeinfo), Feb_Control_GetDynamicRange(), - Feb_Control_triggerMode, Feb_Control_GetNExposures(), - Feb_Control_exposure_time_in_sec, Feb_Control_exposure_period_in_sec)); -} - -int Feb_Control_SendBitModeToBebServer() { - - unsigned int just_bit_mode = - (DAQ_STATIC_BIT_M4 | DAQ_STATIC_BIT_M8) & Feb_Control_staticBits; - unsigned int bit_mode = 16; // default - if (just_bit_mode == DAQ_STATIC_BIT_M4) - bit_mode = 4; - else if (just_bit_mode == DAQ_STATIC_BIT_M8) - bit_mode = 8; - else if (Feb_Control_subFrameMode & - DAQ_NEXPOSURERS_ACTIVATE_AUTO_SUBIMAGING) - bit_mode = 32; - - if (!Beb_SetUpTransferParameters(bit_mode)) { - LOG(logERROR, ("Error: sending bit mode ...\n")); - return 0; - } - - return 1; -} - -int Feb_Control_PrepareForAcquisition() { // return 1; - LOG(logINFO, ("Going to Prepare for Acquisition\n\n\n")); - static unsigned int reg_nums[20]; - static unsigned int reg_vals[20]; - - Feb_Control_PrintAcquisitionSetup(); - - // if (!Reset()||!ResetDataStream()) { - if (Feb_Control_Reset() == STATUS_ERROR) { - LOG(logERROR, ("Trouble reseting daq or data stream...\n")); - return 0; - } - - if (!Feb_Control_SetStaticBits1(Feb_Control_staticBits & - (DAQ_STATIC_BIT_M4 | DAQ_STATIC_BIT_M8))) { - LOG(logERROR, ("Trouble setting static bits ...\n")); - return 0; - } - - if (!Feb_Control_SendBitModeToBebServer()) { - LOG(logERROR, ("Trouble sending static bits to server ...\n")); - return 0; - } - - int ret = 0; - if (Feb_Control_counter_bit) - ret = Feb_Control_ResetChipCompletely(); - else - ret = Feb_Control_ResetChipPartially(); - if (!ret) { - LOG(logERROR, ("Trouble resetting chips ...\n")); - return 0; - } - - reg_nums[0] = DAQ_REG_CTRL; - reg_vals[0] = 0; - reg_nums[1] = DAQ_REG_NEXPOSURES; - reg_vals[1] = Feb_Control_nimages; - reg_nums[2] = DAQ_REG_EXPOSURE_TIMER; - reg_vals[2] = - Feb_Control_ConvertTimeToRegister(Feb_Control_exposure_time_in_sec); - reg_nums[3] = DAQ_REG_EXPOSURE_REPEAT_TIMER; - reg_vals[3] = - Feb_Control_ConvertTimeToRegister(Feb_Control_exposure_period_in_sec); - reg_nums[4] = DAQ_REG_CHIP_CMDS; - reg_vals[4] = (Feb_Control_acquireNReadoutMode | Feb_Control_triggerMode | - Feb_Control_externalEnableMode | Feb_Control_subFrameMode); - reg_nums[5] = DAQ_REG_SUBFRAME_EXPOSURES; - reg_vals[5] = - Feb_Control_subframe_exposure_time_in_10nsec; //(1 means 10ns, 100 means - // 1000ns) - reg_nums[6] = DAQ_REG_SUBFRAME_PERIOD; - reg_vals[6] = Feb_Control_subframe_period_in_10nsec; //(1 means 10ns, 100 - // means 1000ns) - // if - // (!Feb_Interface_WriteRegisters((Module_GetTopLeftAddress(&modules[1])|Module_GetTopRightAddress(&modules[1])),20,reg_nums,reg_vals,0,0)) - // { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteRegisters(Feb_Control_AddressToAll(), 7, - reg_nums, reg_vals, 0, 0)) { - LOG(logERROR, ("Trouble starting acquisition....\n")); - return 0; - } - } - - return 1; -} - -int Feb_Control_StartAcquisition() { - LOG(logINFOBLUE, ("Starting Acquisition\n")); - - static unsigned int reg_nums[20]; - static unsigned int reg_vals[20]; - - for (int i = 0; i < 14; i++) { - reg_nums[i] = DAQ_REG_CTRL; - reg_vals[i] = 0; - } - reg_nums[14] = DAQ_REG_CTRL; - reg_vals[14] = ACQ_CTRL_START; - - if (Feb_Control_activated) { - if (!Feb_Interface_WriteRegisters(Feb_Control_AddressToAll(), 15, - reg_nums, reg_vals, 0, 0)) { - LOG(logERROR, ("Trouble starting acquisition....\n")); - return 0; - } - } - - return 1; -} - -int Feb_Control_StopAcquisition() { return Feb_Control_Reset(); } - -int Feb_Control_SaveAllTrimbitsTo(int value, int top) { - unsigned int chanregs[Feb_Control_trimbit_size]; - for (int i = 0; i < Feb_Control_trimbit_size; i++) - chanregs[i] = value; - return Feb_Control_SetTrimbits(0, chanregs, top); -} - void Feb_Control_Set_Counter_Bit(int value) { Feb_Control_counter_bit = value; } int Feb_Control_Get_Counter_Bit() { return Feb_Control_counter_bit; } +int Feb_Control_SetInterruptSubframe(int val) { + LOG(logINFO, ("Setting Interrupt Subframe to %d\n", val)); + // they need to be written separately because the left and right registers + // have different values for this particular register + uint32_t offset = DAQ_REG_HRDWRE; + uint32_t regVal = 0; + char side[2][10] = {"right", "left"}; + unsigned int addr[2] = {Module_GetRightAddress(&modules[1]), + Module_GetLeftAddress(&modules[1])}; + for (int iloop = 0; iloop < 2; ++iloop) { + // get previous value to keep it + if (!Feb_Interface_ReadRegister(addr[iloop], offset, ®Val)) { + LOG(logERROR, + ("Could not read %s interrupt subframe\n", side[iloop])); + return 0; + } + uint32_t data = ((val == 0) ? (regVal & ~DAQ_REG_HRDWRE_INTRRPT_SF_MSK) + : (regVal | DAQ_REG_HRDWRE_INTRRPT_SF_MSK)); + if (!Feb_Interface_WriteRegister(addr[iloop], offset, data, 0, 0)) { + LOG(logERROR, + ("Could not write 0x%x to %s interrupt subframe addr 0x%x\n", + data, side[iloop], offset)); + return 0; + } + } + return 1; +} + +int Feb_Control_GetInterruptSubframe() { + // they need to be written separately because the left and right registers + // have different values for this particular register + uint32_t offset = DAQ_REG_HRDWRE; + uint32_t regVal = 0; + + char side[2][10] = {"right", "left"}; + unsigned int addr[2] = {Module_GetRightAddress(&modules[1]), + Module_GetLeftAddress(&modules[1])}; + uint32_t value[2] = {0, 0}; + for (int iloop = 0; iloop < 2; ++iloop) { + if (!Feb_Interface_ReadRegister(addr[iloop], offset, ®Val)) { + LOG(logERROR, + ("Could not read back %s interrupt subframe\n", side[iloop])); + return -1; + } + value[iloop] = (regVal & DAQ_REG_HRDWRE_INTRRPT_SF_MSK) >> + DAQ_REG_HRDWRE_INTRRPT_SF_OFST; + } + // inconsistent + if (value[0] != value[1]) { + LOG(logERROR, ("Inconsistent values of interrupt subframe betweeen " + "left %d and right %d\n", + value[0], value[1])); + return -1; + } + return value[0]; +} + +int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right) { + uint32_t offset = DAQ_REG_HRDWRE; + unsigned int addr[2] = {0, 0}; + if (left) { + addr[0] = Module_GetLeftAddress(&modules[1]); + } + if (right) { + addr[1] = Module_GetRightAddress(&modules[1]); + } + char *top_names[] = {TOP_NAMES}; + for (int i = 0; i < 2; ++i) { + if (addr[i] == 0) { + continue; + } + uint32_t value = 0; + if (!Feb_Interface_ReadRegister(addr[i], offset, &value)) { + LOG(logERROR, ("Could not read %s Feb reg to set Top flag\n", + (i == 0 ? "left" : "right"))); + return 0; + } + switch (ind) { + case TOP_HARDWARE: + value &= ~DAQ_REG_HRDWRE_OW_TOP_MSK; + break; + case OW_TOP: + value |= DAQ_REG_HRDWRE_OW_TOP_MSK; + value |= DAQ_REG_HRDWRE_TOP_MSK; + break; + case OW_BOTTOM: + value |= DAQ_REG_HRDWRE_OW_TOP_MSK; + value &= ~DAQ_REG_HRDWRE_TOP_MSK; + break; + default: + LOG(logERROR, ("Unknown top index in Feb: %d\n", ind)); + return 0; + } + if (!Feb_Interface_WriteRegister(addr[i], offset, value, 0, 0)) { + LOG(logERROR, ("Could not set Top flag to %s in %s Feb\n", + top_names[ind], (i == 0 ? "left" : "right"))); + return 0; + } + } + if (left && right) { + LOG(logINFOBLUE, ("%s Top flag to %s Feb\n", + (ind == TOP_HARDWARE ? "Resetting" : "Overwriting"), + top_names[ind])); + } + return 1; +} + +void Feb_Control_SetMasterVariable(int val) { Feb_control_master = val; } + +int Feb_Control_SetMaster(enum MASTERINDEX ind) { + uint32_t offset = DAQ_REG_HRDWRE; + unsigned int addr[2] = {Module_GetLeftAddress(&modules[1]), + Module_GetRightAddress(&modules[1])}; + char *master_names[] = {MASTER_NAMES}; + for (int i = 0; i < 2; ++i) { + uint32_t value = 0; + if (!Feb_Interface_ReadRegister(addr[i], offset, &value)) { + LOG(logERROR, ("Could not read %s Feb reg to set Master flag\n", + (i == 0 ? "left" : "right"))); + return 0; + } + switch (ind) { + case MASTER_HARDWARE: + value &= ~DAQ_REG_HRDWRE_OW_MASTER_MSK; + break; + case OW_MASTER: + value |= DAQ_REG_HRDWRE_OW_MASTER_MSK; + value |= DAQ_REG_HRDWRE_MASTER_MSK; + break; + case OW_SLAVE: + value |= DAQ_REG_HRDWRE_OW_MASTER_MSK; + value &= ~DAQ_REG_HRDWRE_MASTER_MSK; + break; + default: + LOG(logERROR, ("Unknown master index in Feb: %d\n", ind)); + return 0; + } + + if (!Feb_Interface_WriteRegister(addr[i], offset, value, 0, 0)) { + LOG(logERROR, ("Could not set Master flag to %s in %s Feb\n", + master_names[ind], (i == 0 ? "left" : "right"))); + return 0; + } + } + LOG(logINFOBLUE, ("%s Master flag to %s Feb\n", + (ind == MASTER_HARDWARE ? "Resetting" : "Overwriting"), + master_names[ind])); + return 1; +} + +int Feb_Control_SetQuad(int val) { + LOG(logINFO, ("Setting Quad to %d in Feb\n", val)); + // only setting on the right feb if quad + return Feb_Control_SetTop(val == 0 ? TOP_HARDWARE : OW_TOP, 0, 1); +} + +int Feb_Control_SetReadNLines(int value) { + LOG(logINFO, ("Setting Read N Lines to %d\n", value)); + if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), + DAQ_REG_PARTIAL_READOUT, value, 0, 0)) { + LOG(logERROR, ("Could not write %d to read n lines reg\n", value)); + return 0; + } + return 1; +} + +int Feb_Control_GetReadNLines() { + uint32_t regVal = 0; + if (!Feb_Interface_ReadRegister(Feb_Control_AddressToAll(), + DAQ_REG_PARTIAL_READOUT, ®Val)) { + LOG(logERROR, ("Could not read back read n lines reg\n")); + return -1; + } + LOG(logDEBUG1, ("Retval read n lines: %d\n", regVal)); + return regVal; +} + +int Feb_Control_WriteRegister(uint32_t offset, uint32_t data) { + uint32_t actualOffset = offset; + char side[2][10] = {"right", "left"}; + unsigned int addr[2] = {Module_GetRightAddress(&modules[1]), + Module_GetLeftAddress(&modules[1])}; + + int run[2] = {0, 0}; + // both registers + if (offset < 0x100) { + run[0] = 1; + run[1] = 1; + } + // right registers only + else if (offset >= 0x200) { + run[0] = 1; + actualOffset = offset - 0x200; + } + // left registers only + else { + run[1] = 1; + actualOffset = offset - 0x100; + } + + for (int iloop = 0; iloop < 2; ++iloop) { + if (run[iloop]) { + LOG(logINFO, + ("Writing 0x%x to %s 0x%x\n", data, side[iloop], actualOffset)); + if (!Feb_Interface_WriteRegister(addr[iloop], actualOffset, data, 0, + 0)) { + LOG(logERROR, ("Could not write 0x%x to %s addr 0x%x\n", data, + side[iloop], actualOffset)); + return 0; + } + } + } + + return 1; +} + +int Feb_Control_ReadRegister(uint32_t offset, uint32_t *retval) { + uint32_t actualOffset = offset; + char side[2][10] = {"right", "left"}; + unsigned int addr[2] = {Module_GetRightAddress(&modules[1]), + Module_GetLeftAddress(&modules[1])}; + uint32_t value[2] = {0, 0}; + int run[2] = {0, 0}; + // both registers + if (offset < 0x100) { + run[0] = 1; + run[1] = 1; + } + // right registers only + else if (offset >= 0x200) { + run[0] = 1; + actualOffset = offset - 0x200; + } + // left registers only + else { + run[1] = 1; + actualOffset = offset - 0x100; + } + + for (int iloop = 0; iloop < 2; ++iloop) { + if (run[iloop]) { + if (!Feb_Interface_ReadRegister(addr[iloop], actualOffset, + &value[iloop])) { + LOG(logERROR, ("Could not read from %s addr 0x%x\n", + side[iloop], actualOffset)); + return 0; + } + LOG(logINFO, ("Read 0x%x from %s 0x%x\n", value[iloop], side[iloop], + actualOffset)); + *retval = value[iloop]; + // if not the other (left, not right OR right, not left), return the + // value + if (!run[iloop ? 0 : 1]) { + return 1; + } + } + } + // Inconsistent values + if (value[0] != value[1]) { + LOG(logERROR, + ("Inconsistent values read from left 0x%x and right 0x%x\n", + value[0], value[1])); + return 0; + } + return 1; +} + +// pulsing int Feb_Control_Pulse_Pixel(int npulses, int x, int y) { // this function is not designed for speed - int pulse_multiple = 0; // has to be 0 or 1 if (x < 0) { @@ -1792,7 +1724,6 @@ int Feb_Control_Pulse_Pixel(int npulses, int x, int y) { Feb_Control_SendTokenIn(); Feb_Control_ClockRowClock(nrowclocks); Feb_Control_PulsePixelNMove(npulses, 0, 0); - return 1; } @@ -1811,11 +1742,9 @@ int Feb_Control_PulsePixelNMove(int npulses, int inc_x_pos, int inc_y_pos) { return 0; } } - return 1; } -/**new*/ int Feb_Control_Shift32InSerialIn(unsigned int value_to_shift_in) { if (Feb_Control_activated) { if (!Feb_Control_SetCommandRegister(DAQ_SERIALIN_SHIFT_IN_32) || @@ -1846,7 +1775,6 @@ int Feb_Control_ClockRowClock(unsigned int ntimes) { ntimes)); ntimes = 1023; } - if (Feb_Control_activated) { if (!Feb_Control_SetCommandRegister(DAQ_CLK_ROW_CLK_NTIMES) || !Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), @@ -1857,13 +1785,11 @@ int Feb_Control_ClockRowClock(unsigned int ntimes) { return 0; } } - return 1; } int Feb_Control_PulseChip(int npulses) { int on = 1; - if (npulses == -1) { on = 0; LOG(logINFO, ("\nResetting to normal mode\n")); @@ -1889,7 +1815,6 @@ int Feb_Control_PulseChip(int npulses) { Feb_Control_SetExternalEnableMode(on, 1); Feb_Control_counter_bit = (on ? 0 : 1); LOG(logINFO, ("Feb_Control_counter_bit:%d\n", Feb_Control_counter_bit)); - if (on) { LOG(logINFO, ("Pulse chip success\n\n")); } else { @@ -1898,15 +1823,16 @@ int Feb_Control_PulseChip(int npulses) { return 1; } +// rate correction int64_t Feb_Control_Get_RateTable_Tau_in_nsec() { return Feb_Control_RateTable_Tau_in_nsec; } + int64_t Feb_Control_Get_RateTable_Period_in_nsec() { return Feb_Control_RateTable_Period_in_nsec; } int Feb_Control_SetRateCorrectionTau(int64_t tau_in_Nsec) { - // period = exptime if 16bit, period = subexptime if 32 bit int dr = Feb_Control_GetDynamicRange(); double period_in_sec = @@ -1936,7 +1862,6 @@ int Feb_Control_SetRateCorrectionTau(int64_t tau_in_Nsec) { LOG(logINFO, ("Changing Rate Correction Table tau:%0.8f sec, period:%f sec", tau_in_sec, period_in_sec)); - LOG(logINFO, ("\tCalculating table for tau of %lld ns.\n", tau_in_Nsec)); for (int i = 0; i < np; i++) { Feb_Control_rate_meas[i] = i * exp(-i / period_in_sec * tau_in_sec); @@ -2070,37 +1995,17 @@ int Feb_Control_SetRateCorrectionTable(unsigned int *table) { } LOG(logINFO, ("daq reset completely\n")); - if (Module_TopAddressIsValid(&modules[1])) { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteMemoryInLoops( - Module_GetTopLeftAddress( - &modules[Feb_Control_current_index]), - 1, 0, 1024, Feb_Control_rate_correction_table) || - !Feb_Interface_WriteMemoryInLoops( - Module_GetTopRightAddress( - &modules[Feb_Control_current_index]), - 1, 0, 1024, Feb_Control_rate_correction_table) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("could not write to memory (top) " - "::Feb_Control_SetRateCorrectionTable\n")); - return 0; - } - } - } else { - if (Feb_Control_activated) { - if (!Feb_Interface_WriteMemoryInLoops( - Module_GetBottomLeftAddress( - &modules[Feb_Control_current_index]), - 1, 0, 1024, Feb_Control_rate_correction_table) || - !Feb_Interface_WriteMemoryInLoops( - Module_GetBottomRightAddress( - &modules[Feb_Control_current_index]), - 1, 0, 1024, Feb_Control_rate_correction_table) || - (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { - LOG(logERROR, ("could not write to memory (bottom) " - "::Feb_Control_SetRateCorrectionTable\n")); - return 0; - } + if (Feb_Control_activated) { + if (!Feb_Interface_WriteMemoryInLoops( + Module_GetLeftAddress(&modules[1]), 1, 0, 1024, + Feb_Control_rate_correction_table) || + !Feb_Interface_WriteMemoryInLoops( + Module_GetRightAddress(&modules[1]), 1, 0, 1024, + Feb_Control_rate_correction_table) || + (Feb_Control_StartDAQOnlyNWaitForFinish(5000) != STATUS_IDLE)) { + LOG(logERROR, ("could not write to memory (top) " + "::Feb_Control_SetRateCorrectionTable\n")); + return 0; } } return 1; @@ -2142,17 +2047,14 @@ int Feb_Control_PrintCorrectedValues() { return 1; } +// adcs // So if software says now 40.00 you neeed to convert to mdegrees 40000(call it // A1) and then A1/65536/0.00198421639-273.15 - int Feb_Control_GetLeftFPGATemp() { unsigned int temperature = 0; - if (Module_TopAddressIsValid(&modules[1])) - Feb_Interface_ReadRegister(Module_GetTopLeftAddress(&modules[1]), - FEB_REG_STATUS, &temperature); - else - Feb_Interface_ReadRegister(Module_GetBottomLeftAddress(&modules[1]), - FEB_REG_STATUS, &temperature); + Feb_Interface_ReadRegister(Module_GetLeftAddress(&modules[1]), + FEB_REG_STATUS, &temperature); + temperature = temperature >> 16; temperature = ((((float)(temperature) / 65536.0f) / 0.00198421639f) - 273.15f) * @@ -2163,12 +2065,8 @@ int Feb_Control_GetLeftFPGATemp() { int Feb_Control_GetRightFPGATemp() { unsigned int temperature = 0; - if (Module_TopAddressIsValid(&modules[1])) - Feb_Interface_ReadRegister(Module_GetTopRightAddress(&modules[1]), - FEB_REG_STATUS, &temperature); - else - Feb_Interface_ReadRegister(Module_GetBottomRightAddress(&modules[1]), - FEB_REG_STATUS, &temperature); + Feb_Interface_ReadRegister(Module_GetRightAddress(&modules[1]), + FEB_REG_STATUS, &temperature); temperature = temperature >> 16; temperature = ((((float)(temperature) / 65536.0f) / 0.00198421639f) - 273.15f) * @@ -2178,357 +2076,15 @@ int Feb_Control_GetRightFPGATemp() { } int64_t Feb_Control_GetMeasuredPeriod() { - unsigned int sub_num = (Module_TopAddressIsValid(&modules[1])) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - unsigned int value = 0; - Feb_Interface_ReadRegister(sub_num, MEAS_PERIOD_REG, &value); + Feb_Interface_ReadRegister(Module_GetLeftAddress(&modules[1]), + MEAS_PERIOD_REG, &value); return (int64_t)value * 10; } int64_t Feb_Control_GetSubMeasuredPeriod() { - unsigned int sub_num = (Module_TopAddressIsValid(&modules[1])) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - unsigned int value = 0; - Feb_Interface_ReadRegister(sub_num, MEAS_SUBPERIOD_REG, &value); + Feb_Interface_ReadRegister(Module_GetLeftAddress(&modules[1]), + MEAS_SUBPERIOD_REG, &value); return (int64_t)value * 10; } - -int Feb_Control_SoftwareTrigger() { - unsigned int orig_value = 0; - Feb_Interface_ReadRegister(Feb_Control_AddressToAll(), DAQ_REG_CHIP_CMDS, - &orig_value); - - unsigned int cmd = orig_value | DAQ_REG_CHIP_CMDS_INT_TRIGGER; - - if (Feb_Control_activated) { - // set trigger bit - LOG(logDEBUG1, ("Setting Trigger, Register:0x%x\n", cmd)); - if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_CHIP_CMDS, cmd, 0, 0)) { - LOG(logERROR, ("Could not give software trigger\n")); - return 0; - } - // unset trigger bit - LOG(logDEBUG1, ("Unsetting Trigger, Register:0x%x\n", orig_value)); - if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_CHIP_CMDS, orig_value, 0, 0)) { - LOG(logERROR, ("Could not give software trigger\n")); - return 0; - } - LOG(logINFO, ("Software Internal Trigger Sent!\n")); - } - - return 1; -} - -int Feb_Control_SetInterruptSubframe(int val) { - LOG(logINFO, ("Setting Interrupt Subframe to %d\n", val)); - - // they need to be written separately because the left and right registers - // have different values for this particular register - uint32_t offset = DAQ_REG_HRDWRE; - uint32_t regVal = 0; - char side[2][10] = {"right", "left"}; - char isTop[10]; - strcpy(isTop, Module_TopAddressIsValid(&modules[1]) ? "top" : "bottom"); - unsigned int addr[2]; - addr[0] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopRightAddress(&modules[1]) - : Module_GetBottomRightAddress(&modules[1]); - addr[1] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - - for (int iloop = 0; iloop < 2; ++iloop) { - // get previous value to keep it - if (!Feb_Interface_ReadRegister(addr[iloop], offset, ®Val)) { - LOG(logERROR, ("Could not read %s %s interrupt subframe\n", isTop, - side[iloop])); - return 0; - } - uint32_t data = ((val == 0) ? (regVal & ~DAQ_REG_HRDWRE_INTRRPT_SF_MSK) - : (regVal | DAQ_REG_HRDWRE_INTRRPT_SF_MSK)); - if (!Feb_Interface_WriteRegister(addr[iloop], offset, data, 0, 0)) { - LOG(logERROR, - ("Could not write 0x%x to %s %s interrupt subframe addr 0x%x\n", - data, isTop, side[iloop], offset)); - return 0; - } - } - return 1; -} - -int Feb_Control_GetInterruptSubframe() { - // they need to be written separately because the left and right registers - // have different values for this particular register - uint32_t offset = DAQ_REG_HRDWRE; - uint32_t regVal = 0; - - char side[2][10] = {"right", "left"}; - char isTop[10]; - strcpy(isTop, Module_TopAddressIsValid(&modules[1]) ? "top" : "bottom"); - unsigned int addr[2]; - addr[0] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopRightAddress(&modules[1]) - : Module_GetBottomRightAddress(&modules[1]); - addr[1] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - uint32_t value[2] = {0, 0}; - - for (int iloop = 0; iloop < 2; ++iloop) { - if (!Feb_Interface_ReadRegister(addr[iloop], offset, ®Val)) { - LOG(logERROR, ("Could not read back %s %s interrupt subframe\n", - isTop, side[iloop])); - return -1; - } - value[iloop] = (regVal & DAQ_REG_HRDWRE_INTRRPT_SF_MSK) >> - DAQ_REG_HRDWRE_INTRRPT_SF_OFST; - } - - // inconsistent - if (value[0] != value[1]) { - LOG(logERROR, ("Inconsistent values of interrupt subframe betweeen " - "left %d and right %d\n", - value[0], value[1])); - return -1; - } - return value[0]; -} - -int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right) { - uint32_t offset = DAQ_REG_HRDWRE; - unsigned int addr[2] = {0, 0}; - if (left) { - addr[0] = Module_GetTopLeftAddress(&modules[1]); - } - if (right) { - addr[1] = Module_GetTopRightAddress(&modules[1]); - } - char *top_names[] = {TOP_NAMES}; - for (int i = 0; i < 2; ++i) { - if (addr[i] == 0) { - continue; - } - uint32_t value = 0; - if (!Feb_Interface_ReadRegister(addr[i], offset, &value)) { - LOG(logERROR, ("Could not read %s Feb reg to set Top flag\n", - (i == 0 ? "left" : "right"))); - return 0; - } - switch (ind) { - case TOP_HARDWARE: - value &= ~DAQ_REG_HRDWRE_OW_TOP_MSK; - break; - case OW_TOP: - value |= DAQ_REG_HRDWRE_OW_TOP_MSK; - value |= DAQ_REG_HRDWRE_TOP_MSK; - break; - case OW_BOTTOM: - value |= DAQ_REG_HRDWRE_OW_TOP_MSK; - value &= ~DAQ_REG_HRDWRE_TOP_MSK; - break; - default: - LOG(logERROR, ("Unknown top index in Feb: %d\n", ind)); - return 0; - } - if (!Feb_Interface_WriteRegister(addr[i], offset, value, 0, 0)) { - LOG(logERROR, ("Could not set Top flag to %s in %s Feb\n", - top_names[ind], (i == 0 ? "left" : "right"))); - return 0; - } - } - if (left && right) { - LOG(logINFOBLUE, ("%s Top flag to %s Feb\n", - (ind == TOP_HARDWARE ? "Resetting" : "Overwriting"), - top_names[ind])); - } - return 1; -} - -void Feb_Control_SetMasterVariable(int val) { Feb_control_master = val; } - -int Feb_Control_SetMaster(enum MASTERINDEX ind) { - uint32_t offset = DAQ_REG_HRDWRE; - unsigned int addr[2] = {0, 0}; - addr[0] = Module_GetTopLeftAddress(&modules[1]); - addr[1] = Module_GetTopRightAddress(&modules[1]); - char *master_names[] = {MASTER_NAMES}; - for (int i = 0; i < 2; ++i) { - uint32_t value = 0; - if (!Feb_Interface_ReadRegister(addr[i], offset, &value)) { - LOG(logERROR, ("Could not read %s Feb reg to set Master flag\n", - (i == 0 ? "left" : "right"))); - return 0; - } - switch (ind) { - case MASTER_HARDWARE: - value &= ~DAQ_REG_HRDWRE_OW_MASTER_MSK; - break; - case OW_MASTER: - value |= DAQ_REG_HRDWRE_OW_MASTER_MSK; - value |= DAQ_REG_HRDWRE_MASTER_MSK; - break; - case OW_SLAVE: - value |= DAQ_REG_HRDWRE_OW_MASTER_MSK; - value &= ~DAQ_REG_HRDWRE_MASTER_MSK; - break; - default: - LOG(logERROR, ("Unknown master index in Feb: %d\n", ind)); - return 0; - } - - if (!Feb_Interface_WriteRegister(addr[i], offset, value, 0, 0)) { - LOG(logERROR, ("Could not set Master flag to %s in %s Feb\n", - master_names[ind], (i == 0 ? "left" : "right"))); - return 0; - } - } - LOG(logINFOBLUE, ("%s Master flag to %s Feb\n", - (ind == MASTER_HARDWARE ? "Resetting" : "Overwriting"), - master_names[ind])); - return 1; -} - -int Feb_Control_SetQuad(int val) { - // no bottom for quad - if (!Module_TopAddressIsValid(&modules[1])) { - return 1; - } - LOG(logINFO, ("Setting Quad to %d in Feb\n", val)); - // only setting on the right feb if quad - return Feb_Control_SetTop(val == 0 ? TOP_HARDWARE : OW_TOP, 0, 1); -} - -int Feb_Control_SetReadNLines(int value) { - LOG(logINFO, ("Setting Read N Lines to %d\n", value)); - if (!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(), - DAQ_REG_PARTIAL_READOUT, value, 0, 0)) { - LOG(logERROR, ("Could not write %d to read n lines reg\n", value)); - return 0; - } - - return 1; -} - -int Feb_Control_GetReadNLines() { - uint32_t regVal = 0; - if (!Feb_Interface_ReadRegister(Feb_Control_AddressToAll(), - DAQ_REG_PARTIAL_READOUT, ®Val)) { - LOG(logERROR, ("Could not read back read n lines reg\n")); - return -1; - } - LOG(logDEBUG1, ("Retval read n lines: %d\n", regVal)); - return regVal; -} - -int Feb_Control_WriteRegister(uint32_t offset, uint32_t data) { - uint32_t actualOffset = offset; - char side[2][10] = {"right", "left"}; - char isTop[10]; - strcpy(isTop, Module_TopAddressIsValid(&modules[1]) ? "top" : "bottom"); - unsigned int addr[2]; - addr[0] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopRightAddress(&modules[1]) - : Module_GetBottomRightAddress(&modules[1]); - addr[1] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - - int run[2] = {0, 0}; - // both registers - if (offset < 0x100) { - run[0] = 1; - run[1] = 1; - } - // right registers only - else if (offset >= 0x200) { - run[0] = 1; - actualOffset = offset - 0x200; - } - // left registers only - else { - run[1] = 1; - actualOffset = offset - 0x100; - } - - for (int iloop = 0; iloop < 2; ++iloop) { - if (run[iloop]) { - LOG(logINFO, ("Writing 0x%x to %s %s 0x%x\n", data, isTop, - side[iloop], actualOffset)); - if (!Feb_Interface_WriteRegister(addr[iloop], actualOffset, data, 0, - 0)) { - LOG(logERROR, ("Could not write 0x%x to %s %s addr 0x%x\n", - data, isTop, side[iloop], actualOffset)); - return 0; - } - } - } - - return 1; -} - -int Feb_Control_ReadRegister(uint32_t offset, uint32_t *retval) { - uint32_t actualOffset = offset; - char side[2][10] = {"right", "left"}; - char isTop[10]; - strcpy(isTop, Module_TopAddressIsValid(&modules[1]) ? "top" : "bottom"); - unsigned int addr[2]; - addr[0] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopRightAddress(&modules[1]) - : Module_GetBottomRightAddress(&modules[1]); - addr[1] = Module_TopAddressIsValid(&modules[1]) - ? Module_GetTopLeftAddress(&modules[1]) - : Module_GetBottomLeftAddress(&modules[1]); - - uint32_t value[2] = {0, 0}; - - int run[2] = {0, 0}; - // both registers - if (offset < 0x100) { - run[0] = 1; - run[1] = 1; - } - // right registers only - else if (offset >= 0x200) { - run[0] = 1; - actualOffset = offset - 0x200; - } - // left registers only - else { - run[1] = 1; - actualOffset = offset - 0x100; - } - - for (int iloop = 0; iloop < 2; ++iloop) { - if (run[iloop]) { - if (!Feb_Interface_ReadRegister(addr[iloop], actualOffset, - &value[iloop])) { - LOG(logERROR, ("Could not read from %s %s addr 0x%x\n", isTop, - side[iloop], actualOffset)); - return 0; - } - LOG(logINFO, ("Read 0x%x from %s %s 0x%x\n", value[iloop], isTop, - side[iloop], actualOffset)); - *retval = value[iloop]; - // if not the other (left, not right OR right, not left), return the - // value - if (!run[iloop ? 0 : 1]) { - return 1; - } - } - } - - // Inconsistent values - if (value[0] != value[1]) { - LOG(logERROR, - ("Inconsistent values read from left 0x%x and right 0x%x\n", - value[0], value[1])); - return 0; - } - return 1; -} diff --git a/slsDetectorServers/eigerDetectorServer/FebControl.h b/slsDetectorServers/eigerDetectorServer/FebControl.h index bd38c4476..dbd946fd2 100644 --- a/slsDetectorServers/eigerDetectorServer/FebControl.h +++ b/slsDetectorServers/eigerDetectorServer/FebControl.h @@ -11,10 +11,8 @@ struct Module { float high_voltage; int *dac; }; - -void Module_Module(struct Module *mod, unsigned int number, - unsigned int address_top); -unsigned int Module_GetModuleNumber(struct Module *mod); +// module struct +void Module_Module(struct Module *mod, unsigned int number); unsigned int Module_GetBaseAddress(struct Module *mod); unsigned int Module_GetLeftAddress(struct Module *mod); unsigned int Module_GetRightAddress(struct Module *mod); @@ -25,72 +23,76 @@ float Module_SetHighVoltage(struct Module *mod, float value); float Module_GetHighVoltage(struct Module *mod); int Module_SetDACValue(struct Module *mod, unsigned int i, int value); int Module_GetDACValue(struct Module *mod, unsigned int i); - +// setup void Feb_Control_activate(int activate); int Feb_Control_GetModuleNumber(); -void Feb_Control_PrintModuleList(); -int Feb_Control_GetModuleIndex(unsigned int module_number, - unsigned int *module_index); -int Feb_Control_CheckModuleAddresses(struct Module *m); -int Feb_Control_AddModule(unsigned int module_number, unsigned int top_address); -int Feb_Control_GetDACNumber(char *s, unsigned int *n); -int Feb_Control_SendDACValue(unsigned int dst_num, unsigned int ch, - unsigned int *value); -int Feb_Control_VoltageToDAC(float value, unsigned int *digital, - unsigned int nsteps, float vmin, float vmax); -float Feb_Control_DACToVoltage(unsigned int digital, unsigned int nsteps, - float vmin, float vmax); -int Feb_Control_SendIDelays(unsigned int dst_num, int chip_lr, - unsigned int channels, unsigned int ndelay_units); -int Feb_Control_SetStaticBits(); -int Feb_Control_SetStaticBits1(unsigned int the_static_bits); -int Feb_Control_SendBitModeToBebServer(); -unsigned int Feb_Control_ConvertTimeToRegister(float time_in_sec); -unsigned int Feb_Control_AddressToAll(); -int Feb_Control_SetCommandRegister(unsigned int cmd); -int Feb_Control_GetDAQStatusRegister(unsigned int dst_address, - unsigned int *ret_status); -int Feb_Control_StartDAQOnlyNWaitForFinish(int sleep_time_us); -int Feb_Control_ResetChipCompletely(); -int Feb_Control_ResetChipPartially(); void Feb_Control_FebControl(); int Feb_Control_Init(int master, int normal, int module_num); int Feb_Control_OpenSerialCommunication(); void Feb_Control_CloseSerialCommunication(); +void Feb_Control_PrintModuleList(); +void Feb_Control_AddModule(unsigned int module_number); int Feb_Control_CheckSetup(); -unsigned int Feb_Control_GetNModules(); -unsigned int Feb_Control_GetNHalfModules(); - +unsigned int Feb_Control_AddressToAll(); +int Feb_Control_SetCommandRegister(unsigned int cmd); +int Feb_Control_SetStaticBits(); +int Feb_Control_SetStaticBits1(unsigned int the_static_bits); +int Feb_Control_SetInTestModeVariable(int on); +int Feb_Control_GetTestModeVariable(); +// idelays +int Feb_Control_SetIDelays(unsigned int ndelay_units); +int Feb_Control_SetIDelays1(unsigned int chip_pos, unsigned int ndelay_units); +int Feb_Control_SendIDelays(unsigned int dst_num, int chip_lr, + unsigned int channels, unsigned int ndelay_units); +// high voltage int Feb_Control_SetHighVoltage(int value); int Feb_Control_GetHighVoltage(int *value); - int Feb_Control_SendHighVoltage(int dacvalue); int Feb_Control_ReceiveHighVoltage(unsigned int *value); - -int Feb_Control_SetIDelays(unsigned int module_num, unsigned int ndelay_units); -int Feb_Control_SetIDelays1(unsigned int module_num, unsigned int chip_pos, - unsigned int ndelay_units); - -int Feb_Control_DecodeDACString(char *dac_str, unsigned int *module_index, - int *top, int *bottom, unsigned int *dac_ch); +// dacs +int Feb_Control_VoltageToDAC(float value, unsigned int *digital, + unsigned int nsteps, float vmin, float vmax); +float Feb_Control_DACToVoltage(unsigned int digital, unsigned int nsteps, + float vmin, float vmax); +int Feb_Control_DecodeDACString(char *dac_str, unsigned int *dac_ch); int Feb_Control_SetDAC(char *s, int value, int is_a_voltage_mv); int Feb_Control_GetDAC(char *s, int *ret_value, int voltage_mv); int Feb_Control_GetDACName(unsigned int dac_num, char *s); +int Feb_Control_GetDACNumber(char *s, unsigned int *n); +int Feb_Control_SendDACValue(unsigned int dst_num, unsigned int ch, + unsigned int *value); -int Feb_Control_SetTrimbits(unsigned int module_num, unsigned int *trimbits, - int top); -unsigned int *Feb_Control_GetTrimbits(); +// trimbits +int Feb_Control_SetTrimbits(unsigned int *trimbits, int top); int Feb_Control_SaveAllTrimbitsTo(int value, int top); -int Feb_Control_Reset(); -int Feb_Control_PrepareForAcquisition(); +unsigned int *Feb_Control_GetTrimbits(); -int Feb_Control_StartAcquisition(); -int Feb_Control_StopAcquisition(); +// acquisition int Feb_Control_AcquisitionInProgress(); int Feb_Control_AcquisitionStartedBit(); -int Feb_Control_WaitForFinishedFlag(int sleep_time_us); int Feb_Control_WaitForStartedFlag(int sleep_time_us, int prev_flag); +int Feb_Control_WaitForFinishedFlag(int sleep_time_us); +int Feb_Control_GetDAQStatusRegister(unsigned int dst_address, + unsigned int *ret_status); +int Feb_Control_StartDAQOnlyNWaitForFinish(int sleep_time_us); +int Feb_Control_Reset(); +int Feb_Control_ResetChipCompletely(); +int Feb_Control_ResetChipPartially(); +int Feb_Control_SendBitModeToBebServer(); +unsigned int Feb_Control_ConvertTimeToRegister(float time_in_sec); +int Feb_Control_PrepareForAcquisition(); void Feb_Control_PrintAcquisitionSetup(); +int Feb_Control_StartAcquisition(); +int Feb_Control_StopAcquisition(); +int Feb_Control_SoftwareTrigger(); + +// parameters +int Feb_Control_SetDynamicRange(unsigned int four_eight_sixteen_or_thirtytwo); +unsigned int Feb_Control_GetDynamicRange(); +int Feb_Control_SetReadoutSpeed(unsigned int readout_speed); +int Feb_Control_SetReadoutMode(unsigned int readout_mode); +int Feb_Control_SetTriggerMode(unsigned int trigger_mode); +int Feb_Control_SetExternalEnableMode(int use_external_enable, int polarity); int Feb_Control_SetNExposures(unsigned int n_images); unsigned int Feb_Control_GetNExposures(); int Feb_Control_SetExposureTime(double the_exposure_time_in_sec); @@ -103,25 +105,20 @@ int Feb_Control_SetSubFramePeriod(int64_t the_subframe_period_in_10nsec); int64_t Feb_Control_GetSubFramePeriod(); int Feb_Control_SetExposurePeriod(double the_exposure_period_in_sec); double Feb_Control_GetExposurePeriod(); -int Feb_Control_SetDynamicRange(unsigned int four_eight_sixteen_or_thirtytwo); -unsigned int Feb_Control_GetDynamicRange(); -int Feb_Control_SetReadoutSpeed( - unsigned int readout_speed); // 0 was default, 0->full,1->half,2->quarter or - // 3->super_slow -int Feb_Control_SetReadoutMode( - unsigned int readout_mode); /// 0 was - /// default,0->parallel,1->non-parallel,2-> - /// safe_mode -int Feb_Control_SetTriggerMode(unsigned int trigger_mode, - int polarity); // 0 and 1 was default, -int Feb_Control_SetExternalEnableMode(int use_external_enable, - int polarity); // 0 and 1 was default, - -int Feb_Control_SetInTestModeVariable(int on); -int Feb_Control_GetTestModeVariable(); - void Feb_Control_Set_Counter_Bit(int value); int Feb_Control_Get_Counter_Bit(); +int Feb_Control_SetInterruptSubframe(int val); +int Feb_Control_GetInterruptSubframe(); +int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right); +void Feb_Control_SetMasterVariable(int val); +int Feb_Control_SetMaster(enum MASTERINDEX ind); +int Feb_Control_SetQuad(int val); +int Feb_Control_SetReadNLines(int value); +int Feb_Control_GetReadNLines(); +int Feb_Control_WriteRegister(uint32_t offset, uint32_t data); +int Feb_Control_ReadRegister(uint32_t offset, uint32_t *retval); + +// pulsing int Feb_Control_Pulse_Pixel(int npulses, int x, int y); int Feb_Control_PulsePixelNMove(int npulses, int inc_x_pos, int inc_y_pos); int Feb_Control_Shift32InSerialIn(unsigned int value_to_shift_in); @@ -129,6 +126,7 @@ int Feb_Control_SendTokenIn(); int Feb_Control_ClockRowClock(unsigned int ntimes); int Feb_Control_PulseChip(int npulses); +// rate correction int64_t Feb_Control_Get_RateTable_Tau_in_nsec(); int64_t Feb_Control_Get_RateTable_Period_in_nsec(); int Feb_Control_SetRateCorrectionTau(int64_t tau_in_Nsec); @@ -137,22 +135,8 @@ int Feb_Control_GetRateCorrectionVariable(); void Feb_Control_SetRateCorrectionVariable(int activate_rate_correction); int Feb_Control_PrintCorrectedValues(); +// adcs int Feb_Control_GetLeftFPGATemp(); int Feb_Control_GetRightFPGATemp(); - int64_t Feb_Control_GetMeasuredPeriod(); int64_t Feb_Control_GetSubMeasuredPeriod(); - -int Feb_Control_SoftwareTrigger(); -int Feb_Control_SetInterruptSubframe(int val); -int Feb_Control_GetInterruptSubframe(); - -int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right); -void Feb_Control_SetMasterVariable(int val); -int Feb_Control_SetMaster(enum MASTERINDEX ind); -int Feb_Control_SetQuad(int val); -int Feb_Control_SetReadNLines(int value); -int Feb_Control_GetReadNLines(); - -int Feb_Control_WriteRegister(uint32_t offset, uint32_t data); -int Feb_Control_ReadRegister(uint32_t offset, uint32_t *retval); diff --git a/slsDetectorServers/eigerDetectorServer/FebInterface.c b/slsDetectorServers/eigerDetectorServer/FebInterface.c index d39ff7c04..20f5cd4e1 100644 --- a/slsDetectorServers/eigerDetectorServer/FebInterface.c +++ b/slsDetectorServers/eigerDetectorServer/FebInterface.c @@ -41,13 +41,13 @@ void Feb_Interface_FebInterface() { ll, XPAR_PLB_LL_FIFO_AURORA_DUAL_CTRL_FEB_RIGHT_BASEADDR); } -void Feb_Interface_SendCompleteList(unsigned int n, unsigned int *list) { +void Feb_Interface_SetAddress(unsigned int leftAddr, unsigned int rightAddr) { if (Feb_Interface_feb_numb) free(Feb_Interface_feb_numb); - Feb_Interface_nfebs = n; - Feb_Interface_feb_numb = malloc(n * sizeof(unsigned int)); - for (unsigned int i = 0; i < n; i++) - Feb_Interface_feb_numb[i] = list[i]; + Feb_Interface_nfebs = 2; + Feb_Interface_feb_numb = malloc(2 * sizeof(unsigned int)); + Feb_Interface_feb_numb[0] = leftAddr; + Feb_Interface_feb_numb[1] = rightAddr; } int Feb_Interface_WriteTo(unsigned int ch) { @@ -104,7 +104,9 @@ int Feb_Interface_SetByteOrder() { for (unsigned int i = 0; i < Feb_Interface_nfebs; i++) dst = (dst | Feb_Interface_feb_numb[i]); int passed = Feb_Interface_WriteTo(dst); - + if (!passed) { + LOG(logERROR, ("Could not set byte order in Feb Interface\n")); + } return passed; } diff --git a/slsDetectorServers/eigerDetectorServer/FebInterface.h b/slsDetectorServers/eigerDetectorServer/FebInterface.h index 64216023d..d80785695 100644 --- a/slsDetectorServers/eigerDetectorServer/FebInterface.h +++ b/slsDetectorServers/eigerDetectorServer/FebInterface.h @@ -3,7 +3,7 @@ int Feb_Interface_WriteTo(unsigned int ch); int Feb_Interface_ReadFrom(unsigned int ch, unsigned int ntrys); void Feb_Interface_FebInterface(); -void Feb_Interface_SendCompleteList(unsigned int n, unsigned int *list); +void Feb_Interface_SetAddress(unsigned int n, unsigned int *list); int Feb_Interface_SetByteOrder(); int Feb_Interface_ReadRegister(unsigned int sub_num, unsigned int reg_num, unsigned int *value_read); diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index 5733743c0..37a8131d5 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -333,11 +333,24 @@ void initControlServer() { Feb_Interface_FebInterface(); Feb_Control_FebControl(); // same addresses for top and bottom - Feb_Control_Init(master, normal, getDetectorNumber()); + if (!Feb_Control_Init(master, normal, getDetectorNumber()) { + initError = FAIL; + sprintf(initErrorMessage, "Could not intitalize feb control\n"); + LOG(logERROR, (initErrorMessage)); + initCheckDone = 1; + return; + } // master of 9M, check high voltage serial communication to blackfin if (master && !normal) { - if (Feb_Control_OpenSerialCommunication()) - ; // Feb_Control_CloseSerialCommunication(); + if (!Feb_Control_OpenSerialCommunication()) { + initError = FAIL; + sprintf( + initErrorMessage, + "Could not intitalize feb control serial communication\n"); + LOG(logERROR, (initErrorMessage)); + initCheckDone = 1; + return; + } } LOG(logDEBUG1, ("Control server: FEB Initialization done\n")); Beb_SetTopVariable(top); @@ -695,7 +708,12 @@ void setupDetector() { #endif setHighVoltage(DEFAULT_HIGH_VOLTAGE); #ifndef VIRTUAL - Feb_Control_CheckSetup(); + if (!Feb_Control_CheckSetup()) { + initError = FAIL; + sprintf(initErrorMessage, "Could not pass feb control setup checks\n"); + LOG(logERROR, (initErrorMessage)); + return; + } #endif // force top or master if in config file if (readConfigFile() == FAIL) { @@ -1049,7 +1067,7 @@ int setModule(sls_detector_module myMod, char *mess) { } // set trimbits - if (!Feb_Control_SetTrimbits(Feb_Control_GetModuleNumber(), tt, top)) { + if (!Feb_Control_SetTrimbits(tt, top)) { sprintf(mess, "Could not set module. Could not set trimbits\n"); LOG(logERROR, (mess)); setSettings(UNDEFINED); @@ -1355,7 +1373,7 @@ void setTiming(enum timingMode arg) { } LOG(logDEBUG1, ("Setting Triggering Mode: %d\n", (int)ret)); #ifndef VIRTUAL - if (Feb_Control_SetTriggerMode(ret, 1)) + if (Feb_Control_SetTriggerMode(ret)) #endif eiger_triggermode = ret; } @@ -1476,6 +1494,11 @@ int setQuad(int value) { if (value < 0) { return OK; } + // only top can be set to quad + if (!top & value > 0) { + LOG(logERROR, ("Only a top can be set to quad\n")); + return FAIL; + } #ifndef VIRTUAL if (Beb_SetQuad(value) == FAIL) { return FAIL; @@ -1580,7 +1603,7 @@ int setIODelay(int val) { if (val != -1) { LOG(logDEBUG1, ("Setting IO Delay: %d\n", val)); #ifndef VIRTUAL - if (Feb_Control_SetIDelays(Feb_Control_GetModuleNumber(), val)) + if (Feb_Control_SetIDelays(val)) #endif eiger_iodelay = val; } @@ -1813,6 +1836,8 @@ int64_t getCurrentTau() { } void setExternalGating(int enable[]) { + // not configured from client + // default: disable gating with positive polarity if (enable[0] >= 0 && enable[1] >= 0) { #ifndef VIRTUAL Feb_Control_SetExternalEnableMode(