/*****************************************************************************\

  File Name: MediaPlayer.c
  
  Description: This is the application main file for the media player.
               
               
  Copyright(c) 2007 Analog Devices Incorporated. All Rights Reserved.
  
  Revision History: 
  

\*****************************************************************************/
#define VER_MAJOR 0
#define VER_MINOR 10

/*****************************************************************************\

  Includes:

\*****************************************************************************/

#include <services/services.h>				/* system service includes 	*/
#include <drivers/adi_dev.h>			
#include <SDK-ezkitutilities.h>

#include "adi_ssl_init.h"

#include <stdio.h>
#include "mediaplayer.h"




/****************************************************************************
*
*	  Defines
*
****************************************************************************/
//#define USE_DEFERRED_CALLBACKS - be sure to not install DCB at IVG14 - IVG14 reserved by uCOS



/* define Queue Size */
#define SIZE_DCB_QUEUE ((ADI_DCB_ENTRY_SIZE)*4)

/*****************************************************************************\

  Declarations:

\*****************************************************************************/

/* DCB queue Data memory  */
static u8 DCBMgrQueue1[SIZE_DCB_QUEUE];  

/* handle to the callback service */
ADI_DCB_HANDLE adi_dcb_QueueHandle;

section("sdram0_bank2_stack") 
static OS_STK stackmediaplayer[THRD_STACKSIZE * sizeof(OS_STK)];
section("sdram0_bank2_stack") static OS_STK stack[BOOT_THREAD_STACK_SIZE * sizeof(OS_STK)];


u32 MPPState;
u32 MPPMode;
volatile bool StopButton = FALSE;
ADI_SEM_HANDLE PauseSemaphoreHandle;
volatile bool PauseButton = FALSE;


/*****************************************************************************\

  Externs:

\*****************************************************************************/
   
/* CRT Device Driver definitions for Console I/0 */
//extern DevEntry adi_console_entry;

/* queue to hold message from INPUT-devices  */
extern OS_EVENT *mediaplayer_Q;	

/* Empty void* array to hold Q messages */
extern void* mediaplayer_arr[SZ_MSGQ_APP_CMD];

/* queue to hold message from INPUT-devices  */
extern OS_EVENT *mgr_application_Q;	

/* queue to hold APP to FILEMGR commands */
extern OS_EVENT *mgr_filesystem_Q;	

extern semaphore_t mpEvents[];


/****************************************************************************
*
*	  Function prototypes
*
****************************************************************************/

void ucos_ii_boot_thread (void* p);

static int init_ezKit(void);
int init_devicecontrol(void);
void init_managers(void);

void player_mode(void);


void launch_mediaplayer(void);
void thread_mediaplayer(void *pEvent);

static ADI_INT_HANDLER(ExceptionHandler);	/* exception handler */
static ADI_INT_HANDLER(HWErrorHandler);	/* HW error handler */



/****************************************************************************
*
*	  Main program functions
*
****************************************************************************/
void main(void)
{

	INT8U OS_result;

	/* Initialize uCOS-II (app_init is called from within here) */
	OSInit();		
		 
	/* Boot Thread (Priority: OS_LOWEST_PRIO-2)					*/
	/* Description Main thread installs core timer ISR and 		*/
	/* launches all other threads								*/
	if ( (OS_result = OSTaskCreateExt(ucos_ii_boot_thread, 
						(void *)NULL, 
						&stack[BOOT_THREAD_STACK_SIZE-1], 
						(OS_BOOT_THREAD_PRIO), 
						(OS_BOOT_THREAD_ID), 
						&stack[0], 
						BOOT_THREAD_STACK_SIZE, 
						(void *)0, 
						OS_TASK_OPT_STK_CHK+OS_TASK_OPT_STK_CLR )) )
		while(1);

	/* Start uCOS-II - this function call never returns */
	OSStart();					

}// end of main


/*********************************************************************
	Function:	launch_threads											
																			
	Parameters:	None														
																			
	Return:		None														
																			
	Description:	This is called from the ucos_ii_boot_thread and 
	launches the application threads.
*********************************************************************/
section ("sdram0_bank2_thread")
void ucos_ii_boot_thread (void* p)
{
	INT8U OS_result;
	OS_STK_DATA StkData;
	/* 100 mS tick.. Callback is O/S tick function */
	CoreTimerInit(135, 100000);	
	
	/* call the application's main routine */
 	player_mode();

	/* Delete itself */
	if ( (OS_result = OSTaskDel(OS_BOOT_THREAD_PRIO)) )
		while(1);
}	/* end of launch_threads */

			
/*********************************************************************

 	Function:	player_mode											
																			
 																			
 	Description:	Application Main function
 
*********************************************************************/
//section ("sdram0_bank3_app")
void player_mode(void)
{
    u32 Result= ADI_DEV_RESULT_SUCCESS;

   	/* initialize Blackfin system services */
    if (Result == ADI_DEV_RESULT_SUCCESS)	 
    Result = init_ezKit();
    
    
    	
 	/* initialize IO devices */    	
	if (Result == ADI_DEV_RESULT_SUCCESS)	     
		Result = init_devicecontrol();   
		
    /* Initialise managers */
	init_managers();
	
    /* set stdout as a line buffer */
	//setvbuf(stdout,NULL,_IOLBF,BUFSIZ);


	printf("MediaPlayer: uCOS-II, SSL, FileSystem, IODevices and managers started...\n"); //Start of app...

	
}

/*********************************************************************

	Function:		init_ezKit

	Description:	Initializes the necessary system services.  

*********************************************************************/
//section ("sdram0_bank3_app")
static int init_ezKit(void) {
	    
	u32 i, Result= ADI_DEV_RESULT_SUCCESS;
	u32 fcclk,fsclk,fvco;
	
    /* initialise the ezKit power,EBIU,DMA.... */
    if (Result == ADI_DEV_RESULT_SUCCESS) 
		Result = adi_ssl_Init();
		
    /* Workaround anomaly 05000365 */
#if defined(__ADSP_MOAB__) 	
	*pEBIU_DDRQUE = 0x7115;
#endif

#if defined(__ADSP_KOOKABURRA__)/* BF527 EZKit */
	/* assigned priority for external memory fetches from the CORE to DMA */
	*pEBIU_AMGCTL = 0x01F9;
#endif	
		
 		
#if defined(USE_DEFERRED_CALLBACKS)
	if (Result == ADI_DEV_RESULT_SUCCESS) 
	/* Open the DCB manager & setup a queue */
	Result = adi_dcb_Open(13, 
                          &DCBMgrData[ADI_DCB_QUEUE_SIZE], 
	                      (ADI_DCB_ENTRY_SIZE*DCB_QUEUE_ENTRIES),
	                       &ResponseCount, 
	                       &adi_dcb_QueueHandle);
#endif
		
	/* enable all LED's */
	for(i=0;i<EZ_NUM_LEDS;i++){
		ezInitLED(i);
	}
	
	ezTurnOffAllLEDs();
	
/*	Fix related to TAR 35129 Change of speed */
#if defined(__ADSP_MOAB__)  
        u32 imask = cli();
        /* manually set the PLL and VR for 400/133: */
        *pPLL_CTL = 0x2000; 
        ssync();
        idle();
        *pPLL_DIV = 0x3;
        ssync();
        *pVR_CTL = 0x40EB;
        ssync();
        idle(); 
        sti(imask);
        /* set the config structure to match the registers */
        adi_pwr_SetFreq( 400, 133, (ADI_PWR_DF)1);
#else        
		/* cclk = 600000000Hz, sclk = 120000000Hz */
		adi_pwr_SetFreq( 0, 0, ADI_PWR_DF_NONE);
#endif
 
	/* hook the exception interrupt*/
	if (Result == ADI_DEV_RESULT_SUCCESS) 
		Result = adi_int_CECHook(3, ExceptionHandler, NULL, FALSE);

#if 0  
    /* hook the hardware error*/
    if (Result == ADI_DEV_RESULT_SUCCESS)
		Result = adi_int_CECHook(5, HWErrorHandler, NULL, FALSE);
#endif    

  						
	// return
    return (Result);

}

/*********************************************************************

	Function:		init_managers

	Description:	Initialize managers.  

*********************************************************************/
static void init_managers(void)
{
	/* Initialise the media players's managers */
	init_mgr_display();
	init_mgr_application();
	init_mgr_player();
	init_mgr_filesystem();
	
}


/*********************************************************************
 
	Function:	ExceptionHandler, HWErrorHandler
																			
																			
 	Description:	ExceptionHandler and HWErrorHandler
 	
*********************************************************************/
static ADI_INT_HANDLER(ExceptionHandler)	        /* exception handler        */
{
	printf("Exception!\n");
	while(1);
}

static ADI_INT_HANDLER(HWErrorHandler)		        /* hardware error handler   */
{
	printf("HWerror!\n");
	while(1);
}















