/*
Copyright 2005, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2006
*/

#include <msp430x16x.h>
#include <ScatterWeb.TypeDefs.h>
#include <ScatterWeb.Programming.h>

bool Prog_TestMetadata( appHeader_t* md ) {
	unsigned char checksum;

	checksum  = md->type;
	checksum += md->appVer[0] + md->appVer[1];
	checksum += md->platformRev;

	return( md->checksum == checksum );
}

bool Prog_SetLoader(appHeader_t* target) {
	// current loader
	appHeader_t* bldHeader = (appHeader_t*)&__bld_header;

	// test for bootloader
	if( (appHeader_t*)&__bld_header == target )
		return false;

	// test for valid header
	if( !Prog_TestMetadata(bldHeader) )
		return false;

	// set reset address
	if( ADRTOPTR(0xFFFE) != (uint16_t)target->resetAdr ) {
		Prog_Flash_clr( (uint16_t*)0xFFE0 );
		Prog_Flash_ww( (uint16_t*)0xFFFE, target->resetAdr );
	}
	return true;
}

/** \name Flash modifying functions.
 *
 *
 *  The erase operation starts with the following sequence (write is nearly the same):
 *  <ol>
 *  <li> Set the correct input-clock frequency of the timing generator by selecting the clock source and predivider.
 *  <li> Reset the LOCK control bit, if set.
 *  <li> Watch the BUSY bit. Continue to the next steps only if the BUSY bit is reset.
 *  <li> Set the erase control bit Erase to erase a segment, or
 *  <li> Set the mass-erase control bit MEras to erase all numbered segments
 *  <li> Set the mass-erase (MEras) and erase (Erase) control bits to erase all flash memory segments
 *  <li> Execute a dummy write to any address in the range to be erased.
 *  </ol>
 *  [slau049f.pdf, chapter 8]
 * @{
 */
void Prog_Flash_clr( uint16_t* Data_ptr ) {
	FCTL3 = FWKEY; 					// Lock = 0
	while(FCTL3 & BUSY) _NOP();		// Wait for BUSY = 0, not needed unless run from RAM
	FCTL1 = FWKEY | ERASE;			// ERASE = 1
	*Data_ptr=0;					// erase Flash segment by dummy write
	/*
	 * Please note the following information regarding the erase process:
	 * 1)	cpu held if erasing from flash memory
	 * 2)	if erase process initialized from RAM the cpu continues code execution - don't access
	 * 		flash until BUSY flag in FCTL3 is reset!!! (or else unpredictable flash result possible
	 * 		due to system reset)
	 */
	FCTL3 = FWKEY | LOCK;			// Lock = 1
}

void Prog_Flash_ww( uint16_t* Data_ptr, uint16_t word ) {
	FCTL3 = FWKEY;					// Lock = 0
	while(FCTL3 & BUSY) _NOP();		// Wait for BUSY = 0, not needed unless run from RAM
	FCTL1 = FWKEY | WRT;			// WRT = 1
	*Data_ptr=word;					// program Flash word
	/*
	 * Please note the following information regarding the write process:
	 * 1)	cpu held if writing from flash memory
	 * 2)	if write process initialized from RAM the cpu continues code execution - don't access
	 * 		flash until BUSY flag in FCTL3 is reset!!! (or else unpredictable write result possible
	 * 		due to system reset)
	 */
	FCTL1 = FWKEY;					// WRT = 0
	FCTL3 = FWKEY | LOCK;           // Lock = 1
}
//@}

/////////////@}

