/*****************************************************************************
Copyright(c) 2005 Analog Devices, Inc.  All Rights Reserved. This software is 
proprietary and confidential to Analog Devices, Inc. and its licensors.
******************************************************************************

$RCSfile: JPEGImageEdgeDetection_main.c,v $
$Revision: 1.5 $
$Date: 2007/11/21 06:12:43 $

Project:	BlackfinSDK (JPEG Image Edge Detection)
Title:		main program 
Author(s):	bmk
Revised by: bmk

Description:
			Main program for JPEG Image Edge Detection

References:
			None

******************************************************************************
Tab Setting:			4

Target Processor:		ADSP-BF5xx
Target Tools Revision:	ADSP VisualDSP++ v4.5
******************************************************************************

Modification History:
====================
$Log: JPEGImageEdgeDetection_main.c,v $
Revision 1.5  2007/11/21 06:12:43  randreol
Fix for ECO 2007101 SCLK must be set to no greater than 100 MHz

Revision 1.4  2007/11/15 04:00:55  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.2  2006/11/20 05:56:50  bmk
Merged BF533 & BF561
Fixed BF561 cache issue

Revision 1.1  2006/11/03 07:11:18  bmk
SDK 2.0  files - Initial Entry

*****************************************************************************/

#pragma default_section(CODE, "sdram0_bank1") 

/****************
Includes
****************/

#include <JPEGImageEdgeDetection_System.h>		// system includes

// USB includes
#include <drivers\usb\usb_core\adi_usb_objects.h>
#include <drivers\usb\usb_core\adi_usb_core.h>
#include <drivers\usb\usb_core\adi_usb_ids.h>
#include <drivers\usb\class\peripheral\vendor_specific\adi\bulkadi\adi_usb_bulkadi.h>		// adi bulk driver header file
#include <adi_usbcmd.h>							// supported USB commands
#include <adi_usbio_blackfin.h>					// USB I/O functions for Blackfin

/********************************
Firmware version string info
********************************/
#pragma align 4
char FwVersionInfo[NUM_VERSION_STRINGS][MAX_VERSION_STRING_LEN] = {
					"JPEG Image Edge Detection",// Application name    
#if defined(__ADSPBF533__)
					"ADSP-BF533",				// target processor
#elif defined(__ADSPBF561__)
					"ADSP-BF561",				// target processor
#endif
					__DATE__,					// build date
					__TIME__,					// build time
					"02.00.00",					// version number
#if defined(__ADSPBF533__)
					"Onboard Video Codecs in ADSP-BF533 EZ-KIT Lite"};	// Video Interface
#elif defined(__ADSPBF561__)
					"Onboard Video Codecs in ADSP-BF561 EZ-KIT Lite"};	// Video Interface
#endif

/********************************
USB data structures
********************************/

// one-dimensional buffer to Tx/Rx Data to/from USB host
static ADI_DEV_1D_BUFFER HostBuffer;
section ("L1_data_b")
static USBCB usbcb;								// USB command block
static USBCB *pusbcb = &usbcb;					// pointer to USBCB

/*********************************************************************

	Function:		Main

	Description:	Main Function for JPEG Image Edge Detection

*********************************************************************/
// place this part of code in L1
section ("App_Code_L1")
void main(void)
{	

	u32 Result = ADI_DEV_RESULT_SUCCESS;	// default result
	
	PDEVICE_DESCRIPTOR pDevDesc;		// device descriptor ptr
	
	// USB device not configured yet
	Net2272Ready = FALSE;
		
	// Initialises system parameters
	InitSystem();	
	// Initialises & calls Cache/Heap install for JPEG	
	InstallMemory();		
	// Install system services	
	InstallSystemServices();		
	
#if defined(__ADSPBF561__) 
	// Workaround for EzKit 561 and USB/LAN Extender Rev 2.0 BOM 1.6 see ECO 2007101
	// SCLK must be set to no greater than 100 MHz
	adi_pwr_SetFreq( 500000000, 100000000, ADI_PWR_DF_NONE );
#endif		
		
	/* get a pointer to the device descriptor which is maintained by the usb core */
	pDevDesc = adi_usb_GetDeviceDescriptor();
	if (!pDevDesc)
	{
		idle();//failure();
	}

	/* customize the device descriptor for this device */
	pDevDesc->wIdVendor = USB_VID_ADI_TOOLS;
#if defined(__ADSPBF533__)
	pDevDesc->wIdProduct = USB_PID_BF533KIT_NET2272EXT_BULK;
#elif defined(__ADSPBF537__)
	pDevDesc->wIdProduct = USB_PID_BF537KIT_NET2272EXT_BULK;
#elif defined(__ADSPBF561__)
	pDevDesc->wIdProduct = USB_PID_BF561KIT_NET2272EXT_BULK;
#else
#error *** Processor not supported ***
#endif
	
	// Enable USB
	EnableUSBdriver();
	
	// Initialise Blackfin Port flag connected to Ez-Kit Toggle Display button
	InitButton(TOGGLE_DISPLAY_BUTTON);	
	
	// initialize the USB Host buffer to process usbcb		
	HostBuffer.Data = (void*)&usbcb;					// always point to usb command block as default
	HostBuffer.ElementCount = sizeof(usbcb);	// Transfer/Receive data of usb command block size
	HostBuffer.ElementWidth = 1;				// Tx/Rx 1 byte at a time 
	HostBuffer.CallbackParameter = NULL;		// Callback parameter
	HostBuffer.ProcessedFlag = FALSE;
	HostBuffer.ProcessedElementCount = 0;
	HostBuffer.pNext = NULL;

	// get endpoint IDs
	Result = ConfigureEndpoints();
	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Endpoint Configuration Failed!\n");	
	
			    	
	// wait for commands over USB
	while(1)
	{
	    // wait until USB device is configured
	    while (!Net2272Ready);
	    	asm("nop;");
	    
		// wait for a USB command block from the host indicating what function we should perform
		Result = USB_Read(USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&HostBuffer,TRUE);
		
		if ( ADI_DEV_RESULT_SUCCESS == Result )
		{
			// switch on the command we just received
			switch( pusbcb->ulCommand )
		    {
		    	// host is asking if we support a given command
		    	case QUERY_SUPPORT:
		    		Result = QueryUSBSupport( USBDeviceHandle, pusbcb->ulData );
		    		if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Query USB support Failed!\n");
		    			
		    		break;
	
		    	// get the firmware version
			    case GET_FW_VERSION:
			    	
			    	// Configure the 1D USB data buffer to send firmware version string info to Host
					HostBuffer.Data = (u8*)FwVersionInfo;	// point to Firmware version string
					HostBuffer.ElementCount = pusbcb->ulCount;	// size of Firmware version string
					
			    	// send the Firmware Version
			    	Result = USB_Write ( USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&HostBuffer,TRUE);			    	
			    	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Failed to send Firmware Version!\n");
			    	
			    	// Reset the USB Host buffer to point to USB command block
					HostBuffer.Data = (void*)&usbcb;					// always point to usb command block as default
					HostBuffer.ElementCount = sizeof(usbcb);	// Transfer/Receive data of usb command block size
					HostBuffer.ProcessedFlag = FALSE;			// clear processed flag
	
			        break;

		    	// get the Application ID
			    case GET_APP_ID:
				    // load the application ID unique to JPEG-MJPEG app
			    	pusbcb->ulData	= ADI_JPEG_IMAGE_EDGE_DETECTION;
			    	// send the application ID to PC Host
			    	Result = USB_Write(USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&HostBuffer,TRUE);	    	
			    	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Failed to send Application ID!\n");

			        break;
			        			        	
			    // perform USB IO
			    case USBIO_START:			    
					Result= PerformUSBIO();
			    	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("USB I/O operation failed!\n");
			    	
			        break;
	
				// set codec parameters corresponding to user selected option
			    case SET_PARAMS:
			    	// set codec parameters			    
			    	Result = SetCodecParameters((u32*) &pusbcb->ulParams[0]);
			    	// check the result
			    	if (Result != ADI_DEV_RESULT_SUCCESS)
			    	{
			    		// inform PC host as this operation has failed
			    		pusbcb->ulData	= OPERATION_FAILED;			    		
		    			printf ("Failed to set Codec Parameters!\n");
			    	}
		    		else
		    			// confirm Host as Device parameters were set successfully
						pusbcb->ulData	= OPERATION_PASSED;

			    	// pass the result to PC host
			    	Result = USB_Write(USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&HostBuffer,TRUE);

				    break;
			        	
				// unsupported command
			    default:
			        break;
	    	}
		}
	}
}
/*****/

