/******************** (C) COPYRIGHT 2017 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2017/07
* AUTHOR:			SA1
* IC:					SN32F240B
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2017/07/07	SA1			1. First version released
*																2. Compatible to CMSIS DFP Architecture in Keil MDK v5.X (http://www.keil.com/dd2/pack/)
*																3. Run HexConvert to generate bin file and show checksum after building.
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F240B.h>
#include <SN32F200_Def.h>
#include "Interface\Flash\Flash.h"


/*_____ D E C L A R A T I O N S ____________________________________________*/


/*_____ D E F I N I T I O N S ______________________________________________*/

//Assign ISP Address
#define	ISP_TARGET_ADDR		0xF00						//Word-alignment


/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: main
* Description	: Demo code of Flash Erase/Program
* Input			: None
* Output		: None
* Return		: None
* Note			: 1. ISP area shall inside User ROM
*             2. End address shall <= Maximal User ROM size
*****************************************************************************/						  
int	main (void)
{
//	uint32_t	wFlashBackup[2];
	volatile uint32_t	hwChecksum;

	//User can configure System Clock with Configuration Wizard in system_SN32F240B.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F240B, Please do NOT remove!!!

	//Note: User can refer to ClockSwitch sample code to switch various HCLK if needed.
	//--------------------------------------------------------------------------
	//User Code starts HERE!!!

	//1. Backup data @ISP_TARGET_ADDR if needed
//	wFlashBackup[0] = *((uint32_t *)ISP_TARGET_ADDR);
//	wFlashBackup[1] = *((uint32_t *)(ISP_TARGET_ADDR+4));

	//2. Erase page address @ISP_TARGET_ADDR
	//==>Not Necessary to erase for SN32F260 series!!!

	//3. Program content of wFLASH_PGRAM[0] & [1] to User ROM address ISP_TARGET_ADDR & (ISP_TARGET_ADDR+4)
	wFLASH_PGRAM[0] = 0xA55A3CC3;
	wFLASH_PGRAM[1] = 0x12345678;
	
	if (FLASH_ProgramPage(ISP_TARGET_ADDR, 8, (uint8_t *)(wFLASH_PGRAM)) == FAIL){
			while(1);			//Program Fail
	}

	hwChecksum = FLASH_Checksum();

	while (1);				//Program OK
}


/*****************************************************************************
* Function		: HardFault_Handler
* Description	: ISR of Hard fault interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void HardFault_Handler(void)
{
	NVIC_SystemReset();
}

