/** @file sample_apci1710.c
 
   This demonstarte how to use the incremental counter in user mode.
 
   @par CREATION  
   @author Krauth Julien
   @date   28.11.08
   
   @par VERSION
   @verbatim
   $LastChangedRevision:$
   $LastChangedDate:$
   @endverbatim   
   
   @par LICENCE
   @verbatim
    Copyright (C) 2009  ADDI-DATA GmbH for the source code of this module.
        
    ADDI-DATA GmbH
    Airpark Business Center
    Airport Boulevard B210
    77836 Rheinm�nster
    Germany
    Tel: +49(0)7229/1847-0
    Fax: +49(0)7229/1847-200
    http://www.addi-data-com
    info@addi-data.com
        
   This program is free software; you can redistribute it and/or modify it under 
   the terms of the GNU General Public License as published by the Free Software 
   Foundation; either version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
   or FITNESS FOR A PARTICULAR PURPOSE. 
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with 
   this program; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

   You shoud also find the complete GPL in the COPYING file 
   accompanying this source code.
   @endverbatim   
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <signal.h>
#include <sys/sysmacros.h> /* major() minor() */

#include <xpci3009.h>
#include "findboards.h"

//--------------------------------------------------------------------------------

int sample (int fd)
{
	uint8_t b_ModuleNbr = 0;
	int i_ReturnValue = 0;
	uint32_t ul_CounterValue = 0;
	uint8_t i = 0;
	uint8_t b_ArgArray[6];
		
		/* Initialise the incremental counter */
		b_ArgArray[0] = b_ModuleNbr;			// The module to use (0 to 3).
		b_ArgArray[1] = XPCI3009_32BIT_COUNTER;	// Selection form counter range.
		b_ArgArray[2] = XPCI3009_SIMPLE_MODE;	// First counter operating mode.
		b_ArgArray[3] = XPCI3009_HYSTERESIS_ON;	// First counter option.
		b_ArgArray[4] = XPCI3009_SIMPLE_MODE;	// Second counter operating mode.
		b_ArgArray[5] = XPCI3009_HYSTERESIS_ON;	// Second counter option.
	   	   	   	   
		i_ReturnValue = ioctl (fd,CMD_XPCI3009_InitCounter, b_ArgArray);

		/* Test the return value */
		switch (i_ReturnValue)
		{
			case 0:
			{
				printf ("\nCMD_XPCI3009_InitCounter OK");
				
				/* Clear the counter */
				i_ReturnValue = ioctl (fd,CMD_XPCI3009_ClearCounterValue, b_ModuleNbr);
	
				/* Test the return value */
				switch (i_ReturnValue)
				{
					case 0:
						printf ("\nCMD_XPCI3009_ClearCounterValue OK");
					break;
	
					case 1:
						printf ("\nCMD_XPCI3009_ClearCounterValue error");
						printf ("\nError = %d. The handle parameter of the board is wrong", i_ReturnValue);
						return 0;						
					break;
	
					case 2:
						printf ("\nCMD_XPCI3009_ClearCounterValue error");
						printf ("\nError = %d. The selected module number parameter is wrong", i_ReturnValue);
						return 0;						
					break;
	
					case 3:
						printf ("\nCMD_XPCI3009_ClearCounterValue error");
						printf ("\nError = %d. Counter not initialised", i_ReturnValue);
						return 0;						
					break;
	
					default:
						printf ("\nCMD_XPCI3009_ClearCounterValue error");
						printf ("\nError = %d.", i_ReturnValue);
						perror("\nioctl");
						return 0;
					break;
				}
	
				printf ("\n\n");
	
				do
				{
					/* Read the 32-Bit counter */
					ul_CounterValue = b_ModuleNbr; /* Initialize the module number (0 to 3) */
					
					i_ReturnValue = ioctl (fd,CMD_XPCI3009_Read32BitCounterValue, &ul_CounterValue);
	
					/* Test the return value */
					switch (i_ReturnValue)
					{
						case 0:
						break;
	
						case 1:
							printf ("\nCMD_Read32BitCounterValue for the first counter error");
							printf ("\nError = %d. The handle parameter of the board is wrong", i_ReturnValue);
							return 0;							
						break;
	
						case 2:
							printf ("\nCMD_Read32BitCounterValue for the first counter error");
							printf ("\nError = %d. No counter module found", i_ReturnValue);
							return 0;							
						break;
	
						case 3:
							printf ("\nCMD_Read32BitCounterValue for the first counter error");
							printf ("\nError = %d. Counter not initialised", i_ReturnValue);
							return 0;
						break;
	
						default:
							printf ("\nCMD_Read32BitCounterValue for the first counter error");
							printf ("\nError = %d.", i_ReturnValue);
							perror("\nioctl");
							return 0;							
						break;
					}
	
					printf ("\nCounter value from module %d  = %u", b_ModuleNbr, ul_CounterValue);
						
					sleep (1);
				}
				while (i++ < 100);			   		
			}
			break;

			case 1:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The handle parameter of the board is wrong", i_ReturnValue);
			break;

			case 2:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The module is not a counter module", i_ReturnValue);
			break;

			case 3:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The selected counter range is wrong", i_ReturnValue);
			break;

			case 4:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The selected first counter operating mode is wrong", i_ReturnValue);
			break;

			case 5:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The selected first counter operating option is wrong", i_ReturnValue);
			break;

			case 6:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The selected second counter operating mode is wrong", i_ReturnValue);
			break;

			case 7:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d. The selected second counter operating option is wrong", i_ReturnValue);
			break;

			default:
				printf ("\nCMD_XPCI3009_InitCounter error");
				printf ("\nError = %d", i_ReturnValue);
				perror("\nioctl");			
			break;
			}

			printf ("\nProgram concluded.");
	return 0;
}

//--------------------------------------------------------------------------------

int main(int argc, char** argv)
{

	int * xpci3009_card_fd;
	unsigned int xpci3009_card_number = 0;

	xpci3009_card_number = apci_find_boards("xpci3009",&xpci3009_card_fd);
		
		printf("total: %d cards\n",xpci3009_card_number);
		
		/* Use the incremental counter on the first APCI-1710 */
		sample (xpci3009_card_fd[0]);

	return 0;
}

//--------------------------------------------------------------------------------
