#include "hardware_init.h"
#include "CC2530_transparent_analyze.h"
#include "mini58series.h"
#include "Key.h"
#include "BL55072A.h"
#include "SHTC3.h"
#include "VCNL3040.h"
#include "ZH_protocol.h"
#include "fmc.h"
#include "mfg_factory.h"
#include "display_common.h"
#define PLL_CLOCK       32000000



extern int time_sec;
extern OLED_ALL_DSD Oled_Display_Info;
void UART0_Send_Bytes(u8* bffs, u8 n);
/***************************** 初始化区 ********************************/
#if 1
 void SYS_Init(void)
{
	//    /* Unlock protected registers */
    SYS_UnlockReg();

    // Enable UART clock
	CLK_EnableModuleClock(UART0_MODULE);
	CLK_EnableModuleClock(TMR0_MODULE);
	CLK_EnableModuleClock(TMR1_MODULE);
	CLK_EnableModuleClock(UART1_MODULE);
	CLK_EnableModuleClock(PWMCH01_MODULE);
	 CLK_SetModuleClock(PWMCH01_MODULE, CLK_CLKSEL1_UARTSEL_XTAL, NULL);
    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and cyclesPerUs automatically. */
    SystemCoreClockUpdate();

	/* Set P1 multi-function pins for UART RXD, TXD */
	SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
	SYS->P1_MFP = SYS_MFP_P12_UART0_RXD | SYS_MFP_P13_UART0_TXD;
	
	GPIO_SetMode(P1, BIT2, GPIO_MODE_QUASI);						//lmm modified 
	P12 = 1;
	
	/* Set P1 multi-function pins for UART RXD, TXD */
	SYS->P1_MFP &= ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk);
    SYS->P1_MFP |= (SYS_MFP_P14_UART1_RXD | SYS_MFP_P15_UART1_TXD);
	
	
	/* Set P2 multi-function pin for PWM Channel 0  */
    SYS->P2_MFP &= ~SYS_MFP_P22_Msk;
    SYS->P2_MFP |= SYS_MFP_P22_PWM0_CH0;
    /* Lock protected registers */
    SYS_LockReg();
	/********************************************************************/
	
//	/* Unlock protected registers */
//    SYS_UnlockReg();

//    /* Read User Config to select internal high speed RC */
//    SystemInit();

//    /* Set P5 multi-function pins for crystal output/input */
//    SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
//    SYS->P5_MFP |= (SYS_MFP_P50_XT1_IN | SYS_MFP_P51_XT1_OUT);

//    /* Enable External XTAL (4~24 MHz) */
//    CLK->PWRCTL &= ~CLK_PWRCTL_XTLEN_Msk;
//    CLK->PWRCTL |= (0x1 << CLK_PWRCTL_XTLEN_Pos); // XTAL12M (HXT) Enabled

//    /* Waiting for 12MHz clock ready */
//    CLK_WaitClockReady( CLK_STATUS_XTLSTB_Msk);

//    /* Switch HCLK clock source to XTAL */
//    CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLKSEL_Msk;
//    CLK->CLKSEL0 |= CLK_CLKSEL0_HCLKSEL_XTAL;

//	CLK_EnableModuleClock(UART0_MODULE);
//    CLK_EnableModuleClock(UART1_MODULE); 
//    CLK_EnableModuleClock(TMR1_MODULE);
//    CLK_EnableModuleClock(TMR0_MODULE);
//	
//    /* Update System Core Clock */
//    /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
//    SystemCoreClockUpdate();

//    /*---------------------------------------------------------------------------------------------------------*/
//    /* Init I/O Multi-function                                                                                 */
//    /*---------------------------------------------------------------------------------------------------------*/
//    /* Set P1 multi-function pins for UART1 RXD and TXD  */
//    SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
//    SYS->P1_MFP |= (SYS_MFP_P12_UART0_RXD | SYS_MFP_P13_UART0_TXD);

//    /* Set P1 multi-function pins for UART RXD, TXD */
//	SYS->P1_MFP &= ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk);
//    SYS->P1_MFP |= (SYS_MFP_P14_UART1_RXD | SYS_MFP_P15_UART1_TXD);

//    /* Lock protected registers */
//    SYS_LockReg();
	
	/********************************************************************/
//    /* Unlock protected registers */
//    SYS_UnlockReg();

//    /* Set P5 multi-function pins for XTAL1 and XTAL2 */
//    SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
//    SYS->P5_MFP |= (SYS_MFP_P50_XT1_IN | SYS_MFP_P51_XT1_OUT);
//    
//    /*---------------------------------------------------------------------------------------------------------*/
//    /* Init System Clock                                                                                       */
//    /*---------------------------------------------------------------------------------------------------------*/

//    /* Enable HIRC clock (Internal RC 22.1184MHz) */
//    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
//    
//    /* Wait for HIRC clock ready */
//    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
//    
//    /* Enable external 12MHz XTAL, HIRC */
//    CLK->PWRCTL |= CLK_PWRCTL_XTL12M | CLK_PWRCTL_HIRCEN_Msk;
//    
//    CLK->APBCLK = CLK_APBCLK_PWMCH01CKEN_Msk;

//    /* Waiting for clock ready 检测时钟源状态  稳定1 异常0 */
//    CLK_WaitClockReady(CLK_STATUS_XTLSTB_Msk | CLK_STATUS_HIRCSTB_Msk);
//    
//    /* Switch HCLK clock source to XTL */
//    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC,CLK_CLKDIV_HCLK(1));

//    /* STCLK to XTL STCLK to XTL  设置系统滴答计数时钟源 */
//    CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_XTAL);

//    /*---------------------------------------------------------------------------------------------------------*/
//    /* Init System Clock                                                                                       */
//    /*---------------------------------------------------------------------------------------------------------*/
//    /* Unlock protected registers */
////    SYS_UnlockReg();

////    /* Enable HIRC clock (Internal RC 22.1184MHz) */
////    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

////    /* Wait for HIRC clock ready */
////    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

////    /* Select HCLK clock source as HIRC and and HCLK source divider as 1 */
////    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV_HCLK(1));

////    /* Set P5 multi-function pins for crystal output/input */
////    SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
////    SYS->P5_MFP |= (SYS_MFP_P50_XT1_IN | SYS_MFP_P51_XT1_OUT);

////    /* Enable HXT clock (external XTAL 12MHz) */
////    CLK_EnableXtalRC(CLK_PWRCTL_XTLEN_HXT);

////    /* Wait for HXT clock ready */
////    CLK_WaitClockReady(CLK_STATUS_XTLSTB_Msk);

////    /* Set core clock as PLL_CLOCK from PLL */
////    CLK_SetCoreClock(PLL_CLOCK);   //32000000
////    /* STCLK to XTL STCLK to XTL  设置系统滴答计数时钟源 */
////    CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_XTAL);
//		
//		
//    /* 使能模块时钟 */
//    //CLK_EnableModuleClock(I2C1_MODULE);
//		CLK_EnableModuleClock(UART0_MODULE);
//    CLK_EnableModuleClock(UART1_MODULE); 
//    CLK_EnableModuleClock(TMR1_MODULE);
//    CLK_EnableModuleClock(TMR0_MODULE);
//   // CLK_EnableModuleClock(PWMCH01_MODULE);
//    
//    /* Select IP clock source */
//		CLK_SetModuleClock(UART0_MODULE,   CLK_CLKSEL1_UARTSEL_XTAL,CLK_CLKDIV_UART(1));
//    CLK_SetModuleClock(UART1_MODULE,   CLK_CLKSEL1_UARTSEL_XTAL,CLK_CLKDIV_UART(1));
//    CLK_SetModuleClock(TMR1_MODULE,    CLK_CLKSEL1_UARTSEL_XTAL,CLK_CLKDIV_UART(1));
//    CLK_SetModuleClock(TMR0_MODULE,    CLK_CLKSEL1_UARTSEL_XTAL,CLK_CLKDIV_UART(1));  
//   // CLK_SetModuleClock(PWMCH01_MODULE, CLK_CLKSEL1_UARTSEL_XTAL, NULL);
//    



}
#endif


void Uart_Int(void)
{	
	UART_Open(UART1, 9600);   //与模块通信
	UART_ENABLE_INT(UART1, (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); 
	NVIC_EnableIRQ(UART1_IRQn);
	
	#if DISABLE_DEBUG
	UART_Open(UART0, 9600);   //与中宏协议通信
	UART_ENABLE_INT(UART0, (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); 
	NVIC_EnableIRQ(UART0_IRQn);
	NVIC_SetPriority(UART0_IRQn, 1);
	uart0_data_deal_init();
	#else
	UART_Open(UART0, 115200);   //打印
	NVIC_SetPriority(UART0_IRQn, 1);
	#endif
	NVIC_SetPriority(UART1_IRQn, 1);
	
	tp_function_init(UART1_Send_Bytes, transparent_data_analyze, not_transparent_data_analyze);
}

static void GPIO_init(void)
{   
	InputButtonInit();
	PWM_init();
}
static void TIMER0_init()
{
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000);

    TIMER_EnableInt(TIMER0);

    NVIC_EnableIRQ(TMR0_IRQn);

	NVIC_SetPriority(UART1_IRQn, 2);
	
    TIMER_Start(TIMER0);
}

void  Timer0_Close(void)
{
   TIMER_DisableInt(TIMER0);
   TIMER_Stop(TIMER0);
}

void  Timer1_Open(u16 input_time)
{
    TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, input_time);
    TIMER_EnableInt(TIMER1);
    NVIC_EnableIRQ(TMR1_IRQn);
    TIMER_Start(TIMER1);
}

void  Timer1_Close(void)
{
   TIMER_DisableInt(TIMER1);
   TIMER_Stop(TIMER1);
}

 void EINT_start(void)
{
    GPIO_SetMode(P2, BIT5, GPIO_MODE_INPUT);

    GPIO_EnableEINT0(P2, 5, GPIO_INT_FALLING);

    NVIC_EnableIRQ(GPIO234_IRQn);   //GPIO_DisableEINT0
}



static int  set_data_flash_base(u32 u32DFBA)
{
     uint32_t   au32Config[2];

    if (FMC_ReadConfig(au32Config, 2) < 0) 
    {
        return -1;
    }

    if ((!(au32Config[0] & 0x1)) && (au32Config[1] == u32DFBA))
    {
        return 0;        
    }

    FMC_ENABLE_CFG_UPDATE();

    au32Config[0] &= ~0x1;
    au32Config[1] = u32DFBA;

    if (FMC_WriteConfig(au32Config, 2) < 0)
    {
        return -1;        
    }
    
    // Perform chip reset to make new User Config take effect
    SYS->IPRST0 = SYS_IPRST0_CHIPRST_Msk;
    
    return 0;
}


static void FMC_init(void)
{    
    SYS_UnlockReg();    

    FMC_Open();
    
    set_data_flash_base(Flash_Base_Addr);
    
    SYS_LockReg();    
}
void hardware_init(void)
{
    SYS_Init();
    Uart_Int();  
	BL55072A_Init(0X3E,100000);
	BL55072A_DisplayON();
	BL55072A_Clear();
	SHTC3_Init(0x70);
	VCNL3040_Init(0x60);
    FMC_init();
    TIMER0_init();
	SHTC3_SoftReset();
	Setup_Task_Start();
	//fmc test
	recover_current_state();
	#if DEVICE_TYPE_FAN
	if(Oled_Display_Info.aging_test_flag == 2)
	{
		UART_Close(UART0);
		UART_DisableFlowCtrl(UART0);
		SYS_UnlockReg();
		SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
		SYS->P1_MFP |= SYS_MFP_P12_GPIO | SYS_MFP_P13_GPIO;
		SYS_LockReg();
	}
	#endif
	GPIO_init();
	
	#if (DEVICE_TYPE_FLOOR_HEATING || DEVICE_TYPE_FAN)
	Tap_Init();
	#endif
}

void UART1_Send_Byte(uint8_t byte)
{
    UART_WRITE(UART1, byte);                     //UART1发送8位数据
    
    UART_WAIT_TX_EMPTY(UART1);                   //等待发送完成
}

void UART1_Send_Bytes(TP_uc8* bffs, TP_u32 n)   
{
	uint8_t i = 0;
    
	if(bffs) 
    {
        for(i=0; i<n; i++) 
        {
            UART1_Send_Byte(bffs[i]);	
        }
    }
}
static void UART0_Send_Byte(u8 byte)
{
    UART_WRITE(UART0, byte);           			 //UART0发送8位数据          
	
    UART_WAIT_TX_EMPTY(UART0);      			  //等待发送完成            
}

void UART0_Send_Bytes(u8* bffs, u8 n)
{
	u8 i = 0;
		
	if((bffs)&&(UART_IS_TX_EMPTY(UART0)))
	{
		for(i = 0; i < n; i++) 
		{
			UART0_Send_Byte(bffs[i]);	
		}
	}
}

