Merge branch 'developer' of git.psi.ch:sls_detectors_software/sls_detector_software into developer

This commit is contained in:
2016-08-22 14:33:09 +02:00
8 changed files with 255 additions and 293 deletions

View File

@ -44,6 +44,7 @@
unsigned int* Beb_recv_data; unsigned int* Beb_recv_data;
short Beb_bit_mode; short Beb_bit_mode;
int BEB_MMAP_SIZE = 0x1000;
@ -149,15 +150,15 @@ void Beb_GetModuleCopnfiguration(int* master, int* top){
*top = 0; *top = 0;
*master = 0; *master = 0;
//mapping new memory to read master top module configuration //mapping new memory to read master top module configuration
u_int32_t baseaddr; u_int32_t* csp0base=0;
int ret; int ret;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0){ if(fd < 0){
cprintf(BG_RED,"Module Configuration FAIL\n"); cprintf(BG_RED,"Module Configuration FAIL\n");
}else{ }else{
//read data //read data
ret = Beb_Read32(baseaddr, MODULE_CONFIGURATION_MASK); ret = Beb_Read32(csp0base, MODULE_CONFIGURATION_MASK);
printf("Module Configuration OK\n"); printf("Module Configuration OK\n");
printf("Beb: value =0x%x\n",ret); printf("Beb: value =0x%x\n",ret);
if(ret&TOP_BIT_MASK) if(ret&TOP_BIT_MASK)
@ -165,24 +166,25 @@ void Beb_GetModuleCopnfiguration(int* master, int* top){
if(ret&MASTER_BIT_MASK) if(ret&MASTER_BIT_MASK)
*master = 1; *master = 1;
//close file pointer //close file pointer
Beb_close(fd); Beb_close(fd,csp0base);
} }
} }
/* do not work at the moment */ /* do not work at the moment */
int Beb_SetMasterViaSoftware(){ int Beb_SetMasterViaSoftware(){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0, ret = 1; u_int32_t* csp0base=0;
u_int32_t value = 0, ret = 1;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Set Master FAIL\n"); cprintf(BG_RED,"Set Master FAIL\n");
else{ else{
value = Beb_Read32(baseaddr, MASTERCONFIG_OFFSET); value = Beb_Read32(csp0base, MASTERCONFIG_OFFSET);
value|=MASTER_BIT; value|=MASTER_BIT;
value|=OVERWRITE_HARDWARE_BIT; value|=OVERWRITE_HARDWARE_BIT;
int newval = Beb_Write32(baseaddr, MASTERCONFIG_OFFSET,value); int newval = Beb_Write32(csp0base, MASTERCONFIG_OFFSET,value);
if(newval!=value) if(newval!=value)
cprintf(BG_RED,"Could not set Master via Software\n"); cprintf(BG_RED,"Could not set Master via Software\n");
else else
@ -191,7 +193,7 @@ int Beb_SetMasterViaSoftware(){
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return ret; return ret;
} }
@ -199,17 +201,18 @@ int Beb_SetMasterViaSoftware(){
/* do not work at the moment */ /* do not work at the moment */
int Beb_SetSlaveViaSoftware(){ int Beb_SetSlaveViaSoftware(){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0, ret = 1; u_int32_t* csp0base=0;
u_int32_t value = 0, ret = 1;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Set Slave FAIL\n"); cprintf(BG_RED,"Set Slave FAIL\n");
else{ else{
value = Beb_Read32(baseaddr, MASTERCONFIG_OFFSET); value = Beb_Read32(csp0base, MASTERCONFIG_OFFSET);
value&=~MASTER_BIT; value&=~MASTER_BIT;
value|=OVERWRITE_HARDWARE_BIT; value|=OVERWRITE_HARDWARE_BIT;
int newval = Beb_Write32(baseaddr, MASTERCONFIG_OFFSET,value); int newval = Beb_Write32(csp0base, MASTERCONFIG_OFFSET,value);
if(newval!=value) if(newval!=value)
cprintf(BG_RED,"Could not set Slave via Software\n"); cprintf(BG_RED,"Could not set Slave via Software\n");
else else
@ -218,29 +221,30 @@ int Beb_SetSlaveViaSoftware(){
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return ret; return ret;
} }
int Beb_Activate(int enable){ int Beb_Activate(int enable){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0, ret = -1; u_int32_t* csp0base=0;
u_int32_t value = 0, ret = -1;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Deactivate FAIL\n"); cprintf(BG_RED,"Deactivate FAIL\n");
else{ else{
if(enable > -1){ if(enable > -1){
value = Beb_Read32(baseaddr, MASTERCONFIG_OFFSET); value = Beb_Read32(csp0base, MASTERCONFIG_OFFSET);
printf("Deactivate register value before:%d\n",value); printf("Deactivate register value before:%d\n",value);
if(enable) if(enable)
value&=~DEACTIVATE_BIT; value&=~DEACTIVATE_BIT;
else else
value|=DEACTIVATE_BIT; value|=DEACTIVATE_BIT;
int newval = Beb_Write32(baseaddr, MASTERCONFIG_OFFSET,value); int newval = Beb_Write32(csp0base, MASTERCONFIG_OFFSET,value);
if(newval!=value){ if(newval!=value){
if(enable) if(enable)
cprintf(BG_RED,"Could not activate via Software\n"); cprintf(BG_RED,"Could not activate via Software\n");
@ -249,7 +253,7 @@ int Beb_Activate(int enable){
} }
} }
value = Beb_Read32(baseaddr, MASTERCONFIG_OFFSET); value = Beb_Read32(csp0base, MASTERCONFIG_OFFSET);
if(value&DEACTIVATE_BIT) ret = 0; if(value&DEACTIVATE_BIT) ret = 0;
else ret = 1; else ret = 1;
if(enable == -1){ if(enable == -1){
@ -262,7 +266,7 @@ int Beb_Activate(int enable){
} }
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return ret; return ret;
} }
@ -270,7 +274,8 @@ int Beb_Activate(int enable){
int Beb_SetNetworkParameter(enum detNetworkParameter mode, int val){ int Beb_SetNetworkParameter(enum detNetworkParameter mode, int val){
//mapping new memory //mapping new memory
u_int32_t baseaddr, valueread = 0; u_int32_t* csp0base=0;
u_int32_t valueread = 0;
u_int32_t offset = TXM_DELAY_LEFT_OFFSET; u_int32_t offset = TXM_DELAY_LEFT_OFFSET;
char modename[100] = ""; char modename[100] = "";
@ -295,25 +300,25 @@ int Beb_SetNetworkParameter(enum detNetworkParameter mode, int val){
default: cprintf(BG_RED,"Unrecognized mode in network parameter: %d\n",mode); return -1; default: cprintf(BG_RED,"Unrecognized mode in network parameter: %d\n",mode); return -1;
} }
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0){ if(fd < 0){
cprintf(BG_RED,"Could not read register to set network parameter. FAIL\n"); cprintf(BG_RED,"Could not read register to set network parameter. FAIL\n");
return -1; return -1;
} }
else{ else{
if(val > -1){ if(val > -1){
valueread = Beb_Read32(baseaddr, offset); valueread = Beb_Read32(csp0base, offset);
//cprintf(BLUE, "%s value before:%d\n",modename,valueread); //cprintf(BLUE, "%s value before:%d\n",modename,valueread);
Beb_Write32(baseaddr, offset,val); Beb_Write32(csp0base, offset,val);
cprintf(BLUE,"%s value:%d\n", modename,valueread); cprintf(BLUE,"%s value:%d\n", modename,valueread);
} }
valueread = Beb_Read32(baseaddr, offset); valueread = Beb_Read32(csp0base, offset);
//cprintf(BLUE,"%s value:%d\n", modename,valueread); //cprintf(BLUE,"%s value:%d\n", modename,valueread);
} }
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return valueread; return valueread;
} }
@ -321,14 +326,15 @@ int Beb_SetNetworkParameter(enum detNetworkParameter mode, int val){
int Beb_ResetToHardwareSettings(){ int Beb_ResetToHardwareSettings(){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0, ret = 1; u_int32_t* csp0base=0;
u_int32_t value = 0, ret = 1;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Reset to Hardware Settings FAIL\n"); cprintf(BG_RED,"Reset to Hardware Settings FAIL\n");
else{ else{
value = Beb_Write32(baseaddr, MASTERCONFIG_OFFSET,0); value = Beb_Write32(csp0base, MASTERCONFIG_OFFSET,0);
if(value) if(value)
cprintf(BG_RED,"Could not reset to hardware settings\n"); cprintf(BG_RED,"Could not reset to hardware settings\n");
else else
@ -337,7 +343,7 @@ int Beb_ResetToHardwareSettings(){
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return ret; return ret;
} }
@ -346,21 +352,22 @@ int Beb_ResetToHardwareSettings(){
u_int32_t Beb_GetFirmwareRevision(){ u_int32_t Beb_GetFirmwareRevision(){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0; u_int32_t* csp0base=0;
u_int32_t value = 0;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_VERSION,&baseaddr); int fd = Beb_open(&csp0base,XPAR_VERSION);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Firmware Revision Read FAIL\n"); cprintf(BG_RED,"Firmware Revision Read FAIL\n");
else{ else{
value = Beb_Read32(baseaddr, FIRMWARE_VERSION_OFFSET); value = Beb_Read32(csp0base, FIRMWARE_VERSION_OFFSET);
if(!value) if(!value)
cprintf(BG_RED,"Firmware Revision Number does not exist in this version\n"); cprintf(BG_RED,"Firmware Revision Number does not exist in this version\n");
} }
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return value; return value;
} }
@ -368,41 +375,42 @@ u_int32_t Beb_GetFirmwareRevision(){
u_int32_t Beb_GetFirmwareSoftwareAPIVersion(){ u_int32_t Beb_GetFirmwareSoftwareAPIVersion(){
//mapping new memory //mapping new memory
u_int32_t baseaddr, value = 0; u_int32_t* csp0base=0;
u_int32_t value = 0;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_VERSION,&baseaddr); int fd = Beb_open(&csp0base,XPAR_VERSION);
if(fd < 0) if(fd < 0)
cprintf(BG_RED,"Firmware Software API Version Read FAIL\n"); cprintf(BG_RED,"Firmware Software API Version Read FAIL\n");
else{ else{
value = Beb_Read32(baseaddr, FIRMWARESOFTWARE_API_OFFSET); value = Beb_Read32(csp0base, FIRMWARESOFTWARE_API_OFFSET);
if(!value) if(!value)
cprintf(BG_RED,"Firmware Software API Version does not exist in this version\n"); cprintf(BG_RED,"Firmware Software API Version does not exist in this version\n");
} }
//close file pointer //close file pointer
if(fd > 0) if(fd > 0)
Beb_close(fd); Beb_close(fd,csp0base);
return value; return value;
} }
void Beb_ResetFrameNumber(){ void Beb_ResetFrameNumber(){
//mapping new memory to read master top module configuration //mapping new memory to read master top module configuration
u_int32_t baseaddr; u_int32_t* csp0base=0;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_PLB_GPIO_SYS_BASEADDR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_PLB_GPIO_SYS_BASEADDR);
if(fd < 0){ if(fd < 0){
cprintf(BG_RED,"Reset Frame Number FAIL\n"); cprintf(BG_RED,"Reset Frame Number FAIL\n");
}else{ }else{
//write a 1 //write a 1
Beb_Write32(baseaddr, FRAME_NUM_RESET_OFFSET, 1); Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 1);
usleep(100000); //100ms usleep(100000); //100ms
//write a 0 //write a 0
Beb_Write32(baseaddr, FRAME_NUM_RESET_OFFSET, 0); Beb_Write32(csp0base, FRAME_NUM_RESET_OFFSET, 0);
printf("Frame Number Reset OK\n"); printf("Frame Number Reset OK\n");
//close file pointer //close file pointer
Beb_close(fd); Beb_close(fd,csp0base);
} }
} }
@ -583,8 +591,8 @@ int Beb_SetByteOrder(){
int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int header_number, char* dst_mac, char* dst_ip, unsigned int dst_port){ int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int header_number, char* dst_mac, char* dst_ip, unsigned int dst_port){
u_int32_t bram_phy_addr; u_int32_t bram_phy_addr;
volatile u_int32_t* bram_ptr; u_int32_t* csp0base=0;
bram_ptr = NULL; /*u_int32_t* bram_ptr = NULL;*/
if (ten_gig) if (ten_gig)
bram_phy_addr = 0xC6002000; bram_phy_addr = 0xC6002000;
else else
@ -592,25 +600,17 @@ int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int header
if(!Beb_SetHeaderData(beb_number,ten_gig,dst_mac,dst_ip,dst_port)) return 0; if(!Beb_SetHeaderData(beb_number,ten_gig,dst_mac,dst_ip,dst_port)) return 0;
int fd = open("/dev/mem", O_RDWR | O_SYNC, 0);
if (fd == -1)
cprintf(BG_RED,"\nCan't find /dev/mem!\n");
else{
#ifdef VERBOSE
printf("/dev/mem opened\n");
#endif
bram_ptr = mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, bram_phy_addr);
if (bram_ptr == MAP_FAILED) {
cprintf(BG_RED,"\nCan't map memmory area!!\n");
fd = -1;
}
#ifdef VERBOSE
else printf("CSP0 mapped\n");
#endif
}
memcpy(bram_ptr+header_number*16, &udp_header, sizeof(udp_header));
close(fd); int fd = Beb_open(&csp0base,bram_phy_addr);
if(fd < 0){
cprintf(BG_RED,"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; return 1;
} }
@ -814,27 +814,27 @@ int Beb_SetUpTransferParameters(short the_bit_mode){
int Beb_StopAcquisition() int Beb_StopAcquisition()
{ {
u_int32_t baseaddr; u_int32_t* csp0base=0;
volatile u_int32_t valuel,valuer; volatile u_int32_t valuel,valuer;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_CMD_GENERATOR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_CMD_GENERATOR);
if(fd < 0){ if(fd < 0){
cprintf(BG_RED,"Beb Stop Acquisition FAIL\n"); cprintf(BG_RED,"Beb Stop Acquisition FAIL\n");
return 0; return 0;
}else{ }else{
//find value //find value
valuel = Beb_Read32(baseaddr, (LEFT_OFFSET+STOP_ACQ_OFFSET)); valuel = Beb_Read32(csp0base, (LEFT_OFFSET+STOP_ACQ_OFFSET));
valuer = Beb_Read32(baseaddr, (RIGHT_OFFSET+STOP_ACQ_OFFSET)); valuer = Beb_Read32(csp0base, (RIGHT_OFFSET+STOP_ACQ_OFFSET));
//high //high
Beb_Write32(baseaddr, (LEFT_OFFSET + STOP_ACQ_OFFSET),(valuel|STOP_ACQ_BIT)); Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET),(valuel|STOP_ACQ_BIT));
Beb_Write32(baseaddr, (RIGHT_OFFSET + STOP_ACQ_OFFSET),(valuer|STOP_ACQ_BIT)); Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET),(valuer|STOP_ACQ_BIT));
//low //low
Beb_Write32(baseaddr, (LEFT_OFFSET + STOP_ACQ_OFFSET),(valuel&(~STOP_ACQ_BIT))); Beb_Write32(csp0base, (LEFT_OFFSET + STOP_ACQ_OFFSET),(valuel&(~STOP_ACQ_BIT)));
Beb_Write32(baseaddr, (RIGHT_OFFSET + STOP_ACQ_OFFSET),(valuer&(~STOP_ACQ_BIT))); Beb_Write32(csp0base, (RIGHT_OFFSET + STOP_ACQ_OFFSET),(valuer&(~STOP_ACQ_BIT)));
printf("Beb Stop Acquisition OK\n"); printf("Beb Stop Acquisition OK\n");
//close file pointer //close file pointer
Beb_close(fd); Beb_close(fd,csp0base);
} }
return 1; return 1;
} }
@ -889,10 +889,10 @@ int Beb_RequestNImages(unsigned int beb_number, int ten_gig, unsigned int dst_nu
u_int32_t right_port_value = 0x2000; u_int32_t right_port_value = 0x2000;
u_int32_t baseaddr; u_int32_t* csp0base=0;
volatile u_int32_t value; volatile u_int32_t value;
//open file pointer //open file pointer
int fd = Beb_open(XPAR_CMD_GENERATOR,&baseaddr); int fd = Beb_open(&csp0base,XPAR_CMD_GENERATOR);
if(fd < 0){ if(fd < 0){
cprintf(BG_RED,"Beb Request N Images FAIL\n"); cprintf(BG_RED,"Beb Request N Images FAIL\n");
return 0; return 0;
@ -915,27 +915,27 @@ int Beb_RequestNImages(unsigned int beb_number, int ten_gig, unsigned int dst_nu
//"0x20 << 8" is dst_number (0x00 for left, 0x20 for right) //"0x20 << 8" is dst_number (0x00 for left, 0x20 for right)
//Left //Left
Beb_Write32(baseaddr, (LEFT_OFFSET + FIRST_CMD_PART1_OFFSET),0); Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART1_OFFSET),0);
Beb_Write32(baseaddr, (LEFT_OFFSET + FIRST_CMD_PART2_OFFSET),send_header_command); Beb_Write32(csp0base, (LEFT_OFFSET + FIRST_CMD_PART2_OFFSET),send_header_command);
Beb_Write32(baseaddr, (LEFT_OFFSET + SECOND_CMD_PART1_OFFSET),0); Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART1_OFFSET),0);
Beb_Write32(baseaddr, (LEFT_OFFSET + SECOND_CMD_PART2_OFFSET),send_frame_command); Beb_Write32(csp0base, (LEFT_OFFSET + SECOND_CMD_PART2_OFFSET),send_frame_command);
value = Beb_Read32(baseaddr,(LEFT_OFFSET + TWO_REQUESTS_OFFSET)); value = Beb_Read32(csp0base,(LEFT_OFFSET + TWO_REQUESTS_OFFSET));
if(in_two_requests) Beb_Write32(baseaddr, (LEFT_OFFSET + TWO_REQUESTS_OFFSET),(value | TWO_REQUESTS_BIT)); if(in_two_requests) Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET),(value | TWO_REQUESTS_BIT));
else Beb_Write32(baseaddr, (LEFT_OFFSET + TWO_REQUESTS_OFFSET),(value &~(TWO_REQUESTS_BIT))); else Beb_Write32(csp0base, (LEFT_OFFSET + TWO_REQUESTS_OFFSET),(value &~(TWO_REQUESTS_BIT)));
// Right // Right
Beb_Write32(baseaddr, (RIGHT_OFFSET + FIRST_CMD_PART1_OFFSET),0); Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART1_OFFSET),0);
Beb_Write32(baseaddr, (RIGHT_OFFSET + FIRST_CMD_PART2_OFFSET),send_header_command | right_port_value); Beb_Write32(csp0base, (RIGHT_OFFSET + FIRST_CMD_PART2_OFFSET),send_header_command | right_port_value);
Beb_Write32(baseaddr, (RIGHT_OFFSET + SECOND_CMD_PART1_OFFSET),0); Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART1_OFFSET),0);
Beb_Write32(baseaddr, (RIGHT_OFFSET + SECOND_CMD_PART2_OFFSET),send_frame_command | right_port_value); Beb_Write32(csp0base, (RIGHT_OFFSET + SECOND_CMD_PART2_OFFSET),send_frame_command | right_port_value);
value = Beb_Read32(baseaddr,(RIGHT_OFFSET + TWO_REQUESTS_OFFSET)); value = Beb_Read32(csp0base,(RIGHT_OFFSET + TWO_REQUESTS_OFFSET));
if(in_two_requests) Beb_Write32(baseaddr, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET),(value | TWO_REQUESTS_BIT)); if(in_two_requests) Beb_Write32(csp0base, (RIGHT_OFFSET + TWO_REQUESTS_OFFSET),(value | TWO_REQUESTS_BIT));
else Beb_Write32(baseaddr, (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 // Set number of frames
Beb_Write32(baseaddr, (LEFT_OFFSET + COMMAND_COUNTER_OFFSET), nimages*(2+in_two_requests)); Beb_Write32(csp0base, (LEFT_OFFSET + COMMAND_COUNTER_OFFSET), nimages*(2+in_two_requests));
Beb_Write32(baseaddr, (RIGHT_OFFSET + COMMAND_COUNTER_OFFSET), nimages*(2+in_two_requests)); Beb_Write32(csp0base, (RIGHT_OFFSET + COMMAND_COUNTER_OFFSET), nimages*(2+in_two_requests));
#ifdef MARTIN #ifdef MARTIN
for (i=0; i < 10; i++) for (i=0; i < 10; i++)
@ -943,7 +943,7 @@ int Beb_RequestNImages(unsigned int beb_number, int ten_gig, unsigned int dst_nu
printf("%d\n",in_two_requests); printf("%d\n",in_two_requests);
#endif #endif
Beb_close(fd); Beb_close(fd,csp0base);
#ifdef MARTIN #ifdef MARTIN
printf("----Beb_RequestNImages----\n"); printf("----Beb_RequestNImages----\n");
@ -991,31 +991,22 @@ int Beb_Test(unsigned int beb_number){
// Temperature value is cropped and not well rounded // Temperature value is cropped and not well rounded
int Beb_GetBebFPGATemp() int Beb_GetBebFPGATemp()
{ {
u_int32_t* csp0base=0;
int temperature=0; int temperature=0;
volatile u_int32_t *ptr1; int ret;
int fd; //open file pointer
int fd = Beb_open(&csp0base,XPAR_SYSMON_0_BASEADDR);
fd = open("/dev/mem", O_RDWR | O_SYNC, 0); if(fd < 0){
if (fd == -1) cprintf(BG_RED,"Module Configuration FAIL\n");
{ }else{
printf("\nCan't find /dev/mem!\n"); //read data
return 0; ret = Beb_Read32(csp0base, FPGA_TEMP_OFFSET);
temperature = ((((float)(ret)/65536.0f)/0.00198421639f ) - 273.15f); // Static conversation, copied from xps sysmon standalone driver
//close file pointer
Beb_close(fd,csp0base);
} }
u_int32_t CSP0BASE = (u_int32_t)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, XPAR_SYSMON_0_BASEADDR );
if (CSP0BASE == (u_int32_t)MAP_FAILED)
{
printf("\nCan't map memmory area!!\n");
return 0;
}
ptr1=(u_int32_t*)(CSP0BASE + 0x200); // temperature register in xps sysmon core is at 0x200
close(fd);
temperature = ((((float)(*ptr1)/65536.0f)/0.00198421639f ) - 273.15f); // Static conversation, copied from xps sysmon standalone driver
return temperature; return temperature;
} }
@ -1023,7 +1014,8 @@ int Beb_GetBebFPGATemp()
int Beb_open(u_int32_t baseaddr, u_int32_t* csp0base){
int Beb_open(u_int32_t** csp0base, u_int32_t offset){
int fd = open("/dev/mem", O_RDWR | O_SYNC, 0); int fd = open("/dev/mem", O_RDWR | O_SYNC, 0);
if (fd == -1) if (fd == -1)
@ -1032,33 +1024,34 @@ int Beb_open(u_int32_t baseaddr, u_int32_t* csp0base){
#ifdef VERBOSE #ifdef VERBOSE
printf("/dev/mem opened\n"); printf("/dev/mem opened\n");
#endif #endif
*csp0base = (u_int32_t)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, baseaddr); *csp0base = (u_int32_t*)mmap(0, BEB_MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, offset);
if (*csp0base == (u_int32_t)MAP_FAILED) { if (*csp0base == MAP_FAILED) {
cprintf(BG_RED,"\nCan't map memmory area!!\n"); cprintf(BG_RED,"\nCan't map memmory area!!\n");
fd = -1; fd = -1;
} }
#ifdef VERBOSE #ifdef VERBOSE
else printf("CSP0 mapped\n"); else printf("CSP0 mapped %p\n",(void*)*csp0base);
#endif #endif
} }
return fd; return fd;
} }
u_int32_t Beb_Read32 (u_int32_t baseaddr, u_int32_t offset){ u_int32_t Beb_Read32 (u_int32_t* baseaddr, u_int32_t offset){
volatile u_int32_t *ptr1; volatile u_int32_t value;
ptr1=(u_int32_t*)(baseaddr + offset); value=* (u_int32_t*)(baseaddr + offset/(sizeof(u_int32_t)));
return *ptr1; return value;
} }
u_int32_t Beb_Write32 (u_int32_t baseaddr, u_int32_t offset, u_int32_t data){ u_int32_t Beb_Write32 (u_int32_t* baseaddr, u_int32_t offset, u_int32_t data){
volatile u_int32_t *ptr1; volatile u_int32_t *ptr1;
ptr1=(u_int32_t*)(baseaddr + offset); ptr1=(u_int32_t*)(baseaddr + offset/(sizeof(u_int32_t)));
*ptr1 = data; *ptr1 = data;
return *ptr1; return *ptr1;
} }
void Beb_close(int fd){ void Beb_close(int fd,u_int32_t* csp0base){
if(fd >= 0) if(fd >= 0)
close(fd); close(fd);
munmap(csp0base,BEB_MMAP_SIZE);
} }

View File

@ -88,10 +88,10 @@ struct BebInfo{
int Beb_GetBebFPGATemp(); int Beb_GetBebFPGATemp();
int Beb_open(u_int32_t baseaddr, u_int32_t* csp0base); int Beb_open(u_int32_t** csp0base, u_int32_t offset);
u_int32_t Beb_Read32 (u_int32_t baseaddr, u_int32_t offset); u_int32_t Beb_Read32 (u_int32_t* baseaddr, u_int32_t offset);
u_int32_t Beb_Write32 (u_int32_t baseaddr, u_int32_t offset, u_int32_t data); u_int32_t Beb_Write32 (u_int32_t* baseaddr, u_int32_t offset, u_int32_t data);
void Beb_close(int fd); void Beb_close(int fd,u_int32_t* csp0base);
#endif #endif

View File

@ -122,6 +122,8 @@
#define OVERWRITE_HARDWARE_BIT 0x2 #define OVERWRITE_HARDWARE_BIT 0x2
#define DEACTIVATE_BIT 0x4 #define DEACTIVATE_BIT 0x4
#define FPGA_TEMP_OFFSET 0x200
#define TXM_DELAY_LEFT_OFFSET 0x180 #define TXM_DELAY_LEFT_OFFSET 0x180
#define TXM_DELAY_RIGHT_OFFSET 0x1A0 #define TXM_DELAY_RIGHT_OFFSET 0x1A0
#define TXM_DELAY_FRAME_OFFSET 0x1C0 #define TXM_DELAY_FRAME_OFFSET 0x1C0

View File

@ -1609,35 +1609,6 @@ int* multiSlsDetector::startAndReadAll(){
int multiSlsDetector::startAndReadAllNoWait(){ int multiSlsDetector::startAndReadAllNoWait(){
int i=0;
int ret=OK, ret1=OK;
for (i=0; i<thisMultiDetector->numberOfDetectors; i++) {
if (i!=thisMultiDetector->masterPosition)
if (detectors[i]) {
ret=detectors[i]->startAndReadAllNoWait();
if(detectors[i]->getErrorMask())
setErrorMask(getErrorMask()|(1<<i));
if (ret!=OK)
ret1=FAIL;
}
}
i=thisMultiDetector->masterPosition;
if (thisMultiDetector->masterPosition>=0) {
if (detectors[i]) {
ret=detectors[i]->startAndReadAllNoWait();
if(detectors[i]->getErrorMask())
setErrorMask(getErrorMask()|(1<<i));
if (ret!=OK)
ret1=FAIL;
}
}
return ret1;
/*
// hanging randomly around 4000-5000 frames at 1sec exptime (threads dont return)
int i=0; int i=0;
int ret=OK; int ret=OK;
int posmin=0, posmax=thisMultiDetector->numberOfDetectors; int posmin=0, posmax=thisMultiDetector->numberOfDetectors;
@ -1683,7 +1654,7 @@ int multiSlsDetector::startAndReadAllNoWait(){
} }
return ret; return ret;
*/
} }

View File

@ -2526,9 +2526,10 @@ int read_frame(int file_des) {
if (differentClients==1 && lockStatus==1) { if (differentClients==1 && lockStatus==1) {
dataret=FAIL; dataret=FAIL;
sprintf(mess,"Detector locked by %s\n",lastClientIP); sprintf(mess,"Detector locked by %s\n",lastClientIP);
cprintf(RED,"%s",mess);
//dataret could be swapped during sendData //dataret could be swapped during sendData
dataret1 = dataret; dataret1 = dataret;
sendData(file_des,&dataret1,sizeof(dataret),INT32); sendData(file_des,&dataret1,sizeof(dataret1),INT32);
sendData(file_des,mess,sizeof(mess),OTHER); sendData(file_des,mess,sizeof(mess),OTHER);
printf("dataret %d\n",dataret); printf("dataret %d\n",dataret);
return dataret; return dataret;
@ -2541,11 +2542,11 @@ int read_frame(int file_des) {
//dataret could be swapped during sendData //dataret could be swapped during sendData
dataret1 = dataret; dataret1 = dataret;
sendData(file_des,&dataret1,sizeof(dataret),INT32); sendData(file_des,&dataret1,sizeof(dataret1),INT32);
if (dataret==FAIL) if (dataret==FAIL)
sendData(file_des,mess,sizeof(mess),OTHER);//sizeof(mess));//sizeof(mess)); sendData(file_des,mess,sizeof(mess),OTHER);//sizeof(mess));//sizeof(mess));
else else if(dataret==OK){printf("shouldnt be sending anything but i am\n");
sendData(file_des,dataretval,dataBytes,OTHER); sendData(file_des,dataretval,dataBytes,OTHER);}
printf("dataret %d\n",dataret); printf("dataret %d\n",dataret);
return dataret; return dataret;

View File

@ -1,178 +1,170 @@
#include "ThreadPool.h" #include "ThreadPool.h"
ThreadPool::ThreadPool(int pool_size) : m_pool_size(pool_size) ThreadPool::ThreadPool(int pool_size) : m_pool_size(pool_size){
{
#ifdef VERBOSE #ifdef VERBOSE
cout << "Constructed ThreadPool of size " << m_pool_size << endl; cout << "Constructed ThreadPool of size " << m_pool_size << endl;
#endif #endif
m_tasks_loaded = false; m_tasks_loaded = false;
thread_started = false; thread_started = false;
current_thread_number = -1; current_thread_number = -1;
number_of_ongoing_tasks = 0; number_of_ongoing_tasks = 0;
} }
ThreadPool::~ThreadPool() ThreadPool::~ThreadPool(){
{ // Release resources
// Release resources if (m_pool_state != STOPPED) {
if (m_pool_state != STOPPED) { destroy_threadpool();
destroy_threadpool(); }
}
} }
// We can't pass a member function to pthread_create.
// So created the wrapper function that calls the member function
// we want to run in the thread.
extern "C" extern "C"
void* start_thread(void* arg) void* start_thread(void* arg)
{ {
ThreadPool* tp = (ThreadPool*) arg; ThreadPool* tp = (ThreadPool*) arg;
tp->execute_thread(); tp->execute_thread();
return NULL; return NULL;
} }
int ThreadPool::initialize_threadpool() int ThreadPool::initialize_threadpool(){
{
if(m_pool_size == 1) if(m_pool_size == 1)
return m_pool_size; return m_pool_size;
// TODO: COnsider lazy loading threads instead of creating all at once m_pool_state = STARTED;
m_pool_state = STARTED; int ret = -1;
int ret = -1; sem_init(&semStart,1,0);
for (int i = 0; i < m_pool_size; i++) { for (int i = 0; i < m_pool_size; i++) {
pthread_t tid; pthread_t tid;
thread_started = false; thread_started = false;
current_thread_number = i; current_thread_number = i;
ret = pthread_create(&tid, NULL, start_thread, (void*) this); ret = pthread_create(&tid, NULL, start_thread, (void*) this);
if (ret != 0) { if (ret != 0) {
cerr << "pthread_create() failed: " << ret << endl; cerr << "pthread_create() failed: " << ret << endl;
return 0; return 0;
} }
m_threads.push_back(tid); m_threads.push_back(tid);
while(!thread_started); while(!thread_started);
} }
#ifdef VERBOSE #ifdef VERBOSE
cout << m_pool_size << " threads created by the thread pool" << endl; cout << m_pool_size << " threads created by the thread pool" << endl;
#endif #endif
return m_pool_size; return m_pool_size;
} }
int ThreadPool::destroy_threadpool() int ThreadPool::destroy_threadpool(){
{ if(m_pool_size == 1) if(m_pool_size == 1)
return 0; return 0;
//cout << "in destroying threadpool" << endl; /*cout << "in destroying threadpool" << endl;*/
// Note: this is not for synchronization, its for thread communication! // thread communication- modified m_pool_state may not show up
// destroy_threadpool() will only be called from the main thread, yet //to other threads until its modified in a lock!
// the modified m_pool_state may not show up to other threads until its m_task_mutex.lock();
// modified in a lock! m_pool_state = STOPPED;
m_task_mutex.lock(); m_task_mutex.unlock();
m_pool_state = STOPPED; /*cout << "Broadcasting STOP signal to all threads..." << endl;*/
m_task_mutex.unlock(); m_task_cond_var.broadcast(); // notify all threads we are shttung down
/*cout << "Broadcasting STOP signal to all threads..." << endl;*/
m_task_cond_var.broadcast(); // notify all threads we are shttung down
int ret = -1; int ret = -1;
for (int i = 0; i < m_pool_size; i++) { for (int i = 0; i < m_pool_size; i++) {
void* result; void* result;
ret = pthread_join(m_threads[i], &result); sem_post(&semStart);
/*cout << "pthread_join() returned " << ret << ": " << strerror(errno) << endl;*/ ret = pthread_join(m_threads[i], &result);
m_task_cond_var.broadcast(); // try waking up a bunch of threads that are still waiting /*cout << "pthread_join() returned " << ret << ": " << strerror(errno) << endl;*/
} m_task_cond_var.broadcast(); // try waking up a bunch of threads that are still waiting
number_of_ongoing_tasks = 0; }
/* cout << m_pool_size << " threads exited from the thread pool" << endl;*/ sem_destroy(&semStart);
return 0; number_of_ongoing_tasks = 0;
/* cout << m_pool_size << " threads exited from the thread pool" << endl;*/
return 0;
} }
void* ThreadPool::execute_thread() void* ThreadPool::execute_thread(){
{
int ithread = current_thread_number; int ithread = current_thread_number;
thread_started = true; thread_started = true;
Task* task = NULL; Task* task = NULL;
m_tasks_loaded = false; m_tasks_loaded = false;
/*cout << "Starting thread " << pthread_self() << endl;*/ /*cout << "Starting thread " << pthread_self() << endl;*/
while(true) { while(true) {
// Try to pick a task // Try to pick a task
/*cout << "Locking: " << pthread_self() << endl;*/ /*cout << "Locking: " << pthread_self() << endl;*/
m_task_mutex.lock(); m_task_mutex.lock();
// We need to put pthread_cond_wait in a loop for two reasons: while ((m_pool_state != STOPPED) && (m_tasks.empty())) {
// 1. There can be spurious wakeups (due to signal/ENITR) // Wait until there is a task in the queue
// 2. When mutex is released for waiting, another thread can be waken up // Unlock mutex while wait, then lock it back when signaled
// from a signal/broadcast and that thread can mess up the condition. /* cout << "Unlocking and waiting: " << pthread_self() << endl;*/
// So when the current thread wakes up the condition may no longer be m_task_cond_var.wait(m_task_mutex.get_mutex_ptr());
// actually true! /* cout << "Signaled and locking: " << pthread_self() << endl;*/
while ((m_pool_state != STOPPED) && (m_tasks.empty())) { }
// Wait until there is a task in the queue
// Unlock mutex while wait, then lock it back when signaled
/* cout << "Unlocking and waiting: " << pthread_self() << endl;*/
m_task_cond_var.wait(m_task_mutex.get_mutex_ptr());
/* cout << "Signaled and locking: " << pthread_self() << endl;*/
}
// If the thread was woken up to notify process shutdown, return from here // If the thread was woken up to notify process shutdown, return from here
if (m_pool_state == STOPPED) { if (m_pool_state == STOPPED) {
/* cout << "Unlocking and exiting: " << pthread_self() << endl;*/ /* cout << "Unlocking and exiting: " << pthread_self() << endl;*/
m_task_mutex.unlock(); m_task_mutex.unlock();
pthread_exit(NULL); pthread_exit(NULL);
} }
task = m_tasks.front(); task = m_tasks.front();
m_tasks.pop_front(); m_tasks.pop_front();
/*cout << "Unlocking: " << pthread_self() << endl;*/ /*cout << "Unlocking: " << pthread_self() << endl;*/
m_task_mutex.unlock(); m_task_mutex.unlock();
//cout << "Executing thread " << pthread_self() << endl; sem_wait(&semStart);
// execute the task
(*task)(); // could also do task->run(arg);
//cout << "Done executing thread " << pthread_self() << endl;
m_all_tasks_mutex.lock(); /*cout << ithread <<" Executing thread " << pthread_self() << endl;*/
number_of_ongoing_tasks--; // cout<<"number_of_ongoing_tasks:"<<number_of_ongoing_tasks<<endl; // execute the task
m_all_tasks_mutex.unlock(); (*task)(); // could also do task->run(arg);
/*cout << ithread <<" Done executing thread " << pthread_self() << endl;*/
//if all required tasks done m_all_tasks_mutex.lock();
if(!ithread && m_tasks_loaded && (m_tasks.empty())){ number_of_ongoing_tasks--;
while(number_of_ongoing_tasks) m_all_tasks_mutex.unlock();
usleep(5000);
m_all_tasks_mutex.lock(); //last task and check m_tasks_loaded to ensure done only once
m_tasks_loaded = false; //cout<<"all tasks done"<<endl; if((!number_of_ongoing_tasks) && m_tasks_loaded){
m_all_tasks_cond_var.signal(); // wake up thread that is waiting for all tasks to be complete /*cout << ithread << " all tasks done."<<endl;*/
m_all_tasks_mutex.unlock(); m_all_tasks_mutex.lock();
} m_tasks_loaded = false;
m_all_tasks_cond_var.signal();// wake up thread that is waiting for all tasks to be complete
m_all_tasks_mutex.unlock();
}
delete task; delete task;
} /*cout << ithread << " task deleted" << endl;*/
return NULL; }
return NULL;
} }
int ThreadPool::add_task(Task* task) int ThreadPool::add_task(Task* task){
{
if(m_pool_size == 1){ if(m_pool_size == 1){
(*task)(); (*task)();
return 0; return 0;
} }
m_task_mutex.lock(); m_task_mutex.lock();
// TODO: put a limit on how many tasks can be added at most // TODO: put a limit on how many tasks can be added at most
m_tasks.push_back(task); m_tasks.push_back(task);
number_of_ongoing_tasks++;// cout<<"number_of_ongoing_tasks:"<<number_of_ongoing_tasks<<endl; number_of_ongoing_tasks++;
m_task_cond_var.signal(); // wake up one thread that is waiting for a task to be available m_task_cond_var.signal(); // wake up one thread that is waiting for a task to be available
m_task_mutex.unlock(); m_task_mutex.unlock();
return 0; return 0;
} }
void ThreadPool::wait_for_tasks_to_complete(){ void ThreadPool::wait_for_tasks_to_complete(){
if(m_pool_size == 1) if(m_pool_size == 1)
return; return;
m_all_tasks_mutex.lock(); m_all_tasks_mutex.lock();
/*cout << "waiting for tasks: locked. gonna wait" << endl;*/
m_tasks_loaded = true; m_tasks_loaded = true;
while ((m_pool_state != STOPPED) && m_tasks_loaded) {
m_all_tasks_cond_var.wait(m_all_tasks_mutex.get_mutex_ptr());
}
m_all_tasks_mutex.unlock();
//giving all threads permission to start as all tasks have been added
//using a different variable as number_of_ongoing_tasks is likely to change during the loop
int totalnumtasks = number_of_ongoing_tasks;
for(int i=0;i<totalnumtasks;i++)
sem_post(&semStart);
while ((m_pool_state != STOPPED) && m_tasks_loaded) {
m_all_tasks_cond_var.wait(m_all_tasks_mutex.get_mutex_ptr());
}
/*cout << "waiting for tasks:totall out, must be locked again" << endl;*/
m_all_tasks_mutex.unlock();
} }

View File

@ -13,7 +13,7 @@
#include "Task.h" #include "Task.h"
#include "CondVar.h" #include "CondVar.h"
#include "Global.h" #include "Global.h"
#include <semaphore.h>
using namespace std; using namespace std;
@ -44,5 +44,8 @@ private:
//volatile uint64_t tasks_done_mask; //volatile uint64_t tasks_done_mask;
volatile int number_of_ongoing_tasks; volatile int number_of_ongoing_tasks;
sem_t semStart;
}; };