#include "includes.h"


/*************************************************************/
//software delay
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void delay_nms(unsigned int t)
{
    volatile unsigned int i,j ,k=0;
    j = 50* t;
    for ( i = 0; i < j; i++ )
    {
        k++;
		SYSCON_IWDCNT_Reload(); 
    }
}
void delay_nus(unsigned int t)
{
    volatile unsigned int i,j ,k=0;
    j = 1* t;
    for ( i = 0; i < j; i++ )
    {
        k++;
    }
}
/*************************************************************/
//GPIO Initial
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void GPIO_CONFIG(void)
{

//	//外部中断初始化
//	GPIO_PullHigh_Init(GPIOA0,4);							//PA0.14
//
//	GPIO_IntGroup_Set(PA0,4,Selete_EXI_PIN4); 					//EXI0 set PB0.2
//	GPIOA0_EXI_Init(EXI4); 										//PB0.2 as input
////	EXTI_trigger_CMD(ENABLE,EXI_PIN14,_EXIFT); 					//ENABLE falling edge，下降沿触发
//	EXTI_trigger_CMD(ENABLE,EXI_PIN4,_EXIRT);
//	EXTI_interrupt_CMD(ENABLE,EXI_PIN4); 						//enable EXI
//	GPIO_EXTI_interrupt(GPIOA0,0b0000000000010000); 			//enable GPIOB02 as EXI
//	EXI3_Int_Enable(); 	

//	//串口1-RX接收中断，用于串口2的通讯总线繁忙状态判断，2025-04-16
//	GPIO_PullHigh_Init(GPIOA0,15);		
//
//	GPIO_IntGroup_Set(PA0,15,Selete_EXI_PIN15); 					//EXI0 set PB0.2
//	GPIOA0_EXI_Init(EXI15); 										//PB0.2 as input
//	EXTI_trigger_CMD(ENABLE,EXI_PIN15,_EXIFT); 					//ENABLE falling edge
//	EXTI_trigger_CMD(ENABLE,EXI_PIN15,_EXIRT);
//	EXTI_interrupt_CMD(ENABLE,EXI_PIN15); 						//enable EXI
//	GPIO_EXTI_interrupt(GPIOA0,0b1000000000000000); 			//enable GPIOB02 as EXI
//	
//	EXI4_Int_Enable(); 	


}

/*************************************************************/
//ETP0 Functions
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/ 
void EPT0_CONFIG(void)
{
	//4路 PWM初始化
	EPT_Software_Prg();								//EPT software reset
//------------  EPT GPIO Setting  --------------------------------/
	//EPT_IO_SET(EPT_IO_CHAX,IO_NUM_PA10);			//AX channel selection
	EPT_IO_SET(EPT_IO_CHBX,IO_NUM_PB02);			//BX channel selection
	//EPT_IO_SET(EPT_IO_CHCX,IO_NUM_PB05);			//CX channel selection
	//EPT_IO_SET(EPT_IO_CHD,IO_NUM_PA08);			//D channel selection
//------------  EPT Control  --------------------------------/
	EPT_PWM_Config(EPT_Selecte_PCLK,EPT_CNTMD_increase,EPT_OPM_Continue,0);//PCLK as clock，increasing mode,continuous mode,TCLK=PCLK/(0+1) 

//	EPT_PWMX_Output_Control(EPT_PWMA,EPT_CA_Selecte_CMPA,EPT_CB_Selecte_CMPA,EPT_PWM_ZRO_Event_OutLow,EPT_PWM_PRD_Event_Nochange,
//							EPT_PWM_CAU_Event_OutHigh,EPT_PWM_CAD_Event_OutHigh,
//							EPT_PWM_CBU_Event_Nochange,EPT_PWM_CBD_Event_Nochange,
//							EPT_PWM_T1U_Event_Nochange,EPT_PWM_T1D_Event_Nochange,
//							EPT_PWM_T2U_Event_Nochange,EPT_PWM_T2D_Event_Nochange);
	EPT_PWMX_Output_Control(EPT_PWMB,EPT_CA_Selecte_CMPB,EPT_CB_Selecte_CMPB,EPT_PWM_ZRO_Event_OutLow,EPT_PWM_PRD_Event_OutLow,
							EPT_PWM_CAU_Event_OutHigh,EPT_PWM_CAD_Event_OutHigh,
							EPT_PWM_CBU_Event_Nochange,EPT_PWM_CBD_Event_Nochange,
							EPT_PWM_T1U_Event_Nochange,EPT_PWM_T1D_Event_Nochange,
							EPT_PWM_T2U_Event_Nochange,EPT_PWM_T2D_Event_Nochange);						
//	EPT_PWMX_Output_Control(EPT_PWMC,EPT_CA_Selecte_CMPC,EPT_CB_Selecte_CMPC,EPT_PWM_ZRO_Event_OutLow,EPT_PWM_PRD_Event_Nochange,
//							EPT_PWM_CAU_Event_OutHigh,EPT_PWM_CAD_Event_OutHigh,
//							EPT_PWM_CBU_Event_Nochange,EPT_PWM_CBD_Event_Nochange,
//							EPT_PWM_T1U_Event_Nochange,EPT_PWM_T1D_Event_Nochange,
//							EPT_PWM_T2U_Event_Nochange,EPT_PWM_T2D_Event_Nochange);	
//	EPT_PWMX_Output_Control(EPT_PWMD,EPT_CA_Selecte_CMPD,EPT_CB_Selecte_CMPD,EPT_PWM_ZRO_Event_OutLow,EPT_PWM_PRD_Event_Nochange,
//							EPT_PWM_CAU_Event_OutHigh,EPT_PWM_CAD_Event_OutHigh,
//							EPT_PWM_CBU_Event_Nochange,EPT_PWM_CBD_Event_Nochange,
//							EPT_PWM_T1U_Event_Nochange,EPT_PWM_T1D_Event_Nochange,
//							EPT_PWM_T2U_Event_Nochange,EPT_PWM_T2D_Event_Nochange);	
	
	EPT_PRDR_CMPA_CMPB_CMPC_CMPD_Config(3000,0,10,0,0);//PRDR=2400,CMPA=1200,CMPB=600,CMPC=2400,CMPD=0	

	EPT_Int_Enable(EPT_PEND);//End of cycle interrupt request raw status
	EPT_Vector_Int_Enable();	//开启EPT中断
//------------  EPT start  --------------------------------/	
	EPT_Start();

}

/*************************************************************/
//GPT0 Functions
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/ 
void GPT0_CONFIG(void)
{


}

/*************************************************************/
//BT Initial
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void BT_CONFIG(void)
{
	//PB 保护电流PWM_CURR_LMT
//	BT_DeInit(BT0);
//	BT_IO_Init(BT0_PB02);
//	BT_Configure(BT0,BTCLK_EN,0,BT_IMMEDIATE,BT_CONTINUOUS,BT_PCLKDIV);//TCLK=PCLK/(0+1)
//	BT_ControlSet_Configure(BT0,BT_START_HIGH,BT_IDLE_LOW,BT_SYNC_DIS,BT_SYNCMD_DIS,BT_OSTMDX_ONCE,BT_AREARM_DIS,BT_CNTRLD_EN);
//	//BT_ControlSet_Configure(BT0,BT_START_HIGH,BT_IDLE_LOW,BT_SYNC_EN,BT_SYNCMD_DIS,BT_OSTMDX_ONCE,BT_AREARM_DIS,BT_CNTRLD_EN);
//	//BT_Trigger_Configure(BT0,BT_TRGSRC_PEND,BT_TRGOE_EN);
//	BT_Period_CMP_Write(BT0,50,1);
//	BT_Start(BT0);
//	BT_ConfigInterrupt_CMD(BT0,ENABLE,BT_PEND);
//	BT0_INT_ENABLE();
	
	
	//100us 定时器初始化
	BT_DeInit(BT1);
	BT_Configure(BT1,BTCLK_EN,0,BT_IMMEDIATE,BT_CONTINUOUS,BT_PCLKDIV);
	BT_ControlSet_Configure(BT1,BT_START_HIGH,BT_IDLE_LOW,BT_SYNC_DIS,BT_SYNCMD_DIS,BT_OSTMDX_ONCE,BT_AREARM_DIS,BT_CNTRLD_EN);
	BT_Period_CMP_Write(BT1,4780,1);		
	BT_Start(BT1);
	BT_ConfigInterrupt_CMD(BT1,ENABLE,BT_CMP);
	BT1_INT_ENABLE();
	
}



/*************************************************************/
//UART0  CONFIG
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void UART0_CONFIG(void)
{
	UART0_DeInit();                                               //clear all UART Register
    UART_IO_Init(IO_UART0,0);                                     //use PA0.1->RXD0, PA0.0->TXD0
	UARTInit(UART0,416,UART_PAR_NONE);							  //baudrate=sysclock 48M/416=115200
	
    UARTInitRxTxIntEn(UART0,416,UART_PAR_NONE);				  //baudrate=sysclock 48M/416=115200,tx rx int enabled 
	UART0_Int_Enable();
}	
/*************************************************************/
//UART1  CONFIG
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void UART1_CONFIG(void)
{
	UART1_DeInit();                                                 //clear all UART Register
    UART_IO_Init(IO_UART1,0);                                    	//use PA0.13->RXD1, PB0.0->TXD1
	UARTInit(UART1,416,UART_PAR_NONE);							  //baudrate=sysclock 48M/416=115200
	
	UARTInitRxTxIntEn(UART1,416,UART_PAR_NONE);					//baudrate=sysclock 48M/416=115200 tx rx int enabled 
	UART1_Int_Enable();
}	
/*************************************************************/
//UART2  CONFIG
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void UART2_CONFIG(void)
{
	UART2_DeInit();                                                 //clear all UART Register
    UART_IO_Init(IO_UART2,2);                                    	//use PA0.7->RXD2, PA0.6->TXD2
	UARTInit(UART2,416,UART_PAR_NONE);							  //baudrate=sysclock 48M/416=115200
	//UARTInitRxTxIntEn(UART2,416,UART_PAR_NONE);					//baudrate=sysclock 48M/416=115200 tx rx int enabled 
	//UART2_Int_Enable();
}	
/*************************************************************/
//adc config
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/	
void ADC12_CONFIG(void)
{

	ADC12_RESET_VALUE();				//ADC所有寄存器复位
	ADC12_Software_Reset();				//ADC软件复位
	ADC12_CLK_CMD(ADC_CLK_CR,ENABLE);	//使能ADC CLK
	

	ADC12_Configure_Mode(ADC12_12BIT,Continuous_mode,0,6,2,4);
	
	ADC12_Configure_VREF_Selecte(ADC12_VREFP_VDD_VREFN_VSS);
	
	ADC12_ConversionChannel_Config(ADC12_ADCIN4,ADC12_CV_RepeatNum1,ADC12_AVGDIS,0);
	ADC12_ConversionChannel_Config(ADC12_ADCIN7,ADC12_CV_RepeatNum1,ADC12_AVGDIS,1);
	ADC12_ConversionChannel_Config(ADC12_ADCIN8,ADC12_CV_RepeatNum1,ADC12_AVGDIS,2);
	ADC12_ConversionChannel_Config(ADC12_ADCIN10,ADC12_CV_RepeatNum1,ADC12_AVGDIS,3);
	
	
	ADC12_CMD(ENABLE);
	
	ADC12_ready_wait();
	
	ADC12_Control(ADC12_START);
}

/*************************************************************/
//SIO Functions
//EntryParameter:NONE
//ReturnValue:NONE
// sio 驱动RGB LED(ws2812), RGB DATA = 24bit; 驱动数据输出排列方式:GRB
/*************************************************************/
void SIO_CONFIG(void)
{
	SIO_DeInit();
	//
	SIO_IO_Init(SIO_PA03);			//配置IO为SIO模式
	//tx clk =4MHz, Ttxshift = 1/4 = 250ns；发送每bit时间是250ns
	SIO_TX_Init(SIOCLK_EN,9);		//
	//TXCR1  TXCR0--空闲输出L
	//TXBUFLEN = 8-1  TXCNT =24-1
	// D0 D1 不使用
	//
	SIO_TX_Configure(SIO_IDLE_LOW,SIO_TX_LSB,7,23,0,0,SIO_OBH_6BIT,SIO_OBL_6BIT,0x0F,0x03);
	//SIO_TXBUF_Set(TX_DH,TX_DL,TX_DH,TX_DL,TX_DH,TX_DL,TX_DH,TX_DL,TX_D1,TX_D0,TX_D1,TX_D0,TX_D1,TX_D0,TX_D1,TX_D0);
//	SIO_ConfigInterrupt_CMD(ENABLE,SIO_TXDNE);		//配置
//	SIO_INT_ENABLE();
}

/*************************************************************/
//syscon Functions
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/
void SYSCON_CONFIG(void)
{
//------SYSTEM CLK AND PCLK FUNTION---------------------------/
	SYSCON_RST_VALUE();                                                         //SYSCON all register clr
	SYSCON_General_CMD(ENABLE,ENDIS_ISOSC);										//SYSCON enable/disable clock source
	//EMOSC_OSTR_Config(0XAD,0X1f,EM_LFSEL_EN,EM_FLEN_EN,EM_FLSEL_10ns);		//EM_CNT=0X3FF,0xAD(36K),EM_GM=0,Low F modedisable,EM filter disable,if enable,cont set 5ns
	//SYSCON_General_CMD(ENABLE,ENDIS_EMOSC);
	SYSCON_HFOSC_SELECTE(HFOSC_SELECTE_48M);									//HFOSC selected 48MHz
	SystemCLK_HCLKDIV_PCLKDIV_Config(SYSCLK_HFOSC,HCLK_DIV_1,PCLK_DIV_1,HFOSC_48M);//system clock set, Hclk div ,Pclk div  set system clock=SystemCLK/Hclk div/Pclk div
//------------  WDT FUNTION  --------------------------------/
    SYSCON_IWDCNT_Config(IWDT_TIME_500MS,IWDT_INTW_DIV_7);      					//WDT TIME 1s,WDT alarm interrupt time=1s-1s*1/8=0.875S
    SYSCON_WDT_CMD(ENABLE);                                                  	//enable/disable WDT		
    SYSCON_IWDCNT_Reload();                                                   	//reload WDT
	IWDT_Int_Enable();
//------------  WWDT FUNTION  --------------------------------/
//	WWDT_CNT_Load(0xFF);
//	WWDT_CONFIG(PCLK_4096_DIV0,0xFF,WWDT_DBGDIS);
//	WWDT_Int_Config(ENABLE);													
	//WWDT_CMD(ENABLE);															//enable wwdt
//------------  CLO Output --------------------------------/	
	//SYSCON_CLO_CONFIG(CLO_PA08);												//CLO output setting
	//SYSCON_CLO_SRC_SET(CLO_HFCLK,CLO_DIV16);									//CLO output clock and div
//------------  LVD FUNTION  --------------------------------/ 
    SYSCON_LVD_Config(ENABLE_LVDEN,INTDET_LVL_3_9V,RSTDET_LVL_1_9V,ENABLE_LVD_INT,INTDET_POL_fall);   //LVD LVR Enable/Disable
    LVD_Int_Enable();	
//------------  SYSCON Vector  --------------------------------/ 	
	SYSCON_Int_Enable();    														//SYSCON VECTOR
	//SYSCON_WakeUp_Enable();    													//Enable WDT wakeup INT
//------------------------------------------------------------/
//OSC CLOCK Calibration
//------------------------------------------------------------/	
	std_clk_calib(CLK_HFOSC_48M);												//Select the same clock source as the system
	
}
/*********************************************************************************/  
/*********************************************************************************/
//APT32F102_init                                                                  /
//EntryParameter:NONE                                                             /
//ReturnValue:NONE                                                                /
/*********************************************************************************/
void APT32F102_init(void) 
{
//------------------------------------------------------------/
//Peripheral clock enable and disable
//EntryParameter:NONE
//ReturnValue:NONE
//------------------------------------------------------------/
    SYSCON->PCER0=0xFFFFFFF;                                        //PCLK Enable
    SYSCON->PCER1=0xFFFFFFF;                                        //PCLK Enable
    while(!(SYSCON->PCSR0&0x1));                                    //Wait PCLK enabled	
//------------------------------------------------------------/
//ISOSC/IMOSC/EMOSC/SYSCLK/IWDT/LVD/EM_CMFAIL/EM_CMRCV/CMD_ERR OSC stable interrupt
//EntryParameter:NONE
//ReturnValue:NONE
//------------------------------------------------------------/
    SYSCON_CONFIG();                                                 //syscon  initial
	CK_CPU_EnAllNormalIrq();                                         //enable all IRQ
	SYSCON_INT_Priority();										 	//initial all Priority=0xC0
	
	//设置中断优先级 	0最高，3最低
	Set_INT_Priority(UART0_IRQ,1);
	Set_INT_Priority(UART1_IRQ,1);									//串口优先级最高
	Set_INT_Priority(UART2_IRQ,1);
	
	GPIO_DeInit();													//复位所有IO，bootload中初始化了所有IO
//------------------------------------------------------------/
//Other IP config
//------------------------------------------------------------/	
	BT_CONFIG();			//BT initial
	
	UARTx_Init(UART_0,HLW8110_RecvData_Processing);
	UARTx_Init(UART_1,BLV_485Recv_Processing);
	UARTx_Init(UART_2,BT_Recv_Processing);
	
	DIP_Switch_Init();
	
	BLV_DetEnergy_Init();
	
	Bsp_I2C_Fram_Init();
	
	Boot_Function_Init();
}

/******************* (C) COPYRIGHT 2019 APT Chip *****END OF FILE****/