/*****************************************************************************

Copyright (c) 2006 Analog Devices, Inc.	All	Rights Reserved. 

This software is proprietary and confidential to Analog	Devices, Inc. 
and	its	licensors.

*****************************************************************************

$RCSfile: JPEGImageEdgeDetection_Callbacks.c,v $
$Revision: 1.2 $
$Date: 2007/10/17 06:52:37 $

Project:	BlackfinSDK (Edge Detection on JPEG Image)
Title:		System Service callback routines
Author(s):	bmk
Revised	by:	

Description:
			PPI/Pushbutton callback service routines 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_Callbacks.c,v $
Revision 1.2  2007/10/17 06:52:37  randreol
Port to VDSP 5.0

Revision 1.1  2007/05/22 18:37:55  gstephan
Creating VDSP 5.00 release folder.  Created from the head copy of
VDSP 4.50 release folder.

Revision 1.4  2006/11/20 05:56:50  bmk
Merged BF533 & BF561
Fixed BF561 cache issue

Revision 1.3  2006/11/03 07:11:20  bmk
SDK 2.0  files - Initial Entry

******************************************************************************
Include files
*****************************************************************************/

#include <JPEGImageEdgeDetection_System.h>	// system includes
#include <drivers\encoder\adi_adv717x.h>	// video encoder driver includes
#include <drivers\usb\usb_core\adi_usb_objects.h>
#include <drivers\usb\usb_core\adi_usb_core.h>
#include <adi_usbio_blackfin.h>				// USB I/O functions for Blackfin

/*****************
Globals
*****************/

// 2D Video Output buffers
static	ADI_DEV_2D_BUFFER VideoOut2DBuf[NUM_VIDEO_OUT_BUFS];

/*********************************************************************

    Function:       VideoOut

    Description:    Video Out callback service routine

*********************************************************************/
section ("Callback_Code")
void VideoOut(
	void *AppHandle,
	u32  Event,
	void *pArg
){            
    // Case of (event type)
    switch (Event) 
    {
        
        // Case (buffer processed)
        case ADI_DEV_EVENT_BUFFER_PROCESSED:
        
			// check UpdateVideoOutBufs flag
        	if (UpdateVideoOutBufs)
        	{
				// Disable Video out dataflow before updating Video out buffer
				ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)FALSE));        	    
				// update the video out buffer
				SubmitVideoOutBufs (1);
				// clear flag as video output has been updated
				UpdateVideoOutBufs = FALSE;
        	}

            break;	

        // Case (DMA Error)
        case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
                      
		default:	// other events
         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
    }
}

/*********************************************************************

	Function:		USB_Callback

	Description:	USB I/O callback function

*********************************************************************/
section ("Callback_Code")
void USB_Callback(
	void 	*AppHandle,
	u32  	Event,
	void 	*pArg
){
	switch (Event)
	{
		// case (NET2272 device configured)
		case ADI_USB_EVENT_SET_CONFIG:

			// pArg holds the configuration number
			if (0x01 == (u32)pArg)
				Net2272Ready = TRUE;	// Net2272 configuration Done
			else
				Net2272Ready = FALSE;	// Net2272 configuration Failed

			break;

		// case (reset signaling detected)
		case ADI_USB_EVENT_ROOT_PORT_RESET:
		// case (cable unplugged)
		case ADI_USB_EVENT_VBUS_FALSE:
		
			// Net2272 yet to be configured
			Net2272Ready = FALSE;

			break;
	    
		// case (USB data Tx complete)
		case ADI_USB_EVENT_DATA_TX:

			Net2272TxDone = TRUE;	// Tx Done

			break;

		// case (USB data Rx complete)
		case ADI_USB_EVENT_DATA_RX:

			Net2272RxDone = TRUE;	// Rx Done

			break;
									
		// CASE (an error)
		case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

			// turn on all LEDs and wait for help
			ezTurnOnAllLEDs();
			while (1) ;
		
		default:
			break;
	}

	return;
}

/*********************************************************************

    Function:       SubmitVideoOutBuffers
    Description:    Prepares and submits Video Output Buffers (for ADV717x)

*********************************************************************/
section("sdram0_bank1_cache")
void	SubmitVideoOutBufs	(
	u8	NumBufs				// number of video out buffers to be submitted
){
    
	int i;
	
	// Close the PPI device allocated for video out (to submit new buffer(s))
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_STATUS, (void *)ADI_ADV717x_PPI_CLOSE));
	
	// if DisplayJPEG flag is FALSE, submit Edge Detected buffer for video out
	if (DisplayJPEG == FALSE)
		VideoOut2DBuf[0].Data 	= (void*)(pEdgeDetectBuf);
		
	else	// display decoded JPEG image
		VideoOut2DBuf[0].Data 	= (void*)(pJPEGImageBuf);

	for (i=0;i<NumBufs;i++)
	{
		// Configure Video Out Buffer 
		VideoOut2DBuf[i].ElementWidth 		= DMA_BUS_SIZE;					// 32 bit transfer for BF561/16 bit transfer for BF533
		VideoOut2DBuf[i].XModify 			= DMA_BUS_SIZE;					// 4 bytes increment for BF561/2 bytes increment for BF533
		VideoOut2DBuf[i].XCount 			= (DataPerLine/DMA_BUS_SIZE);	// Video Data per line
		VideoOut2DBuf[i].YCount 			= FrameLines;					// Total number of lines in a frame
		VideoOut2DBuf[i].YModify 			= DMA_BUS_SIZE;					// 4 bytes increment for BF561/2 bytes increment for BF533
		VideoOut2DBuf[i].CallbackParameter	= &VideoOut2DBuf[i];			// call back parameter
		VideoOut2DBuf[i].pNext				= NULL;							
	}
	
	// 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));

	for (i=0;i<NumBufs;i++)					
		// submit the video output buffer(s) to PPI for display
		ezErrorCheck(adi_dev_Write(ADV717xDeviceHandle, ADI_DEV_2D, (ADI_DEV_BUFFER *)&VideoOut2DBuf[i]));

	// Enable Video out dataflow
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)TRUE));

	return;
}

/*****/
