/**
******************************************************************************************************************************************************************************************
*@file                       cpu_sysctrl.h
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#ifndef CPU_SYSCTRL_H
#define CPU_SYSCTRL_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "cpu.h"
#include "cpu_gpio.h"

#define CRM_PeriphClock_BT                    CRM_CG_CTRL_BT         
#define CRM_PeriphClock_QPADC                   CRM_CG_CTRL_GPADC        
#define CRM_PeriphClock_QDEC                    CRM_CG_CTRL_QDEC         
#define CRM_PeriphClock_GPIO                    CRM_CG_CTRL_GPIO         
#define CRM_PeriphClock_RTC                     CRM_CG_CTRL_RTC          
#define CRM_PeriphClock_WDT                     CRM_CG_CTRL_WDT          
#define CRM_PeriphClock_TIMER23             CRM_CG_CTRL_TIMER23  
#define CRM_PeriphClock_TIMER01             CRM_CG_CTRL_TIMER01  
#define CRM_PeriphClock_RNG                     CRM_CG_CTRL_RNG          
#define CRM_PeriphClock_SYSCFG              CRM_CG_CTRL_SYSCFG   
#define CRM_PeriphClock_KPC                     CRM_CG_CTRL_KPC          
#define CRM_PeriphClock_SPI1                    CRM_CG_CTRL_SPI1         
#define CRM_PeriphClock_SPI0                    CRM_CG_CTRL_SPI0         
#define CRM_PeriphClock_UART1                   CRM_CG_CTRL_UART1        
#define CRM_PeriphClock_UART0                   CRM_CG_CTRL_UART0        
#define CRM_PeriphClock_I2C                     CRM_CG_CTRL_I2C          
     
#define CRM_PeriphReset_GLB                   CRM_SOFT_RST_GLB      
#define CRM_PeriphReset_CM3                   CRM_SOFT_RST_CM3      
#define CRM_PeriphReset_CRM                   CRM_SOFT_RST_CRM      
#define CRM_PeriphReset_BT                    CRM_SOFT_RST_BT        
#define CRM_PeriphReset_GPADC                 CRM_SOFT_RST_GPADC    
#define CRM_PeriphReset_QDEC                    CRM_SOFT_RST_QDEC        
#define CRM_PeriphReset_GPIO                    CRM_SOFT_RST_GPIO        
#define CRM_PeriphReset_RTC                   CRM_SOFT_RST_RTC      
#define CRM_PeriphReset_WDT                   CRM_SOFT_RST_WDT      
#define CRM_PeriphReset_TIMER23           CRM_SOFT_RST_TIMER23
#define CRM_PeriphReset_TIMER01           CRM_SOFT_RST_TIMER01
#define CRM_PeriphReset_RNG                   CRM_SOFT_RST_RNG      
#define CRM_PeriphReset_SYSCFG              CRM_SOFT_RST_SYSCFG  
#define CRM_PeriphReset_KPC                   CRM_SOFT_RST_KPC      
#define CRM_PeriphReset_SPI1                    CRM_SOFT_RST_SPI1        
#define CRM_PeriphReset_SPI0                    CRM_SOFT_RST_SPI0        
#define CRM_PeriphReset_UART1                 CRM_SOFT_RST_UART1    
#define CRM_PeriphReset_UART0                 CRM_SOFT_RST_UART0    
#define CRM_PeriphReset_I2C                   CRM_SOFT_RST_I2C  


/*====typedef uint32_t deepsleep_mode start ============================*/
typedef enum _deepsleep_mode{
	SYSCFG_DeepSleepMode_SLEEP          =                 (0),
	SYSCFG_DeepSleepMode_DEEPSLEEP      =                 (1),
	SYSCFG_DeepSleepMode_DEEPSLEEP_PLUS  =               (2),
}deepsleep_mode;

/*====typedef uint32_t deepsleep_mode end ============================*/





typedef enum _deep_sleep_retain
{
	SYSCFG_Retention_Sram_8k=        (0x20000),	//待机保留8k内存
	SYSCFG_Retention_Sram_16k=       (0x10000),	//待机保留16k内存
	SYSCFG_Retention_Sram_24k=       (0),			//待机保留24k内存
	SYSCFG_noRetention_Sram =      	(1)   		//待机保留不保留内存，复位从0地址开始运行
}deep_sleep_retain;

void crm_cmd_periphclock(uint32_t crm_periph_clock, functional_state_t new_state);
void crm_reset_periph(uint32_t crm_periph_reset);
uint32_t crm_get_sysclockfreq(void);

//==========低功耗类型配置==============================================================================================
void 		syscfg_deepsleep_set_mode(deepsleep_mode syscfg_deepsleep_mode);

//==========浅睡眠唤醒源================================================================================================
void 		syscfg_cpusleep_cmd_wakeup_src(wakeup_mcu_type syscfg_wakeup_src, functional_state_t new_state);

//==========深度睡眠唤醒源==============================================================================================
void 		syscfg_deepsleep_cmd_wakeup_source(deepsleep_wakeup syscfg_deepsleep_wakeup_src,functional_state_t new_state);
void 		syscfg_deepsleep_set_gpiopin_wakeup_src(irq_bank syscfg_gpio_wakeup_source,gpio_wk_type syscfg_gpio_wakeup_type,gpio_pin syscfg_gpiox_pinx);
void 		syscfg_deepsleep_set_ram_retention(deep_sleep_retain retention_sram_x);

//==========睡眠唤醒源配置===============================================================================================
void 		deepsleep_wakeup_source_config(deepsleep_wakeup deepsleep_wakeup_src,uint32_t p_param,functional_state_t new_state);

uint32_t  	syscfg_calib32k(uint32_t cnt32k,uint32_t * cnt16m);
void  		syscfg_calib32k_start(uint32_t cnt32k);
uint32_t  	syscfg_calib32k_check_end(void);
void 		syscfg_external_32M_xtal(void);
void 		syscfg_external_16M_xtal(void);
void 		syscfg_external_32k_xtal(void);
void 		syscfg_internal_32k_rc(void);
void 		mcu_osc_configure(void);

/*
* 简介:设置晶振频偏接口函数
*/
void syscfg_set_oscxtal_config(uint8_t xtal_cfg);
void deepsleep_set_wakeup_gpio(irq_bank wakeup_bank,gpio_wk_type wakeup_type,gpio_irq_type irq_type,gpio_pin gpiox_pinx);
uint32_t rc_get_32k_rtc_value(uint32_t cnt32k);

#ifdef __cplusplus
}
#endif

#endif 
