#include "def21369.h"
#include "Cdef21369.h"
#include "sru21369.h"
#include "21369_SSL_Parallel_FLASH.h"

int app_offset;

void firmware_upgrade()
{
	
	
	int offset;
	
	int temp,i=0,j=0;
	
	int temp1;
	
	int length_ldr;
	
	int baud;
	
	int write_pointer=0;
	
	int sector_mask = 0x0000FFFF;
	
	//Signal the host that the slave is ready to communicate 				
	Write_UART(START_LOADING);
	
	/*----Receive 'length' of the .ldr file ---*/

		
	//Read the first least significant byte of 'length' 
	length_ldr=Read_UART();
	//Send acknowledgement to the host
	Write_UART(LENGTH_ACK);
	
	//Read the second least signnificant byte of 'length'
	temp=Read_UART();
	temp=temp<<8;
	length_ldr|=temp;
	//Send acknowledgement to the host
	Write_UART(LENGTH_ACK);
	
	//Read the third least signnificant byte of 'length'
	temp=Read_UART();
	temp=temp<<16;
	length_ldr|=temp;
	//Send acknowledgement to the host
	Write_UART(LENGTH_ACK);
	
	//Read the  most signnificant byte of 'length'
	temp=Read_UART();
	temp=temp<<24;
	length_ldr|=temp;
	//Send acknowledgement to the host
	Write_UART(LENGTH_ACK);
	
	
	/*----Receive 'FLASH Offset' ---*/
	
	//Read the first least  signnificant byte of 'offset'
	offset=Read_UART();
	//Send acknowledgement to the host
	Write_UART(OFFSET_ACK);

	//Read the second least  signnificant byte of 'offset'
	temp=Read_UART();
	temp=temp<<8;
	offset|=temp;
	//Send acknowledgement to the host
	Write_UART(OFFSET_ACK);
	
	
	//Read the third least  signnificant byte of 'offset'
	temp=Read_UART();
	temp=temp<<16;
	offset|=temp;
	//Send acknowledgement to the host
	Write_UART(OFFSET_ACK);
	
	//Read the most signnificant byte of 'offset'
	temp=Read_UART();
	temp=temp<<24;
	offset|=temp;
	Write_UART(OFFSET_ACK);

	//Convet byte offset to 32 bit word offset	
	app_offset=offset/4;
		
	/*Logical address from where the .ldr file has to be read = 
	
	(byte offset)/4 + MS1 start adddress + 32 bit equivalent address offset
	for the boot kernel size (256 instructions = 384 32bit words) = 
	app_offset+0x4000000+0x180 */	 
	 	
	app_offset+=0x4000180;
					
    										
	//Program the FLASH with the upgraded application
	while(i<length_ldr)
	{
		
		if((i%0x10000)==0)
			erase_sector(offset+i);
				
			//Read next byte sent by the PC		
			temp=Read_UART();
			
			//Program the byte received into the FLASH
			Write_FLASH(offset+i,temp);
			
						
			for(j=0;j<10000;j++);
		
			while((*pUART0LSR&UARTDR)==0)
			{
					
					//Keep sendig the ackowledgement till a new byte is received 
					Write_UART(BYTE_RECEIVED);
					if(i==length_ldr-1)
						break;
			}
					
				i++;	
	}	
	
}



	
void Init_SRU()
{

	*pDPI_PIN_PULLUP = 0xFFFFFFDF;
		
	SRU(HIGH,DPI_PBEN06_I);
	SRU(FLAG9_O,DPI_PB06_I);
	
	SRU(HIGH,DPI_PBEN09_I);
	SRU(UART0_TX_O,DPI_PB09_I);
	
	SRU(LOW,DPI_PBEN10_I);
	SRU(DPI_PB10_O,UART0_RX_I);
}

	
void Init_PLL()
{
	int temp,i;
	
	temp=PLLM27|INDIV|DIVEN;
	
	*pPMCTL=temp;
	
	temp=*pPMCTL;
	
	temp|=PLLBP;
	
	temp^=DIVEN;
	
	*pPMCTL=temp;
	
	for(i=0;i<5000;i++);
	
	*pPMCTL^=PLLBP;
	
}