#include "RCC.h"

//初始化独立看门狗
//prer:分频数:0~7(只有低3位有效!)
//分频因子=4*2^prer.但最大值只能是256!
//rlr:重装载寄存器值:低11位有效.
//时间计算(大概):Tout=40K/((4*2^prer)*rlr)值.
void IWDG_Init(u8 prer,u16 rlr) 
{
	IWDG->KR=0X5555;//使能对IWDG->PR和IWDG->RLR的写		 										  
	IWDG->PR=prer;  //LSI/32=40Khz/4*2^pre   
	IWDG->RLR=rlr;  //从加载寄存器 IWDG->RLR  
	IWDG->KR=0XAAAA;//reload											   
	IWDG->KR=0XCCCC;//使能看门狗	
}

/*******************************************************************************
* Function Name  : SysTick_Configuration
* Description    : Configure a SysTick Base time to 10 ms.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Configuration(void)
{	
  	/* Setup SysTick Timer for 10 msec interrupts  */
  	if (SysTick_Config(SystemCoreClock / 1000))	    /*SysTick配置为1ms中断一次*/
  	{ 
    	/* Capture error */ 
    	while (1);
  	}
  
  	/* Configure the SysTick handler priority */
  	NVIC_SetPriority(SysTick_IRQn, 0x06);		         /*设置SysTick的中断优选级*/
														 /* n=0x00~0x03 设置Systick抢占式优先级0 
															n=0x04~0x07 设置Systick抢占式优先级1 
															n=0x08~0x0B 设置Systick抢占式优先级2 
															n=0x0C~0x0F 设置Systick抢占式优先级3  */	
}

void RCC_Configuration(void)
{	
	ErrorStatus HSEStartUpStatus;
  	
	/* 这里是重置了RCC的设置，类似寄存器复位 */
	RCC_DeInit();
	/* 使能外部高速晶振 */
	RCC_HSEConfig(RCC_HSE_ON);
	/* 等待高速晶振稳定 */
	HSEStartUpStatus = RCC_WaitForHSEStartUp();
	if (HSEStartUpStatus == SUCCESS)
	{
	    /* 使能flash预读取缓冲区 */
	    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
	    /* 令Flash处于等待状态，2是针对高频时钟的，这两句跟RCC没直接关系，可以暂且略过 */
	    FLASH_SetLatency(FLASH_Latency_2);

		/* HCLK = SYSCLK 设置高速总线时钟=系统时钟*/
	    RCC_HCLKConfig(RCC_SYSCLK_Div1);

		/* PCLK2 = HCLK 设置低速总线2时钟=高速总线时钟*/
	    RCC_PCLK2Config(RCC_HCLK_Div1);

		/* PCLK1 = HCLK/2 设置低速总线1的时钟=高速时钟的二分频*/
	    RCC_PCLK1Config(RCC_HCLK_Div2);

		/* ADCCLK = PCLK2/6 设置ADC外设时钟=低速总线2时钟的六分频*/
	    RCC_ADCCLKConfig(RCC_PCLK2_Div6);

		/* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */ 
	    //上面这句例程中缺失了，但却很关键
		/* 利用锁相环讲外部8Mhz晶振9倍频到72Mhz */ 
	    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

		/* Enable PLL 使能锁相环*/
	    RCC_PLLCmd(ENABLE);

		/* Wait till PLL is ready 等待锁相环输出稳定*/
	    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
	    {}

		/* Select PLL as system clock source 将锁相环输出设置为系统时钟 */
	    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

		/* Wait till PLL is used as system clock source 等待校验成功*/
	    while (RCC_GetSYSCLKSource() != 0x08)
	    {}
	}

  	/* ADCCLK = PCLK2/4 */
  	RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
    
  	/* Enable peripheral clocks ------------------------------------------------*/
  	/* Enable DMA1 clocks */
  	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  	/* Enable ADC1, ADC2, ADC3 and GPIOC clocks */
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 |
                         RCC_APB2Periph_AFIO, ENABLE);
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  	
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	/* Set the Vector Table base address at 0x08000000 */
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000);		

	/* Configure the NVIC Preemption Priority Bits */  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);			

	/* Enable the USARTz Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 

	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;			
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); 		

	/**********************后添加*************************************/
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);                        //选择中断分组0
	NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;                   //指定中断源 TIM2中断
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                	   //指定响应优先级别1
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	/*****************************************************************/		
}

void delayus(u32 count)
{
	u32 i;
	
	while(count--)
	{
		for(i=9; i>0; i--);
	}
}

void delayms(u32 count)
{
	while(count--)
	{
		delayus(1000);
	}
}

