/*********************************************************************** * This code is designed to run in CVI. It demnonstrates low level VXI * * function usage by doing the following: * * * * * 1) Initializing the VXI library. * * 2) Saving the old bus error handler, and setting up a new one. * * 3) MapVXIAddress to map the user window to the configuration * * registers (A16 0xC000). * * 4) Prompt the user for a register to read. * * 5) VXIpeek on the register * * 6) Dereferencing a pointer to the register * * * * In order to run this code, you must have a user window at least 8 * * bytes wide. This would be setup in VXIedit>>VXIpc Configuration * * Editor>>Bus Configuration Editor. The user window size is shown * * in VXIinit. * ***********************************************************************/ #include #include #include static char ch; static int *pointer, *addr, value, offset; static int BusError; //global flag used to track bus errors static UINT32 width, accessparms, window, address, windowend, windowbase; static NIVXI_HBUSERROR *OldBusErrorHandler; static INT32 timo; static NIVXI_STATUS ret; /***************************************************************** * The next code sequence is the handler for bus errors. This is * * used to see if an error occurred during a low level function * * since error trapping is not built into the low level functions.* *****************************************************************/ NIVXI_HQUAL void NIVXI_HSPEC MyBusErrorHandler (void) { BusError = 1; //flag indicating a bus error has occured. } void main () { Cls(); //clear STDIO window if ( InitVXIlibrary() ) //This function must be called. { printf ("Unable to Initialize VXI library... exiting"); exit (0); //if not successful -> exit } OldBusErrorHandler = GetBusErrorHandler (); //save old error handler SetBusErrorHandler (MyBusErrorHandler); //setup new one. BusError = 0; //clear bus error flag. /* * In order to do low level functions, we need to call * MapVXIAddress(). The parts of this function are: * * accessparms: specifies the access parameters to set up the window. * address: the address within A16, A24, or A32 * timo: timeout (in milliseconds) * window: window number for use with other functions. * ret: the status of the function (0 is success) * addr: pointer to the local address for a specified VXI address * (null if not successful) */ accessparms = 0x1; //A16 space, not owner, not privildged address = 0xC000; //address in A16 space I want to map to timo = 100; //time out in 100 milliseconds if error addr = MapVXIAddress (accessparms, address, timo, &window, &ret); /* * First we need to find out what offset the user wants. We will * store this choice in "offset" and add this to the base * address of 0xC000 to get to the proper absolute address. */ printf ("Select a Configuration Register to read? (choices below)\n"); printf ("0: ID/Logical Address\n2: Device Type\n4: Status/Control\n6: Offset\n>> "); scanf ("%d",&offset); /* * We need to make sure the user window is wide enought to map * to the entire 40 bytes of the conguration registers for LA 0. * This is good practice to check the size of the User Window. */ GetWindowRange (window, &windowbase, &windowend); if ( (windowend - windowbase) < 0x40 ) { printf ("The User Window is too small to do operation...exiting."); exit(0); //terminate program if not wide enough or get GPF later } /* * First do VXIpeek(). Parameters are as follows: * * pointer: pointer returned from MapVXIAddress() * width: 1=byte, 2=word * value: data read from operation */ width = 2; //read a word pointer = (UINT32*)(offset + (UINT32)addr); //move pointer to absolute address if (ret == 0) //do VXIpeek only if MapVXIaddress was successful VXIpeek (pointer, width, &value); if (BusError == 0) //print result only if there was no bus error printf ("Value from VXIpeek = 0x%x\n", value); else printf ("A bus Error occured during VXIpeek.\n"); BusError = 0; //clear bus error flag /* * Now do the same operation by dereferncing the pointer * we calculated from the user's offset. */ value = *(UINT16*)pointer; //deref "pointer" and put data into "value" if (BusError == 0) //display result if no bus error printf ("Value from dereference = 0x%x\n", value); else printf ("A bus error occured on the pointer dereference.\n"); BusError = 0; //clear bus error flag printf (""); getchar (); //clear out the existing buffer. scanf ("%c", &ch); //wait for user to hit a key to quit. UnMapVXIAddress (window); //Undo mapping of user window SetBusErrorHandler (OldBusErrorHandler); //restore old bus handler CloseVXIlibrary(); //uninitialize VXI library }