/***************************************************************************/
/* example_fifo.c
 * 
 * Build:  
 *  $COMMONCC -o example_fifo example_fifo.c $VMELIBS
 *
 * Example for reading a VME FIFO in VME BLT-D32 DMA.
 */ 
/***************************************************************************/

#include <stdio.h>
#include <sys/types.h>

#include "Vme.h"

#define VME_PIO_ADDRESS 0x10000000
#define VME_PIO_SIZE    (1024*1024) /* 1 MB */ 
#define VME_PIO_AM      0x09        /* Extended VME address */

#define VME_BLT_AM      0x0b        /* BLT D32 */
#define BUFFER_SIZE     (128*1024)

/***************************************************************************/
main (int argc, char *argv[])
/*---------------------------
*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
{
  u_long vme_address = VME_PIO_ADDRESS;
  int    cid, offset, length, count;
  int    *vme_base;
  int    *buffer;
/*.........................................................................*/

  /* -----------------------------------------------------------------------
   * Use a different VME base address if specified.
   */
  if ((argc==2)&&(sscanf(argv[1],"%x",&vme_address)!=1))
    {
      printf("Usage: %s <vme-base-address>\n",argv[0]);
      exit(1);
    }
  
  /* -----------------------------------------------------------------------
   * Open a VME channel for Programmed I/O and DMA.
   */
  cid = VmeOpenChannel ("pio_dma_2", "pio,dma");

  if (cid < 0 ) 
    exit(1);

  /* -----------------------------------------------------------------------
   * Allocate and prepare some buffer space for DMA. With this buffer DMA 
   * might be significantly faster using contiguous memory and locking the 
   * buffer space in memory in advance.
   * 
   * Note: This special function is not needed but if you call it ONLY 
   *       this buffer is allowed for DMA with this channel !
   */
   buffer = (int*)VmeAllocateMemory (cid, NULL, BUFFER_SIZE, 
				     Vme_MEMORY_FASTEST);

   if (buffer == NULL)
     exit(1);

  /* -----------------------------------------------------------------------
   * Enable the FIFO option for this channel. This garanties that the VME
   * address will not be incremented. If the DMA hardware does not support
   * this feature the FIFO will be accessed with PIO. 
   */
  VmeSetProperty(cid, Vme_SET_COPY_FIFO, 1);

  /* -----------------------------------------------------------------------
   * Map VME address space for PIO and define the base address for the VME 
   * copy function including DMA.
   * 
   * Note: the address modifier and size is valid for PIO only! 
   */
  vme_base = (int*)VmeMapAddress (cid, vme_address, 
				  VME_PIO_SIZE, VME_PIO_AM);
  if (vme_base == NULL)
    exit(1);

  /* -----------------------------------------------------------------------
   * Read/write data from/to a FIFO in VME with the specified offset and
   * length in bytes.
   *
   * Note: the functions return the number of bytes actually read or
   *       written which can be less than requested if the transfer was 
   *       aborted (normally by a VME bus error). 
   */
  offset = 0x100;
  length = 0x10000;

  count = VmeRead (cid, offset, (char *)buffer, length);

  if (count != length)
    exit(1);

  count = VmeWrite (cid, offset, (char *)buffer, length);

  if (count != length)
    exit(1);

  /* -----------------------------------------------------------------------
   * Close Vme channel. Do not forget to call this function before
   * you exit otherwise you might get some trouble later.
   */
  (void)VmeCloseChannel(cid);

}