/*****************************************************************************
Copyright (c) 2006 Analog Devices.  All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  
******************************************************************************

$File: main.c $
$Revision: 1.5 $
$Date: 2008/03/17 13:49:38 $

Project:    Audio Kit
Title:      Generic demo Decoder Kit
Author(s):  dwu
Revised by: ss

Description:
	
References:
	AD1836A Preliminary Technical Data
	ADSP-BF533 Hardware reference
	ADSP-BF533 EZ-Kit Lite Schematic

*********************************************************************************			
	
Modification History:
====================
$Log: audio_player_main.c,v $
Revision 1.5  2008/03/17 13:49:38  gstephan
Updates for SDK 3.00

Revision 1.4  2007/11/13 04:32:52  randreol
Modify USBDeviceHandle declaration

Revision 1.3  2007/08/29 04:37:33  randreol
Modified to support porting to new USB driver

Revision 1.2  2007/06/20 02:14:11  randreol
Clean up

Revision 1.1  2007/05/23 02:06:17  randreol
New Audio Application

Revision 1.3  2006/12/07 02:13:18  randreol
Added async memory enable code

Revision 1.2  2006/12/04 06:40:56  randreol
Clean up

Revision 1.1  2006/12/04 04:48:08  randreol
New BF533 MP3 application with 1836 codec intial rev

Revision 1.1  2006/11/30 05:35:33  randreol
modified to support multiple codecs

Revision 1.2  2006/11/10 07:01:33  randreol
New ID for host app

Revision 1.1  2006/11/10 04:53:45  randreol
Initial rev

Revision 1.1.1.1  2006/05/09 04:01:29  dwu
USB based audio demonstration


	
*********************************************************************************/
#include <stdio.h>
#include "system.h"
#include "codec.h"

#include <SDK-ezkitutilities.h> 	// EZ-Kit utilities
#include <adi_ssl_Init.h>
#include <usb_services.h>
#include <audio_codec.h>

#include <adi_usbio_blackfin.h>					// USB I/O functions for Blackfin
#include <adi_usbcmd.h>							// supported USB commands


// 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 <drivers\usb\controller\peripheral\plx\net2272\adi_usb_net2272.h>				// net2272 driver header file


#pragma align 4

char FwVersionInfo[NUM_VERSION_STRINGS][MAX_VERSION_STRING_LEN] = {
					"Audio_Player",   				// Application name 
					"ADSP-BF53x",				    // target processor
					__DATE__,					    // build date
					__TIME__,					    // build time
					"01.00.00",					    // version number
					"None"};                        // hardware info


// USB data structures
section ("L1_data_b")
volatile static ADI_DEV_1D_BUFFER        UsbcbBuffer;		// one-dimensional buffer for processing usbcb
section ("L1_data_b")
volatile static USBCB                    usbcb;				// USB command block
section ("L1_data_b")
volatile static USBCB                    *pusbcb = &usbcb;   // pointer to USBCB

ADI_DEV_DEVICE_HANDLE    USBDeviceHandle;		// USB driver device handle

/* DCB Managed Data */
static u8 DCBMgrData[ADI_DCB_QUEUE_SIZE + (ADI_DCB_ENTRY_SIZE)*8];

ADI_DCB_HANDLE          DCBManagerHandle;
	


void main()
{
	unsigned int Result;

    PDEVICE_DESCRIPTOR pDevDesc;		// device descriptor ptr	
    	
	// enable and configure async memory
	ezInit(1);

	// initialize the ezKit power,EBIU,DMA....
	adi_ssl_Init();
		
	adi_pwr_SetFreq(0,0,ADI_PWR_DF_ON);
		
	// install the USB driver
	//InstallUSBdriver(&USB_DevHandle);
	InstallUSBdriver();
			
	/* 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
	
		
	EnableUSBdriver();

	install_audiodriver(); // install audio driver


	// initialize the USB data buffer
	UsbcbBuffer.Data = (void*)&usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.ProcessedFlag = FALSE;
	UsbcbBuffer.ProcessedElementCount = 0;
	UsbcbBuffer.pNext = NULL;
	
	// get endpoint IDs
	Result = ConfigureEndpoints();
	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Endpoint Configuration Failed!\n");
	
	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 *)&UsbcbBuffer,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 );
		    		break;
	
		    	// get the firmware version
			    case GET_FW_VERSION:
			    	// Configure the 1D USB data buffer to send firmware version string info to Host
					UsbcbBuffer.Data = (u8*)FwVersionInfo;	// point to Firmware version string
					UsbcbBuffer.ElementCount = pusbcb->ulCount;	// size of Firmware version string
					
			    	// send the Firmware Version
			    	Result = USB_Write ( USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer,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
					UsbcbBuffer.Data = (void*)&usbcb;					// always point to usb command block as default
					UsbcbBuffer.ElementCount = sizeof(usbcb);	// Transfer/Receive data of usb command block size
					UsbcbBuffer.ProcessedFlag = FALSE;			// clear processed flag
								    
			        break;

		    	// get the Application ID
			    case GET_APP_ID:
			    	// load the application ID unique to Audio Decoder app
			    	pusbcb->ulData	= ADI_AUDIO_PLAYER;
			    	// send the application ID to PC Host
			    	Result = USB_Write(USBDeviceHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer,TRUE);	    	
			    	if (Result != ADI_DEV_RESULT_SUCCESS)	printf ("Failed to send Application ID!\n");

			        break;
			        			        	
			    // perform IO over USB
			    case USBIO_START:
					Result = PerformUSBIO( USBDeviceHandle );
			        break;
	
				// start codec according to input parameters
			    case SET_PARAMS:

		    		// 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 *)&UsbcbBuffer,TRUE);
			    						
				    break;
			        	
				// unsupported command
			    default:			   		
			        break;
	    	}
		}
	}
	
}

