#include "pdebug.h"

// 定义调试等级
int debug_level = 6;


void pdebug( int level, const char *fmt, ...)
{
	va_list arglist;
	static  CPU_CHAR  buffer[200u + 1u];
    
        rtc_parameter_struct   rtc_initpara;
        
	memset(buffer,'0',sizeof(buffer));
	if( debug_level >= level ){
		va_start( arglist ,fmt );
		switch (level) {
		case DEBUG_LEVEL_DEBUG: 
			sprintf(buffer, "[DEBUG]: ");
			vsprintf((char *)&buffer[9], (char const *)fmt, arglist);
			break;
		case DEBUG_LEVEL_INFO:
			sprintf(buffer, "[INFO]: ");
			vsprintf((char *)&buffer[8], (char const *)fmt, arglist);
			break;
		case DEBUG_LEVEL_NOTICE:
			sprintf(buffer, "[NOTICE]: ");
			vsprintf((char *)&buffer[10], (char const *)fmt, arglist);
			break;
		case DEBUG_LEVEL_WARN:
			sprintf(buffer, "[WARN]: ");
			vsprintf((char *)&buffer[8], (char const *)fmt, arglist);
			break;
		case DEBUG_LEVEL_ERROR:
			sprintf(buffer, "[ERROR]: ");
			vsprintf((char *)&buffer[9], (char const *)fmt, arglist);
			break;
		case DEBUG_LEVEL_FATAL:
			sprintf(buffer, "[FATAL]: ");
			vsprintf((char *)&buffer[9], (char const *)fmt, arglist);
			break;      
		}
        va_end(arglist);
	    // 打印每条信息附加时间戳
        // 获取系统时间
        rtc_current_time_get(&rtc_initpara);       
	  //term_printf("[%02d:%02d:%02d]",rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second);
        
        term_uart_wrstr((CPU_CHAR*) buffer);
	}
}

/****配置实时时钟****/


void rtc_pre_config(void)
{
    //时钟电源管理器配置
    rcu_periph_clock_enable(RCU_PMU);
    /* enable the access of the RTC registers */
    pmu_backup_write_enable();
    //实时时钟源配置****选择外部高频晶振
    rcu_osci_on(RCU_HXTAL);
    rcu_osci_stab_wait(RCU_HXTAL);
    rcu_rtc_clock_config(RCU_RTCSRC_HXTAL_DIV_RTCDIV);
   ///时钟使能
    rcu_periph_clock_enable(RCU_RTC);
    //等待配置完成
    rtc_register_sync_wait();
    //清除标志位
    rcu_all_reset_flag_clear();
}









