/*****************************************************************************

Copyright (c) 2006 Analog Devices, Inc.	All	Rights Reserved. 

This software is proprietary and confidential to Analog	Devices, Inc. 
and	its	licensors.

*****************************************************************************

$RCSfile: JPEGImageEdgeDetection_System.c,v $
$Revision: 1.4 $
$Date: 2007/11/15 04:00:56 $

Project:	BlackfinSDK (JPEG Image Edge Detection)
Title:		System service routines
Author(s):	bmk
Revised	by:	

Description:
			Configures system, start devices and manages PPI/Pushbutton callbacks
			for Edge Detection on JPEG Image

References:
			None

*****************************************************************************
Tab	Setting:			4

Target Processor:		ADSP-BF5xx
Target Tools Revision:	ADSP VisualDSP++ v4.5
******************************************************************************

Modification History:
====================
$Log: JPEGImageEdgeDetection_System.c,v $
Revision 1.4  2007/11/15 04:00:56  randreol
Workaround to resolve interrupt conflict with new USB driver.  Push button Interrupt level is modified by USB driver.

Revision 1.3  2007/10/17 06:52:37  randreol
Port to VDSP 5.0

Revision 1.2  2007/05/23 02:29:17  randreol
Changes in ssl_init and VDSP5.0 porting

Revision 1.5  2006/11/20 05:56:50  bmk
Merged BF533 & BF561
Fixed BF561 cache issue

Revision 1.4  2006/11/03 07:11:17  bmk
SDK 2.0  files - Initial Entry

******************************************************************************
Include files
*****************/

#include <JPEGImageEdgeDetection_System.h>	// system includes
#include <jpeg_mjpeg_cache.h>				// cache setup includes
#include <jpeg_mjpeg_err.h>
#include <JPEGImageEdgeDetection_cplb_tables.h>
// USB includes
#include <drivers\usb\usb_core\adi_usb_objects.h>
#include <drivers\usb\usb_core\adi_usb_core.h>
#include <adi_usbcmd.h>							// supported USB commands
#include <drivers\usb\class\peripheral\vendor_specific\adi\bulkadi\adi_usb_bulkadi.h>	// adi bulk driver header file
#include <drivers\usb\controller\peripheral\plx\net2272\adi_usb_net2272.h>				// net2272 driver header file
#include <adi_usbio_blackfin.h>					// USB I/O functions for Blackfin

// Device Driver includes
#include <drivers\twi\adi_twi.h>				// TWI driver includes
#include <drivers\ppi\adi_ppi.h>				// PPI driver includes
#include <drivers\encoder\adi_adv717x.h>		// video encoder driver includes
#include <drivers\decoder\adi_adv7183.h>		// video decoder driver includes



/*****************
Callback functions
******************/
// Exception handler
static		ADI_INT_HANDLER		(ExceptionHandler);
// Hardware error handler
static		ADI_INT_HANDLER		(HWErrorHandler);
// Video Out callback function (for ADV717x)
static void VideoOutCallback	(void *AppHandle, u32  Event, void *pArg);
// USB driver callback
static void NET2272_Callback	(void *AppHandle, u32  Event, void *pArg);
// Ez-Kit Button press callback function
static void ButtonPressCallback	(void *AppHandle, u32  Event, void *pArg);

/************************
Local function prototype
************************/
// Installs USB driver
void 		InstallUSBdriver	(void);
// Enables USB driver
void 		EnableUSBdriver	(void);

/*****************
Globals
******************/
// Device Driver and System Services Handles
// handle to the callback service Manager
ADI_DCB_HANDLE			DCBManagerHandle;
// handle to the ADV717x device driver
ADI_DEV_DEVICE_HANDLE	ADV717xDeviceHandle;
// handle to the USB device driver
ADI_DEV_DEVICE_HANDLE	USBDeviceHandle;
// handle to the USB controller device driver
ADI_DEV_PDD_HANDLE PeripheralDevHandle;	


// Video parameters
// Video data per line (bytes)
u32	DataPerLine;
// Number of lines per ITU656 video frame (bytes)
u32	FrameLines;
// Number of Active lines per ITU656 video frame (bytes)
u32	ActiveFrameLines;
// Number of bytes to skip to reach active video data in a single line
u32	ActiveVideoSkip;
// Number of bytes to skip to reach active video frame field1 (odd frame)
u32	Field1Skip;
// Number of bytes to skip to reach active video frame field2 (even frame)
u32	Field2Skip;
// Selected Video Output mode
u8	OutputVideoMode;

// Sobel parameters
// Active field spilt to # number of blocks
u8	SobelFieldBlocks;
// Number of Sobel blocks per active field
u32	SobelBlockCount;
// Sobel block - row size
u32	SobelRowSize;

// Device parameters
u8 Coding_Algorithm	= CODING_ALGORITHM_JPEG;
u8 File_Settings	= GET_FILE_SETTINGS_FROM_TXT_FILE;

// System parameters
// flag to indicate Shared MDMA channel status (YUV to ITU656 conversion & Edge Detection shares a MDMA Stream)
volatile bool	MDMASharedFlag;
// Flag to indicate Sobel Edge Detection status
volatile bool	SobelDone;
// Flag to switch display between original JPEG image (TRUE) and Edge Detected output (FALSE)
volatile bool	DisplayJPEG;
// Flag indicates whether video output buffer needs to be updated(TRUE) or not (FALSE)
volatile bool	UpdateVideoOutBufs;

// Buffer pointers
// Pointer to buffer containing YUV data of decoded JPEG image 
u8 	*pYUVBuf;
// Pointer to buffer containing original JPEG image in ITU656 frame format
u8	*pJPEGImageBuf;
// Pointer to edge detected output buffer of ITU656 frame format
u8 	*pEdgeDetectBuf;

// Buffer to Hold YUV data from decoded JPEG image 
// size to support only ITU656 resolution & YUV400/YUV420/YUV422 image formats
#pragma section("YUV_Buffer", NO_INIT)
u8 	YUVBuf[YUV_BUF_SIZE];
// define ITU656 video frame buffer & Edge Detected Output buffer in 
// two different SDRAM banks
// Buffer to Hold original ITU656 frame (JPEG image)
#pragma section("JPEGImageBuffer", NO_INIT)
u8 	JPEGImageBuf[VIDEO_BUF_SIZE];
// Buffer to hold edge detected output
#pragma section("EdgeDetectBuffer", NO_INIT)
u8 	EdgeDetectBuf [VIDEO_BUF_SIZE];

/*****************************************************************************
Device Configuration tables
*****************************************************************************/

#if defined(__ADSPBF533__)

	// BF533 Ports (PF0=SCL,PF1=SDA) & Timer(Timer 0) used for Pseudo TWI
	adi_twi_pseudo_port port={ADI_FLAG_PF0,ADI_FLAG_PF1,ADI_TMR_GP_TIMER_0,(ADI_INT_PERIPHERAL_ID)NULL};

#elif defined(__ADSPBF561__)	

	// BF561 Ports (PF0=SCL,PF1=SDA) & Timer(Timer 2) used for Pseudo TWI
	adi_twi_pseudo_port port={ADI_FLAG_PF0,ADI_FLAG_PF1,ADI_TMR_GP_TIMER_2,(ADI_INT_PERIPHERAL_ID)NULL};

#endif

#if defined(__ADSPBF533__) || defined (__ADSPBF561__)

	// Pseudo TWI Configuration	to access video encoder/decoder registers
	// Run TWI clock at 100MHz & 50% Duty Cycle
	adi_twi_bit_rate rate={100,50};

	ADI_DEV_CMD_VALUE_PAIR TWIConfig[]={	// configuration table for the TWI driver
		{ ADI_TWI_CMD_SET_PSEUDO,			(void *)(&port)					},
		{ ADI_DEV_CMD_SET_DATAFLOW_METHOD,	(void *)ADI_DEV_MODE_SEQ_CHAINED},
		{ ADI_TWI_CMD_SET_FIFO,				(void *)0x0000					},
		{ ADI_TWI_CMD_SET_RATE,				(void *)(&rate)					},
		{ ADI_TWI_CMD_SET_LOSTARB,			(void *)1						},
		{ ADI_TWI_CMD_SET_ANAK,				(void *)0						},
		{ ADI_TWI_CMD_SET_DNAK,				(void *)0						},
		{ ADI_DEV_CMD_SET_DATAFLOW,			(void *)TRUE					},
		{ ADI_DEV_CMD_END,					NULL							}
	};                	        

#else
#error "*** ERROR: Application doesnot support this processor yet ***"
#endif

/*********************************************************************

    Function:       InitSystem

    Description:    Initialises EZ kit & system parameters

*********************************************************************/	
section("sdram0_bank1_nocache")
void InitSystem(void)
{
       
	
	OutputVideoMode		= ITU656_NTSC;		// Default output mode 
	
	MDMASharedFlag		= FALSE;				// Clear Shared MDMA flag (indicates this MDMA is free)
	SobelDone			= FALSE;				// Clear to start Edge Detection
	DisplayJPEG			= TRUE;					// Display original JPEG image by default
	UpdateVideoOutBufs	= FALSE;				// Clear Video out buffer update flag

	pYUVBuf				= &YUVBuf[0];			// Pointer to buffer containing YUV data of decoded JPEG image 	
	pJPEGImageBuf		= &JPEGImageBuf[0];		// Pointer to buffer containing original JPEG image in ITU656 frame format
	pEdgeDetectBuf		= &EdgeDetectBuf[0];	// Pointer to edge detected output buffer of ITU656 frame format
		
	return;
}

/*********************************************************************

    Function:       InitButton

    Description:    Initialises Blackfin Flags connected to
    				EZ-kit buttons

*********************************************************************/	
section("sdram0_bank1_nocache")
void InitButton (
	ADI_FLAG_ID	FlagID
){
    // open the flag    
    ezErrorCheck(adi_flag_Open (FlagID));
    // set flag direction as input
    ezErrorCheck(adi_flag_SetDirection(FlagID,ADI_FLAG_DIRECTION_INPUT));    
    // set flag trigger to sense button press & generate callback
    // senses on falling edge
    ezErrorCheck( adi_flag_InstallCallback	(	FlagID,							// Flag ID
#if defined(__ADSPBF561__)    
    											ADI_INT_PF0_15_A,				// peripheral ID
#elif defined(__ADSPBF533__)
												ADI_INT_PFA,					// peripheral ID
#endif												
    											ADI_FLAG_TRIGGER_RISING_EDGE,	// flag trigger mode
    											FALSE,							// not a wakeup flag
    											(void *)FlagID,					// Flag ID as client handle
    											DCBManagerHandle,				// Callback Manager
    											ButtonPressCallback				// callback function    						
    										));
    
	return;
}

/*********************************************************************

    Function:       InstallMemory

    Description:    Initialises & installs Cache & Heap for JPEG

*********************************************************************/
section("sdram0_bank1_nocache")
void InstallMemory()
{
	// Configure cache
	disable_data_cache();
	configure_data_cache_cplbs(codec_dcplbs_table);
	enable_data_cache();

	disable_instruction_cache();
	configure_instruction_cache_cplbs(codec_icplbs_table);
	enable_instruction_cache();
	
	// Install heaps
	InstallCodecHeaps();

	return;
}	

/*********************************************************************

    Function:       InstallSystemServices

    Description:    Installs system services

*********************************************************************/
// function implementations
section("sdram0_bank1_nocache")
void InstallSystemServices(void)
{

	// initialize the ezKit power,EBIU,DMA....
	adi_ssl_Init();	

	// enable and configure async memory
	ezInit (1);
	
	ezTurnOffAllLEDs ();	// Turn off all LEDs

	// Install the USB driver	
	InstallUSBdriver();
	
	
	return;
}

/*********************************************************************

    Function:       InstallUSBdriver

    Description:    Installs USB device

*********************************************************************/
section("sdram0_bank1_nocache")
void InstallUSBdriver(void)	
{

	void *pConfig = NULL;
									
	// initialize USB Core
    adi_usb_CoreInit(pConfig);

    // open the USB controller driver
    ezErrorCheck( adi_dev_Open(	adi_dev_ManagerHandle, 			// DevMgr handle 
								&ADI_USB_NET2272_Entrypoint,	// pdd entry point 
								0,                          	// device instance 
								(void*)1,						// client handle callback identifier 
								&PeripheralDevHandle,	        // device handle 
								ADI_DEV_DIRECTION_BIDIRECTIONAL,// data direction for this device 
								adi_dma_ManagerHandle,				// handle to DmaMgr for this device 
								NULL,							// handle to deferred callback service 
								NET2272_Callback));		  	    // client's callback function 
			
	// open the vendor specific bulk USB class driver
	ezErrorCheck( adi_dev_Open(	adi_dev_ManagerHandle,			// DevMgr handle
								&ADI_USB_VSBulk_Entrypoint,		// pdd entry point
								0,								// device instance
								(void*)0x1,						// client handle callback identifier
								&USBDeviceHandle,				// DevMgr handle for this device
								ADI_DEV_DIRECTION_BIDIRECTIONAL,// data direction for this device
								adi_dma_ManagerHandle,				// handle to DmaMgr for this device
								NULL,							// handle to deferred callback service
								NET2272_Callback));				// client's callback function
	
		
		
	// give USB controller driver handle to the class drivers.
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_USB_CMD_CLASS_SET_CONTROLLER_HANDLE, (void*)PeripheralDevHandle) );
	// configure the class driver
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_USB_CMD_CLASS_CONFIGURE, (void*)0) );
	// configure the mode
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void*)ADI_DEV_MODE_CHAINED) );
	// enable data flow
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)TRUE) );


	return;
}

/*********************************************************************

    Function:       EnableUSBdriver

    Description:    Enables USB device

*********************************************************************/
section("sdram0_bank1_nocache")
void EnableUSBdriver(void)	
{
	// enable USB
	ezErrorCheck( adi_dev_Control(PeripheralDevHandle, ADI_USB_CMD_ENABLE_USB, (void*)0 ));

	return;
}


/*********************************************************************

    Function:       InstallVideoEncoder
    Description:    Installs Video Encoder (ADV717x)

*********************************************************************/
section("sdram0_bank1_cache")
void InstallVideoEncoder()
{
 
 	// ADV717x register to switch btwn NTSC and PAL
    ADI_DEV_ACCESS_REGISTER ADV717x_Cfg;
	// ADV717x register address to switch btwn NTSC & PAL    
    ADV717x_Cfg.Address = ADV717x_MR0;
       
// For BF533 & BF561, pull the video encoder from reset mode
#if defined (__ADSPBF533__) || defined (__ADSPBF561__)
		
    	// Disable video decoder (ADV7183)
		printf ("Disabling Video Decoder (ADV7183)...\n");
    	ezDisableVideoDecoder();
		// Enable video encoder (ADV717x)
		printf ("Enabling Video Encoder (ADV717x)...\n\n");		
		ezEnableVideoEncoder();
		// Delay to allow codecs to sync
		printf ("Waiting for codecs to synchronize...\n\n");
   		ezDelay (VIDEO_CODEC_SYNC_DELAY);	     
   
#else
#error "*** ERROR: Application doesnot support this processor yet ***"
#endif

#if defined(__ADSPBF561__)
	
    // open the ADV7171 driver for video output
   	ezErrorCheck(adi_dev_Open(  adi_dev_ManagerHandle,     		// Dev manager Handle
                		   		&ADIADV7179EntryPoint,          // Device Entry point
                            	0,                              // Device number
	                            NULL,                           // No client handle
    	                        &ADV717xDeviceHandle,           // Device manager handle address
        	                    ADI_DEV_DIRECTION_OUTBOUND,     // Data Direction (video out)
            	                adi_dma_ManagerHandle,          // Handle to DMA Manager
                	            DCBManagerHandle,				// Callback Manager
                    	        VideoOutCallback));				// callback function
                    	        
#elif defined(__ADSPBF533__)

    // open the AD7179 driver for video output
    ezErrorCheck(adi_dev_Open(  adi_dev_ManagerHandle,            // Dev manager Handle
                 		   		&ADIADV7171EntryPoint,          // Device Entry point
                            	0,                              // Device number
	                            NULL,                           // No client handle
    	                        &ADV717xDeviceHandle,           // Device manager handle address
        	                    ADI_DEV_DIRECTION_OUTBOUND,     // Data Direction (video out)
            	                adi_dma_ManagerHandle,          // Handle to DMA Manager
                	            DCBManagerHandle,      			// Callback Manager
                    	        VideoOutCallback));				// callback function

#else
#error "*** ERROR: Processor not yet supported by the drivers ***"
#endif

	
	// TWI Configuration table
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_TWI_CONFIG_TABLE, (void *) TWIConfig));

	// ADV717x register Configuration    
	// Check Output video mode
	if (OutputVideoMode == ITU656_PAL)
	{
    	// Config ADV717x in PAL mode (SCFR with PAL config values)
		ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_SCF_REG, (void *)ADV717x_SCF_VALUE_PAL_BI));
		// MR0 value to set ADV717x in PAL mode
		ADV717x_Cfg.Data = 0x05;
		// Config other internal regs of ADV717x to PAL video mode
		ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_REGISTER_WRITE, (void *)&ADV717x_Cfg));
		// update video parameters - video mode is ITU656 PAL
  		// Video data per line (bytes)
		DataPerLine		= PAL_DATA_PER_LINE;
		// Number of lines per ITU656 video frame (bytes)
		FrameLines		= PAL_FRAME_LINES;
		// Number of Active lines per ITU656 video frame (bytes)
		ActiveFrameLines= PAL_ACTIVE_LINES;
		// Number of bytes to skip to reach active video data in a single line
		ActiveVideoSkip	= PAL_ACTIVE_VIDEO_SKIP;
		// Number of bytes to skip to reach active video frame field1 (odd frame)
		Field1Skip		= PAL_F1_SKIP;
		// Number of bytes to skip to reach active video frame field2 (even frame)
		Field2Skip		= PAL_F2_SKIP;
		// Update Sobel parameters
		// Active field spilt to # number of blocks
		SobelFieldBlocks= SOBEL_PAL_FIELD_BLOCKS;
		// Number of Sobel blocks per active field
		SobelBlockCount	= (SOBEL_ACTIVE_VIDEO_BLOCKS * SOBEL_PAL_FIELD_BLOCKS);
		// Sobel block - row size
		SobelRowSize	= SOBEL_PAL_ROW_SIZE;	
	}
	else // VideoMode must be ITU656_NTSC
	{
    	// Config ADV717x in PAL mode (SCFR with NTSC config values)
		ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_SCF_REG, (void *)ADV717x_SCF_VALUE_NTSC));
		// MR0 value to set ADV717x in NTSC mode
		ADV717x_Cfg.Data = 0x00;				
		// Config other internal regs of ADV717x to NTSC video mode
		ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_REGISTER_WRITE, (void *)&ADV717x_Cfg));		
   		// update video parameters - video mode is ITU656 NTSC
   		// Video data per line (bytes)
		DataPerLine		= NTSC_DATA_PER_LINE;
		// Number of lines per ITU656 video frame (bytes)
		FrameLines		= NTSC_FRAME_LINES;
		// Number of Active lines per ITU656 video frame (bytes)
		ActiveFrameLines= NTSC_ACTIVE_LINES;
		// Number of bytes to skip to reach active video data in a single line
		ActiveVideoSkip	= NTSC_ACTIVE_VIDEO_SKIP;
		// Number of bytes to skip to reach active video frame field1 (odd frame)
		Field1Skip		= NTSC_F1_SKIP;
		// Number of bytes to skip to reach active video frame field2 (even frame)
		Field2Skip		= NTSC_F2_SKIP;
		// Update Sobel parameters
		// Active field spilt to # number of blocks
		SobelFieldBlocks= SOBEL_NTSC_FIELD_BLOCKS;
		// Number of Sobel blocks per active field
		SobelBlockCount	= (SOBEL_ACTIVE_VIDEO_BLOCKS * SOBEL_NTSC_FIELD_BLOCKS);
		// Sobel block - row size
		SobelRowSize	= SOBEL_NTSC_ROW_SIZE;
	}

// Set PPI port for video out
#if defined(__ADSPBF561__)
	// BF561 - PPI 1 used for Video out
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_DEVICE_NUMBER, (void *) 1));
#else
	// BF533 - PPI 0 used for Video out
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_DEVICE_NUMBER, (void *) 0));
#endif

	// Open PPI device for video out
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_STATUS, (void *)ADI_ADV717x_PPI_OPEN));	
	// Set Dataflow method
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *) ADI_DEV_MODE_CHAINED_LOOPBACK));

	// Enable streaming
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_STREAMING, (void *) TRUE));
			
	return;
}

/*********************************************************************

	Function:		PerformIO

	Description:	Performs USB I/O

*********************************************************************/
section("sdram0_bank1_cache")
u32	PerformUSBIO()
{
    u32	Result = ADI_DEV_RESULT_SUCCESS;
	int	Codec_return_value = CODEC_SUCCESS;

	// Set USB as default I/O device
	Result = ConfigUSB_IO(USBDeviceHandle);
		
	// continue only if USB IO config is a success
	if (Result == ADI_DEV_RESULT_SUCCESS)
	{
		// Check Coding Algorithm
		if (Coding_Algorithm == CODING_ALGORITHM_JPEG)
			Codec_return_value = do_JPEG_Decode();	// start JPEG decoding
		else
			fprintf( fperr, "Error: Invalid coding parameters\n" );

		// Check if JPEG-MJPEG Encode/Decode operation is result
		if (Codec_return_value) 
			fprintf(fperr, "Codec aborted abnormally\n");

		// Done with JPEG-MJPEG Encoding/Decoding. Stop USB I/O
    	StopUSB_IO (USBDeviceHandle);
	}
	
	return (Result);
}

/*********************************************************************

	Function:		SetCodecParameters

	Description:	Set Device (Codec) parameters in relevance to
					selected user option

*********************************************************************/
section("sdram0_bank1_cache")
u32	SetCodecParameters (u32 *UserOptions)
{
    
	u32	Result = ADI_DEV_RESULT_SUCCESS;	// default return code
	
    // switch user option
    switch ((char)UserOptions[0])
    {
        // Case (JPEG Decoding)
        case '1':
        
			// Decode JPEG file(s)
			Coding_Algorithm = CODING_ALGORITHM_JPEG;
				
			// Video output mode?
			if ((char)UserOptions[1] == '1')
				// NTSC video out
				OutputVideoMode = ITU656_NTSC;
			else if ((char)UserOptions[1] == '2')
				// PAL video out
				OutputVideoMode = ITU656_PAL;
			else
				// default as NTSC video out
				OutputVideoMode = ITU656_NTSC;

			// JPEG/MJPEG Decoder file settings?
			if ((char)UserOptions[2] == '1')
				// Prompt for file location & settings
				File_Settings = PROMPT_FOR_FILE_SETTINGS;
			else if ((char)UserOptions[2] == '2')
				// Retrieve settings from '.txt' files under project directory
				File_Settings = GET_FILE_SETTINGS_FROM_TXT_FILE;
			else
				// Retrieve settings from '.txt' files under project directory
				File_Settings = GET_FILE_SETTINGS_FROM_TXT_FILE;
			
			break;
	
		default:
			Result = ADI_DEV_RESULT_FAILED;
			break;
    }
    
    return (Result);
}

/*********************************************************************

	Function:		ExceptionHandler
					HWErrorHandler

	Description:	We should never get an exception or hardware error,
					but just in case we'll catch them and simply turn
					on all the LEDS should one ever occur.

*********************************************************************/
section("sdram0_bank1_nocache")
static ADI_INT_HANDLER(ExceptionHandler)	// exception handler
{
	printf("\nException Error occured!\n");
	ezTurnOnAllLEDs();
	while (1) ;
}

section("sdram0_bank1_nocache")
static ADI_INT_HANDLER(HWErrorHandler)		// hardware error handler
{
	printf("\nHardware Error occured!\n");
	ezTurnOnAllLEDs();
	while (1) ;
}

/*********************************************************************

    Function:       VideoOutCallback

    Description:    Video Out callback function (ADV717x)

*********************************************************************/
section ("Callback_Code")
static void VideoOutCallback(
	void *AppHandle,
	u32  Event,
	void *pArg
){         
	// Call corresponding service routine   
	VideoOut(AppHandle,Event,pArg);
	return;
}

/*********************************************************************

	Function:		NET2272_Callback

	Description:	USB I/O callback function for Net2272 device

*********************************************************************/
section ("Callback_Code")
static void NET2272_Callback(
	void 	*AppHandle,
	u32  	Event,
	void 	*pArg
){
    // Call corresponding service routine   
	USB_Callback(AppHandle,Event,pArg);
	return;
}

/*********************************************************************

    Function:       ButtonPressCallback

    Description:    Ez-Kit button press callback function
    				Corresponding push button related operations
    				will start only after the button is released

*********************************************************************/
section ("Callback_Code")
static void ButtonPressCallback	(	
	void *AppHandle,
	u32  Event,
	void *pArg
){
	switch (Event)
	{
       	// Case (flag interrupt occurred)
        case ADI_FLAG_EVENT_CALLBACK:

        	// check if toggle display button was been pressed
        	if ((int)pArg == TOGGLE_DISPLAY_BUTTON)
        	{
        	    // if so, toggle DisplayJPEG flag
        	    DisplayJPEG ^= 1;
        	    // mark flag to update video out buffers
				UpdateVideoOutBufs = TRUE;
        	}
        	        
			break;

		default:	// other events
         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
	}
}

/*****/
