#include "def21469.h"
#include "Cdef21469.h"
#include "sru21469.h"
#include "21469_SSL_Serial_FLASH.h"


int app_offset=0;

#define FLASH_START_ADDRESS 0x4000000

void firmware_upgrade()
{
	int offset;

	int temp,i=0;

	int temp1;

	int length_ldr;

	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);


	app_offset=offset;


	//Program the FLASH with the upgraded application
	while(i<length_ldr)
	{

		if((i%0x10000)==0)
			Sector_Erase(offset+i);

		//Read next byte sent by the PC
		temp=Read_UART();

		//Program the byte received into the FLASH
		Write_Flash(offset+i,temp);


		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_PLL()
{
	int temp,i;

	temp=PLLM18|DIVEN;

	*pPMCTL=temp;

	temp=*pPMCTL;

	temp|=PLLBP;

	temp^=DIVEN;

	*pPMCTL=temp;

	for(i=0;i<5000;i++);

	*pPMCTL^=PLLBP;

}

void Init_SRU()
{
    // Disable the pull-up resistors on all 20 pins

    *pDAI_PIN_PULLUP = 0x000FFFFF;


    SRU(SPI_MOSI_O,DPI_PB01_I);     //Connect MOSI to DPI PB1.
    SRU(DPI_PB02_O, SPI_MISO_I);     //Connect DPI PB2 to MISO.
    SRU(SPI_CLK_O, DPI_PB03_I);      //Connect SPI CLK to DPI PB3.
    SRU(SPI_FLG0_O, DPI_PB05_I);     //Connect SPI FLAG3 to DPI PB5.

    SRU(SPI_MOSI_PBEN_O, DPI_PBEN01_I);
    SRU(LOW, DPI_PBEN02_I);
    SRU(SPI_CLK_PBEN_O, DPI_PBEN03_I);
    SRU(SPI_FLG0_PBEN_O, DPI_PBEN05_I);

    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);


}


