/************************************************************************************ * This code illustrates how to setup and use shared memory. Before running this * * code you must setup shared memory in VXIedit >> VXIpc Conf Editor (or * * PCI Conf Editor) >> Logical Address Conf Editor. Set the "Address Space" * * to A16/A32, "VXI Shared RAM size" to 1M, and "Shared RAM Pool" to 1000 KB. * * Make sure you reboot. And now when you run resman, LA 0 will request 1 MB of * * space is A32 space. This "VXI register device" actually is your local RAM. So * * anytime you write to one of these registers, it is equivelant to writing * * directly into local RAM. "Mapping" is the term to describe creating the * * virtual VXI register device. Not all 1 MB of these registers will be used to * * share the memory. The parameter "Shared RAM Pool" is the size of the local * * buffer that you want to reserve for use on the VXI bus (this keeps Windows * * from using this, and thus saves you from currupting your O/S). When we call * * VXImemAlloc in our code, we are "allocating" the memory and pointing that many * * registers from the local memory into the VXI space. So for example, we have * * "mapped" 1 MB in A32 space, we have told Windows to stay out of 1000 KBs of RAM * * and then we have allocated an ammount set by the user during run-time of this * * code. Let's say the user selects 0x100 bytes to "share." This means that within * * the VXI A32 space, 0x100 bytes of the memory requested by this device actually * * reside in local RAM. The rest of the A32 address space requested by this device * * is not used and can not be written to with success. When we call the function * * VXImemAlloc, it will return the exact address of the local buffer (a pointer), * * and the exact VXI address within A32 space where this buffer begins. Both are * * displayed on the screen during run-time. * * * * The flow of the code is as follows: * * 1) InitVXIlibrary(): Initializes VXI library. * * 2) get input on how much memory user wants to share. * * 3) VXImemAlloc(): allocate the memory. Now whenever user writes to the right * * VXI address, it is equivelant to writing to the local RAM. * * 4) VXImove(): Move A16 configuration registers into the shared memory. * * 5) VXIin(): read from the shared memory. * * 6) deref a pointer: read data by dereferencing a pointer * * 7) VXImemFree(): free allocated memory. * * 8) CloseVXIlibrary(): Uninitialize the VXI library. * ************************************************************************************/ #include #include #include #include static NIVXI_STATUS Status, AllocStat; static UINT32 vxiaddr; static void *useraddr; static UINT32 size; static int value; static UINT16 direction; static char ch; void main () { Cls(); //clear the STDIO window if ( InitVXIlibrary() ) //This function must be called. { printf ("Unable to Initialize VXI library... exiting"); exit (0); //if not successful -> exit } /* * Get user input to determin how much memory to allocate. */ printf ("How many bytes would you like to allocate? >> 0x"); scanf ("%x", &size); /* * Now we will actually allocate the memory by using VXImemAlloc. * Parameters are: * * size: number of bytes to allocate in the buffer * (ret)useraddr: the address of the local allocated buffer. * (ret)vxiaddr: VXI address where the allocated buffer begins * inside the mapped memory. */ AllocStat = VXImemAlloc (size, &useraddr, &vxiaddr); if ( Status < 0 ) //alloc not successful { printf ("\nAllocation failed. Have you set up shared memory in VXIedit?\n... exiting\n"); CloseVXIlibrary(); exit (0); } /* * Now that the shared memory is setup, let's use it for something simple. I * will now do VXImove to move the A16 configuration registers into the shared * memory. */ Status = VXImove (1, 0xC000, 3, vxiaddr, 4, 2); if (Status) printf("Error moving from A16 to shared memory\n"); else printf ("\nConfiguration registers successfully VXImove'd into shared memory\n"); /* * Now that the configuration registers have been copied into shared memory. I will * prove that they are there. Shared memory can be accessed by 2 different methods: * through VXI space (as though it were a register based instrument), or through * dereferencing the local buffer. */ Status = VXIin (0x3, vxiaddr, 2, &value); if ( Status ) printf("Error doing VXIin\n"); else printf ("\nVXIin from shared memory = 0x%x\n", value); /* * Now I will access the shared memory by dereferencing the local buffer. To do * this, there are 2 possible methods, depending on the status from VXImemAlloc. * If the Status from VXImemAlloc was 0, we can directly dereference the pointer. If * the Status was 1, we can only access the local buffer by using VXImemCopy. */ if ( AllocStat == 0 ) //can access memory directly { value = *(UINT16 *)useraddr; //deref the pointer directly } else //need to use VXImemCopy to access memory { /* * This function copies a local buffer to/from sharable local VXI memory. * Parameters are: * * useraddr: pointer to the local shared memory returned from VXImemAlloc * (ret) value: data at that local buffer address * size: number of bytes to read * direction: direction of transfer */ direction = 1; //move from local RAM to "value" size = 4; //read 1 word. Status = VXImemCopy (useraddr, &value, size, direction); if ( Status ) printf("Error using VXImemCopy"); } /* * Print out results of deref'ing */ printf ("Deref a pointer = 0x%x\n", value); printf (""); getchar (); scanf ("%c", &ch); //wait for user to hit a key to quit VXImemFree (useraddr); //free allocated memory CloseVXIlibrary(); //close VXI library }