/** @par LICENCE
* @verbatim
 Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code and the documentation.
        
        ADDI-DATA GmbH
        Dieselstrasse 3 
        D-77833 Ottersweier
        Tel: +19(0)7223/9493-0
        Fax: +49(0)7223/9493-92
        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 shoud find the complete GPL in the COPYING file accompanying 
    this source code. 
* @endverbatim
*/
#include <linux/module.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
	#include <linux/config.h>
#else
	#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
		#include <linux/autoconf.h>
	#else
		#include <generated/autoconf.h>
	#endif
#endif

#include <linux/version.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <apci.h>
#include <apci3600.h>
#include <apci3600-kapi.h>

//#include "../apci3600-private.h"

MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("ADDI-DATA GmbH <info@addi-data.com>");
MODULE_DESCRIPTION ("APCI-3600 IOCTL driver sample10");

/*minor of the board*/
static int board_minor=0;

/*can be override*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)  
	MODULE_PARM (board_minor,"i");
	EXPORT_NO_SYMBOLS;
#else
	module_param(board_minor, int, S_IRUGO);
#endif
static int sample10 (struct pci_dev *pdev)
{ // Chronometer Single Read Test
	uint8_t b_Status = 0;
	int i_ReturnValue = 0;
	uint8_t b_Exit = 0;
	uint32_t dw_Value = 0;
	int i_Cpt = 0;


	printk("Chronometer single read Test\n");



	/* Initialise the board */
	i_ReturnValue = i_PCI3600_InitBoard (pdev,
										 PCI3600_MASTER_MODE,
										 PCI3600_DIVISOR_4,
										 PCI3600_DIVISOR_4,
										 PCI3600_EXT_CLOCK_1,
										 PCI3600_EXT_CLOCK_1,
										 0,
										 0,
										 PCI3600_CHRONOMETER_DIVISOR_1);

	if (i_ReturnValue == 0)
	{
		printk ("i_PCI3600_InitBoard OK\n");


		/******************************/
		/* Initialise the chronometer */
		/******************************/
		i_ReturnValue = i_PCI3600_InitChronometerModule (pdev,
						0,
						0);

		if (i_ReturnValue == 0) {
			printk ("i_PCI3600_InitChronometerModule OK\n");

			/*************************/
			/* Start the chronometer */
			/*************************/
			i_ReturnValue = i_PCI3600_StartChronometerModuleAcquisition (pdev,
							0);

			if (i_ReturnValue == 0) {
				printk ("i_PCI3600_StartChronometerModuleAcquisition OK\n");

				for (i_Cpt = 0;i_Cpt < 10; i_Cpt++) {
					do {
						

						/***********************************/
						/* Get the chronometer FIFO Status */
						/***********************************/
						i_ReturnValue = i_PCI3600_GetChronometerModuleFIFOStatus (pdev,
										0,
										&b_Status);

						if (i_ReturnValue == 0) {}
						else {
							printk ("i_PCI3600_GetChronometerModuleFIFOStatus Error : %d\n",i_ReturnValue);
							b_Exit = 1;
							break;

						}
						/********************************************/
						/* Check the FIFO status of the chronometer */
						/********************************************/
					} while (b_Status == 0);

					if (b_Exit == 0) {
						if ((b_Status & 0x1) != 0) {
							/********************************************/
							/* Read the chronometer value from the FIFO */
							/********************************************/
							i_ReturnValue = i_PCI3600_ReadChronometerModuleValue (pdev,
											0,
											&dw_Value);

							if (i_ReturnValue == 0) {
								printk("Value = %u\n",dw_Value);

							} else {
								printk ("i_PCI3600_ReadChronometerModuleValue Error:%d\n",i_ReturnValue);
								break;

							}
						} else {
							if ((b_Status & 0x2) != 0) {
								printk("FIFO Error Overflow\n");
								break;

							}
						}
					} else {
						break;

					}
				}
				/*******************************************/
				/* Stop the chronometer module acquisition */
				/*******************************************/
				i_ReturnValue = i_PCI3600_StopChronometerModuleAcquisition (pdev, 0);

				if (i_ReturnValue == 0) {
					printk ("i_PCI3600_StopChronometerModuleAcquisition OK\n");

				} else {
					printk ("i_PCI3600_StopChronometerModuleAcquisition Error: %d\n",i_ReturnValue);

				}
			} else {
				printk ("i_PCI3600_StartChronometerModuleAcquisition Error : %d\n",i_ReturnValue);

			}
			/***************************/
			/* Release the chronometer */
			/***************************/
			i_ReturnValue = i_PCI3600_ReleaseChronometerModule (pdev, 0);

			if (i_ReturnValue == 0) {
				printk ("i_PCI3600_ReleaseChronometerModule OK\n");

			} else {
				printk ("i_PCI3600_ReleaseChronometerModule Error :%d\n",i_ReturnValue);

			}
		} else {
			printk ("i_PCI3600_InitChronometerModule Error : %d\n",i_ReturnValue);

		}


	} else
	{
		printk ("i_PCI3600_InitBoard Error : %d\n",i_ReturnValue);
	}
	return (i_ReturnValue);
}

//------------------------------------------------------------------------------
/** Called when module loads. */
static int __init
apci3600_sample10_init (void)
{

	printk("Using board minor %d (override with board_minor parameter)\n",board_minor);
	sample10(apci3600_lookup_board_by_index(board_minor));
	printk("Succesfull end of the sample, you can unload it...\n");

	return 0;

}

//-------------------------------------------------------------------
/** Called when module is unloaded. */
static void __exit
apci3600_sample10_exit (void)
{



}


//------------------------------------------------------------------------------
module_exit (apci3600_sample10_exit);

module_init (apci3600_sample10_init);

