diff --git a/slsDetectorServers/eigerDetectorServer/Beb.c b/slsDetectorServers/eigerDetectorServer/Beb.c index 354b1e13a..8c047d21c 100644 --- a/slsDetectorServers/eigerDetectorServer/Beb.c +++ b/slsDetectorServers/eigerDetectorServer/Beb.c @@ -170,7 +170,6 @@ void Beb_GetModuleConfiguration(int *master, int *top, int *normal) { LOG(logDEBUG1, ("Beb: value =0x%x\n", ret)); if (ret & BEB_CONFIG_TOP_RD_MSK) { *top = 1; - Beb_top = 1; } if (ret & BEB_CONFIG_MASTER_RD_MSK) *master = 1; @@ -298,6 +297,8 @@ int Beb_IsTransmitting(int *retval, int tengiga, int waitForDelay) { return OK; } +void Beb_SetTopVariable(int val) { Beb_top = val;} + int Beb_SetTop(enum TOPINDEX ind) { if (!Beb_activated) return 0; @@ -308,6 +309,7 @@ int Beb_SetTop(enum TOPINDEX ind) { if (fd < 0) { LOG(logERROR, ("Set Top FAIL, could not open fd in Beb\n")); return 0; + } value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); switch (ind) { case TOP_HARDWARE: @@ -340,1284 +342,1284 @@ int Beb_SetTop(enum TOPINDEX ind) { top_names[ind])); Beb_close(fd, csp0base); return 1; +} + +int Beb_SetMaster(enum MASTERINDEX ind) { + if (!Beb_activated) + return 0; + + u_int32_t *csp0base = 0; + u_int32_t value = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Set Master FAIL, could not open fd in Beb\n")); + return 0; } - - int Beb_SetMaster(enum MASTERINDEX ind) { - if (!Beb_activated) - return 0; - - u_int32_t *csp0base = 0; - u_int32_t value = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Set Master FAIL, could not open fd in Beb\n")); - return 0; - } - value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); - switch (ind) { - case MASTER_HARDWARE: - value &= ~BEB_CONFIG_OW_MASTER_MSK; - break; - case OW_MASTER: - value |= BEB_CONFIG_OW_MASTER_MSK; - value |= BEB_CONFIG_MASTER_MSK; - break; - case OW_SLAVE: - value |= BEB_CONFIG_OW_MASTER_MSK; - value &= ~BEB_CONFIG_MASTER_MSK; - break; - default: - LOG(logERROR, ("Unknown master index in Beb: %d\n", ind)); - Beb_close(fd, csp0base); - return 0; - } - - char *master_names[] = {MASTER_NAMES}; - int newval = Beb_Write32(csp0base, BEB_CONFIG_WR_OFST, value); - if (newval != value) { - LOG(logERROR, ("Could not set Master flag to %s in Beb\n", - master_names[ind])); - Beb_close(fd, csp0base); - return 0; - } - LOG(logINFOBLUE, ("%s Master flag to %s in Beb\n", - (ind == TOP_HARDWARE ? "Resetting" : "Overwriting"), - master_names[ind])); - + value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); + switch (ind) { + case MASTER_HARDWARE: + value &= ~BEB_CONFIG_OW_MASTER_MSK; + break; + case OW_MASTER: + value |= BEB_CONFIG_OW_MASTER_MSK; + value |= BEB_CONFIG_MASTER_MSK; + break; + case OW_SLAVE: + value |= BEB_CONFIG_OW_MASTER_MSK; + value &= ~BEB_CONFIG_MASTER_MSK; + break; + default: + LOG(logERROR, ("Unknown master index in Beb: %d\n", ind)); Beb_close(fd, csp0base); - return 1; - } - - int Beb_Activate(int enable) { - // mapping new memory - u_int32_t *csp0base = 0; - u_int32_t value = 0, ret = -1; - - // open file pointer - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Activate FAIL, could not open fd\n")); - } else { - if (enable > -1) { - value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); - LOG(logINFO, ("Activate register value before:%d\n", value)); - if (enable) - value |= BEB_CONFIG_ACTIVATE_MSK; - else - value &= ~BEB_CONFIG_ACTIVATE_MSK; - - int newval = Beb_Write32(csp0base, BEB_CONFIG_WR_OFST, value); - if (newval != value) { - LOG(logERROR, ("Could not %s\n", (enable ? "activate" : "deactivate")); - } - } - - value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); - ret = (value & BEB_CONFIG_ACTIVATE_MSK) ? 1 : 0; - if (enable == -1) { - if (ret) { - LOG(logINFOBLUE, ("Detector is active\n")); - } else { - LOG(logINFORED, ("Detector is deactivated!\n")); - } - } - } - // close file pointer - if (fd > 0) - Beb_close(fd, csp0base); - - Beb_activated = ret; - - return ret; - } - - int Beb_GetActivate() { return Beb_activated; } - - int Beb_Set32bitOverflow(int val) { - if (!Beb_activated) - return val; - - // mapping new memory - u_int32_t *csp0base = 0; - u_int32_t valueread = 0; - u_int32_t offset = FLOW_REG_OFFSET; - if (val > 0) - val = 1; - - // open file pointer - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd < 0) { - LOG(logERROR, - ("Could not read register to set overflow flag in 32 bit " - "mode. FAIL\n")); - return -1; - } else { - if (val > -1) { - // reset bit - valueread = Beb_Read32(csp0base, offset); - Beb_Write32(csp0base, offset, - valueread & ~FLOW_REG_OVERFLOW_32_BIT_MSK); - - // set bit - valueread = Beb_Read32(csp0base, offset); - Beb_Write32(csp0base, offset, - valueread | - ((val << FLOW_REG_OVERFLOW_32_BIT_OFST) & - FLOW_REG_OVERFLOW_32_BIT_MSK)); - } - - valueread = - (Beb_Read32(csp0base, offset) & FLOW_REG_OVERFLOW_32_BIT_MSK) >> - FLOW_REG_OVERFLOW_32_BIT_OFST; - } - // close file pointer - if (fd > 0) - Beb_close(fd, csp0base); - - return valueread; - } - - int Beb_GetTenGigaFlowControl() { - u_int32_t offset = FLOW_REG_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to get ten giga flow " - "control. FAIL\n")); - return -1; - } else { - u_int32_t retval = Beb_Read32(csp0base, offset); - retval = (retval & FLOW_REG_TXM_FLOW_CNTRL_10G_MSK) >> - FLOW_REG_TXM_FLOW_CNTRL_10G_OFST; - - Beb_close(fd, csp0base); - return retval; - } - } - - int Beb_SetTenGigaFlowControl(int value) { - LOG(logINFO, ("Setting ten giga flow control to %d\n", value)); - value = value == 0 ? 0 : 1; - u_int32_t offset = FLOW_REG_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to set ten giga flow " - "control. FAIL\n")); - return 0; - } else { - // reset bit - u_int32_t retval = Beb_Read32(csp0base, offset); - Beb_Write32(csp0base, offset, - retval & ~FLOW_REG_TXM_FLOW_CNTRL_10G_MSK); - - // set bit - retval = Beb_Read32(csp0base, offset); - Beb_Write32(csp0base, offset, - retval | ((value << FLOW_REG_TXM_FLOW_CNTRL_10G_OFST) & - FLOW_REG_TXM_FLOW_CNTRL_10G_MSK)); - - Beb_close(fd, csp0base); - return 1; - } - } - - int Beb_GetTransmissionDelayFrame() { - u_int32_t offset = TXM_DELAY_FRAME_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to get transmission delay " - "frame. FAIL\n")); - return -1; - } else { - u_int32_t retval = Beb_Read32(csp0base, offset); - Beb_close(fd, csp0base); - return retval; - } - } - - int Beb_SetTransmissionDelayFrame(int value) { - LOG(logINFO, ("Setting transmission delay frame to %d\n", value)); - if (value < 0) { - LOG(logERROR, - ("Invalid transmission delay frame value %d\n", value)); - return 0; - } - u_int32_t offset = TXM_DELAY_FRAME_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to set transmission delay " - "frame. FAIL\n")); - return 0; - } else { - Beb_Write32(csp0base, offset, value); - Beb_close(fd, csp0base); - return 1; - } - } - - int Beb_GetTransmissionDelayLeft() { - u_int32_t offset = TXM_DELAY_LEFT_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to get transmission delay " - "left. FAIL\n")); - return -1; - } else { - u_int32_t retval = Beb_Read32(csp0base, offset); - Beb_close(fd, csp0base); - return retval; - } - } - - int Beb_SetTransmissionDelayLeft(int value) { - LOG(logINFO, ("Setting transmission delay left to %d\n", value)); - if (value < 0) { - LOG(logERROR, - ("Invalid transmission delay left value %d\n", value)); - return 0; - } - u_int32_t offset = TXM_DELAY_LEFT_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to set transmission delay " - "left. FAIL\n")); - return 0; - } else { - Beb_Write32(csp0base, offset, value); - Beb_close(fd, csp0base); - return 1; - } - } - - int Beb_GetTransmissionDelayRight() { - u_int32_t offset = TXM_DELAY_RIGHT_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to get transmission delay " - "right. FAIL\n")); - return -1; - } else { - u_int32_t retval = Beb_Read32(csp0base, offset); - Beb_close(fd, csp0base); - return retval; - } - } - - int Beb_SetTransmissionDelayRight(int value) { - LOG(logINFO, ("Setting transmission delay right to %d\n", value)); - if (value < 0) { - LOG(logERROR, - ("Invalid transmission delay right value %d\n", value)); - return 0; - } - u_int32_t offset = TXM_DELAY_RIGHT_OFFSET; - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd <= 0) { - LOG(logERROR, ("Could not read register to set transmission delay " - "right. FAIL\n")); - return 0; - } else { - Beb_Write32(csp0base, offset, value); - Beb_close(fd, csp0base); - return 1; - } - } - - int Beb_SetNetworkParameter(enum NETWORKINDEX mode, int val) { - - if (!Beb_activated) - return val; - - // mapping new memory - u_int32_t *csp0base = 0; - u_int32_t valueread = 0; - u_int32_t offset = TXM_DELAY_LEFT_OFFSET; - char modename[100] = ""; - - switch (mode) { - case TXN_LEFT: - offset = TXM_DELAY_LEFT_OFFSET; - strcpy(modename, "Transmission Delay Left"); - break; - case TXN_RIGHT: - offset = TXM_DELAY_RIGHT_OFFSET; - strcpy(modename, "Transmission Delay Right"); - break; - - default: - LOG(logERROR, - ("Unrecognized mode in network parameter: %d\n", mode)); - return -1; - } - // open file pointer - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd < 0) { - LOG(logERROR, - ("Could not read register to set network parameter. FAIL\n")); - return -1; - } else { - if (val > -1) { - valueread = Beb_Read32(csp0base, offset); - Beb_Write32(csp0base, offset, val); - } - - valueread = Beb_Read32(csp0base, offset); - } - // close file pointer - if (fd > 0) - Beb_close(fd, csp0base); - - return valueread; - } - - u_int32_t Beb_GetFirmwareRevision() { - // mapping new memory - u_int32_t *csp0base = 0; - u_int32_t value = 0; - - // open file pointer - int fd = Beb_open(&csp0base, XPAR_VERSION); - if (fd < 0) { - LOG(logERROR, ("Firmware Revision Read FAIL\n")); - } else { - value = Beb_Read32(csp0base, FIRMWARE_VERSION_OFFSET); - if (!value) { - LOG(logERROR, ("Firmware Revision Number does not exist in " - "this version\n")); - } - } - - // close file pointer - if (fd > 0) - Beb_close(fd, csp0base); - - return value; - } - - u_int32_t Beb_GetFirmwareSoftwareAPIVersion() { - // mapping new memory - u_int32_t *csp0base = 0; - u_int32_t value = 0; - - // open file pointer - int fd = Beb_open(&csp0base, XPAR_VERSION); - if (fd < 0) { - LOG(logERROR, ("Firmware Software API Version Read FAIL\n")); - } else { - value = Beb_Read32(csp0base, FIRMWARESOFTWARE_API_OFFSET); - if (!value) { - LOG(logERROR, - ("Firmware Software API Version does not exist in " - "this version\n")); - } - } - - // close file pointer - if (fd > 0) - Beb_close(fd, csp0base); - - return value; - } - - void Beb_ResetFrameNumber() { - - if (!Beb_activated) - return; - - // mapping new memory to read master top module configuration - u_int32_t *csp0base = 0; - // open file pointer - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Reset Frame Number FAIL\n")); - } else { - // write a 1 - Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 1); - usleep(100000); // 100ms - // write a 0 - Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 0); - LOG(logINFO, ("Frame Number Reset OK\n")); - // close file pointer - Beb_close(fd, csp0base); - } - } - - void Beb_ClearBebInfos() { - // unsigned int i; - // for(i=0;iSetSerialAddress(0); //0xc4000000 - b->SetHeaderInfo(0,"00:50:c2:46:d9:34","129.129.205.78",42000 + 26); // 1 - GbE, ip address can be acquire from the network "arp" - b->SetHeaderInfo(1,"00:50:c2:46:d9:35","10.0.26.1",52000 + 26); //10 GbE, - everything calculable/setable beb_infos.push_back(b); - */ - - return Beb_CheckSourceStuffBebInfo(); - } - - int Beb_SetBebSrcHeaderInfos(unsigned int beb_number, int ten_gig, - char *src_mac, char *src_ip, - unsigned int src_port) { - // so that the values can be reset externally for now.... - - unsigned int i = 1; /*Beb_GetBebInfoIndex(beb_number);*/ - /******* if (!i) return 0;****************************/ // i must be - // greater than - // 0, zero is - // the global - // send - BebInfo_SetHeaderInfo(&beb_infos[i], ten_gig, src_mac, src_ip, - src_port); - - LOG(logINFO, ("Printing Beb info number (%d) :\n", i)); - BebInfo_Print(&beb_infos[i]); - - return 1; - } - - int Beb_CheckSourceStuffBebInfo() { - unsigned int i; - for (i = 1; i < bebInfoSize; i++) { // header stuff always starts from 1 - if (!Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]), 0, - "00:00:00:00:00:00", "10.0.0.1", 20000) || - !Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]), 1, - "00:00:00:00:00:00", "10.0.0.1", 20000)) { - LOG(logINFO, ("Error in BebInfo for module number %d.\n", - BebInfo_GetBebNumber(&beb_infos[i]))); - BebInfo_Print(&beb_infos[i]); - return 0; - } - } - return 1; - } - - unsigned int Beb_GetBebInfoIndex(unsigned int beb_numb) { - /******************** if (!beb_numb) return - * 0;******************************/ - unsigned int i; - for (i = 1; i < bebInfoSize; i++) - if (beb_numb == BebInfo_GetBebNumber(&beb_infos[i])) { - LOG(logDEBUG1, ("*****found beb index:%d, for beb number:%d\n", - i, beb_numb)); - return i; - } - LOG(logDEBUG1, ("*****Returning 0\n")); return 0; } - int Beb_WriteTo(unsigned int index) { - - if (!Beb_activated) - return 1; - - if (index >= bebInfoSize) { - LOG(logERROR, ("WriteTo index error.\n")); - return 0; - } - - Beb_send_data_raw[0] = - 0x90000000 | BebInfo_GetSerialAddress(&beb_infos[index]); - if (Local_Write(ll_beb, 4, Beb_send_data_raw) != 4) - return 0; - - Beb_send_data_raw[0] = 0xc0000000; - if ((Beb_send_ndata + 1) * 4 != - Local_Write(ll_beb, (Beb_send_ndata + 1) * 4, Beb_send_data_raw)) - return 0; - - return 1; + char *master_names[] = {MASTER_NAMES}; + int newval = Beb_Write32(csp0base, BEB_CONFIG_WR_OFST, value); + if (newval != value) { + LOG(logERROR, ("Could not set Master flag to %s in Beb\n", + master_names[ind])); + Beb_close(fd, csp0base); + return 0; } + LOG(logINFOBLUE, ("%s Master flag to %s in Beb\n", + (ind == MASTER_HARDWARE ? "Resetting" : "Overwriting"), + master_names[ind])); - void Beb_SwapDataFun(int little_endian, unsigned int n, unsigned int *d) { - unsigned int i; - if (little_endian) - for (i = 0; i < n; i++) - d[i] = (((d[i] & 0xff) << 24) | ((d[i] & 0xff00) << 8) | - ((d[i] & 0xff0000) >> 8) | - ((d[i] & 0xff000000) >> 24)); // little_endian + Beb_close(fd, csp0base); + return 1; +} + +int Beb_SetActivate(int enable) { + enable = enable == 0 ? 0 : 1; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Activate FAIL, could not open fd\n")); + return 0; + } else { + u_int32_t value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); + LOG(logDEBUG, ("Activate register value before:%d\n", value)); + if (enable) + value |= BEB_CONFIG_ACTIVATE_MSK; else - for (i = 0; i < n; i++) - d[i] = (((d[i] & 0xffff) << 16) | ((d[i] & 0xffff0000) >> 16)); - } + value &= ~BEB_CONFIG_ACTIVATE_MSK; - int Beb_SetByteOrder() { return 1; } - - int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, - unsigned int header_number, char *dst_mac, - char *dst_ip, unsigned int dst_port) { - - if (!Beb_activated) - return 1; - - u_int32_t bram_phy_addr; - u_int32_t *csp0base = 0; - /*u_int32_t* bram_ptr = NULL;*/ - if (ten_gig) - bram_phy_addr = 0xC6002000; - else - bram_phy_addr = 0xC6001000; - - if (!Beb_SetHeaderData(beb_number, ten_gig, dst_mac, dst_ip, dst_port)) - return 0; - - int fd = Beb_open(&csp0base, bram_phy_addr); - if (fd < 0) { - LOG(logERROR, ("Set up UDP Header FAIL\n")); - } else { - // read data - memcpy(csp0base + header_number * 16, &udp_header, - sizeof(udp_header)); - // close file pointer + u_int32_t retval = Beb_Write32(csp0base, BEB_CONFIG_WR_OFST, value); + if (retval != value) { + LOG(logERROR, ("Could not %s. WRote 0x%x, read 0x%x\n", (enable ? "activate" : "deactivate"), value, retval)); Beb_close(fd, csp0base); } - return 1; } + Beb_activated = enable; + Beb_close(fd, csp0base); + return 1; +} - int Beb_SetHeaderData(unsigned int beb_number, int ten_gig, char *dst_mac, - char *dst_ip, unsigned int dst_port) { - unsigned int i = 1; /*Beb_GetBebInfoIndex(beb_number);*/ - /***********************************if (!i) return 0; - * *************************************///i must be greater than 0, zero is the global send - return Beb_SetHeaderData1(BebInfo_GetSrcMAC(&beb_infos[i], ten_gig), - BebInfo_GetSrcIP(&beb_infos[i], ten_gig), - BebInfo_GetSrcPort(&beb_infos[i], ten_gig), - dst_mac, dst_ip, dst_port); - } - - int Beb_SetHeaderData1(char *src_mac, char *src_ip, unsigned int src_port, - char *dst_mac, char *dst_ip, unsigned int dst_port) { - /* example header*/ - // static unsigned int* word_ptr = new unsigned int [16]; - /*static*/ - /* - udp_header_type udp_header = { - {0x00, 0x50, 0xc5, 0xb2, 0xcb, 0x46}, // DST MAC - {0x00, 0x50, 0xc2, 0x46, 0xd9, 0x02}, // SRC MAC - {0x08, 0x00}, - {0x45}, - {0x00}, - {0x00, 0x00}, - {0x00, 0x00}, - {0x40}, - {0x00}, - {0xff}, - {0x11}, - {0x00, 0x00}, - {129, 205, 205, 128}, // Src IP - {129, 205, 205, 122}, // Dst IP - {0x0f, 0xa1}, - {0x13, 0x89}, - {0x00, 0x00}, //{0x00, 0x11}, - {0x00, 0x00} - }; - */ - - if (!Beb_SetMAC(src_mac, &(udp_header.src_mac[0]))) - return 0; - LOG(logINFO, ("Setting Source MAC to %s\n", src_mac)); - if (!Beb_SetIP(src_ip, &(udp_header.src_ip[0]))) - return 0; - LOG(logINFO, ("Setting Source IP to %s\n", src_ip)); - if (!Beb_SetPortNumber(src_port, &(udp_header.src_port[0]))) - return 0; - LOG(logINFO, ("Setting Source port to %d\n", src_port)); - - if (!Beb_SetMAC(dst_mac, &(udp_header.dst_mac[0]))) - return 0; - LOG(logINFO, ("Setting Destination MAC to %s\n", dst_mac)); - if (!Beb_SetIP(dst_ip, &(udp_header.dst_ip[0]))) - return 0; - LOG(logINFO, ("Setting Destination IP to %s\n", dst_ip)); - if (!Beb_SetPortNumber(dst_port, &(udp_header.dst_port[0]))) - return 0; - LOG(logINFO, ("Setting Destination port to %d\n", dst_port)); - - Beb_AdjustIPChecksum(&udp_header); - - unsigned int *base_ptr = (unsigned int *)&udp_header; - unsigned int num_words = (sizeof(struct udp_header_type) + 3) / 4; - // for(unsigned int i=0; i %s\n", macVal)); - return 0; - } - - int itemp; - sscanf(pch, "%x", &itemp); - dst_ptr[i] = (u_int8_t)itemp; - pch = strtok(NULL, ":"); - i++; - } - return 1; - } - - int Beb_SetIP(char *ip, uint8_t *dst_ptr) { - char ipVal[50]; - strcpy(ipVal, ip); - int i = 0; - char *pch = strtok(ipVal, "."); - while (pch != NULL) { - if (((i != 3) && ((strlen(pch) > 3) || (strlen(pch) < 1))) || - ((i == 3) && ((strlen(pch) < 1) || (strlen(pch) > 3)))) { - LOG(logERROR, ("Error: in ip address -> %s\n", ipVal)); - return 0; - } - - int itemp; - sscanf(pch, "%d", &itemp); - dst_ptr[i] = (u_int8_t)itemp; - pch = strtok(NULL, "."); - i++; - } - return 1; - } - - int Beb_SetPortNumber(unsigned int port_number, uint8_t *dst_ptr) { - dst_ptr[0] = (port_number >> 8) & 0xff; - dst_ptr[1] = port_number & 0xff; - return 1; - } - - void Beb_AdjustIPChecksum(struct udp_header_type * ip) { - unsigned char *cptr = (unsigned char *)ip->ver_headerlen; - - ip->ip_header_checksum[0] = 0; - ip->ip_header_checksum[1] = 0; - ip->total_length[0] = 0; - ip->total_length[1] = 28; // IP + UDP Header Length - - // calc ip checksum - unsigned int ip_checksum = 0; - unsigned int i; - for (i = 0; i < 10; i++) { - ip_checksum += ((cptr[2 * i] << 8) + (cptr[2 * i + 1])); - if (ip_checksum & 0x00010000) - ip_checksum = (ip_checksum + 1) & 0x0000ffff; - } - - ip->ip_header_checksum[0] = (ip_checksum >> 8) & 0xff; - ip->ip_header_checksum[1] = ip_checksum & 0xff; - } - - int Beb_SendMultiReadRequest( - unsigned int beb_number, unsigned int left_right, int ten_gig, - unsigned int dst_number, unsigned int npackets, - unsigned int packet_size, int stop_read_when_fifo_empty) { - - // This is a dead function, will be removed in future - // ================================================== - - unsigned int i = - 1; /*Beb_GetBebInfoIndex(beb_number); //zero is the global send*/ - - Beb_send_ndata = 3; - if (left_right == 1) - Beb_send_data[0] = 0x00040000; - else if (left_right == 2) - Beb_send_data[0] = 0x00080000; - else if (left_right == 3) - Beb_send_data[0] = 0x000c0000; - else - return 0; - - // packet_size/=2; - if (dst_number > 0x3f) - return 0; - if (packet_size > 0x3ff) - return 0; - if (npackets == 0 || npackets > 0x100) - return 0; - npackets--; - - Beb_send_data[1] = 0x62000000 | (!stop_read_when_fifo_empty) << 27 | - (ten_gig == 1) << 24 | packet_size << 14 | - dst_number << 8 | npackets; - LOG(logDEBUG1, ("Beb_send_data[1]:%X\n", Beb_send_data[1])); - Beb_send_data[2] = 0; - - Beb_SwapDataFun(0, 2, &(Beb_send_data[1])); - LOG(logDEBUG1, ("Beb_send_data[1] Swapped:%X\n", Beb_send_data[1])); - +int Beb_GetActivate(int* retval) { + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Activate FAIL, could not open fd\n")); + return 0; + } else { + u_int32_t value = Beb_Read32(csp0base, BEB_CONFIG_WR_OFST); + Beb_activated = (value & BEB_CONFIG_ACTIVATE_MSK) ? 1 : 0; if (Beb_activated) { - if (!Beb_WriteTo(i)) - return 0; + LOG(logINFOBLUE, ("Detector is active\n")); + } else { + LOG(logINFORED, ("Detector is deactivated!\n")); + } + } + Beb_close(fd, csp0base); + *retval = Beb_activated; + return 1; +} + +int Beb_Set32bitOverflow(int val) { + if (!Beb_activated) + return val; + + // mapping new memory + u_int32_t *csp0base = 0; + u_int32_t valueread = 0; + u_int32_t offset = FLOW_REG_OFFSET; + if (val > 0) + val = 1; + + // open file pointer + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, + ("Could not read register to set overflow flag in 32 bit " + "mode. FAIL\n")); + return -1; + } else { + if (val > -1) { + // reset bit + valueread = Beb_Read32(csp0base, offset); + Beb_Write32(csp0base, offset, + valueread & ~FLOW_REG_OVERFLOW_32_BIT_MSK); + + // set bit + valueread = Beb_Read32(csp0base, offset); + Beb_Write32(csp0base, offset, + valueread | + ((val << FLOW_REG_OVERFLOW_32_BIT_OFST) & + FLOW_REG_OVERFLOW_32_BIT_MSK)); } + valueread = + (Beb_Read32(csp0base, offset) & FLOW_REG_OVERFLOW_32_BIT_MSK) >> + FLOW_REG_OVERFLOW_32_BIT_OFST; + } + // close file pointer + Beb_close(fd, csp0base); + + return valueread; +} + +int Beb_GetTenGigaFlowControl() { + u_int32_t offset = FLOW_REG_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to get ten giga flow " + "control. FAIL\n")); + return -1; + } else { + u_int32_t retval = Beb_Read32(csp0base, offset); + retval = (retval & FLOW_REG_TXM_FLOW_CNTRL_10G_MSK) >> + FLOW_REG_TXM_FLOW_CNTRL_10G_OFST; + + Beb_close(fd, csp0base); + return retval; + } +} + +int Beb_SetTenGigaFlowControl(int value) { + LOG(logINFO, ("Setting ten giga flow control to %d\n", value)); + value = value == 0 ? 0 : 1; + u_int32_t offset = FLOW_REG_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to set ten giga flow " + "control. FAIL\n")); + return 0; + } else { + // reset bit + u_int32_t retval = Beb_Read32(csp0base, offset); + Beb_Write32(csp0base, offset, + retval & ~FLOW_REG_TXM_FLOW_CNTRL_10G_MSK); + + // set bit + retval = Beb_Read32(csp0base, offset); + Beb_Write32(csp0base, offset, + retval | ((value << FLOW_REG_TXM_FLOW_CNTRL_10G_OFST) & + FLOW_REG_TXM_FLOW_CNTRL_10G_MSK)); + + Beb_close(fd, csp0base); return 1; } +} - int Beb_SetUpTransferParameters(short the_bit_mode) { - if (the_bit_mode != 4 && the_bit_mode != 8 && the_bit_mode != 16 && - the_bit_mode != 32) - return 0; - Beb_bit_mode = the_bit_mode; - - // nimages = the_number_of_images; - // on_dst = 0; +int Beb_GetTransmissionDelayFrame() { + u_int32_t offset = TXM_DELAY_FRAME_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to get transmission delay " + "frame. FAIL\n")); + return -1; + } else { + u_int32_t retval = Beb_Read32(csp0base, offset); + Beb_close(fd, csp0base); + return retval; + } +} +int Beb_SetTransmissionDelayFrame(int value) { + LOG(logINFO, ("Setting transmission delay frame to %d\n", value)); + if (value < 0) { + LOG(logERROR, + ("Invalid transmission delay frame value %d\n", value)); + return 0; + } + u_int32_t offset = TXM_DELAY_FRAME_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to set transmission delay " + "frame. FAIL\n")); + return 0; + } else { + Beb_Write32(csp0base, offset, value); + Beb_close(fd, csp0base); return 1; } +} - int Beb_StopAcquisition() { - if (!Beb_activated) - return 1; +int Beb_GetTransmissionDelayLeft() { + u_int32_t offset = TXM_DELAY_LEFT_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to get transmission delay " + "left. FAIL\n")); + return -1; + } else { + u_int32_t retval = Beb_Read32(csp0base, offset); + Beb_close(fd, csp0base); + return retval; + } +} - u_int32_t *csp0base = 0; - volatile u_int32_t valuel, valuer; - // open file pointer - int fd = Beb_open(&csp0base, XPAR_CMD_GENERATOR); - if (fd < 0) { - LOG(logERROR, ("Beb Stop Acquisition FAIL\n")); - return 0; - } else { - // find value - valuel = Beb_Read32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET)); - valuer = Beb_Read32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET)); - // high - Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET), - (valuel | STOP_ACQ_BIT)); - Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET), - (valuer | STOP_ACQ_BIT)); - // low - Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET), - (valuel & (~STOP_ACQ_BIT))); - Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET), - (valuer & (~STOP_ACQ_BIT))); - - LOG(logINFO, ("Beb Stop Acquisition OK\n")); - // close file pointer - Beb_close(fd, csp0base); - } +int Beb_SetTransmissionDelayLeft(int value) { + LOG(logINFO, ("Setting transmission delay left to %d\n", value)); + if (value < 0) { + LOG(logERROR, + ("Invalid transmission delay left value %d\n", value)); + return 0; + } + u_int32_t offset = TXM_DELAY_LEFT_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to set transmission delay " + "left. FAIL\n")); + return 0; + } else { + Beb_Write32(csp0base, offset, value); + Beb_close(fd, csp0base); return 1; } +} - int Beb_RequestNImages(unsigned int beb_number, int ten_gig, - unsigned int dst_number, unsigned int nimages, - int test_just_send_out_packets_no_wait) { - if (!Beb_activated) - return 1; - - if (dst_number > 64) - return 0; - - unsigned int maxnl = MAX_ROWS_PER_READOUT; - unsigned int maxnp = (ten_gig ? 4 : 16) * Beb_bit_mode; - unsigned int nl = Beb_readNLines; - unsigned int npackets = (nl * maxnp) / maxnl; - if ((nl * maxnp) % maxnl) { - LOG(logERROR, ("Read N Lines is incorrect. Switching to Full Image " - "Readout\n")); - npackets = maxnp; - } - int in_two_requests = (npackets > MAX_PACKETS_PER_REQUEST) ? 1 : 0; - if (in_two_requests) { - npackets /= 2; - } - unsigned int header_size = 4; // 4*64 bits - unsigned int packet_size = ten_gig ? 0x200 : 0x80; // 4k or 1k packets - - LOG(logDEBUG1, ("----Beb_RequestNImages Start----\n")); - LOG(logINFO, - ("beb_number:%d, ten_gig:%d,dst_number:%d, npackets:%d, " - "Beb_bit_mode:%d, header_size:%d, nimages:%d, " - "test_just_send_out_packets_no_wait:%d\n", - beb_number, ten_gig, dst_number, npackets, Beb_bit_mode, - header_size, nimages, test_just_send_out_packets_no_wait)); - - u_int32_t right_port_value = 0x2000; - u_int32_t *csp0base = 0; - volatile u_int32_t value; - // open file pointer - int fd = Beb_open(&csp0base, XPAR_CMD_GENERATOR); - if (fd < 0) { - LOG(logERROR, ("Beb Request N Images FAIL\n")); - return 0; - } else { - { - int i; - for (i = 0; i < 10; i++) - LOG(logDEBUG1, - ("%X\n", Beb_Read32(csp0base, (LEFT_OFFSET + i * 4)))); - } - // Generating commands - u_int32_t send_header_command = - 0x62000000 | (!test_just_send_out_packets_no_wait) << 27 | - (ten_gig == 1) << 24 | header_size << 14 | 0; - u_int32_t send_frame_command = - 0x62000000 | (!test_just_send_out_packets_no_wait) << 27 | - (ten_gig == 1) << 24 | packet_size << 14 | (npackets - 1); - { - int i; - for (i = 0; i < 10; i++) - LOG(logDEBUG1, - ("%X\n", Beb_Read32(csp0base, (LEFT_OFFSET + i * 4)))); - LOG(logDEBUG1, ("%d\n", in_two_requests)); - } - //"0x20 << 8" is dst_number (0x00 for left, 0x20 for right) - // Left - Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART1_OFFSET), 0); - Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART2_OFFSET), - send_header_command); - Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART1_OFFSET), 0); - Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART2_OFFSET), - send_frame_command); - value = Beb_Read32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET)); - if (in_two_requests) - Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET), - (value | TWO_REQUESTS_BIT)); - else - Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET), - (value & ~(TWO_REQUESTS_BIT))); - - // Right - Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART1_OFFSET), 0); - Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART2_OFFSET), - send_header_command | right_port_value); - Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART1_OFFSET), 0); - Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART2_OFFSET), - send_frame_command | right_port_value); - value = Beb_Read32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET)); - if (in_two_requests) - Beb_Write32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET), - (value | TWO_REQUESTS_BIT)); - else - Beb_Write32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET), - (value & ~(TWO_REQUESTS_BIT))); - - // Set number of frames - Beb_Write32(csp0base, (LEFT_OFFSET + COMMAND_COUNTER_OFFSET), - nimages * (2 + in_two_requests)); - Beb_Write32(csp0base, (RIGHT_OFFSET + COMMAND_COUNTER_OFFSET), - nimages * (2 + in_two_requests)); - { - int i; - for (i = 0; i < 10; i++) - LOG(logDEBUG1, ("%X\n", Beb_Read32(csp0base, - (LEFT_OFFSET + - i * 4)))); //*(ptrl+i)); - LOG(logDEBUG1, ("%d\n", in_two_requests)); - } - Beb_close(fd, csp0base); - - LOG(logDEBUG1, ("----Beb_RequestNImages----\n")); - } +int Beb_GetTransmissionDelayRight() { + u_int32_t offset = TXM_DELAY_RIGHT_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to get transmission delay " + "right. FAIL\n")); + return -1; + } else { + u_int32_t retval = Beb_Read32(csp0base, offset); + Beb_close(fd, csp0base); + return retval; + } +} +int Beb_SetTransmissionDelayRight(int value) { + LOG(logINFO, ("Setting transmission delay right to %d\n", value)); + if (value < 0) { + LOG(logERROR, + ("Invalid transmission delay right value %d\n", value)); + return 0; + } + u_int32_t offset = TXM_DELAY_RIGHT_OFFSET; + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd <= 0) { + LOG(logERROR, ("Could not read register to set transmission delay " + "right. FAIL\n")); + return 0; + } else { + Beb_Write32(csp0base, offset, value); + Beb_close(fd, csp0base); return 1; } +} - int Beb_Test(unsigned int beb_number) { - LOG(logINFO, ("Testing module number: %d\n", beb_number)); +int Beb_SetNetworkParameter(enum NETWORKINDEX mode, int val) { - // int SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int - // header_number, string dst_mac, string dst_ip, unsigned int dst_port) - // { SetUpUDPHeader(26,0,0,"60:fb:42:f4:e3:d2","129.129.205.186",22000); + if (!Beb_activated) + return val; - unsigned int index = Beb_GetBebInfoIndex(beb_number); - if (!index) { - LOG(logERROR, - ("Error beb number (%d)not in list????\n", beb_number)); - return 0; - } + // mapping new memory + u_int32_t *csp0base = 0; + u_int32_t valueread = 0; + u_int32_t offset = TXM_DELAY_LEFT_OFFSET; + char modename[100] = ""; - unsigned int i; - for (i = 0; i < 64; i++) { - if (!Beb_SetUpUDPHeader(beb_number, 0, i, "60:fb:42:f4:e3:d2", - "129.129.205.186", 22000 + i)) { - LOG(logERROR, ("Error setting up header table....\n")); - return 0; - } - } + switch (mode) { + case TXN_LEFT: + offset = TXM_DELAY_LEFT_OFFSET; + strcpy(modename, "Transmission Delay Left"); + break; + case TXN_RIGHT: + offset = TXM_DELAY_RIGHT_OFFSET; + strcpy(modename, "Transmission Delay Right"); + break; - // SendMultiReadRequest(unsigned int beb_number, unsigned int - // left_right, int ten_gig, unsigned int dst_number, unsigned int - // npackets, unsigned int packet_size, int - // stop_read_when_fifo_empty=1); - for (i = 0; i < 64; i++) { - if (!Beb_SendMultiReadRequest(beb_number, i % 3 + 1, 0, i, 1, 0, - 1)) { - LOG(logERROR, ("Error requesting data....\n")); - return 0; - } - } - - return 1; + default: + LOG(logERROR, + ("Unrecognized mode in network parameter: %d\n", mode)); + return -1; } - - // Returns the FPGA temperature from the xps sysmon ip core - // Temperature value is cropped and not well rounded - int Beb_GetBebFPGATemp() { - - u_int32_t *csp0base = 0; - int temperature = 0; - int ret; - // open file pointer - int fd = Beb_open(&csp0base, XPAR_SYSMON_0_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Module Configuration FAIL\n")); - } else { - // read data - ret = Beb_Read32(csp0base, FPGA_TEMP_OFFSET); - temperature = - ((((float)(ret) / 65536.0f) / 0.00198421639f) - 273.15f) * - 1000; // Static conversation, copied from xps sysmon - // standalone driver - // close file pointer - Beb_close(fd, csp0base); + // open file pointer + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, + ("Could not read register to set network parameter. FAIL\n")); + return -1; + } else { + if (val > -1) { + valueread = Beb_Read32(csp0base, offset); + Beb_Write32(csp0base, offset, val); } - return temperature; + valueread = Beb_Read32(csp0base, offset); } - - void Beb_SetDetectorNumber(uint32_t detid) { - if (!Beb_activated) - return; - - uint32_t swapid = Beb_swap_uint16(detid); - // LOG(logINFO, "detector id %d swapped %d\n", detid, swapid)); - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Set Detector ID FAIL\n")); - return; - } else { - uint32_t value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); - value &= UDP_HEADER_X_MSK; // to keep previous x value - Beb_Write32( - csp0base, UDP_HEADER_A_LEFT_OFST, - value | ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); - if ((value & UDP_HEADER_ID_MSK) != - ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)) { - LOG(logERROR, ("Set Detector ID FAIL\n")); - } - value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); - value &= UDP_HEADER_X_MSK; // to keep previous x value - Beb_Write32( - csp0base, UDP_HEADER_A_RIGHT_OFST, - value | ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); - if ((value & UDP_HEADER_ID_MSK) != - ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)) { - LOG(logERROR, ("Set Detector ID FAIL\n")); - } - Beb_close(fd, csp0base); - } - LOG(logINFO, ("Detector id %d set in UDP Header\n\n", detid)); - } - - int Beb_SetQuad(int value) { - if (value < 0) - return OK; - LOG(logINFO, ("Setting Quad to %d in Beb\n", value)); - Beb_quadEnable = (value == 0 ? 0 : 1); - return Beb_SetDetectorPosition(Beb_positions); - } - - int Beb_GetQuad() { return Beb_quadEnable; } - - int *Beb_GetDetectorPosition() { return Beb_positions; } - - int Beb_SetDetectorPosition(int pos[]) { - if (!Beb_activated) - return OK; - LOG(logINFO, ("Got Position values %d %d...\n", pos[0], pos[1])); - - // save positions - Beb_positions[0] = pos[0]; - Beb_positions[1] = pos[1]; - - // get left and right - int posLeft[2] = {pos[0], Beb_top ? pos[1] : pos[1] + 1}; - int posRight[2] = {pos[0], Beb_top ? pos[1] + 1 : pos[1]}; - - if (Beb_quadEnable) { - posRight[0] = 1; // right is next row - posRight[1] = 0; // right same first column - } - - int ret = FAIL; - // mapping new memory to read master top module configuration - u_int32_t *csp0base = 0; - // open file pointer - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Set Detector Position FAIL\n")); - return FAIL; - } else { - uint32_t value = 0; - ret = OK; - // x left - int posval = Beb_swap_uint16(posLeft[0]); - value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); - value &= UDP_HEADER_ID_MSK; // to keep previous id value - Beb_Write32(csp0base, UDP_HEADER_A_LEFT_OFST, - value | - ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); - if ((value & UDP_HEADER_X_MSK) != - ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)) { - LOG(logERROR, ("Could not set row position for left port\n")); - ret = FAIL; - } - // x right - posval = Beb_swap_uint16(posRight[0]); - value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); - value &= UDP_HEADER_ID_MSK; // to keep previous id value - Beb_Write32(csp0base, UDP_HEADER_A_RIGHT_OFST, - value | - ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); - if ((value & UDP_HEADER_X_MSK) != - ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)) { - LOG(logERROR, ("Could not set row position for right port\n")); - ret = FAIL; - } - - // y left (column) - posval = Beb_swap_uint16(posLeft[1]); - value = Beb_Read32(csp0base, UDP_HEADER_B_LEFT_OFST); - value &= UDP_HEADER_Z_MSK; // to keep previous z value - Beb_Write32(csp0base, UDP_HEADER_B_LEFT_OFST, - value | - ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_B_LEFT_OFST); - if ((value & UDP_HEADER_Y_MSK) != - ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)) { - LOG(logERROR, - ("Could not set column position for left port\n")); - ret = FAIL; - } - - // y right - posval = Beb_swap_uint16(posRight[1]); - value = Beb_Read32(csp0base, UDP_HEADER_B_RIGHT_OFST); - value &= UDP_HEADER_Z_MSK; // to keep previous z value - Beb_Write32(csp0base, UDP_HEADER_B_RIGHT_OFST, - value | - ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)); - value = Beb_Read32(csp0base, UDP_HEADER_B_RIGHT_OFST); - if ((value & UDP_HEADER_Y_MSK) != - ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)) { - LOG(logERROR, - ("Could not set column position for right port\n")); - ret = FAIL; - } - - // close file pointer - Beb_close(fd, csp0base); - } - if (ret == OK) { - LOG(logINFO, ("Position set to...\n" - "\tLeft: [%d, %d]\n" - "\tRight:[%d, %d]\n", - posLeft[0], posLeft[1], posRight[0], posRight[1])); - } - - return ret; - } - - int Beb_SetStartingFrameNumber(uint64_t value) { - if (!Beb_activated) { - Beb_deactivatedStartFrameNumber = value; - return OK; - } - LOG(logINFO, ("Setting start frame number: %llu\n", - (long long unsigned int)value)); - - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Set Start Frame Number FAIL\n")); - return FAIL; - } - // since the read is not implemented in firmware yet - Beb_deactivatedStartFrameNumber = value; - - // decrement for firmware - uint64_t valueInFirmware = value - 1; - Beb_Write32(csp0base, UDP_HEADER_FRAME_NUMBER_LSB_OFST, - valueInFirmware & (0xffffffff)); - Beb_Write32(csp0base, UDP_HEADER_FRAME_NUMBER_MSB_OFST, - (valueInFirmware >> 32) & (0xffffffff)); + // close file pointer + if (fd > 0) Beb_close(fd, csp0base); - LOG(logINFO, ("Going to reset Frame Number\n")); - Beb_ResetFrameNumber(); + return valueread; +} + +u_int32_t Beb_GetFirmwareRevision() { + // mapping new memory + u_int32_t *csp0base = 0; + u_int32_t value = 0; + + // open file pointer + int fd = Beb_open(&csp0base, XPAR_VERSION); + if (fd < 0) { + LOG(logERROR, ("Firmware Revision Read FAIL\n")); + } else { + value = Beb_Read32(csp0base, FIRMWARE_VERSION_OFFSET); + if (!value) { + LOG(logERROR, ("Firmware Revision Number does not exist in " + "this version\n")); + } + } + + // close file pointer + if (fd > 0) + Beb_close(fd, csp0base); + + return value; +} + +u_int32_t Beb_GetFirmwareSoftwareAPIVersion() { + // mapping new memory + u_int32_t *csp0base = 0; + u_int32_t value = 0; + + // open file pointer + int fd = Beb_open(&csp0base, XPAR_VERSION); + if (fd < 0) { + LOG(logERROR, ("Firmware Software API Version Read FAIL\n")); + } else { + value = Beb_Read32(csp0base, FIRMWARESOFTWARE_API_OFFSET); + if (!value) { + LOG(logERROR, + ("Firmware Software API Version does not exist in " + "this version\n")); + } + } + + // close file pointer + if (fd > 0) + Beb_close(fd, csp0base); + + return value; +} + +void Beb_ResetFrameNumber() { + + if (!Beb_activated) + return; + + // mapping new memory to read master top module configuration + u_int32_t *csp0base = 0; + // open file pointer + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_SYS_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Reset Frame Number FAIL\n")); + } else { + // write a 1 + Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 1); + usleep(100000); // 100ms + // write a 0 + Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 0); + LOG(logINFO, ("Frame Number Reset OK\n")); + // close file pointer + Beb_close(fd, csp0base); + } +} + +void Beb_ClearBebInfos() { + // unsigned int i; + // for(i=0;iSetSerialAddress(0); //0xc4000000 +b->SetHeaderInfo(0,"00:50:c2:46:d9:34","129.129.205.78",42000 + 26); // 1 +GbE, ip address can be acquire from the network "arp" +b->SetHeaderInfo(1,"00:50:c2:46:d9:35","10.0.26.1",52000 + 26); //10 GbE, +everything calculable/setable beb_infos.push_back(b); + */ + + return Beb_CheckSourceStuffBebInfo(); +} + +int Beb_SetBebSrcHeaderInfos(unsigned int beb_number, int ten_gig, + char *src_mac, char *src_ip, + unsigned int src_port) { + // so that the values can be reset externally for now.... + + unsigned int i = 1; /*Beb_GetBebInfoIndex(beb_number);*/ + /******* if (!i) return 0;****************************/ // i must be + // greater than + // 0, zero is + // the global + // send + BebInfo_SetHeaderInfo(&beb_infos[i], ten_gig, src_mac, src_ip, + src_port); + + LOG(logINFO, ("Printing Beb info number (%d) :\n", i)); + BebInfo_Print(&beb_infos[i]); + + return 1; +} + +int Beb_CheckSourceStuffBebInfo() { + unsigned int i; + for (i = 1; i < bebInfoSize; i++) { // header stuff always starts from 1 + if (!Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]), 0, + "00:00:00:00:00:00", "10.0.0.1", 20000) || + !Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]), 1, + "00:00:00:00:00:00", "10.0.0.1", 20000)) { + LOG(logINFO, ("Error in BebInfo for module number %d.\n", + BebInfo_GetBebNumber(&beb_infos[i]))); + BebInfo_Print(&beb_infos[i]); + return 0; + } + } + return 1; +} + +unsigned int Beb_GetBebInfoIndex(unsigned int beb_numb) { + /******************** if (!beb_numb) return + * 0;******************************/ + unsigned int i; + for (i = 1; i < bebInfoSize; i++) + if (beb_numb == BebInfo_GetBebNumber(&beb_infos[i])) { + LOG(logDEBUG1, ("*****found beb index:%d, for beb number:%d\n", + i, beb_numb)); + return i; + } + LOG(logDEBUG1, ("*****Returning 0\n")); + return 0; +} + +int Beb_WriteTo(unsigned int index) { + + if (!Beb_activated) + return 1; + + if (index >= bebInfoSize) { + LOG(logERROR, ("WriteTo index error.\n")); + return 0; + } + + Beb_send_data_raw[0] = + 0x90000000 | BebInfo_GetSerialAddress(&beb_infos[index]); + if (Local_Write(ll_beb, 4, Beb_send_data_raw) != 4) + return 0; + + Beb_send_data_raw[0] = 0xc0000000; + if ((Beb_send_ndata + 1) * 4 != + Local_Write(ll_beb, (Beb_send_ndata + 1) * 4, Beb_send_data_raw)) + return 0; + + return 1; +} + +void Beb_SwapDataFun(int little_endian, unsigned int n, unsigned int *d) { + unsigned int i; + if (little_endian) + for (i = 0; i < n; i++) + d[i] = (((d[i] & 0xff) << 24) | ((d[i] & 0xff00) << 8) | + ((d[i] & 0xff0000) >> 8) | + ((d[i] & 0xff000000) >> 24)); // little_endian + else + for (i = 0; i < n; i++) + d[i] = (((d[i] & 0xffff) << 16) | ((d[i] & 0xffff0000) >> 16)); +} + +int Beb_SetByteOrder() { return 1; } + +int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, + unsigned int header_number, char *dst_mac, + char *dst_ip, unsigned int dst_port) { + + if (!Beb_activated) + return 1; + + u_int32_t bram_phy_addr; + u_int32_t *csp0base = 0; + /*u_int32_t* bram_ptr = NULL;*/ + if (ten_gig) + bram_phy_addr = 0xC6002000; + else + bram_phy_addr = 0xC6001000; + + if (!Beb_SetHeaderData(beb_number, ten_gig, dst_mac, dst_ip, dst_port)) + return 0; + + int fd = Beb_open(&csp0base, bram_phy_addr); + if (fd < 0) { + LOG(logERROR, ("Set up UDP Header FAIL\n")); + } else { + // read data + memcpy(csp0base + header_number * 16, &udp_header, + sizeof(udp_header)); + // close file pointer + Beb_close(fd, csp0base); + } + return 1; +} + +int Beb_SetHeaderData(unsigned int beb_number, int ten_gig, char *dst_mac, + char *dst_ip, unsigned int dst_port) { + unsigned int i = 1; /*Beb_GetBebInfoIndex(beb_number);*/ + /***********************************if (!i) return 0; + * *************************************///i must be greater than 0, zero is the global send + return Beb_SetHeaderData1(BebInfo_GetSrcMAC(&beb_infos[i], ten_gig), + BebInfo_GetSrcIP(&beb_infos[i], ten_gig), + BebInfo_GetSrcPort(&beb_infos[i], ten_gig), + dst_mac, dst_ip, dst_port); +} + +int Beb_SetHeaderData1(char *src_mac, char *src_ip, unsigned int src_port, + char *dst_mac, char *dst_ip, unsigned int dst_port) { + /* example header*/ + // static unsigned int* word_ptr = new unsigned int [16]; + /*static*/ + /* +udp_header_type udp_header = { + {0x00, 0x50, 0xc5, 0xb2, 0xcb, 0x46}, // DST MAC + {0x00, 0x50, 0xc2, 0x46, 0xd9, 0x02}, // SRC MAC + {0x08, 0x00}, + {0x45}, + {0x00}, + {0x00, 0x00}, + {0x00, 0x00}, + {0x40}, + {0x00}, + {0xff}, + {0x11}, + {0x00, 0x00}, + {129, 205, 205, 128}, // Src IP + {129, 205, 205, 122}, // Dst IP + {0x0f, 0xa1}, + {0x13, 0x89}, + {0x00, 0x00}, //{0x00, 0x11}, + {0x00, 0x00} + }; + */ + + if (!Beb_SetMAC(src_mac, &(udp_header.src_mac[0]))) + return 0; + LOG(logINFO, ("Setting Source MAC to %s\n", src_mac)); + if (!Beb_SetIP(src_ip, &(udp_header.src_ip[0]))) + return 0; + LOG(logINFO, ("Setting Source IP to %s\n", src_ip)); + if (!Beb_SetPortNumber(src_port, &(udp_header.src_port[0]))) + return 0; + LOG(logINFO, ("Setting Source port to %d\n", src_port)); + + if (!Beb_SetMAC(dst_mac, &(udp_header.dst_mac[0]))) + return 0; + LOG(logINFO, ("Setting Destination MAC to %s\n", dst_mac)); + if (!Beb_SetIP(dst_ip, &(udp_header.dst_ip[0]))) + return 0; + LOG(logINFO, ("Setting Destination IP to %s\n", dst_ip)); + if (!Beb_SetPortNumber(dst_port, &(udp_header.dst_port[0]))) + return 0; + LOG(logINFO, ("Setting Destination port to %d\n", dst_port)); + + Beb_AdjustIPChecksum(&udp_header); + + unsigned int *base_ptr = (unsigned int *)&udp_header; + unsigned int num_words = (sizeof(struct udp_header_type) + 3) / 4; + // for(unsigned int i=0; i %s\n", macVal)); + return 0; + } + + int itemp; + sscanf(pch, "%x", &itemp); + dst_ptr[i] = (u_int8_t)itemp; + pch = strtok(NULL, ":"); + i++; + } + return 1; +} + +int Beb_SetIP(char *ip, uint8_t *dst_ptr) { + char ipVal[50]; + strcpy(ipVal, ip); + int i = 0; + char *pch = strtok(ipVal, "."); + while (pch != NULL) { + if (((i != 3) && ((strlen(pch) > 3) || (strlen(pch) < 1))) || + ((i == 3) && ((strlen(pch) < 1) || (strlen(pch) > 3)))) { + LOG(logERROR, ("Error: in ip address -> %s\n", ipVal)); + return 0; + } + + int itemp; + sscanf(pch, "%d", &itemp); + dst_ptr[i] = (u_int8_t)itemp; + pch = strtok(NULL, "."); + i++; + } + return 1; +} + +int Beb_SetPortNumber(unsigned int port_number, uint8_t *dst_ptr) { + dst_ptr[0] = (port_number >> 8) & 0xff; + dst_ptr[1] = port_number & 0xff; + return 1; +} + +void Beb_AdjustIPChecksum(struct udp_header_type * ip) { + unsigned char *cptr = (unsigned char *)ip->ver_headerlen; + + ip->ip_header_checksum[0] = 0; + ip->ip_header_checksum[1] = 0; + ip->total_length[0] = 0; + ip->total_length[1] = 28; // IP + UDP Header Length + + // calc ip checksum + unsigned int ip_checksum = 0; + unsigned int i; + for (i = 0; i < 10; i++) { + ip_checksum += ((cptr[2 * i] << 8) + (cptr[2 * i + 1])); + if (ip_checksum & 0x00010000) + ip_checksum = (ip_checksum + 1) & 0x0000ffff; + } + + ip->ip_header_checksum[0] = (ip_checksum >> 8) & 0xff; + ip->ip_header_checksum[1] = ip_checksum & 0xff; +} + +int Beb_SendMultiReadRequest( + unsigned int beb_number, unsigned int left_right, int ten_gig, + unsigned int dst_number, unsigned int npackets, + unsigned int packet_size, int stop_read_when_fifo_empty) { + + // This is a dead function, will be removed in future + // ================================================== + + unsigned int i = + 1; /*Beb_GetBebInfoIndex(beb_number); //zero is the global send*/ + + Beb_send_ndata = 3; + if (left_right == 1) + Beb_send_data[0] = 0x00040000; + else if (left_right == 2) + Beb_send_data[0] = 0x00080000; + else if (left_right == 3) + Beb_send_data[0] = 0x000c0000; + else + return 0; + + // packet_size/=2; + if (dst_number > 0x3f) + return 0; + if (packet_size > 0x3ff) + return 0; + if (npackets == 0 || npackets > 0x100) + return 0; + npackets--; + + Beb_send_data[1] = 0x62000000 | (!stop_read_when_fifo_empty) << 27 | + (ten_gig == 1) << 24 | packet_size << 14 | + dst_number << 8 | npackets; + LOG(logDEBUG1, ("Beb_send_data[1]:%X\n", Beb_send_data[1])); + Beb_send_data[2] = 0; + + Beb_SwapDataFun(0, 2, &(Beb_send_data[1])); + LOG(logDEBUG1, ("Beb_send_data[1] Swapped:%X\n", Beb_send_data[1])); + + if (Beb_activated) { + if (!Beb_WriteTo(i)) + return 0; + } + + return 1; +} + +int Beb_SetUpTransferParameters(short the_bit_mode) { + if (the_bit_mode != 4 && the_bit_mode != 8 && the_bit_mode != 16 && + the_bit_mode != 32) + return 0; + Beb_bit_mode = the_bit_mode; + + // nimages = the_number_of_images; + // on_dst = 0; + + return 1; +} + +int Beb_StopAcquisition() { + if (!Beb_activated) + return 1; + + u_int32_t *csp0base = 0; + volatile u_int32_t valuel, valuer; + // open file pointer + int fd = Beb_open(&csp0base, XPAR_CMD_GENERATOR); + if (fd < 0) { + LOG(logERROR, ("Beb Stop Acquisition FAIL\n")); + return 0; + } else { + // find value + valuel = Beb_Read32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET)); + valuer = Beb_Read32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET)); + // high + Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET), + (valuel | STOP_ACQ_BIT)); + Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET), + (valuer | STOP_ACQ_BIT)); + // low + Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET), + (valuel & (~STOP_ACQ_BIT))); + Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET), + (valuer & (~STOP_ACQ_BIT))); + + LOG(logINFO, ("Beb Stop Acquisition OK\n")); + // close file pointer + Beb_close(fd, csp0base); + } + return 1; +} + +int Beb_RequestNImages(unsigned int beb_number, int ten_gig, + unsigned int dst_number, unsigned int nimages, + int test_just_send_out_packets_no_wait) { + if (!Beb_activated) + return 1; + + if (dst_number > 64) + return 0; + + unsigned int maxnl = MAX_ROWS_PER_READOUT; + unsigned int maxnp = (ten_gig ? 4 : 16) * Beb_bit_mode; + unsigned int nl = Beb_readNLines; + unsigned int npackets = (nl * maxnp) / maxnl; + if ((nl * maxnp) % maxnl) { + LOG(logERROR, ("Read N Lines is incorrect. Switching to Full Image " + "Readout\n")); + npackets = maxnp; + } + int in_two_requests = (npackets > MAX_PACKETS_PER_REQUEST) ? 1 : 0; + if (in_two_requests) { + npackets /= 2; + } + unsigned int header_size = 4; // 4*64 bits + unsigned int packet_size = ten_gig ? 0x200 : 0x80; // 4k or 1k packets + + LOG(logDEBUG1, ("----Beb_RequestNImages Start----\n")); + LOG(logINFO, + ("beb_number:%d, ten_gig:%d,dst_number:%d, npackets:%d, " + "Beb_bit_mode:%d, header_size:%d, nimages:%d, " + "test_just_send_out_packets_no_wait:%d\n", + beb_number, ten_gig, dst_number, npackets, Beb_bit_mode, + header_size, nimages, test_just_send_out_packets_no_wait)); + + u_int32_t right_port_value = 0x2000; + u_int32_t *csp0base = 0; + volatile u_int32_t value; + // open file pointer + int fd = Beb_open(&csp0base, XPAR_CMD_GENERATOR); + if (fd < 0) { + LOG(logERROR, ("Beb Request N Images FAIL\n")); + return 0; + } else { + { + int i; + for (i = 0; i < 10; i++) + LOG(logDEBUG1, + ("%X\n", Beb_Read32(csp0base, (LEFT_OFFSET + i * 4)))); + } + // Generating commands + u_int32_t send_header_command = + 0x62000000 | (!test_just_send_out_packets_no_wait) << 27 | + (ten_gig == 1) << 24 | header_size << 14 | 0; + u_int32_t send_frame_command = + 0x62000000 | (!test_just_send_out_packets_no_wait) << 27 | + (ten_gig == 1) << 24 | packet_size << 14 | (npackets - 1); + { + int i; + for (i = 0; i < 10; i++) + LOG(logDEBUG1, + ("%X\n", Beb_Read32(csp0base, (LEFT_OFFSET + i * 4)))); + LOG(logDEBUG1, ("%d\n", in_two_requests)); + } + //"0x20 << 8" is dst_number (0x00 for left, 0x20 for right) + // Left + Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART1_OFFSET), 0); + Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART2_OFFSET), + send_header_command); + Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART1_OFFSET), 0); + Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART2_OFFSET), + send_frame_command); + value = Beb_Read32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET)); + if (in_two_requests) + Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET), + (value | TWO_REQUESTS_BIT)); + else + Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET), + (value & ~(TWO_REQUESTS_BIT))); + + // Right + Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART1_OFFSET), 0); + Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART2_OFFSET), + send_header_command | right_port_value); + Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART1_OFFSET), 0); + Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART2_OFFSET), + send_frame_command | right_port_value); + value = Beb_Read32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET)); + if (in_two_requests) + Beb_Write32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET), + (value | TWO_REQUESTS_BIT)); + else + Beb_Write32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET), + (value & ~(TWO_REQUESTS_BIT))); + + // Set number of frames + Beb_Write32(csp0base, (LEFT_OFFSET + COMMAND_COUNTER_OFFSET), + nimages * (2 + in_two_requests)); + Beb_Write32(csp0base, (RIGHT_OFFSET + COMMAND_COUNTER_OFFSET), + nimages * (2 + in_two_requests)); + { + int i; + for (i = 0; i < 10; i++) + LOG(logDEBUG1, ("%X\n", Beb_Read32(csp0base, + (LEFT_OFFSET + + i * 4)))); //*(ptrl+i)); + LOG(logDEBUG1, ("%d\n", in_two_requests)); + } + Beb_close(fd, csp0base); + + LOG(logDEBUG1, ("----Beb_RequestNImages----\n")); + } + + return 1; +} + +int Beb_Test(unsigned int beb_number) { + LOG(logINFO, ("Testing module number: %d\n", beb_number)); + + // int SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int + // header_number, string dst_mac, string dst_ip, unsigned int dst_port) + // { SetUpUDPHeader(26,0,0,"60:fb:42:f4:e3:d2","129.129.205.186",22000); + + unsigned int index = Beb_GetBebInfoIndex(beb_number); + if (!index) { + LOG(logERROR, + ("Error beb number (%d)not in list????\n", beb_number)); + return 0; + } + + unsigned int i; + for (i = 0; i < 64; i++) { + if (!Beb_SetUpUDPHeader(beb_number, 0, i, "60:fb:42:f4:e3:d2", + "129.129.205.186", 22000 + i)) { + LOG(logERROR, ("Error setting up header table....\n")); + return 0; + } + } + + // SendMultiReadRequest(unsigned int beb_number, unsigned int + // left_right, int ten_gig, unsigned int dst_number, unsigned int + // npackets, unsigned int packet_size, int + // stop_read_when_fifo_empty=1); + for (i = 0; i < 64; i++) { + if (!Beb_SendMultiReadRequest(beb_number, i % 3 + 1, 0, i, 1, 0, + 1)) { + LOG(logERROR, ("Error requesting data....\n")); + return 0; + } + } + + return 1; +} + +// Returns the FPGA temperature from the xps sysmon ip core +// Temperature value is cropped and not well rounded +int Beb_GetBebFPGATemp() { + + u_int32_t *csp0base = 0; + int temperature = 0; + int ret; + // open file pointer + int fd = Beb_open(&csp0base, XPAR_SYSMON_0_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Module Configuration FAIL\n")); + } else { + // read data + ret = Beb_Read32(csp0base, FPGA_TEMP_OFFSET); + temperature = + ((((float)(ret) / 65536.0f) / 0.00198421639f) - 273.15f) * + 1000; // Static conversation, copied from xps sysmon + // standalone driver + // close file pointer + Beb_close(fd, csp0base); + } + + return temperature; +} + +void Beb_SetDetectorNumber(uint32_t detid) { + if (!Beb_activated) + return; + + uint32_t swapid = Beb_swap_uint16(detid); + // LOG(logINFO, "detector id %d swapped %d\n", detid, swapid)); + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Set Detector ID FAIL\n")); + return; + } else { + uint32_t value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); + value &= UDP_HEADER_X_MSK; // to keep previous x value + Beb_Write32( + csp0base, UDP_HEADER_A_LEFT_OFST, + value | ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); + if ((value & UDP_HEADER_ID_MSK) != + ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)) { + LOG(logERROR, ("Set Detector ID FAIL\n")); + } + value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); + value &= UDP_HEADER_X_MSK; // to keep previous x value + Beb_Write32( + csp0base, UDP_HEADER_A_RIGHT_OFST, + value | ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); + if ((value & UDP_HEADER_ID_MSK) != + ((swapid << UDP_HEADER_ID_OFST) & UDP_HEADER_ID_MSK)) { + LOG(logERROR, ("Set Detector ID FAIL\n")); + } + Beb_close(fd, csp0base); + } + LOG(logINFO, ("Detector id %d set in UDP Header\n\n", detid)); +} + +int Beb_SetQuad(int value) { + if (value < 0) + return OK; + LOG(logINFO, ("Setting Quad to %d in Beb\n", value)); + Beb_quadEnable = (value == 0 ? 0 : 1); + return Beb_SetDetectorPosition(Beb_positions); +} + +int Beb_GetQuad() { return Beb_quadEnable; } + +int *Beb_GetDetectorPosition() { return Beb_positions; } + +int Beb_SetDetectorPosition(int pos[]) { + if (!Beb_activated) + return OK; + LOG(logINFO, ("Got Position values %d %d...\n", pos[0], pos[1])); + + // save positions + Beb_positions[0] = pos[0]; + Beb_positions[1] = pos[1]; + + // get left and right + int posLeft[2] = {pos[0], Beb_top ? pos[1] : pos[1] + 1}; + int posRight[2] = {pos[0], Beb_top ? pos[1] + 1 : pos[1]}; + + if (Beb_quadEnable) { + posRight[0] = 1; // right is next row + posRight[1] = 0; // right same first column + } + + int ret = FAIL; + // mapping new memory to read master top module configuration + u_int32_t *csp0base = 0; + // open file pointer + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Set Detector Position FAIL\n")); + return FAIL; + } else { + uint32_t value = 0; + ret = OK; + // x left + int posval = Beb_swap_uint16(posLeft[0]); + value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); + value &= UDP_HEADER_ID_MSK; // to keep previous id value + Beb_Write32(csp0base, UDP_HEADER_A_LEFT_OFST, + value | + ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_A_LEFT_OFST); + if ((value & UDP_HEADER_X_MSK) != + ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)) { + LOG(logERROR, ("Could not set row position for left port\n")); + ret = FAIL; + } + // x right + posval = Beb_swap_uint16(posRight[0]); + value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); + value &= UDP_HEADER_ID_MSK; // to keep previous id value + Beb_Write32(csp0base, UDP_HEADER_A_RIGHT_OFST, + value | + ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_A_RIGHT_OFST); + if ((value & UDP_HEADER_X_MSK) != + ((posval << UDP_HEADER_X_OFST) & UDP_HEADER_X_MSK)) { + LOG(logERROR, ("Could not set row position for right port\n")); + ret = FAIL; + } + + // y left (column) + posval = Beb_swap_uint16(posLeft[1]); + value = Beb_Read32(csp0base, UDP_HEADER_B_LEFT_OFST); + value &= UDP_HEADER_Z_MSK; // to keep previous z value + Beb_Write32(csp0base, UDP_HEADER_B_LEFT_OFST, + value | + ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_B_LEFT_OFST); + if ((value & UDP_HEADER_Y_MSK) != + ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)) { + LOG(logERROR, + ("Could not set column position for left port\n")); + ret = FAIL; + } + + // y right + posval = Beb_swap_uint16(posRight[1]); + value = Beb_Read32(csp0base, UDP_HEADER_B_RIGHT_OFST); + value &= UDP_HEADER_Z_MSK; // to keep previous z value + Beb_Write32(csp0base, UDP_HEADER_B_RIGHT_OFST, + value | + ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)); + value = Beb_Read32(csp0base, UDP_HEADER_B_RIGHT_OFST); + if ((value & UDP_HEADER_Y_MSK) != + ((posval << UDP_HEADER_Y_OFST) & UDP_HEADER_Y_MSK)) { + LOG(logERROR, + ("Could not set column position for right port\n")); + ret = FAIL; + } + + // close file pointer + Beb_close(fd, csp0base); + } + if (ret == OK) { + LOG(logINFO, ("Position set to...\n" + "\tLeft: [%d, %d]\n" + "\tRight:[%d, %d]\n", + posLeft[0], posLeft[1], posRight[0], posRight[1])); + } + + return ret; +} + +int Beb_SetStartingFrameNumber(uint64_t value) { + if (!Beb_activated) { + Beb_deactivatedStartFrameNumber = value; + return OK; + } + LOG(logINFO, ("Setting start frame number: %llu\n", + (long long unsigned int)value)); + + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_PLB_GPIO_TEST_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Set Start Frame Number FAIL\n")); + return FAIL; + } + // since the read is not implemented in firmware yet + Beb_deactivatedStartFrameNumber = value; + + // decrement for firmware + uint64_t valueInFirmware = value - 1; + Beb_Write32(csp0base, UDP_HEADER_FRAME_NUMBER_LSB_OFST, + valueInFirmware & (0xffffffff)); + Beb_Write32(csp0base, UDP_HEADER_FRAME_NUMBER_MSB_OFST, + (valueInFirmware >> 32) & (0xffffffff)); + Beb_close(fd, csp0base); + + LOG(logINFO, ("Going to reset Frame Number\n")); + Beb_ResetFrameNumber(); + return OK; +} + +int Beb_GetStartingFrameNumber(uint64_t * retval, int tengigaEnable) { + if (!Beb_activated) { + *retval = Beb_deactivatedStartFrameNumber; return OK; } - int Beb_GetStartingFrameNumber(uint64_t * retval, int tengigaEnable) { - if (!Beb_activated) { - *retval = Beb_deactivatedStartFrameNumber; - return OK; - } - - LOG(logDEBUG1, ("Getting start frame number\n")); - u_int32_t *csp0base = 0; - int fd = Beb_open(&csp0base, XPAR_COUNTER_BASEADDR); - if (fd < 0) { - LOG(logERROR, ("Get Start Frame Number FAIL\n")); - return FAIL; - } - - uint32_t temp = 0; - if (!tengigaEnable) { - uint64_t left1g = - Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); - temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); - left1g = ((left1g << 32) | temp) >> 16; - ++left1g; // increment for firmware - - uint64_t right1g = - Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); - temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); - right1g = ((right1g << 32) | temp) >> 16; - ++right1g; // increment for firmware - - Beb_close(fd, csp0base); - if (left1g != right1g) { - LOG(logERROR, - ("Retrieved inconsistent frame numbers from 1g left " - "%llu and right %llu\n", - (long long int)left1g, (long long int)right1g)); - *retval = (left1g > right1g) - ? left1g - : right1g; // give max to set it to when stopping - // acq & different value - return -2; // to differentiate between failed address mapping - } - *retval = left1g; - } - - else { - uint64_t left10g = - Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); - temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); - left10g = ((left10g << 32) | temp) >> 16; - ++left10g; // increment for firmware - - uint64_t right10g = - Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); - temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); - right10g = ((right10g << 32) | temp) >> 16; - Beb_close(fd, csp0base); - ++right10g; // increment for firmware - - if (left10g != right10g) { - LOG(logERROR, - ("Retrieved inconsistent frame numbers from `0g left " - "%llu and right %llu\n", - (long long int)left10g, (long long int)right10g)); - *retval = (left10g > right10g) - ? left10g - : right10g; // give max to set it to when stopping - // acq & different value - return -2; // to differentiate between failed address mapping - } - *retval = left10g; - } - return OK; + LOG(logDEBUG1, ("Getting start frame number\n")); + u_int32_t *csp0base = 0; + int fd = Beb_open(&csp0base, XPAR_COUNTER_BASEADDR); + if (fd < 0) { + LOG(logERROR, ("Get Start Frame Number FAIL\n")); + return FAIL; } - void Beb_SetReadNLines(int value) { Beb_readNLines = value; } + uint32_t temp = 0; + if (!tengigaEnable) { + uint64_t left1g = + Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); + temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); + left1g = ((left1g << 32) | temp) >> 16; + ++left1g; // increment for firmware - uint16_t Beb_swap_uint16(uint16_t val) { return (val << 8) | (val >> 8); } + uint64_t right1g = + Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); + temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); + right1g = ((right1g << 32) | temp) >> 16; + ++right1g; // increment for firmware - int Beb_open(u_int32_t * *csp0base, u_int32_t offset) { - - int fd = open("/dev/mem", O_RDWR | O_SYNC, 0); - if (fd == -1) { - LOG(logERROR, ("\nCan't find /dev/mem!\n")); - } else { - LOG(logDEBUG1, ("/dev/mem opened\n")); - *csp0base = - (u_int32_t *)mmap(0, BEB_MMAP_SIZE, PROT_READ | PROT_WRITE, - MAP_FILE | MAP_SHARED, fd, offset); - if (*csp0base == MAP_FAILED) { - LOG(logERROR, ("\nCan't map memmory area!!\n")); - fd = -1; - } else - LOG(logDEBUG1, ("CSP0 mapped %p\n", (void *)*csp0base)); + Beb_close(fd, csp0base); + if (left1g != right1g) { + LOG(logERROR, + ("Retrieved inconsistent frame numbers from 1g left " + "%llu and right %llu\n", + (long long int)left1g, (long long int)right1g)); + *retval = (left1g > right1g) + ? left1g + : right1g; // give max to set it to when stopping + // acq & different value + return -2; // to differentiate between failed address mapping } - return fd; + *retval = left1g; } - u_int32_t Beb_Read32(u_int32_t * baseaddr, u_int32_t offset) { - volatile u_int32_t value; - value = *(u_int32_t *)(baseaddr + offset / (sizeof(u_int32_t))); - return value; - } + else { + uint64_t left10g = + Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); + temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); + left10g = ((left10g << 32) | temp) >> 16; + ++left10g; // increment for firmware - u_int32_t Beb_Write32(u_int32_t * baseaddr, u_int32_t offset, - u_int32_t data) { - volatile u_int32_t *ptr1; - ptr1 = (u_int32_t *)(baseaddr + offset / (sizeof(u_int32_t))); - *ptr1 = data; - return *ptr1; - } + uint64_t right10g = + Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_MSB_OFST); + temp = Beb_Read32(csp0base, UDP_HEADER_GET_FNUM_1G_LEFT_LSB_OFST); + right10g = ((right10g << 32) | temp) >> 16; + Beb_close(fd, csp0base); + ++right10g; // increment for firmware - void Beb_close(int fd, u_int32_t *csp0base) { - if (fd >= 0) - close(fd); - munmap(csp0base, BEB_MMAP_SIZE); + if (left10g != right10g) { + LOG(logERROR, + ("Retrieved inconsistent frame numbers from `0g left " + "%llu and right %llu\n", + (long long int)left10g, (long long int)right10g)); + *retval = (left10g > right10g) + ? left10g + : right10g; // give max to set it to when stopping + // acq & different value + return -2; // to differentiate between failed address mapping + } + *retval = left10g; } + return OK; +} + +void Beb_SetReadNLines(int value) { Beb_readNLines = value; } + +uint16_t Beb_swap_uint16(uint16_t val) { return (val << 8) | (val >> 8); } + +int Beb_open(u_int32_t * *csp0base, u_int32_t offset) { + + int fd = open("/dev/mem", O_RDWR | O_SYNC, 0); + if (fd == -1) { + LOG(logERROR, ("\nCan't find /dev/mem!\n")); + } else { + LOG(logDEBUG1, ("/dev/mem opened\n")); + *csp0base = + (u_int32_t *)mmap(0, BEB_MMAP_SIZE, PROT_READ | PROT_WRITE, + MAP_FILE | MAP_SHARED, fd, offset); + if (*csp0base == MAP_FAILED) { + LOG(logERROR, ("\nCan't map memmory area!!\n")); + fd = -1; + } else + LOG(logDEBUG1, ("CSP0 mapped %p\n", (void *)*csp0base)); + } + return fd; +} + +u_int32_t Beb_Read32(u_int32_t * baseaddr, u_int32_t offset) { + volatile u_int32_t value; + value = *(u_int32_t *)(baseaddr + offset / (sizeof(u_int32_t))); + return value; +} + +u_int32_t Beb_Write32(u_int32_t * baseaddr, u_int32_t offset, + u_int32_t data) { + volatile u_int32_t *ptr1; + ptr1 = (u_int32_t *)(baseaddr + offset / (sizeof(u_int32_t))); + *ptr1 = data; + return *ptr1; +} + +void Beb_close(int fd, u_int32_t *csp0base) { + if (fd >= 0) + close(fd); + munmap(csp0base, BEB_MMAP_SIZE); +} diff --git a/slsDetectorServers/eigerDetectorServer/Beb.h b/slsDetectorServers/eigerDetectorServer/Beb.h index 6b58694ae..0a2e97e52 100644 --- a/slsDetectorServers/eigerDetectorServer/Beb.h +++ b/slsDetectorServers/eigerDetectorServer/Beb.h @@ -36,6 +36,7 @@ unsigned int Beb_GetBebInfoIndex(unsigned int beb_numb); void Beb_GetModuleConfiguration(int *master, int *top, int *normal); int Beb_IsTransmitting(int *retval, int tengiga, int waitForDelay); +void Beb_SetTopVariable(int val); int Beb_SetTop(enum TOPINDEX ind); int Beb_SetMaster(enum MASTERINDEX ind); int Beb_Activate(int enable); diff --git a/slsDetectorServers/eigerDetectorServer/FebControl.c b/slsDetectorServers/eigerDetectorServer/FebControl.c index 0402a0734..f8f1bcd98 100644 --- a/slsDetectorServers/eigerDetectorServer/FebControl.c +++ b/slsDetectorServers/eigerDetectorServer/FebControl.c @@ -2463,10 +2463,10 @@ 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[0]); + addr[0] = Module_GetTopLeftAddress(&modules[1]); } if (right) { - addr[1] = Module_GetTopRightAddress(&modules[0]); + addr[1] = Module_GetTopRightAddress(&modules[1]); } char *top_names[] = {TOP_NAMES}; int i = 0; @@ -2497,8 +2497,7 @@ int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right) { 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", val, - top_names[ind], (i == 0 ? "left" : "right"))); + LOG(logERROR, ("Could not set Top flag to %s in %s Feb\n", top_names[ind], (i == 0 ? "left" : "right"))); return 0; } } @@ -2510,11 +2509,13 @@ int Feb_Control_SetTop(enum TOPINDEX ind, int left, int right) { 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[0]); - addr[1] = Module_GetTopRightAddress(&modules[0]); + addr[0] = Module_GetTopLeftAddress(&modules[1]); + addr[1] = Module_GetTopRightAddress(&modules[1]); char *master_names[] = {MASTER_NAMES}; int i = 0; for (i = 0; i < 2; ++i) { @@ -2538,12 +2539,11 @@ int Feb_Control_SetMaster(enum MASTERINDEX ind) { break; default: LOG(logERROR, ("Unknown master index in Feb: %d\n", ind)); - Beb_close(fd, csp0base); 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", val, + LOG(logERROR, ("Could not set Master flag to %s in %s Feb\n", master_names[ind], (i == 0 ? "left" : "right"))); return 0; } @@ -2560,7 +2560,8 @@ int Feb_Control_SetQuad(int val) { return 1; } LOG(logINFO, ("Setting Quad to %d in Feb\n", val)); - return Feb_Control_SetTop(val, 0, 1); + // 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) { @@ -2644,6 +2645,7 @@ int Feb_Control_ReadRegister(uint32_t offset, uint32_t *retval) { 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}; diff --git a/slsDetectorServers/eigerDetectorServer/FebControl.h b/slsDetectorServers/eigerDetectorServer/FebControl.h index 7aa057cce..7968e97c6 100644 --- a/slsDetectorServers/eigerDetectorServer/FebControl.h +++ b/slsDetectorServers/eigerDetectorServer/FebControl.h @@ -1,5 +1,6 @@ #pragma once #include "FebInterface.h" +#include "slsDetectorServer_defs.h" #include struct Module { @@ -176,6 +177,7 @@ 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); diff --git a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer index c856bbc73..4942984d2 100755 Binary files a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer and b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer differ diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index e5f0375f1..b640b84e0 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -226,6 +226,20 @@ u_int64_t getFirmwareAPIVersion() { #endif } +void readDetectorNumber() { +#ifdef VIRTUAL + return; +#endif + char output[255]; + FILE *sysFile = popen(IDFILECOMMAND, "r"); + fgets(output, sizeof(output), sysFile); + pclose(sysFile); + sscanf(output, "%u", &detid); + if (isControlServer) { + LOG(logINFOBLUE, ("Detector ID: %u\n", detid)); + } +} + u_int32_t getDetectorNumber() { #ifdef VIRTUAL return 0; @@ -309,97 +323,67 @@ u_int32_t getDetectorIP() { /* initialization */ void initControlServer() { - master = -1; - top = -1; - - // force top or master if in config file - if (readConfigFile() == FAIL) { - return; - } - -#ifdef VIRTUAL + LOG(logINFOBLUE, ("Configuring Control server\n")); if (initError == OK) { + readDetectorNumber(); getModuleConfiguration(); - setupDetector(); - } - eiger_virtual_activate = 0; - LOG(logINFORED, ("Deactivated!\n")); - initCheckDone = 1; - return; -#else - if (initError == OK) { - // Feb and Beb Initializations - getModuleConfiguration(); +#ifndef VIRTUAL + Feb_Control_SetMasterVariable(master); Feb_Interface_FebInterface(); Feb_Control_FebControl(); - // different addresses for top and bottom - if (getFirmwareVersion() < FIRMWARE_VERSION_SAME_TOP_BOT_ADDR) { - Feb_Control_Init(master, top, normal, getDetectorNumber()); - } // same addresses for top and bottom - else { - Feb_Control_Init(master, 1, normal, getDetectorNumber()); - } + Feb_Control_Init(master, 1, normal, getDetectorNumber()); // master of 9M, check high voltage serial communication to blackfin if (master && !normal) { if (Feb_Control_OpenSerialCommunication()) ; // Feb_Control_CloseSerialCommunication(); } LOG(logDEBUG1, ("Control server: FEB Initialization done\n")); + Beb_SetTopVariable(top); Beb_Beb(detid); Beb_SetDetectorNumber(getDetectorNumber()); LOG(logDEBUG1, ("Control server: BEB Initialization done\n")); - +#endif + // also reads config file and deactivates setupDetector(); - // client first connect (from shm) will activate - Beb_Activate(0); - Feb_Control_activate(0); } initCheckDone = 1; -#endif } void initStopServer() { - master = -1; - top = -1; - #ifdef VIRTUAL - // force top or master if in config file - if (readConfigFile() == FAIL) { - return; - } - + LOG(logINFOBLUE, ("Configuring Stop server\n")); getModuleConfiguration(); virtual_stop = 0; if (!isControlServer) { ComVirtual_setStop(virtual_stop); } - eiger_virtual_activate = 0; - LOG(logINFORED, ("Deactivated!\n")); - return; + // get top/master in virtual + readConfigFile(); #else - // wait till control server has configured top/master - usleep(2 * 1000 * 1000); - + // wait a few s (control server is setting top/master from config file) + usleep(WAIT_STOP_SERVER_START); + LOG(logINFOBLUE, ("Configuring Stop server\n")); + //exit(-1); + readDetectorNumber(); getModuleConfiguration(); + Feb_Control_SetMasterVariable(master); Feb_Interface_FebInterface(); Feb_Control_FebControl(); - // different addresses for top and bottom - if (getFirmwareVersion() < FIRMWARE_VERSION_SAME_TOP_BOT_ADDR) { - Feb_Control_Init(master, top, normal, getDetectorNumber()); - } // same addresses for top and bottom - else { - Feb_Control_Init(master, 1, normal, getDetectorNumber()); - } + Feb_Control_Init(master, 1, normal, getDetectorNumber()); LOG(logDEBUG1, ("Stop server: FEB Initialization done\n")); - // client first connect (from shm) will activate - Beb_Activate(0); - Feb_Control_activate(0); #endif + // client first connect (from shm) will activate + if (setActivate(0) == FAIL) { + LOG(logERROR, ("Could not deactivate in stop server\n")); + } } void getModuleConfiguration() { + if (initError == FAIL) { + return; + } #ifdef VIRTUAL // if master not modified by config file if (master == -1) { @@ -429,16 +413,6 @@ void getModuleConfiguration() { #endif #else - // read detector id - char output[255]; - FILE *sysFile = popen(IDFILECOMMAND, "r"); - fgets(output, sizeof(output), sysFile); - pclose(sysFile); - sscanf(output, "%u", &detid); - if (isControlServer) { - LOG(logINFOBLUE, ("Detector ID: %u\n\n", detid)); - } - Beb_GetModuleConfiguration(&master, &top, &normal); #endif if (isControlServer) { @@ -453,12 +427,14 @@ int readConfigFile() { if (initError == FAIL) { return initError; } - + master = -1; + top = -1; FILE *fd = fopen(CONFIG_FILE, "r"); if (fd == NULL) { + LOG(logINFO, ("No config file found. Resetting to hardware settings (Top/Master)\n")); // reset to hardware settings if not in config file (if overwritten) resetToHardwareSettings(); - return OK; + return initError; } LOG(logINFO, ("Reading config file %s\n", CONFIG_FILE)); @@ -470,7 +446,6 @@ int readConfigFile() { // keep reading a line while (fgets(line, LZ, fd)) { - // ignore comments if (line[0] == '#') { LOG(logDEBUG1, ("Ignoring Comment\n")); @@ -521,6 +496,15 @@ int readConfigFile() { top, line); break; } + // validate change + int actual_top = -1, temp = -1, temp2 = -1; + Beb_GetModuleConfiguration(&temp, &actual_top, &temp2); + if (actual_top != top) { + sprintf(initErrorMessage, + "Could not set top to %d. Read %d\n", top, actual_top); + break; + } + Beb_SetTopVariable(top); #endif } @@ -552,6 +536,15 @@ int readConfigFile() { master, line); break; } + // validate change + int actual_master = -1, temp = -1, temp2 = -1; + Beb_GetModuleConfiguration(&actual_master, &temp, &temp2); + if (actual_master != master) { + sprintf(initErrorMessage, + "Could not set master to %d. Read %d\n", master, actual_master); + break; + } + Feb_Control_SetMasterVariable(master); #endif } @@ -566,51 +559,66 @@ int readConfigFile() { } fclose(fd); - // reset to hardware settings if not in config file (if overwritten) - resetToHardwareSettings(); - if (strlen(initErrorMessage)) { initError = FAIL; LOG(logERROR, ("%s\n\n", initErrorMessage)); } else { LOG(logINFO, ("Successfully read config file\n")); } + + // reset to hardware settings if not in config file (if overwritten) + resetToHardwareSettings(); + return initError; } -int resetToHardwareSettings() { +void resetToHardwareSettings() { #ifndef VIRTUAL + if (initError == FAIL) { + return; + } + // top not set in config file if (top == -1) { if (!Beb_SetTop(TOP_HARDWARE)) { initError = FAIL; - sprintf(initErrorMessage, + strcpy(initErrorMessage, "Could not reset Top flag to Beb hardware settings.\n"); + LOG(logERROR, ("%s\n\n", initErrorMessage)); return; } if (!Feb_Control_SetTop(TOP_HARDWARE, 1, 1)) { initError = FAIL; - sprintf(initErrorMessage, + strcpy(initErrorMessage, "Could not reset Top flag to Feb hardware settings.\n"); + LOG(logERROR, ("%s\n\n", initErrorMessage)); return; } + getModuleConfiguration(); + Beb_SetTopVariable(top); } + // master not set in config file if (master == -1) { if (!Beb_SetMaster(TOP_HARDWARE)) { initError = FAIL; - sprintf(initErrorMessage, + strcpy(initErrorMessage, "Could not reset Master flag to Beb hardware settings.\n"); + LOG(logERROR, ("%s\n\n", initErrorMessage)); return; } if (!Feb_Control_SetMaster(TOP_HARDWARE)) { initError = FAIL; - sprintf(initErrorMessage, + strcpy(initErrorMessage, "Could not reset Master flag to Feb hardware settings.\n"); + LOG(logERROR, ("%s\n\n", initErrorMessage)); return; } + getModuleConfiguration(); + Feb_Control_SetMasterVariable(master); } #endif } + /* set up detector */ void allocateDetectorStructureMemory() { @@ -696,6 +704,17 @@ void setupDetector() { #ifndef VIRTUAL Feb_Control_CheckSetup(); #endif + // force top or master if in config file + if (readConfigFile() == FAIL) { + return; + } + // client first connect (from shm) will activate + if (setActivate(0) == FAIL) { + initError = FAIL; + sprintf(initErrorMessage, "Could not deactivate\n"); + LOG(logERROR, (initErrorMessage)); + } + LOG(logDEBUG1, ("Setup detector done\n\n")); } @@ -1860,21 +1879,37 @@ int getBebFPGATemp() { #endif } -int activate(int enable) { -#ifdef VIRTUAL - if (enable >= 0) - eiger_virtual_activate = enable; - if (eiger_virtual_activate == 0) { - LOG(logINFORED, ("Deactivated!\n")); - } else { - LOG(logINFOGREEN, ("Activated!\n")); +int setActivate(int enable) { + if (enable < 0) { + LOG(logERROR, ("Invalid activate argument: %d\n", enable)); + return FAIL; } - return eiger_virtual_activate; + enable = enable == 0 ? 0 : 1; +#ifdef VIRTUAL + eiger_virtual_activate = enable; #else - int ret = Beb_Activate(enable); - Feb_Control_activate(ret); - return ret; + if (!Beb_SetActivate(enable)) { + return FAIL; + } + Feb_Control_activate(enable); #endif + if (enable == 0) { + LOG(logINFORED, ("Deactivated in %s Server!\n", isControlServer ? " Control" : "Stop")); + } else { + LOG(logINFOGREEN, ("Activated in %s Server!\n", isControlServer ? " Control" : "Stop")); + } + return OK; +} + +int getActivate(int* retval) { +#ifdef VIRTUAL + *retval = eiger_virtual_activate; +#else + if (!Beb_GetActivate(retval)) { + return FAIL; + } +#endif + return OK; } int getTenGigaFlowControl() { @@ -2310,7 +2345,7 @@ enum runStatus getRunStatus() { return ERROR; } if (isTransmitting) { - printf("Status: TRANSMITTING\n"); + LOG(logINFOBLUE, ("Status: TRANSMITTING\n")); return TRANSMITTING; } LOG(logINFOBLUE, ("Status: IDLE\n")); @@ -2360,7 +2395,7 @@ void readFrame(int *ret, char *mess) { printf("Transmitting...\n"); } } - printf("Detector has sent all data\n"); + LOG(logINFO, ("Detector has sent all data\n")); LOG(logINFOGREEN, ("Acquisition successfully finished\n")); #endif } diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/eigerDetectorServer/slsDetectorServer_defs.h index f39b73471..ffe9631c0 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorServer_defs.h @@ -4,7 +4,7 @@ #define REQUIRED_FIRMWARE_VERSION (26) #define IDFILECOMMAND "more /home/root/executables/detid.txt" #define CONFIG_FILE ("config.txt") -#define FIRMWARE_VERSION_SAME_TOP_BOT_ADDR (26) +#define WAIT_STOP_SERVER_START (1 * 1000 * 1000) #define STATUS_IDLE 0 #define STATUS_RUNNING 1 diff --git a/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h b/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h index 214879623..299268959 100644 --- a/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h +++ b/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h @@ -84,6 +84,9 @@ u_int16_t getHardwareSerialNumber(); #ifdef JUNGFRAUD int isHardwareVersion2(); #endif +#ifdef EIGERD +void readDetectorNumber(); +#endif u_int32_t getDetectorNumber(); u_int64_t getDetectorMAC(); u_int32_t getDetectorIP(); @@ -116,7 +119,7 @@ int setDefaultDacs(); int readConfigFile(); #endif #ifdef EIGERD -int resetToHardwareSettings(); +void resetToHardwareSettings(); #endif // advanced read/write reg @@ -435,7 +438,8 @@ void setExternalGating(int enable[]); int setAllTrimbits(int val); int getAllTrimbits(); int getBebFPGATemp(); -int activate(int enable); +int setActivate(int enable); +int getActivate(int* retval); // gotthard specific - adc phase #elif GOTTHARDD diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index fba68c070..13c4ce993 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -4119,9 +4119,23 @@ int set_activate(int file_des) { #else // set & get if ((arg == -1) || (Server_VerifyLock() == OK)) { - retval = activate(arg); - LOG(logDEBUG1, ("Activate: %d\n", retval)); - validate(arg, retval, "set activate", DEC); + if (arg >= 0) { + if (setActivate(arg) == FAIL) { + ret = FAIL; + sprintf(mess, "Could not %s\n", (arg == 0 ? "deactivate" : "activate")); + LOG(logERROR, (mess)); + } + } + if (ret == OK) { + if (getActivate(&retval) == FAIL) { + ret = FAIL; + sprintf(mess, "Could not get activate flag\n"); + LOG(logERROR, (mess)); + } else { + LOG(logDEBUG1, ("Activate: %d\n", retval)); + validate(arg, retval, "set/get activate", DEC); + } + } } #endif return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); @@ -7264,7 +7278,8 @@ int get_receiver_parameters(int file_des) { // activate #ifdef EIGERD - i32 = activate(-1); + i32 = 0; + getActivate(&i32); #else i32 = 0; #endif diff --git a/slsSupportLib/include/versionAPI.h b/slsSupportLib/include/versionAPI.h index f4d94ea03..9a6d6199a 100644 --- a/slsSupportLib/include/versionAPI.h +++ b/slsSupportLib/include/versionAPI.h @@ -10,4 +10,4 @@ #define APIJUNGFRAU 0x200508 #define APIMYTHEN3 0x200508 #define APIMOENCH 0x200508 -#define APIEIGER 0x200508 +#define APIEIGER 0x200511