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

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

View File

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

View File

@ -88,10 +88,10 @@ struct BebInfo{
int Beb_GetBebFPGATemp();
int Beb_open(u_int32_t baseaddr, u_int32_t* csp0base);
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);
void Beb_close(int fd);
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_Write32 (u_int32_t* baseaddr, u_int32_t offset, u_int32_t data);
void Beb_close(int fd,u_int32_t* csp0base);
#endif

View File

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

View File

@ -1609,35 +1609,6 @@ int* multiSlsDetector::startAndReadAll(){
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 ret=OK;
int posmin=0, posmax=thisMultiDetector->numberOfDetectors;
@ -1683,7 +1654,7 @@ int multiSlsDetector::startAndReadAllNoWait(){
}
return ret;
*/
}

View File

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

View File

@ -1,178 +1,170 @@
#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
cout << "Constructed ThreadPool of size " << m_pool_size << endl;
cout << "Constructed ThreadPool of size " << m_pool_size << endl;
#endif
m_tasks_loaded = false;
thread_started = false;
current_thread_number = -1;
number_of_ongoing_tasks = 0;
m_tasks_loaded = false;
thread_started = false;
current_thread_number = -1;
number_of_ongoing_tasks = 0;
}
ThreadPool::~ThreadPool()
{
// Release resources
if (m_pool_state != STOPPED) {
destroy_threadpool();
}
ThreadPool::~ThreadPool(){
// Release resources
if (m_pool_state != STOPPED) {
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"
void* start_thread(void* arg)
{
ThreadPool* tp = (ThreadPool*) arg;
tp->execute_thread();
return NULL;
ThreadPool* tp = (ThreadPool*) arg;
tp->execute_thread();
return NULL;
}
int ThreadPool::initialize_threadpool()
{
int ThreadPool::initialize_threadpool(){
if(m_pool_size == 1)
return m_pool_size;
// TODO: COnsider lazy loading threads instead of creating all at once
m_pool_state = STARTED;
int ret = -1;
for (int i = 0; i < m_pool_size; i++) {
pthread_t tid;
thread_started = false;
current_thread_number = i;
ret = pthread_create(&tid, NULL, start_thread, (void*) this);
if (ret != 0) {
cerr << "pthread_create() failed: " << ret << endl;
return 0;
}
m_threads.push_back(tid);
while(!thread_started);
}
m_pool_state = STARTED;
int ret = -1;
sem_init(&semStart,1,0);
for (int i = 0; i < m_pool_size; i++) {
pthread_t tid;
thread_started = false;
current_thread_number = i;
ret = pthread_create(&tid, NULL, start_thread, (void*) this);
if (ret != 0) {
cerr << "pthread_create() failed: " << ret << endl;
return 0;
}
m_threads.push_back(tid);
while(!thread_started);
}
#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
return m_pool_size;
return m_pool_size;
}
int ThreadPool::destroy_threadpool()
{ if(m_pool_size == 1)
int ThreadPool::destroy_threadpool(){
if(m_pool_size == 1)
return 0;
//cout << "in destroying threadpool" << endl;
// Note: this is not for synchronization, its for thread communication!
// destroy_threadpool() will only be called from the main thread, yet
// the modified m_pool_state may not show up to other threads until its
// modified in a lock!
m_task_mutex.lock();
m_pool_state = STOPPED;
m_task_mutex.unlock();
/*cout << "Broadcasting STOP signal to all threads..." << endl;*/
m_task_cond_var.broadcast(); // notify all threads we are shttung down
/*cout << "in destroying threadpool" << endl;*/
// thread communication- modified m_pool_state may not show up
//to other threads until its modified in a lock!
m_task_mutex.lock();
m_pool_state = STOPPED;
m_task_mutex.unlock();
/*cout << "Broadcasting STOP signal to all threads..." << endl;*/
m_task_cond_var.broadcast(); // notify all threads we are shttung down
int ret = -1;
for (int i = 0; i < m_pool_size; i++) {
void* result;
ret = pthread_join(m_threads[i], &result);
/*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;*/
return 0;
int ret = -1;
for (int i = 0; i < m_pool_size; i++) {
void* result;
sem_post(&semStart);
ret = pthread_join(m_threads[i], &result);
/*cout << "pthread_join() returned " << ret << ": " << strerror(errno) << endl;*/
m_task_cond_var.broadcast(); // try waking up a bunch of threads that are still waiting
}
sem_destroy(&semStart);
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;
thread_started = true;
Task* task = NULL;
m_tasks_loaded = false;
/*cout << "Starting thread " << pthread_self() << endl;*/
while(true) {
// Try to pick a task
/*cout << "Locking: " << pthread_self() << endl;*/
m_task_mutex.lock();
// We need to put pthread_cond_wait in a loop for two reasons:
// 1. There can be spurious wakeups (due to signal/ENITR)
// 2. When mutex is released for waiting, another thread can be waken up
// from a signal/broadcast and that thread can mess up the condition.
// So when the current thread wakes up the condition may no longer be
// actually true!
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;*/
}
Task* task = NULL;
m_tasks_loaded = false;
/*cout << "Starting thread " << pthread_self() << endl;*/
while(true) {
// Try to pick a task
/*cout << "Locking: " << pthread_self() << endl;*/
m_task_mutex.lock();
// If the thread was woken up to notify process shutdown, return from here
if (m_pool_state == STOPPED) {
/* cout << "Unlocking and exiting: " << pthread_self() << endl;*/
m_task_mutex.unlock();
pthread_exit(NULL);
}
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;*/
}
task = m_tasks.front();
m_tasks.pop_front();
/*cout << "Unlocking: " << pthread_self() << endl;*/
m_task_mutex.unlock();
// If the thread was woken up to notify process shutdown, return from here
if (m_pool_state == STOPPED) {
/* cout << "Unlocking and exiting: " << pthread_self() << endl;*/
m_task_mutex.unlock();
pthread_exit(NULL);
}
//cout << "Executing thread " << pthread_self() << endl;
// execute the task
(*task)(); // could also do task->run(arg);
//cout << "Done executing thread " << pthread_self() << endl;
task = m_tasks.front();
m_tasks.pop_front();
/*cout << "Unlocking: " << pthread_self() << endl;*/
m_task_mutex.unlock();
m_all_tasks_mutex.lock();
number_of_ongoing_tasks--; // cout<<"number_of_ongoing_tasks:"<<number_of_ongoing_tasks<<endl;
m_all_tasks_mutex.unlock();
sem_wait(&semStart);
//if all required tasks done
if(!ithread && m_tasks_loaded && (m_tasks.empty())){
while(number_of_ongoing_tasks)
usleep(5000);
/*cout << ithread <<" Executing thread " << pthread_self() << endl;*/
// execute the task
(*task)(); // could also do task->run(arg);
/*cout << ithread <<" Done executing thread " << pthread_self() << endl;*/
m_all_tasks_mutex.lock();
m_tasks_loaded = false; //cout<<"all tasks done"<<endl;
m_all_tasks_cond_var.signal(); // wake up thread that is waiting for all tasks to be complete
m_all_tasks_mutex.unlock();
}
m_all_tasks_mutex.lock();
number_of_ongoing_tasks--;
m_all_tasks_mutex.unlock();
delete task;
}
return NULL;
//last task and check m_tasks_loaded to ensure done only once
if((!number_of_ongoing_tasks) && m_tasks_loaded){
/*cout << ithread << " all tasks done."<<endl;*/
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;
/*cout << ithread << " task deleted" << endl;*/
}
return NULL;
}
int ThreadPool::add_task(Task* task)
{
int ThreadPool::add_task(Task* task){
if(m_pool_size == 1){
(*task)();
return 0;
(*task)();
return 0;
}
m_task_mutex.lock();
// TODO: put a limit on how many tasks can be added at most
m_tasks.push_back(task);
number_of_ongoing_tasks++;// cout<<"number_of_ongoing_tasks:"<<number_of_ongoing_tasks<<endl;
m_task_cond_var.signal(); // wake up one thread that is waiting for a task to be available
m_task_mutex.unlock();
return 0;
m_task_mutex.lock();
// TODO: put a limit on how many tasks can be added at most
m_tasks.push_back(task);
number_of_ongoing_tasks++;
m_task_cond_var.signal(); // wake up one thread that is waiting for a task to be available
m_task_mutex.unlock();
return 0;
}
void ThreadPool::wait_for_tasks_to_complete(){
if(m_pool_size == 1)
return;
m_all_tasks_mutex.lock();
/*cout << "waiting for tasks: locked. gonna wait" << endl;*/
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 "CondVar.h"
#include "Global.h"
#include <semaphore.h>
using namespace std;
@ -44,5 +44,8 @@ private:
//volatile uint64_t tasks_done_mask;
volatile int number_of_ongoing_tasks;
sem_t semStart;
};