//=============================================================================
// (c) Copyright 2005 Diamond Systems Corporation. Use of this source code
// is subject to the terms of Diamond Systems' Software License Agreement.
// Diamond Systems provides no warranty of proper performance if this
// source code is modified.
//
// File: WatchdogFunctions.c   v5.9
// Desc: Sample program that demonstrates how to use watchdog functions
// Created by 	KL
//=============================================================================

#include <stdio.h>

#ifdef _WIN32
#ifndef _WIN32_WCE
#include <conio.h>
#endif

#include <windows.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "dscud.h"
#endif

// DOS
#ifdef __BORLANDC__
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
// diamond driver includes
#include "../../../../current/dev/source/dscud.h"
#endif

// Standard (non-RT) Linux
#if defined(linux) || defined(__QNXNTO__) || defined(_WRS_VXWORKS_5_X)
#include <stdlib.h>
#include <math.h>
#include <time.h>
// diamond driver includes
#include "dscud.h"

#ifdef _WRS_VXWORKS_5_X
#include <selectLib.h>
#define main HERCWatchdogFunctions
#else
#include <sys/time.h>
#endif

static int kbhit()
{
	struct timeval timeout;
	fd_set rfds;

	timeout.tv_sec = 0;
	timeout.tv_usec = 0;

	FD_ZERO(&rfds);
	FD_SET(0, &rfds);

	if ( select(0+1, &rfds, NULL, NULL, &timeout) > 0 )
		return getchar();

	return 0;
}
#endif


// var declarations
BYTE result;  // returned error code
DSCB dscb;    // handle used to refer to the board
DSCCB dsccb;  // structure containing board settings
DSCS dscs;    // used for interrupts
WORD timer16; // 16-bit timer value
BYTE timer8;  // 8-bit timer value
SWORD options;// watchdog options
ERRPARAMS errorParams; // structure for returning error code and error string
int intBuff;  // temp variable of size int
int i;

//=============================================================================
// Name: main()
// Desc: Starting function that calls the driver functions used
//
//		 NOTE: By convention, you should capture the BYTE return value for each
//		 driver API call, and check the error code.
//
//	     I. Driver Initialization
//	    II. Board Initialization
//	   III. Setup and initiate Watchdog Timer
//		IV. Watchdog retrigger and disable
//		 V. Cleanup
//
//=============================================================================

int main( void )
{
	//=========================================================================
	// I. DRIVER INITIALIZATION
	//
	//    Initializes the DSCUD library.
	//
	//	  Initialize the driver, using the driver version for validation
	//
	//=========================================================================

	if( dscInit( DSC_VERSION ) != DE_NONE )
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInit error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
		return 0;
	}

	//=========================================================================
	// II. BOARD INITIALIZATION
	//
	//	   Initialize the HERCULES board. This function passes the various
	//	   hardware parameters to the driver and resets the hardware.
	//
	//=========================================================================

	printf( "\nHERCULES BOARD INITIALIZATION:\n" );

	dsccb.boardtype = DSC_HERCEBX;

	printf("Enter the base address (default 0x240): ");
	scanf( "%hx", &dsccb.base_address );

	dsccb.int_level = 5;

	if(dscInitBoard(dsccb.boardtype, &dsccb, &dscb) != DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscInitBoard error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
		return 0;
	}

	//=========================================================================
	// III. SETUP AND INITIATE WATCHDOG TIMER
	//
	//		The watchdog timer follows the following sequence. Upon enable the 
	//		watchdog will count down from the 16-bit timer. When the timer hits
	//		zero, depending on the option programmed into the watchdog either
	//		the 8-bit timer will count down or the board will reset immediately.
	//		When the 8-bit timer counts down and hits zero, the board will reset.
	//
	//		The following programs the 16-bit and 8-bit timers, and watchdog option
	//=========================================================================

	printf("Enter value for 16-bit timer: ");
	scanf("%hu", &timer16);

	printf("Enter value for 8-bit timer: ");
	scanf("%d", &intBuff);
	timer8 = (BYTE) intBuff;

	printf("Enter value for watchdog option (default is 0): ");
	scanf("%d", &intBuff);
	options = (SWORD) intBuff;

	if( dscWatchdogEnable(dscb, timer16, timer8, options) != DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscWatchdogEnable error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
		return 0;
	}

	//=========================================================================
	// IV. WATCHDOG RETRIGGER AND DISABLE
	//
	//		Watchdog Triger retriggers the watchdog. Upon retrigger the watchdog
	//		counters will reinitialize to the original value programmed.
	//		Watchdog will then run like normal
	//
	//		Watchdog Disable will disable the watchdo. It will clear the counter
	//		values and counter options
	//=========================================================================

	if( dscWatchdogTrigger(dscb) != DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscWatchdogTrigger error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );
		return 0;
	}

	////////////////////////////////////////////////////
	// If you want to run watchdog and reset the board, 
	// comment out the following code
	////////////////////////////////////////////////////

	if( dscWatchdogDisable(dscb) != DE_NONE)
	{
		dscGetLastError(&errorParams);
		fprintf( stderr, "dscWatchdogDisable error: %s %s\n", dscGetErrorString(errorParams.ErrCode), errorParams.errstring );	
		return 0;
	}


	//=========================================================================
	// IV. CLEANUP
	//
	//	   Cleanup any remnants left by the program and free the resources used
	//	   by the driver.
	//
	//     STEPS TO FOLLOW:
	//
	//	   1. free the driver resources
	//=========================================================================

	dscFree();

	printf( "\n Watchdog Functions complete.\n" );

	return 0;
} // end main()



#ifdef _WIN32_WCE
int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	main();

	return 0;
}

#endif
