/** @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/delay.h>
#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 sample07");

/*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 sample07 (struct pci_dev *pdev)
{ // Write 1 Analog Output Test

	uint8_t b_Status = 0;
	int i_ReturnValue = 0;
	uint16_t w_Value = 0;


	printk("Write 1 Analog Output Test\n");

	mdelay (2000);


	/* 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 analog output */
		/********************************/
		i_ReturnValue = i_PCI3600_InitAnalogOutChannel (pdev,
						0,
						PCI3600_SAMPLING_CLOCK1,
						PCI3600_SINGLE_SPEED);

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

			do {
				i_ReturnValue = i_PCI3600_GetAnalogOutReadyBitStatus (pdev,
								0,
								&b_Status);

				if (i_ReturnValue == 0) {
					printk ("i_PCI3600_GetAnalogOutReadyBitStatus OK\n");
				} else {
					printk ("i_PCI3600_GetAnalogOutReadyBitStatus Error : %d\n",i_ReturnValue);
					break;
				}
				/***************************************************/
				/* Check the ready bit status of the analog output */
				/***************************************************/
			} while (b_Status == 0);

			if ((i_ReturnValue == 0) && (b_Status == 1)) {
				w_Value = 0;

				/******************************************/
				/* Write a new value on the analog output */
				/******************************************/
				i_ReturnValue = i_PCI3600_WriteAnalogOutValue (pdev,
								0,
								w_Value);

				if (i_ReturnValue == 0) {
					printk ("i_PCI3600_WriteAnalogOutValue OK : Value = 0\n");
				} else {
					printk ("i_PCI3600_WriteAnalogOutValue Error : %d\n",i_ReturnValue);
				}
			}

			mdelay (2000);
			w_Value = 65535;

			/********************************/
			/* Write 0 to the analog output */
			/********************************/
			i_ReturnValue = i_PCI3600_Set1AnalogOutChannel (pdev,
							0,
							w_Value);

			if (i_ReturnValue == 0) {
				printk ("i_PCI3600_Set1AnalogOutChannel OK Valu = 65535\n");

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

			/*************************************/
			/* Release the analog output channel */
			/*************************************/
			i_ReturnValue = i_PCI3600_ReleaseAnalogOutChannel (pdev, 0);

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

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

			}


		} else {
			printk ("i_PCI3600_InitAnalogOutChannel 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_sample07_init (void)
{

	printk("Using board minor %d (override with board_minor parameter)\n",board_minor);
	sample07(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_sample07_exit (void)
{



}


//------------------------------------------------------------------------------
module_exit (apci3600_sample07_exit);

module_init (apci3600_sample07_init);

