/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/02
* AUTHOR:				SA1
* IC:						SN32F100
* DESCRIPTION:	PMU related functions.
*____________________________________________________________________________
* REVISION	Date				User		Description
*	1.0				2013/04/09	SA1			First release
*	2.0				2014/02/27	SA1			1. Update PMU_Setting
*
*____________________________________________________________________________
* 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 <SN32F100.h>
#include "PMU_drive.h"
#include "..\Utility\Utility.h"
#include "..\Interface\GPIO\GPIO.h"


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


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


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


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

/***************************************************************************************************
* Function		: PMU_Setting
* Description	: Setting and enter specified Low power mode
* Input				: mode - specified Low power mode (PMU_SLEEP, PMU_DEEP_SLEEP, PMU_DEEP_PWR_DOWN)
* Output			: None
* Return			: None
* Note				: None
****************************************************************************************************/
void PMU_Setting(uint16_t mode)
{	
	if (mode == PMU_DEEP_PWR_DOWN)		//Deep Power Down mode
	{
		//set P0.14 as DPD wakeup pin
		SN_GPIO0->MODE_b.MODE14 = 1;			//Set P0.14 as output
		SN_GPIO0->BSET = 0x4000;					//Set P0.14=H
		SN_GPIO0->CFG_b.CFG14 = 0;				//Set P0.14 as pull-up
		SN_GPIO0->MODE_b.MODE14 = 0;			//Set P0.14 as input

		//(Optional) Save data to be retained during Deep power-down to the DATA bits in Backup registers
		PMU_Backup_data();
		
		UT_DelayNx10us(5);							//delay > 20us@IHRC=12MHz
	}

	SN_PMU->CTRL = mode;

	__WFI();

	SN_PMU->CTRL = 0x0;
}

/*****************************************************************************
* Function		: PMU_Backup_data
* Description	: Fill in backup value in Bakeup registers.
* Input				: None
* Output			: None
* Return			: None
* Note				: User may manage the backup registers on demand.
*****************************************************************************/
void PMU_Backup_data(void)
{
	//User may modify the following backup registers on demand.
	SN_PMU->BKP0 = 0;
	SN_PMU->BKP1 = 1;
	SN_PMU->BKP2 = 2;
	SN_PMU->BKP3 = 3;
	SN_PMU->BKP4 = 4;
	SN_PMU->BKP5 = 5;
	SN_PMU->BKP6 = 6;
	SN_PMU->BKP7 = 7;
	SN_PMU->BKP8 = 8;
	SN_PMU->BKP9 = 9;
	SN_PMU->BKP10 = 10;
	SN_PMU->BKP11 = 11;
	SN_PMU->BKP12 = 12;
	SN_PMU->BKP13 = 13;
	SN_PMU->BKP14 = 14;
	SN_PMU->BKP15 = 15;
}
