/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2020/12/21
* Description        : CH32F103 IAP 
*******************************************************************************/ 
#include "debug.h"
#include "usb_lib.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "iap.h"
#include "usb_istr.h"
#include "usb_desc.h"

/* Global define */


/* Global Variable */    


/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{   
	u8 usbstatus=0;	
	USART_Printf_Init(115200);  //PA2 tx
	printf("ok\n");
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	GPIO_Cfg_init();
	Delay_Init(); 
	
#if (PRINT == 1)	
USART_Printf_Init(115200);  //PA2 tx
	printf("SystemClk:%d\r\n",SystemCoreClock);
	 
	printf("IAP Test\r\n");
	
#endif
	
	
	if(PA0_Check() == 0){  //jump APP
#if (PRINT == 1)
		printf("jump APP\r\n");
#endif		
		iap_load_app(0x08000000 + User_APP_Addr_offset);
		
		while(1);
	}
	
  USART1_CFG(57600);	
 	USBD_CFG();
	  
	while(1)
	{
		if(usbstatus!=bDeviceState)
		{
			usbstatus=bDeviceState;
			
			if(usbstatus==CONFIGURED)
			{
#if (PRINT == 1)					
				
				printf("USB Connected\r\n");
				
#endif
			}else
			{
#if (PRINT == 1)					
				printf("USB disConnected\r\n");		

#endif				
			}
		}
		
		EP2_RecData_Deal();
		if( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET){
			UART_Rx_Deal();
		}		
		
		IWDG_ReloadCounter();
	}
}







