Exemplo n.º 1
0
int main(int argc, char **argv)
{
	vme_bus_handle_t handle;

	/* Initialize
	 */
	if (vme_init(&handle)) {
		perror("vme_init");
		return -1;
	}

	/* Release the bus
	 */
	if (vme_release_bus_ownership(handle)) {
		perror("vme_release_bus_ownership");
		vme_term(handle);
		return -1;
	}

	if (vme_term(handle)) {
		perror("vme_term");
		return -1;
	}

	return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	vme_bus_handle_t bus_handle;
	int rval;

	if (vme_init(&bus_handle)) {
		perror("vme_init");
		return -1;
	}

	/* If there are no arguments, print the current endian conversion
	   hardware setup, else set the requested values.
	 */
	if (1 == argc) {
		rval = print_status(bus_handle);
	} else {
		rval = set_status(bus_handle, argc, argv);
	}

	if (vme_term(bus_handle)) {
		perror("vme_term");
		rval = -1;
	}

	return rval;
}
Exemplo n.º 3
0
int main()
{
	vme_bus_handle_t bus_handle;
	vme_vrai_handle_t vrai_handle;
	vme_interrupt_handle_t interrupt_handle;
	int data;

	if (vme_init(&bus_handle)) {
		perror("Error initializing the VMEbus");
		return -1;
	}
	if (vme_register_image_create(bus_handle, &vrai_handle, VME_ADDRESS,
					  ADDRESS_SPACE, 0)) {
		perror("Error creating VMEbus register image");
		vme_term(bus_handle);
		return -1;
	}

	if (vme_interrupt_attach(bus_handle, &interrupt_handle, LEVEL,
				     0, VME_INTERRUPT_BLOCKING, &data)) {
		perror("Error attaching to the interrupt");
		vme_register_image_release(bus_handle, vrai_handle);
		vme_term(bus_handle);
		return -1;
	}

	printf("Mailbox interrupt occurred, data is 0x%x\n", data);

	if (vme_register_image_release(bus_handle, vrai_handle)) {
		perror("Error releasing VMEbus register image");
		vme_term(bus_handle);
		return -1;
	}

	if (vme_term(bus_handle)) {
		perror("Error terminating");
		return -1;
	}

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
	vme_bus_handle_t bus_handle;
	vme_master_handle_t handle = NULL;
	uint64_t vme_addr;
	int am, dw, flags, use_memcpy, c;
	void *phys_addr, *buffer, *addr;
	size_t nelem, size;
	int rval = 0;

	/* Set up default values
	 */
	vme_addr = 0;
	am = VME_A32SD;
	dw = VME_D8;
	flags = VME_CTL_PWEN;
	nelem = 1;
	phys_addr = NULL;
	use_memcpy = 1;
	rval = 0;

	/* Parse the argument list
	 */
	while (-1 != (c = getopt(argc, argv, "a:A:d:e:f:p:")))
		switch (c) {
		case 'a':	/* Address modifier */
			if (strtoam(optarg, &am)) {
				fprintf(stderr, "Invalid address modifier\n");
				return -1;
			}
			break;
		case 'A':	/* VMEbus address */
			vme_addr = strtoul(optarg, NULL, 0);
			break;
		case 'd':	/* VMEbus access data width */
			if (strtodw(optarg, &dw)) {
				fprintf(stderr, "Invalid access data width\n");
				return -1;
			}
			/* If a specific data width was requested, then don't
			   use memcpy. */
			use_memcpy = 0;
			break;
		case 'e':	/* Number of elements */
			nelem = strtoul(optarg, NULL, 0);
			break;
		case 'f':	/* Flags */
			if (strtoflags(optarg, &flags)) {
				fprintf(stderr, "Invalid flags\n");
				return -1;
			}
			break;
		case 'p':	/* Physical address */
			phys_addr = (void *) strtoul(optarg, NULL, 0);
			break;
		default:
			fprintf(stderr,
				"USAGE: vme_peek [-a addr_mod] [-A vme_addr]"
				"[-d dwidth][-e num_elem][-f flags]"
				"[-p phys_addr]");
			return -1;
		}

	/* Use number of elements and data width to calculate the size.
	 */
	size = nelem * dw;

	/* Initialize
	 */
	if (vme_init(&bus_handle)) {
		perror("vme_init");
		return -1;
	}

	DPRINTF("vme_addr=0x%lx\n", (unsigned long) vme_addr);
	DPRINTF("am=0x%x\n", am);
	DPRINTF("dw=0x%x\n", dw);
	DPRINTF("size=0x%x\n", size);
	DPRINTF("flags=0x%x\n", flags);
	DPRINTF("phys_addr=0x%lx\n", (unsigned long) phys_addr);

	/* Create a handle to the necessary window
	 */
	if (vme_master_window_create(bus_handle, &handle, vme_addr, am,
				     size, flags, phys_addr)) {
		perror("vme_master_window_create");
		rval = -1;
		goto error_create;
	}

	/* This is not necessary, I just put this function here to test it and
	   demonstrate it's use.
	 */
	phys_addr = vme_master_window_phys_addr(bus_handle, handle);
	if (NULL == phys_addr) {
		perror("vme_master_window_phys_addr");
	}

	DPRINTF("Window physical address = 0x%lx\n", (unsigned long) phys_addr);

	/* Map in the window
	 */
	addr = vme_master_window_map(bus_handle, handle, 0);
	if (NULL == addr) {
		perror("vme_master_window_map");
		rval = -1;
		goto error_map;
	}

	/* Create a temporary buffer to copy data into before printing it so 
	   performance measurements won't account for local I/O.
	 */
	buffer = malloc(size);
	if (NULL == buffer) {
		perror("malloc");
		rval = -1;
		goto error_malloc;
	}

	/* Do the transfer. If data width was not given at the command line,
	   then use memcpy for fast transfers. Note that this may cause the
	   output to be byte-swapped.
	 */
	if (use_memcpy) {
		memcpy(buffer, addr, size);
	} else {
		if (vmemcpy(buffer, addr, nelem, dw)) {
			perror("vmemcpy");
			rval = -1;
			goto error_transfer;
		}
	}

	/* Print data to stdout.
	 */
	if (vdump(buffer, nelem, dw)) {
		perror("vdump");
		rval = -1;
		goto error_transfer;
	}

      error_transfer:
	free(buffer);

      error_malloc:
	if (vme_master_window_unmap(bus_handle, handle)) {
		perror("vme_master_window_unmap");
		rval = -1;
	}

      error_map:
	if (vme_master_window_release(bus_handle, handle)) {
		perror("vme_master_window_release");
		rval = -1;
	}

      error_create:
	if (vme_term(bus_handle)) {
		perror("vme_term");
		return -1;
	}

	return rval;
}
Exemplo n.º 5
0
main()
{
   key_t     skey=SEMAPHORE_KEY;
   int       nsems=NSEM;
   int       nsops=NSEM;
   int       semid;
   int       semno;
   int       semval;
   int       reply; 
   union  semun {
      int val;
      struct semid_ds *buf;
      ushort *array;
   } arg;
   struct sembuf sops[NSEM];
   int       shmid1, shmid2;
   struct shmid_ds *mbuf;
   struct shminfo  *inf; 
   int       shmemsize=BUFFER_LENGTH;
   int       *shm1ptr, *shm2ptr;
   key_t     bkey=SHAREDMEM_KEY;
   key_t     mkey=MESSAGE_KEY;
   int       i, j, c;
   pid_t     mypid;
   pid_t     *pid;
   FILE      *fp, *strm;
   int       fd;
   int       exades;                /* File descriptor for Exabyte            */
   int       opendes;               /* File descripter of opened file         */
   int       new_des_min = 10;      /* Offset for new file descriptor ?       */
   int       write_data = 0;        /* Write raw data to tape or not          */
   ssize_t   bytes_written;
   off_t     offset=0;
   int       nbytes;
   int       bufs=BUFFER_LENGTH;
   int       chan;                  /* VME bit3 link device channel           */
   int       *bufp;                 /* Pointer to databuffer in shared memory */
   int       *messp;                /* Pointer to message_box in shared memory*/
   int       *buf1p, *buf2p, *buf3p, *buf4p;

   long      message_length = 40;   /* VME Message_Box is 10 Words long       */
   long      *mess_p;               /* Pointer to Message_Box                 */

   long      movebytes;             /* Length (bytes) of movebuffer from RTPC */
   long      Message_Box[10];       /* Vector to hold Message_Box             */
   u_long    moveaddress;           /* Data buffer address in VME             */
   long      *sem1ptr;              /* Pointer to VME "semaphore" 1           */
   long      *sem2ptr;              /* Pointer to VME "semaphore" 2           */
   long      *sem3ptr;              /* Pointer to VME "semaphore" 3           */
   long      *sem4ptr;              /* Pointer to VME "semaphore" 4           */
   long      sem1val, sem2val, sem3val, sem4val;
   long      *engstatptr;
   long      engstatval;
   long      semph1, semph2, semph3, semph4, suns, stbox;
   int       record_count = 0;

   int       *sortp;               /* Pointer to message_box(0) - sort flag   */
   int       *engp;                /* Pointer to message_box(1) - engine flag */
   int       *stgp;                /* Pointer to message_box(2) - storage     */
   int       *vmesp;               /* Pointer to message_box(3) - VME status  */
   int       *recp;                /* Pointer to message_box(5) - record count*/
   int       sort_off      = 0;    /* Offset for pointer to message_box(0)    */
   int       engine_off    = 1;    /* Offset for pointer to message_box(1)    */
   int       stgflag_off   = 2;    /* Offset for pointer to message_box(2)    */
   int       vmes_off      = 3;    /* Offset for pointer to message_box(3)    */
   int       reccount_off  = 5;    /* Offset for pointer to message_box(5)    */
   int       part_offset   = 8192; /* Pointer off in databuffer part          */


   char err1[1024] = "ERROR: ENGINE: Cannot start acq_engine, lock file present ";
   char err2[1024] = "ERROR: ENGINE: Get semaphore identifier failed            ";
   char err3[1024] = "ERROR: ENGINE: Initialise VME front-end system failed     ";
   char err4[1024] = "ERROR: ENGINE: Shared data buffer memory allocation failed";
   char err5[1024] = "ERROR: ENGINE: Attach shared data buffer to process failed";
   char err6[1024] = "ERROR: ENGINE: Shared message_box memory allocation failed";
   char err7[1024] = "ERROR: ENGINE: Attach shared message_box to process failed";
   char err8[1024] = "ERROR: ENGINE: Could not position to status location      ";
   char err9[1024] = "ERROR: ENGINE: Could not set RUNNING status               ";
   char err10[1024]= "ERROR: ENGINE: Could not position to VMEbus message_box   ";
   char err11[1024]= "ERROR: ENGINE: Could not read data from VMEbus message_box";
   char err12[1024]= "ERROR: ENGINE: lseek failed                               ";
   char err13[1024]= "ERROR: ENGINE: Error getting VME sempahore                ";
   char err14[1024]= "ERROR: ENGINE: Error reading VME databuffer 1             ";
   char err15[1024]= "ERROR: ENGINE: Error resetting VME sempahore              ";
   char err16[1024]= "ERROR: ENGINE: Set Sort Semaphore to Zero Failed          ";
   char err17[1024]= "ERROR: ENGINE: Writing databuffer to tape failed          ";
   char err18[1024]= "ERROR: ENGINE: Could not position to VME status location  ";
   char err19[1024]= "ERROR: ENGINE: Could not set STOPPED status               ";
   char err20[1024]= "ERROR: ENGINE: Could not detach databuffer shared memory  ";
   char err21[1024]= "ERROR: ENGINE: Could not detach message_box shared memory ";
   char err22[1024]= "ERROR: ENGINE: Could not remove lock-file                 ";
   char err23[1024]= "ERROR: ENGINE: Cannot open SBS device driver             	";
   char err24[1024]= "ERROR: ENGINE: Cannot generate file name                  ";
   char err25[1024]= "ERROR: ENGINE: Unknown MESSAGE_ADDRESS in buffer_defs.h   ";
   char err26[1024]= "ERROR: ENGINE: Error reading VME databuffer 2             ";

   arg.buf = (void *) malloc (100); 
   mess_p  = &Message_Box[0];
   sem1ptr = &sem1val;
   sem2ptr = &sem2val;
   sem3ptr = &sem3val;
   sem4ptr = &sem4val;
   engstatptr = &engstatval;
    
   /* Check if the engine lock-file is present, create if not. This is */
   /* a security precaution, multiple engine process causes system crash*/
   
   if(fd=open("/Applications/sirius/system/engine.lock",O_CREAT|O_EXCL,PERMS)==-1 )
   {
      printf("%s\n",err1);
      exit(errno);
   }

   /* Get semaphore identifier */
   semid = semget(skey, nsems, PERMS );
   if (semid == -1) {
      printf("%s\n",err2);
      exit(errno);
   }  

   /* Initialize VME front-end system link */
   chan = vme_init();
   if (chan == -1) {
      printf("%s\n",err3);
      exit(errno);
   }

   /* Attach shared data buffer segment to process */
   shmid1 = shmget(bkey, shmemsize, PERMS);
   if (shmid1 == -1) {
      printf("%s\n",err4);
      exit(errno);
   }

   shm1ptr = shmat(shmid1, NULL, 0);
   if (*shm1ptr == -1) {
      printf("%s\n",err5);
      exit(errno);
   } 

   /* Attach shared message_box segment to process */
   shmemsize = 80;
   shmid2 = shmget(mkey, shmemsize, PERMS);
   if (shmid2 == -1) {
      printf("%s\n",err6);
      exit(errno);
   }

   shm2ptr = shmat(shmid2, NULL, 0);
   if (*shm2ptr == -1) {
      printf("%s\n",err7);
      exit(errno);
   } 

   /* Take a copy of the pointers to shared memory segments           */
   bufp  = shm1ptr;          /* Points to databuffer in shared memory */
   messp = shm2ptr;          /* Points to message_box n shared memory */

   /* Pointers used in write to Exabyte, buffer is divided into 4     */
   buf1p = shm1ptr;                 /* Points to part 1 of databuffer */
   buf2p = shm1ptr + part_offset;   /* Points to part 2 of databuffer */
   buf3p = shm1ptr + part_offset*2; /* Points to part 3 of databuffer */
   buf4p = shm1ptr + part_offset*3; /* Points to part 4 of databuffer */

   /* Initialize pointers to message_box variables */
   sortp = messp + sort_off;
   recp  = messp + reccount_off;
   stgp  = messp + stgflag_off;
   engp  = messp + engine_off;
   vmesp = messp + vmes_off;

   /* Clear data buffer segment */
   for (i = 0; i < 32768; i++)  {  
      *(bufp+i) = 0;
   } 
   bufp = shm1ptr;                                   /* Restore pointer */


   /* Signal to VME front-end system that the acquisition is running    */
   /* This is done by setting the VME Message_Box(x) to '1'             */
   if (lseek(chan, ENGSTATUS, SEEK_SET) == -1) {
      printf("%s\n",err8);
      exit(errno);
   }
   engstatval = 1;
   if (write(chan, engstatptr, 4) != 4) {
      printf("%s\n",err9);
      exit(errno);
   }

   /* Get the VMEbus Message_box to get eventbuffer address in VME A24*/
   if (lseek(chan, MESSAGE_ADDRESS, SEEK_SET) == -1) {
      printf("%s\n",err10);
      exit(errno);
   }
   if (read(chan, mess_p, message_length) != message_length) {
      printf("%s\n",err11);
      exit(errno);
   }

   if ( Message_Box[6] == 1) {
      *vmesp = 1;                     /* VME system is in running state */
   }else {
      *vmesp = 0;
   }
   movebytes = BUFFER_LENGTH/4;               /*Bufferlength is in bytes*/
   moveaddress = Message_Box[0];

   /* Write status to file /help/status.tmp */
   strm = fopen("/Applications/sirius/help/vmestatus.tmp","w");
   if ( Message_Box[6] == 1 ) {
      fprintf(strm," VME system status\t\t: RUNNING\n");
   }
   else if ( Message_Box[6] == 0 ) {
      fprintf(strm," VME system status\t\t: STOPPED\n");
   }else {
      fprintf(strm," VME system status\t\t: UNKNOWN ?\n");
   }
   fprintf(strm, " Buffer address\t\t: %x (hex) \n", moveaddress);
   fprintf(strm, " Buffer length\t\t: %x (hex) \n\n", Message_Box[1]);
   fclose( strm );

   /* Take a copy of the file descriptor for storage unit if used */
   if ( *stgp > 0 ) {
      opendes = *stgp;
      exades = fcntl( opendes, F_DUPFD, new_des_min );
      write_data = 1;
      if ( exades == -1) {
         perror("\n ERROR: Could not copy file descriptor \n");
         write_data = 0;
      }
   }

   semph1 = Message_Box[2]&0x00000001;               /*Test on the last 4 bits*/
   semph2 = Message_Box[3]&0x00000001;
   semph1 = Message_Box[4]&0x00000001;               /*Test on the last 4 bits*/
   semph2 = Message_Box[5]&0x00000001;
   suns   = Message_Box[7]&0x00000001;
   stbox  = MESSAGE_ADDRESS;
   printf("\n Engine4.0 parameters:");
   printf("\n Devicenumber for messagebox and buffers: %d",chan);
   printf("\n VME message box starts at              : 0x%x",stbox);
   printf("\n Buffer starts at                       : 0x%x",moveaddress);
   printf("\n Start of buffer address (messagebox 0) : 0x%x",Message_Box[0]);
   printf("\n Buffer length (words)   (messagebox 1) : %d",Message_Box[1]);
   printf("\n Semaphore 1             (messagebox 2) : %d",semph1);
   printf("\n Semaphore 2             (messagebox 3) : %d",semph2);
   printf("\n Semaphore 3             (messagebox 4) : %d",semph3);
   printf("\n Semaphore 4             (messagebox 5) : %d",semph4);
   printf("\n VME-CPU ready or not    (messagebox 6) : %d",Message_Box[6]);
   printf("\n SUN-CPU ready or not    (messagebox 7) : %d",suns);
   printf("\n Not used                (messagebox 8) : %d",Message_Box[8]);
   printf("\n Not used                (messagebox 9) : %d\n",Message_Box[9]);

   for(;;) {                               /*MAIN DATA ACQUISITION LOOP*/
      if ( *engp == 9 ) goto exitengine;   /*STOP COMMAND FROM MASTER  */

/***********************************************************************/
/*                            B U F F E R                              */
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*  New from version 4.0 and higher:  Since A32 slave DRAM gives crash */
/*  after typically 2 - 5 hours, we use A24 SRAM. However, this space  */
/*  is only 128 kbytes. Thus, we cannot use the two-buffer technique   */
/*  from versions 2.0 - 3.0 (originally designed by Tore Ramsoey). We  */
/*  have to segment 1 eventbuffer (32 kwords) into 4 movebuffers,      */
/*  each 32 kbytes. Tests show that this is almost as fast as before,  */
/*  and no more crashes of the eventbuilder+ program of RTPC!!!        */
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/


/*1.0 Check if movebuffer is ready                                     */
/*    Loop on VME "semaphore" #1 until it becomes 1 (FULL)             */
/*    The call to usleep suspends the process 10 ms                    */
      for (;;) {
         if (lseek(chan, SEM_1, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         if (read(chan, sem1ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
         if (sem1val == 1 ) break;                 /*OK, buffer is FULL*/    
         if (*engp == 9) goto exitengine;
         usleep(WAIT10MS);
      }

/*1.1 Signal the sorting task to disrupt sorting                       */
/*    This is done by setting message_box(0) to '3'                    */
      *sortp = 3;


/*1.2 Fetch movebuffer #1 from the VMEbus system                       */
      if (lseek(chan, moveaddress, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      if (read(chan, bufp, movebytes) != movebytes) {
         printf("%s\n",err14);
         exit(errno);
      }
 

/*1.3 Reset VMEbus semaphore # 1 to empty                              */
      if (lseek(chan, SEM_1, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      sem1val = 0; 
      if (write(chan, sem1ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }

/***********************************************************************/ 
/*2.0 Check if next movebuffer is ready                                */
/*    Loop on VME "semaphore" #2 until it becomes 1 (FULL)             */
      for (;;) {
         if (lseek(chan, SEM_2, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         if (read(chan, sem2ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
         if (sem2val == 1) break;                  /*OK, buffer is FULL*/
         if (*engp   == 9) goto exitengine;
      }

/*2.2 Fetch movebuffer #2 from the VMEbus system                       */
      if (lseek(chan, moveaddress, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      if (read(chan, bufp + 0x2000, movebytes) != movebytes) {
         printf("%s\n",err26);
         exit(errno);
      }
 
/*2.3 Reset VMEbus semaphore # 2 to empty                              */
      if (lseek(chan, SEM_2, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      sem2val = 0; 
      if (write(chan, sem2ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }
       
/***********************************************************************/ 
/*3.0 Check if next movebuffer is ready                                */
/*    Loop on VME "semaphore" #3 until it becomes 1 (FULL)             */
      for (;;) {
         if (lseek(chan, SEM_3, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         if (read(chan, sem3ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
         if (sem3val == 1) break;                  /*OK, buffer is FULL*/
         if (*engp   == 9) goto exitengine;
      }

/*3.2 Fetch movebuffer #3 from the VMEbus system                       */
      if (lseek(chan, moveaddress, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      if (read(chan, bufp + 0x4000, movebytes) != movebytes) {
         printf("%s\n",err26);
         exit(errno);
      }
 
/*3.3 Reset VMEbus semaphore # 3 to empty                              */
      if (lseek(chan, SEM_3, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      sem3val = 0; 
      if (write(chan, sem3ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }

/***********************************************************************/ 
/*4.0 Check if next movebuffer is ready                                */
/*    Loop on VME "semaphore" #4 until it becomes 1 (FULL)             */
      for (;;) {
         if (lseek(chan, SEM_4, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         if (read(chan, sem4ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
         if (sem4val == 1) break;                  /*OK, buffer is FULL*/
         if (*engp   == 9) goto exitengine;
      }

/*4.2 Fetch movebuffer #4 from the VMEbus system                       */
      if (lseek(chan, moveaddress, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      if (read(chan, bufp + 0x6000, movebytes) != movebytes) {
         printf("%s\n",err26);
         exit(errno);
      }
 
/*4.3 Reset VMEbus semaphore # 4 to empty                              */
      if (lseek(chan, SEM_4, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
      sem4val = 0; 
      if (write(chan, sem4ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }


/* testing, magne
   for (i = 0; i < 32768; i++)  {  
   if(*(bufp+i) < 40000 ){
   printf(" no %d buffer read = %d \n",i, *(bufp+i) );}
   } 
   exit(0);
*/

/***********************************************************************/ 
/*5.0 Activate sorting task                                            */
/*    This is done by setting the sort semaphore to 0                  */
      *sortp  = 0;                                /*Remove disrupt flag*/
      semno   = 0;
      arg.val = 0;
      if ( semctl(semid, semno, SETVAL, arg) == -1) {
         printf("%s\n",err16);
         exit(errno);
      }

/*6.0 Start dump of total eventbuffer to exabyte tape                  */
/*    Buffer is segmented into 4 records, each 32 kbytes long          */
      if ( write_data == 1 ) {
         bytes_written = write(exades, buf1p, RECORD_LENGTH);
         bytes_written = write(exades, buf2p, RECORD_LENGTH);
         bytes_written = write(exades, buf3p, RECORD_LENGTH);
         bytes_written = write(exades, buf4p, RECORD_LENGTH);
         if (bytes_written != RECORD_LENGTH) {
            printf("%s\n",err17);
         }
      } 

/*7.0 Update record count message_box(9) variable                      */
      *recp = *recp + 1;

   }                                /*END OF MAIN DATA ACQUISITION LOOP*/

   exitengine:

/* Signal to VME front-end system that the acquisition is stopped */
/* This is done by setting the VME Message_Box(x) to '0'          */
   if (lseek(chan, ENGSTATUS, SEEK_SET) == -1) {
      printf("%s\n",err18);
      exit(errno);
   }
   engstatval = 0;
   if (write(chan, engstatptr, 4) != 4) {
      printf("%s\n",err19);
      exit(errno);
   }

/* Detach shared memory */
   if ( shmdt( shm1ptr ) == -1) {
      printf("%s\n",err20);
   }
   if ( shmdt( shm2ptr ) == -1) {
      printf("%s\n",err21);
   } 

/* Remove the lock file */
   if ( system("rm -f /Applications/sirius/system/engine.lock") == -1) {
      printf("%s\n",err22);
      exit(errno);      
   }
   exit(0);
} 
Exemplo n.º 6
0
int main()
{
        vme_bus_handle_t bus_handle;
        vme_slave_handle_t window_handle;
#ifdef EIGHTBIT
        uint8_t *ptr;
#else
	uint32_t *ptr;
	uint32_t count;
#endif
        int ii;
	int c;

        if (vme_init(&bus_handle)) {
                perror("Error initializing the VMEbus");
                return -1;
        }

        if (vme_slave_window_create(bus_handle, &window_handle,
#ifdef EIGHTBIT
                                    VME_ADDRESS, ADDRESS_SPACE, NBYTES,
#else
                                    VME_ADDRESS, ADDRESS_SPACE, NBYTES*4,
#endif
                                    VME_CTL_PWEN | VME_CTL_PREN, 
				    NULL)) {
				/*	(void *)(2044*MB))) {*/
                perror("Error creating the window");
                vme_term(bus_handle);
                return -1;
        }

        ptr = vme_slave_window_map(bus_handle, window_handle, 0);
        if (!ptr) {
                perror("Error mapping the window");
                vme_slave_window_release(bus_handle, window_handle);
                vme_term(bus_handle);
                return -1;
        }

	count=0x12345678;
        /* Print the data */
        for (ii = 0; ii < NBYTES; ++ii, ++ptr) {

		*ptr=count;
		count+=4;
#ifdef EIGHTBIT
                printf("%.2x ", *ptr);
#else
                printf("%x ", *ptr);
#endif
                /* Add a newline every 16 bytes */
                if (!((ii + 1) % 0x10))
                        printf("\n");
        }
        printf("\n");

	printf ("Press <enter> to unmap window\n");
	c = getchar();

printf("unmapping the window\n");
        if (vme_slave_window_unmap(bus_handle, window_handle)) {
                perror("Error unmapping the window");
                vme_slave_window_release(bus_handle, window_handle);
                vme_term(bus_handle);
                return -1;
        }

printf("releasing the window\n");
        if (vme_slave_window_release(bus_handle, window_handle)) {
                perror("Error releasing the window");
                vme_term(bus_handle);
                return -1;
        }

printf("terminating the window\n");
        if (vme_term(bus_handle)) {
                perror("Error terminating");
                return -1;
        }

        return 0;
}
Exemplo n.º 7
0
Arquivo: vme7700.c Projeto: jeisch/vme
int
main(int argc, char **argv)
{
 char *rw = 0;
 int  addr = 0;
 int iarg = 0;
 int slot=0;
 
 if (++iarg>=argc) goto usage;
 rw = argv[iarg];
 if (++iarg>=argc) goto usage;
 sscanf(argv[iarg], "%d", &slot);
 
 if (++iarg>=argc) goto usage;
  sscanf(argv[iarg], "%x", &addr);
  addr=slot*(0x8000000) + addr;
 
 if (!strcmp(rw, "read")) {
    if (++iarg!=argc) goto usage;
    vme_bus_handle_t bus_handle;
	vme_master_handle_t window_handle;
	uint32_t *ptr;
    
    if (vme_init(&bus_handle)) {
		perror("Error initializing the VMEbus");
		return -1;
	}

	if (vme_master_window_create(bus_handle, &window_handle,
				     addr, ADDRESS_MODIFIER, NBYTES,
				     VME_CTL_PWEN, NULL)) {
		perror("Error creating the window");
		vme_term(bus_handle);
		return -1;
	}
    
    ptr = vme_master_window_map(bus_handle, window_handle, 0);
	if (!ptr) {
		perror("Error mapping the window");
		vme_master_window_release(bus_handle, window_handle);
		vme_term(bus_handle);
		return -1;
	}

    printf("%08x\n ", *ptr);

    if (vme_master_window_unmap(bus_handle, window_handle)) {
		perror("Error unmapping the window");
		vme_master_window_release(bus_handle, window_handle);
		vme_term(bus_handle);
		return -1;
	}

	if (vme_master_window_release(bus_handle, window_handle)) {
		perror("Error releasing the window");
		vme_term(bus_handle);
		return -1;
	}

	if (vme_term(bus_handle)) {
		perror("Error terminating");
		return -1;
	}
  }
 else if (!strcmp(rw, "write")) {
    int value=0;
    if (++iarg>=argc) goto usage;
  sscanf(argv[iarg], "%x", &value);
    if (++iarg!=argc) goto usage;

    vme_bus_handle_t bus_handle;
	vme_master_handle_t window_handle;
	uint32_t *ptr;
    
    if (vme_init(&bus_handle)) {
		perror("Error initializing the VMEbus");
		return -1;
	}

	if (vme_master_window_create(bus_handle, &window_handle,
				     addr, ADDRESS_MODIFIER, NBYTES,
				     VME_CTL_PWEN, NULL)) {
		perror("Error creating the window");
		vme_term(bus_handle);
		return -1;
	}
    
    ptr = vme_master_window_map(bus_handle, window_handle, 0);
	if (!ptr) {
		perror("Error mapping the window");
		vme_master_window_release(bus_handle, window_handle);
		vme_term(bus_handle);
		return -1;
	}

    *ptr=value;

    if (vme_master_window_unmap(bus_handle, window_handle)) {
		perror("Error unmapping the window");
		vme_master_window_release(bus_handle, window_handle);
		vme_term(bus_handle);
		return -1;
	}

	if (vme_master_window_release(bus_handle, window_handle)) {
		perror("Error releasing the window");
		vme_term(bus_handle);
		return -1;
	}

	if (vme_term(bus_handle)) {
		perror("Error terminating");
		return -1;
	}
  }
 else {
    goto usage;
  }
 return 0;
usage:
  fprintf(stderr, "Usage: %s read <slot number> <addr> \n", argv[0]);
  fprintf(stderr, "       %s write <slot number> <addr> <value>\n", argv[0]);
  fprintf(stderr, "where slot is dec, addr and value are hex\n");
  return -1;
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
	vme_bus_handle_t bus_handle;
	vme_slave_handle_t handle;
	uint64_t vme_addr;
	int as, dw, flags, use_memcpy, c, rval;
	void *phys_addr, *buffer, *addr;
	size_t nelem, size;
	FILE *datafile;

	/* Set up default values
	 */
	vme_addr = 0;
	as = VME_A32;
	dw = VME_D8;
	flags = VME_CTL_PWEN | VME_CTL_PREN;
	nelem = 1;
	phys_addr = NULL;
	use_memcpy = 1;
	datafile = stdin;
	rval = 0;

	/* Parse the argument list
	 */
	while (-1 != (c = getopt(argc, argv, "a:A:d:f:F:p:")))
		switch (c) {
		case 'a':	/* Address modifier */
			if (strtoas(optarg, &as)) {
				fprintf(stderr, "Invalid address space\n");
				return -1;
			}
			break;
		case 'A':	/* VMEbus address */
			vme_addr = strtoul(optarg, NULL, 0);
			break;
		case 'd':	/* VMEbus access data width */
			if (strtodw(optarg, &dw)) {
				fprintf(stderr, "Invalid access data width\n");
				return -1;
			}
			/* If a specific data width was requested, then don't
			   use memcpy. */
			use_memcpy = 0;
			break;
		case 'f':	/* Flags */
			if (strtoflags(optarg, &flags)) {
				fprintf(stderr, "Invalid flags\n");
				return -1;
			}
			break;
		case 'F':	/* Get data from a file */
			if (NULL == (datafile = fopen(optarg, "r"))) {
				perror("fopen");
				return -1;
			}
			break;
		case 'p':	/* Physical address */
			phys_addr = (void *) strtoul(optarg, NULL, 0);
			break;
		default:
			fprintf(stderr, "USAGE: vme_slave_poke [-a addr_mod]"
				"[-A vme_addr][-d dwidth][-f flags][-F file]"
				"[-p phys_addr][hexdata...]");
			return -1;
		}

	/* Get the data to be transfered
	 */
	if (argc > optind) {	/* Get argument from the command line */
		nelem = argc - optind;
		if (vconvert_hexargs(&buffer, nelem, dw, argv + optind)) {
			return -1;
		}
	} else {		/* Read data from file or stdin */
		nelem = vconvert_hexvals(&buffer, dw, datafile);
		if (0 == nelem) {
			fprintf(stderr, "No data to transfer\n");
			return -1;
		}
	}


	/* Use number of elements and data width to calculate the size.
	 */
	size = nelem * dw;

	/* Initialize
	 */
	if (vme_init(&bus_handle)) {
		perror("vme_init");
		rval = -1;
		goto error_init;
	}

	DPRINTF("vme_addr=0x%lx\n", (unsigned long) vme_addr);
	DPRINTF("as=0x%x\n", as);
	DPRINTF("dw=0x%x\n", dw);
	DPRINTF("size=0x%x\n", size);
	DPRINTF("flags=0x%x\n", flags);
	DPRINTF("phys_addr=0x%lx\n", (unsigned long) phys_addr);

	/* Create a handle to the necessary window
	 */
	if (vme_slave_window_create(bus_handle, &handle, vme_addr, as, size,
				    flags, phys_addr)) {
		perror("vme_slave_window_create");
		rval = -1;
		goto error_create;
	}

	/* This is not necessary, I just put this function here to test it and
	   demonstrate it's use.
	 */
	phys_addr = vme_slave_window_phys_addr(bus_handle, handle);
	if (NULL == phys_addr) {
		perror("vme_slave_window_phys_addr");
	}

	DPRINTF("Window physical address = 0x%lx\n", (unsigned long) phys_addr);

	/* Map in the window
	 */
	addr = vme_slave_window_map(bus_handle, handle, 0);
	if (NULL == addr) {
		perror("vme_slave_window_map");
		rval = -1;
		goto error_map;
	}

	/* If no data width is specified, the use memcpy to transfer the data.
	   This may result in byte-swapping though.
	 */
	if (use_memcpy) {
		memcpy(addr, buffer, size);
	} else {
		if (vmemcpy(addr, buffer, nelem, dw)) {
			perror("vmemcpy");
			rval = -1;
		}
	}

	if (vme_slave_window_unmap(bus_handle, handle)) {
		perror("vme_slave_window_unmap");
		rval = -1;
	}

      error_map:
	if (vme_slave_window_release(bus_handle, handle)) {
		perror("vme_slave_window_release");
		rval = -1;
	}

      error_create:
	if (vme_term(bus_handle)) {
		perror("vme_term");
		rval = -1;
	}

      error_init:
	free(buffer);

	return rval;
}
Exemplo n.º 9
0
int main()
{
        vme_bus_handle_t bus_handle;
        vme_dma_handle_t dma_handle;
        uint8_t *ptr;
        int ii;

        if (vme_init(&bus_handle)) {
                perror("Error initializing the VMEbus");
                return -1;
        }

        if (vme_dma_buffer_create(bus_handle, &dma_handle, NBYTES, 0, NULL)) {
                perror("Error creating the buffer");
                vme_term(bus_handle);
                return -1;
        }

        ptr = vme_dma_buffer_map(bus_handle, dma_handle, 0);
        if (!ptr) {
                perror("Error mapping the buffer");
                vme_dma_buffer_release(bus_handle, dma_handle);
                vme_term(bus_handle);
                return -1;
        }

        /* Transfer the data */
        if (vme_dma_read(bus_handle, dma_handle, 0, VME_ADDRESS,
                         ADDRESS_MODIFIER, NBYTES, 0)) {
                perror("Error reading data");
                vme_dma_buffer_unmap(bus_handle, dma_handle);
                vme_dma_buffer_release(bus_handle, dma_handle);
                vme_term(bus_handle);
                return -1;
        }

        /* Print the VMEbus data */
        for (ii = 0; ii < NBYTES; ++ii, ++ptr) {
                printf("%.2x ", *ptr);
                /* Add a newline every 16 bytes */
                if (!((ii + 1) % 0x10))
                        printf("\n");
        }
        printf("\n");

        if (vme_dma_buffer_unmap(bus_handle, dma_handle)) {
                perror("Error unmapping the buffer");
                vme_dma_buffer_release(bus_handle, dma_handle);
                vme_term(bus_handle);
                return -1;
        }

        if (vme_dma_buffer_release(bus_handle, dma_handle)) {
                perror("Error releasing the buffer");
                vme_term(bus_handle);
                return -1;
        }

        if (vme_term(bus_handle)) {
                perror("Error terminating");
                return -1;
        }

        return 0;
}
Exemplo n.º 10
0
main()
{
   char     *cpbuf;                      /* These 7 lines for RTPC A32 memory */
   char     *device = BT_DEVNAME;
   char     *bt_devname();
   bt_status_t status_flags;
   int      chanbuf;
   int      unit = 0;
   int      typebuf = BT_AXSRR;

   key_t     skey=SEMAPHORE_KEY;
   int       nsems=NSEM;
   int       nsops=NSEM;
   int       semid;
   int       semno;
   int       semval;
   int       reply; 
   union  semun {
      int val;
      struct semid_ds *buf;
      ushort *array;
   } arg;
   struct sembuf sops[NSEM];
   int       shmid1, shmid2;
   struct shmid_ds *mbuf;
   struct shminfo  *inf; 
   int       shmemsize=BUFFER_LENGTH;
   int       *shm1ptr, *shm2ptr;
   key_t     bkey=SHAREDMEM_KEY;
   key_t     mkey=MESSAGE_KEY;
   int       i, j, c;
   pid_t     mypid;
   pid_t     *pid;
   FILE      *fp, *strm;
   int       fd;
   int       exades;                /* File descriptor for Exabyte            */
   int       opendes;               /* File descripter of opened file         */
   int       new_des_min = 10;      /* Offset for new file descriptor ?       */
   int       write_data = 0;        /* Write raw data to tape or not          */
   ssize_t   bytes_written;
   off_t     offset=0;
   int       nbytes;
   int       bufs=BUFFER_LENGTH;
   int       chan;                  /* VME bit3 link device channel           */
   int       *bufp;                 /* Pointer to databuffer in shared memory */
   int       *messp;                /* Pointer to message_box in shared memory*/
   int       *buf1p, *buf2p, *buf3p, *buf4p;

   long      message_length = 40;   /* VME Message_Box is 10 Words long       */
   long      *mess_p;               /* Pointer to Message_Box                 */
   long      Message_Box[10];       /* Vector to hold Message_Box             */
   u_long    buffer1_address;       /* Data buffer 1 address (variable)       */
   u_long    buffer2_address;       /* Data buffer 2 address (variable)       */
   long      *sem1ptr;              /* Pointer to VME "semaphore" 1           */
   long      *sem2ptr;              /* Pointer to VME "semaphore" 2           */
   long      sem1val, sem2val;
   long      *engstatptr;
   long      engstatval;
   long      semph1, semph2, suns, stbox;

   int       record_count=0;

   int       *sortp;               /* Pointer to message_box(0) - sort flag   */
   int       *engp;                /* Pointer to message_box(1) - engine flag */
   int       *stgp;                /* Pointer to message_box(2) - storage     */
   int       *vmesp;               /* Pointer to message_box(3) - VME status  */
   int       *recp;                /* Pointer to message_box(5) - record count*/
   int       sort_off      = 0;    /* Offset for pointer to message_box(0)    */
   int       engine_off    = 1;    /* Offset for pointer to message_box(1)    */
   int       stgflag_off   = 2;    /* Offset for pointer to message_box(2)    */
   int       vmes_off      = 3;    /* Offset for pointer to message_box(3)    */
   int       reccount_off  = 5;    /* Offset for pointer to message_box(5)    */
   int       part_offset   = 8192; /* Pointer off in databuffer part          */

   char err1[1024] = "ERROR: ENGINE: Cannot start acq_engine, lock file present ";
   char err2[1024] = "ERROR: ENGINE: Get semaphore identifier failed            ";
   char err3[1024] = "ERROR: ENGINE: Initialise VME front-end system failed     ";
   char err4[1024] = "ERROR: ENGINE: Shared data buffer memory allocation failed";
   char err5[1024] = "ERROR: ENGINE: Attach shared data buffer to process failed";
   char err6[1024] = "ERROR: ENGINE: Shared message_box memory allocation failed";
   char err7[1024] = "ERROR: ENGINE: Attach shared message_box to process failed";
   char err8[1024] = "ERROR: ENGINE: Could not position to status location      ";
   char err9[1024] = "ERROR: ENGINE: Could not set RUNNING status               ";
   char err10[1024]= "ERROR: ENGINE: Could not position to VMEbus message_box   ";
   char err11[1024]= "ERROR: ENGINE: Could not read data from VMEbus message_box";
   char err12[1024]= "ERROR: ENGINE: lseek failed                               ";
   char err13[1024]= "ERROR: ENGINE: Error getting VME sempahore                ";
   char err14[1024]= "ERROR: ENGINE: Error reading VME databuffer 1             ";
   char err15[1024]= "ERROR: ENGINE: Error resetting VME sempahore              ";
   char err16[1024]= "ERROR: ENGINE: Set Sort Semaphore to Zero Failed          ";
   char err17[1024]= "ERROR: ENGINE: Writing databuffer to tape failed          ";
   char err18[1024]= "ERROR: ENGINE: Could not position to VME status location  ";
   char err19[1024]= "ERROR: ENGINE: Could not set STOPPED status               ";
   char err20[1024]= "ERROR: ENGINE: Could not detach databuffer shared memory  ";
   char err21[1024]= "ERROR: ENGINE: Could not detach message_box shared memory ";
   char err22[1024]= "ERROR: ENGINE: Could not remove lock-file                 ";
   char err23[1024]= "ERROR: ENGINE: Cannot open bit3 device driver             ";
   char err24[1024]= "ERROR: ENGINE: Cannot generate file name                  ";
   char err25[1024]= "ERROR: ENGINE: Unknown MESSAGE_ADDRESS in buffer_defs.h   ";
   char err26[1024]= "ERROR: ENGINE: Error reading VME databuffer 2             ";


   arg.buf = (void *) malloc (100); 
   mess_p  = &Message_Box[0];
   sem1ptr = &sem1val;
   sem2ptr = &sem2val;
   engstatptr = &engstatval;
    
   /* Check if the engine lock-file is present, create if not                   */
   /* This is a security precaution, multiple engine process causes system crash*/
   if(fd=open("/usr/local/sirius+/system/engine.lock",O_CREAT|O_EXCL,PERMS)==-1 )
   {
      printf("%s\n",err1);
      exit(errno);
   }

   /* Get semaphore identifier */
   semid = semget(skey, nsems, PERMS );
   if (semid == -1) {
      printf("%s\n",err2);
      exit(errno);
   }  

   /* Initialize VME front-end system link */
   chan = vme_init();
   if (chan == -1) {
      printf("%s\n",err3);
      exit(errno);
   }

   /* Attach shared data buffer segment to process */
   shmid1 = shmget(bkey, shmemsize, PERMS);
   if (shmid1 == -1) {
      printf("%s\n",err4);
      exit(errno);
   }

   shm1ptr = shmat(shmid1, NULL, 0);
   if (*shm1ptr == -1) {
      printf("%s\n",err5);
      exit(errno);
   } 

   /* Attach shared message_box segment to process */
   shmemsize = 80;
   shmid2 = shmget(mkey, shmemsize, PERMS);
   if (shmid2 == -1) {
      printf("%s\n",err6);
      exit(errno);
   }

   shm2ptr = shmat(shmid2, NULL, 0);
   if (*shm2ptr == -1) {
      printf("%s\n",err7);
      exit(errno);
   } 

   /* Take a copy of the pointers to shared memory segments           */
   bufp  = shm1ptr;          /* Points to databuffer in shared memory */
   messp = shm2ptr;          /* Points to message_box n shared memory */

   /* Pointers used in write to Exabyte, buffer is divided into 4     */
   buf1p = shm1ptr;                 /* Points to part 1 of databuffer */
   buf2p = shm1ptr + part_offset;   /* Points to part 2 of databuffer */
   buf3p = shm1ptr + part_offset*2; /* Points to part 3 of databuffer */
   buf4p = shm1ptr + part_offset*3; /* Points to part 4 of databuffer */

   /* Initialize pointers to message_box variables */
   sortp = messp + sort_off;
   recp  = messp + reccount_off;
   stgp  = messp + stgflag_off;
   engp  = messp + engine_off;
   vmesp = messp + vmes_off;

   /* Clear data buffer segment */
   for (j = 0; j < 32768; j++)  {  
      *(bufp+j) = 0;
   /*   bufp++;      endret av Magne, ellers overflow? */
   } 
   bufp = shm1ptr;                                   /* Restore pointer */

   /* Signal to VME front-end system that the acquisition is running    */
   /* This is done by setting the VME Message_Box(x) to '1'             */
   if (lseek(chan, ENGSTATUS, SEEK_SET) == -1) {
      printf("%s\n",err8);
      exit(errno);
   }

   engstatval = 1;
   if (write(chan, engstatptr, 4) != 4) {
      printf("%s\n",err9);
      exit(errno);
   }

   /* Get the VMEbus Message_box, defines data buffer address in VME A32*/
   /* WARNING: This address is dynamically allocated by the RTPC        */
   if (lseek(chan, MESSAGE_ADDRESS, SEEK_SET) == -1) {
      printf("%s\n",err10);
      exit(errno);
   }

   if (read(chan, mess_p, message_length) != message_length) {
      printf("%s\n",err11);
      exit(errno);
   }

   if ( Message_Box[5] == 1) {
      *vmesp = 1;                     /* VME system is in running state */
   }else {
      *vmesp = 0;
   }

   buffer1_address = Message_Box[0];
   buffer2_address = buffer1_address + BUFFER_LENGTH;

   /* Write status to file /help/status.tmp */
   strm = fopen("/usr/local/sirius+/help/vmestatus.tmp","w");
   if ( Message_Box[5] == 1 ) {
      fprintf(strm," VME system status\t\t: RUNNING\n");
   }
   else if ( Message_Box[5] == 0 ) {
      fprintf(strm," VME system status\t\t: STOPPED\n");
   }else {
      fprintf(strm," VME system status\t\t: UNKNOWN ?\n");
   }
   fprintf(strm, " Buffer1 address\t\t: %x (hex) \n", buffer1_address);
   fprintf(strm, " Buffer length\t\t: %x (hex) \n\n", Message_Box[1]);
   fclose( strm );

   /* Take a copy of the file descriptor for storage unit if used */
   if ( *stgp > 0 ) {
      opendes = *stgp;
      exades = fcntl( opendes, F_DUPFD, new_des_min );
      write_data = 1;
      if ( exades == -1) {
         perror("\n ERROR: Could not copy file descriptor \n");
         write_data = 0;
      }
   } 

   /* Establish the correct channel for bit3 according to A24 or A32       */
   /* for the two buffers. RTPC uses A32 and FIC (old CPU) uses A24        */
   /* This is tested by the fixed address of the message box given         */
   /* by the MESSAGE_ADDRESS in /sirius/include/buffer_defs.h              */
   /* MESSAGE_ADDRESS = 0x201FFFD0 is for FIC  using A24 (buffer uses A24) */
   /* MESSAGE_ADDRESS = 0x00850000 is for RTPC using A24 (buffer uses A32) */
   /* I hope this works, Magne                                             */

   if(MESSAGE_ADDRESS == 0x201FFFD0) {
      chanbuf = chan;                   /* Already determined by vme_init()*/
   }
   else if(MESSAGE_ADDRESS == 0x00850000) {
      if((cpbuf = bt_devname(unit, &buffer1_address, typebuf, device)) != NULL) {
         if ((chanbuf = open(cpbuf, O_RDONLY)) < 0 ) {
            printf("%s\n",err23);
            exit(errno);
         }
      }else{
         printf("%s\n",err24);
         exit(errno);
      }
      /* Make sure the bit3 adaptor is connected*/
      (void) ioctl(chanbuf, BIOC_CLR_STATUS, &status_flags);
      if (status_flags & BT_STATUS_MASK) {
         bit3_perror(chanbuf);
         exit(errno);
      }
   }else{
       printf("%s\n",err25);
       exit(errno);
   }

   semph1 = Message_Box[2]&0x00000001;               /*Test on the last 4 bits*/
   semph2 = Message_Box[3]&0x00000001;
   suns   = Message_Box[6]&0x00000001;
   stbox  = MESSAGE_ADDRESS;
   printf("\n test1 parameters:");
   printf("\n Device number for VME message box      : %d",chan);
   printf("\n Device number for VME buffers          : %d",chanbuf);
   printf("\n VME message box starts at              : 0x%x",stbox);
   printf("\n Buffer 1 starts at                     : 0x%x",buffer1_address);
   printf("\n Buffer 2 starts at                     : 0x%x",buffer2_address);
   printf("\n Start of buffer address (messagebox 0) : 0x%x",Message_Box[0]);
   printf("\n Buffer length (words)   (messagebox 1) : %d",Message_Box[1]);
   printf("\n Semaphore 1             (messagebox 2) : %d",semph1);
   printf("\n Semaphore 2             (messagebox 3) : %d",semph2);
   printf("\n Not used                (messagebox 4) : %d",Message_Box[4]);
   printf("\n VME-CPU ready or not    (messagebox 5) : %d",Message_Box[5]);
   printf("\n SUN-CPU ready or not    (messagebox 6) : %d",suns);
   printf("\n Not used                (messagebox 7) : %d",Message_Box[7]);
   printf("\n Not used                (messagebox 8) : %d",Message_Box[8]);
   printf("\n Not used                (messagebox 9) : %d\n",Message_Box[9]);


   for(;;) {                               /*MAIN DATA ACQUISITION LOOP*/
      if ( *engp == 9 ) goto exitengine;   /*STOP COMMAND FROM MASTER  */

/***********************************************************************/
/*                        B U F F E R  # 1                             */
/***********************************************************************/ 
/* 1. Check if buffer # 1 is ready                  */
/*    Loop on VME "semaphore" #1 until it becomes 1 */
/*    The call to usleep suspends the process 10 ms */
       
      for (;;) {

         /* new test, Magne

         if (lseek(chan, MESSAGE_ADDRESS, SEEK_SET) == -1) {
            printf("%s\n",err10);
            exit(errno);
         }
         if (read(chan, mess_p, message_length) != message_length) {
            printf("%s\n",err11);
            exit(errno);
         }
         if ( Message_Box[5] == 1) {
            *vmesp = 1;                     
         }else {
            *vmesp = 0;
         }
*/
         usleep(WAIT10MS);

         if (lseek(chan, SEM_1, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         usleep(WAIT10MS);

         if (read(chan, sem1ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
 *sem1ptr=1;
         if (sem1val == 1 ) break;	/* OK buffer ready */
         if (  *engp == 9) goto exitengine;
         usleep(WAIT10MS);
      }




/* 2. Signal the sorting task to disrupt sorting    */
/*    This is done by setting message_box(0) to '3' */
      *sortp = 3;

/* 3. Fetch data buffer #1 from the VMEbus system */
         usleep(WAIT10MS);

      if (lseek(chanbuf, buffer1_address, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
         usleep(WAIT10MS);

/*      if (read(chanbuf, bufp, BUFFER_LENGTH) != BUFFER_LENGTH) {
         printf("%s\n",err14);
         exit(errno);
      }*/
 
/* 4. Reset VMEbus semaphore # 1 */
         usleep(WAIT10MS);

      if (lseek(chan, SEM_1, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }

      sem1val=0; 
         usleep(WAIT10MS);

      if (write(chan, sem1ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }
     



/* 5. Activate sorting task                           */
/*    This is done by setting the sort semaphore to 0 */
      *sortp = 0;              /* Remove disrupt flag */
      semno  = 0;
      arg.val = 0;
      if ( semctl(semid, semno, SETVAL, arg) == -1) {
         printf("%s\n",err16);
         exit(errno);
      }

/* 6. Start dump of buffer 1 to exabyte tape              */
/*    Buffer is segmented into 4 records, each 32 kB long */
      if ( write_data == 1 ) {
         bytes_written = write(exades, buf1p, RECORD_LENGTH);
         bytes_written = write(exades, buf2p, RECORD_LENGTH);
         bytes_written = write(exades, buf3p, RECORD_LENGTH);
         bytes_written = write(exades, buf4p, RECORD_LENGTH);
         if (bytes_written != RECORD_LENGTH) {
            printf("%s\n",err17);
         }
      } 

/* Update record count message_box(9) variable */
      *recp = *recp + 1;

/*********************************************************************/
/*                        B U F F E R  # 2                           */
/*********************************************************************/
/* 1. Check if buffer # 2 is ready                  */
/*    Loop on VME "semaphore" #2 until it becomes 1 */
/*    The call to usleep suspends the process 10 ms */
       
      for (;;) {

 /* new test, Magne

         if (lseek(chan, MESSAGE_ADDRESS, SEEK_SET) == -1) {
            printf("%s\n",err10);
            exit(errno);
         }
         if (read(chan, mess_p, message_length) != message_length) {
            printf("%s\n",err11);
            exit(errno);
         }
         if ( Message_Box[5] == 1) {
            *vmesp = 1;      
         }else {
            *vmesp = 0;
         }
*/
         usleep(WAIT10MS);

         if (lseek(chan, SEM_2, SEEK_SET) == -1) {
            printf("%s\n",err12);
            exit(errno);
         }
         usleep(WAIT10MS);

         if (read(chan, sem2ptr, 4) != 4) {
            printf("%s\n",err13);
            exit(errno);
         }
  *sem2ptr=1;
         if (sem2val == 1 ) break;	/* OK buffer ready */
         if (  *engp == 9) goto exitengine;
         usleep(WAIT10MS);
      }
       


/* 2. Signal the sorting task to disrupt sorting of current buffer */
/*    This is done by setting message_box(0) to '3'                */
      *sortp = 3;

/* 3. Fetch data buffer #2 from the VMEbus system */
         usleep(WAIT10MS);

      if (lseek(chanbuf, buffer2_address, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
         usleep(WAIT10MS);

/*      if (read(chanbuf, bufp, BUFFER_LENGTH) != BUFFER_LENGTH) {
         printf("%s\n",err26);
         exit(errno);
      }*/

/* 4. Reset VMEbus semaphore # 2 */
         usleep(WAIT10MS);

      if (lseek(chan, SEM_2, SEEK_SET) == -1) {
         printf("%s\n",err12);
         exit(errno);
      }
         usleep(WAIT10MS);

      sem2val = 0; 
      if (write(chan, sem2ptr, 4) != 4) {
         printf("%s\n",err15);
         exit(errno);
      }
     




/* 5. Activate sorting task                           */
/*    This is done by setting the sort semaphore to 0 */
      *sortp  = 0; 	/* Remove disrupt flag */
      semno   = 0;
      arg.val = 0;
      if ( semctl(semid, semno, SETVAL, arg) == -1) {
         printf("%s\n",err16);
         exit(errno);
      }

/* 6. Start dump of buffer 2 to exabyte tape              */
/*    Buffer is segmented into 4 records, each 32 kB long */
      if ( write_data == 1 ) {
         bytes_written = write(exades, buf1p, RECORD_LENGTH);
         bytes_written = write(exades, buf2p, RECORD_LENGTH);
         bytes_written = write(exades, buf3p, RECORD_LENGTH);
         bytes_written = write(exades, buf4p, RECORD_LENGTH);
 
         if (bytes_written != RECORD_LENGTH) {
            printf("%s\n",err17);
            exit(errno);
         }
      } 

/* Update record count message_box(9) variable */
      *recp = *recp + 1;
   }                           /*END OF MAIN DATA ACQUISITION LOOP*/

   exitengine:

/* Signal to VME front-end system that the acquisition is stopped */
/* This is done by setting the VME Message_Box(x) to '0'          */
   if (lseek(chan, ENGSTATUS, SEEK_SET) == -1) {
      printf("%s\n",err18);
      exit(errno);
   }
   engstatval = 0;
   if (write(chan, engstatptr, 4) != 4) {
      printf("%s\n",err19);
      exit(errno);
   }

/* Detach shared memory */
   if ( shmdt( shm1ptr ) == -1) {
      printf("%s\n",err20);
   }
   if ( shmdt( shm2ptr ) == -1) {
      printf("%s\n",err21);
   } 

/* Remove the lock file */
   if ( system("rm -f /usr/local/sirius+/system/engine.lock") == -1) {
      printf("%s\n",err22);
      exit(errno);      
   }
   exit(0);
} 
Exemplo n.º 11
0
Arquivo: vme_api.c Projeto: jeisch/vme
/*============================================================================
 * Initialize the VMEbus driver interface
 */
int                             /* Returns 0 on success or -1 on failure */
vmeInit ()
{
  return (vme_init (&bus_handle));
}