/*
******************************************************************************************************
 @Module        	
 @Filename     	Code_Memory_Access.c
 @Project        	SEM-3000
-----------------------------------------------------------------------------------------------------
 @Controller     	Si1001

 @Compiler      	Keil 8.12

 @ Frequency   	Using Internal clock 24.5MHz

 @Codegenerator 	0.1

 @Description   

------------------------------------------------------------------------------------------------------
 @Date          		2013-07-05 11:00:00

******************************************************************************************************
*/


#define _Code_Memory_Access_C_

/*
********************************************************************************************************
									 @Project Includes
********************************************************************************************************
*/

#include "PROJECT_INCLUDES.H"

/*
*********************************************************************************************************
 									@Macros
*********************************************************************************************************
*/



//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) FLADDR addr - address of the byte to write to
//                    valid range is 0x0000 to 0x1DFE for 8K Flash devices
//                    valid range is 0x0000 to 0x0FFE for 4K Flash devices
//                    valid range is 0x0000 to 0x07FE for 2K Flash devices
//   2) char byte - byte to write to Flash.
//
// This routine writes <byte> to the linear FLASH address <addr>.
//-----------------------------------------------------------------------------

void FLASH_ByteWrite (FLADDR addr, char byte)
{
	bit EA_SAVE = EA;                   // Preserve EA
	char xdata * data pwrite;           // FLASH write pointer

	EA = 0;                             // Disable interrupts

	VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

	RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

	pwrite = (char xdata *) addr;

	FLKEY  = 0xA5;                      // Key Sequence 1
	FLKEY  = 0xF1;                      // Key Sequence 2
	PSCTL |= 0x01;                      // PSWE = 1 which enables writes

	VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

	RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

	*pwrite = byte;                     // Write the byte

	PSCTL &= ~0x01;                     // PSWE = 0 which disable writes

	EA = EA_SAVE;                       // Restore interrupts
	
}


//-----------------------------------------------------------------------------
// FLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
//      unsigned char - byte read from Flash
// Parameters   :
//   1) FLADDR addr - address of the byte to read to
//                    valid range is 0x0000 to 0x1DFE for 8K Flash devices
//                    valid range is 0x0000 to 0x0FFE for 4K Flash devices
//                    valid range is 0x0000 to 0x07FE for 2K Flash devices
//
// This routine reads a <byte> from the linear FLASH address <addr>.
//-----------------------------------------------------------------------------

unsigned char FLASH_ByteRead (FLADDR addr)
{	
	char code * data pread;             // FLASH read pointer	
	
	unsigned char byte;
	
	bit EA_SAVE = EA;                   // Preserve EA

	EA = 0;                             // Disable interrupts
	
	PSCTL |= SFLE; 

	pread = (char code *) addr;

	byte = *pread;                      // Read the byte

	EA = EA_SAVE;                       // Restore interrupts

	return byte;
	
}
/**/

//-----------------------------------------------------------------------------
// FLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) FLADDR addr - address of any byte in the page to erase
//                    valid range is 0x0000 to 0x1BFF for 8K Flash devices
//                    valid range is 0x0000 to 0x0DFF for 4K Flash devices
//                    valid range is 0x0000 to 0x05FF for 2K Flash devices
//
// This routine erases the FLASH page containing the linear FLASH address
// <addr>.  Note that the page of Flash containing the Lock Byte cannot be
// erased from application code.
//-----------------------------------------------------------------------------

void FLASH_Page_Erase (FLADDR addr, bit Memory_Area )
{

	bit EA_SAVE = EA;                   // Preserve EA
	char xdata * data pwrite;           // FLASH write pointer

	EA = 0;                             // Disable interrupts

	VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold
	RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

	if(Memory_Area==SCRATCHPAD_FLASH_MEMORY)	PSCTL |= SFLE;     
	else PSCTL &= ~SFLE;   


	pwrite = (char xdata *) addr;

	FLKEY  = 0xA5;                      // Key Sequence 1
	FLKEY  = 0xF1;                      // Key Sequence 2
	PSCTL |= 0x03;                      // PSWE = 1; PSEE = 1 


	VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

	RSTSRC = 0x02;                      // Enable VDD monitor as a reset source
	*pwrite = 0;                        // Initiate page erase

	PSCTL &= ~0x03;                     // PSWE = 0; PSEE = 0

	EA = EA_SAVE;                       // Restore interrupts
	
}




void Buff_To_Flash_Memory(U16 dest, U8 *src, U16 length)
{
	U16 i;

	for(i = 0; i < length; i++)FLASH_ByteWrite( dest+i, src[i]);
	
}

void Flash_Memory_To_Buff(U8 * dest, U16 src, U16 length)
{	
	U16 i;

	for(i = 0; i < length; i++)dest[i]=FLASH_ByteRead( src+i);
	
}

