Beispiel #1
0
void RCC_Config(void)
{


	//jelenleg belso orarol megy a cucc 64MHz-en
	RCC_GetClocksFreq(&RCC_ClockFreq);

	if (SysTick_Config(SystemCoreClock / 10000))
	{
		/* Capture error */
		while (1);
	}

	RCC_ClockSecuritySystemCmd(ENABLE);

	RCC_ADCCLKConfig(RCC_PCLK2_Div4);

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);



	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC
			| RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);


	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM3,ENABLE);






}
Beispiel #2
0
    void initClocks() {
        // enable HSI and set it as the system clock source
        RCC_HSICmd(ENABLE);
        while(!(RCC->CR & RCC_CR_HSIRDY)); // wait for it to be ready
        RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

        // disable PLL and PLLI2S
        RCC_PLLCmd(DISABLE);
        RCC_PLLI2SCmd(DISABLE);

        // disable HSE and CSS (disabling the HSE also disables CSS)
        RCC_HSEConfig(RCC_HSE_OFF);

        // Configure PLL values and set source to HSE
        RCC_PLLConfig(
                RCC_PLLSource_HSE,
                DESIRED_PLL_M,
                DESIRED_PLL_N,
                DESIRED_PLL_P,
                DESIRED_PLL_Q
        );

        // set PLL I2S to our new values
        RCC_PLLI2SConfig(
                DESIRED_PLL_I2S_N,
                DESIRED_PLL_I2S_R
        );

        // set I2S clock source to PLLI2S
        RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S);

        // set AHB, APB1, APB2 prescalers
        RCC_HCLKConfig(DESIRED_HCLK_DIV);
        RCC_PCLK1Config(DESIRED_PCLK1_DIV);
        RCC_PCLK2Config(DESIRED_PCKL2_DIV);

        // enable HSE
        RCC_HSEConfig(RCC_HSE_ON);
        if(RCC_WaitForHSEStartUp() == ERROR) RCC_DeInit(); // SHUT DOWN, EVERYTHING!

        // enable CSS
        RCC_ClockSecuritySystemCmd(ENABLE);

        // enable PLL
        RCC_PLLCmd(ENABLE);
        while(!(RCC->CR & RCC_CR_PLLRDY)); // wait for ready

        // enable PLL I2S
        RCC_PLLI2SCmd(ENABLE);

        // set system clock source to PLL
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        if(!checkClocks()){
            // No actual error reporting done here because we don't want to depend on GPIO etc
            while(1);
        }

    }
Beispiel #3
0
void MCU_init(void) {

	clocks_init();
	interrupts_init();

	RCC_ClockSecuritySystemCmd(ENABLE);

}
/*******************************************************************************
* Function Name  : LowPower_Init
* Description    : Initializes Low Power application. 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LowPower_Init(void)
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Enable WakeUp pin */
  PWR_WakeUpPinCmd(ENABLE);

  /* Enable Clock Security System(CSS) */
  RCC_ClockSecuritySystemCmd(ENABLE);
}
/*==================================================================================
* 函 数 名: rcc_init
* 参    数: None
* 功能描述:  rcc初始化
* 返 回 值: None
* 备    注: 初始化系统时钟,需要注意stm32f10x.h中对系统时钟的定义
* 作    者: gaodb
* 创建时间: 2012.10
==================================================================================*/ 
static void rcc_init(void)
{
    ErrorStatus HSEStartUpStatus;

		RCC_DeInit();
		
		wait_sys_peri_ready();
		
    /* Enable HSE */  
    RCC_HSEConfig(RCC_HSE_ON);
    RCC_HSEConfig(RCC_HSE_Bypass);//外部晶振为24M有源晶振
    
    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp(); 
    
    if (HSEStartUpStatus == SUCCESS)
    {    
				FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
				
				FLASH_SetLatency(FLASH_Latency_2);
				
				RCC_HCLKConfig(RCC_SYSCLK_Div1); 
				
				RCC_PCLK1Config(RCC_HCLK_Div2);//低速时钟最高36M
				
				RCC_PCLK2Config(RCC_HCLK_Div1);
				
				RCC_ADCCLKConfig(RCC_PCLK2_Div6);
		
        /* PLLCLK = 24MHz * 3 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
        
        /* 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)
        {}
    }
    RCC_ClockSecuritySystemCmd(ENABLE);  //Enable CSSON(Clock securuty system)  
    
		/* Enable the LSI OSC */
	  RCC_LSICmd(ENABLE);									 //为独立看门狗提供时钟
	  
	  /* Wait till LSI is ready */
	  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
	  {}
}
Beispiel #6
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  SetSysClock();

  /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) */
  RCC_GetClocksFreq(&RCC_ClockFreq);

  /* Enable Clock Security System(CSS): this will generate an NMI exception
     when HSE clock fails */
  RCC_ClockSecuritySystemCmd(ENABLE);
     
  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();
 
  /* Initialize Leds mounted on STM3210X-EVAL board --------------------------*/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Output HSE clock on MCO pin ---------------------------------------------*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  RCC_MCOConfig(RCC_MCO_HSE);
  
  while (1)
  {
    /* Toggle LED1 */
    STM_EVAL_LEDToggle(LED1);
    /* Insert delay */
    Delay(DELAY_COUNT);

    /* Toggle LED2 */
    STM_EVAL_LEDToggle(LED2);
	/* Insert delay */
    Delay(DELAY_COUNT);

    /* Toggle LED3 */
    STM_EVAL_LEDToggle(LED3);
    /* Insert delay */
    Delay(DELAY_COUNT);

    /* Toggle LED4 */
    STM_EVAL_LEDToggle(LED4);
    /* Insert a delay */
    Delay(DELAY_COUNT);
  }
}
Beispiel #7
0
void disable_clock(void)
{
	
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_ClockSecuritySystemCmd(ENABLE);
	/* Enable GPIOs clocks */
	return;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);			
	/* Configure MCO (PA8) */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  //UP
	GPIO_Init(GPIOA, &GPIO_InitStructure);
}
Beispiel #8
0
void enable_clock(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
			RCC_ClockSecuritySystemCmd(ENABLE);
			/* Enable GPIOs clocks */
			RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
			GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);
			/* Configure MCO (PA8) */
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
			GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
			GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  //UP
			GPIO_Init(GPIOA, &GPIO_InitStructure);
			RCC_MCO1Config(RCC_MCO1Source_HSI, RCC_MCO1Div_1);// 16MHZ
}
Beispiel #9
0
/***************************************************************************//**
 * @brief  RCC example.This example shows how to configure the System clock(SYSCLK) 
 *            to have different frequencies: 24MHz, 36MHz, 48MHz, 56MHz and 72MHz.
 *            You will see the LED PB8 PB9 PB10 blink successively.
 ******************************************************************************/
void RCC_Init(void)
{
    /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
    SetSysClock();

    /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) */
    RCC_GetClocksFreq(&RCC_ClockFreq);

    /* Enable Clock Security System(CSS): this will generate an NMI exception
     when HSE clock fails */
    RCC_ClockSecuritySystemCmd(ENABLE);

    /* NVIC configuration ------------------------------------------------------*/
    NVIC_Configuration();

}
void MCO1_init(void) {
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_ClockSecuritySystemCmd(ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	// GPIO config
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;		//PA8 - XCLK
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// GPIO AF config
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);

	// MCO clock source
//	RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_4); // Using the fast PLL clock results in garbage output, using HSI (at 16Mhz works fine)
	RCC_MCO1Config(RCC_MCO1Source_HSI, RCC_MCO1Div_1);
}
Beispiel #11
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  RCC_ClocksTypeDef RCC_ClockFreq;
  
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  files (startup_stm32f429_439xx.s) before to branch to application main. 
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f4xx.c file
  */ 
 
  /* Initialize LEDs available on STM32F429I-DISCO ****************************/
  STM_EVAL_LEDInit(LED3); 
  STM_EVAL_LEDInit(LED4);

  /* Turn on LED3 */
  STM_EVAL_LEDOn(LED3);
  
  /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) **************/
  RCC_GetClocksFreq(&RCC_ClockFreq);
  
  /* Enable Clock Security System(CSS): this will generate an NMI exception
     when HSE clock fails *****************************************************/
  RCC_ClockSecuritySystemCmd(ENABLE);
 
  /* Enable and configure RCC global IRQ channel, will be used to manage HSE ready 
     and PLL ready interrupts. 
     These interrupts are enabled in stm32f4xx_it.c file **********************/
  NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Output HSE clock on MCO1 pin(PA8) ****************************************/ 
  /* Enable the GPIOA peripheral */ 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
  /* Configure MCO1 pin(PA8) in alternate function */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);
    
  /* HSE clock selected to output on MCO1 pin(PA8)*/
  RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);

  while (1)
  {
    /* Toggle LED4 */
    STM_EVAL_LEDToggle(LED4);

    /* Insert a delay */
    Delay(0x3FFFFF);

    /* Toggle LED3 */
    STM_EVAL_LEDToggle(LED3);

    /* Insert a delay */
    Delay(0x3FFFFF);    
  }
}
Beispiel #12
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */     

  /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) */
  RCC_GetClocksFreq(&RCC_ClockFreq);
  
  /* Enable Clock Security System(CSS): this will generate an NMI exception
     when HSE clock fails */
  RCC_ClockSecuritySystemCmd(ENABLE);
 
  /* Initialize LEDs mounted on STM32L152-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Turn on LED1 and LED3 */
  STM_EVAL_LEDOn(LED1);
  STM_EVAL_LEDOn(LED3);
  
  /* Enable and configure RCC global IRQ channel */
  NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the GPIOA peripheral */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* Output the system clock on MCO pin (PA.08) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);
    
  /* System clock selected to output on MCO pin (PA.08)*/
  RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCODiv_1);

  while (1)
  {
    /* Toggle LED2 and LED4 */
    STM_EVAL_LEDToggle(LED2);
    STM_EVAL_LEDToggle(LED4);

    /* Insert a delay */
    Delay(0xFFFF);

    /* Toggle LED1 and LED3 */
    STM_EVAL_LEDToggle(LED1);
    STM_EVAL_LEDToggle(LED3);

    /* Insert a delay */
    Delay(0xFFFF);    
  }
}
Beispiel #13
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_stm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f10x.c file
       */

    /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
    SetSysClock();

    /* This function fills the RCC_ClockFreq structure with the current
       frequencies of different on chip clocks (for debug purpose) */
    RCC_GetClocksFreq(&RCC_ClockFreq);

    /* Enable Clock Security System(CSS): this will generate an NMI exception
       when HSE clock fails */
    RCC_ClockSecuritySystemCmd(ENABLE);

    /* NVIC configuration ------------------------------------------------------*/
    NVIC_Configuration();

    /* Initialize Leds mounted on STM3210X-EVAL board --------------------------*/
    STM_EVAL_LEDInit(LED1);
    STM_EVAL_LEDInit(LED2);
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED4);

    /* Output HSE clock on MCO pin ---------------------------------------------*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    RCC_MCOConfig(RCC_MCO_HSE);

    while (1)
    {
        /* Toggle LED1 */
        STM_EVAL_LEDToggle(LED1);
        /* Insert delay */
        Delay(DELAY_COUNT);

        /* Toggle LED2 */
        STM_EVAL_LEDToggle(LED2);
        /* Insert delay */
        Delay(DELAY_COUNT);

        /* Toggle LED3 */
        STM_EVAL_LEDToggle(LED3);
        /* Insert delay */
        Delay(DELAY_COUNT);

        /* Toggle LED4 */
        STM_EVAL_LEDToggle(LED4);
        /* Insert a delay */
        Delay(DELAY_COUNT);
    }
}
Beispiel #14
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 RCC_ClocksTypeDef RCC_ClockFreq;

  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */     

  /* Initialize Leds mounted on STM32072B-EVAL*/
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Turn on LED3 and LED4 */
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOn(LED4);

  /* This function fills the RCC_ClockFreq structure with the current
     frequencies of different on chip clocks (for debug purpose) **************/
  RCC_GetClocksFreq(&RCC_ClockFreq);

  /* Enable Clock Security System(CSS): this will generate an NMI exception
  when HSE clock fails *****************************************************/
  RCC_ClockSecuritySystemCmd(ENABLE);

  /* Enable and configure RCC global IRQ channel, will be used to manage HSE ready 
     and PLL ready interrupts. 
     These interrupts are enabled in stm32f0xx_it.c file **********************/
#ifdef USE_STM320518_EVAL
  NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
#else 
  NVIC_InitStructure.NVIC_IRQChannel = RCC_CRS_IRQn; 
#endif /* USE_STM320518_EVAL */
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Output HSE clock on MCO1 pin(PA8) ****************************************/     
  /* Enable the GPIOA Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* MCO pin configuration: PA8 */
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_0);

  /* Output System Clock on MCO pin */
#ifdef USE_STM320518_EVAL
  RCC_MCOConfig(RCC_MCOSource_SYSCLK); 
#else 
  RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCOPrescaler_1);  
#endif /* USE_STM320518_EVAL */   

  while (1)
  {
    /* Toggle LED4 */
    STM_EVAL_LEDToggle(LED4);

    /* Insert a delay */
    Delay(0x4FFFF);

    /* Toggle LED3 */
    STM_EVAL_LEDToggle(LED3);

    /* Insert a delay */
    Delay(0x3FFFF);
  }
}
Beispiel #15
0
//**************************************************************************//
void hal_system_RCC_init(void)
{
	
	RCC_DeInit();//RCC system reset(for debug purpose) 
	RCC_HSEConfig(RCC_HSE_ON);//Enable HSE 
	//RCC_HSEConfig(RCC_HSE_ON);

	if ( RCC_WaitForHSEStartUp() == SUCCESS)
	{
		FLASH_ReadAccess64Cmd(ENABLE);
		FLASH_PrefetchBufferCmd(ENABLE);

		// Flash 0 wait state 
		FLASH_SetLatency(FLASH_Latency_1);

		//Power enable 
		RCC->APB1ENR |= RCC_APB1ENR_PWREN;

		// Select the Voltage Range 1 (1.8 V) */
		PWR->CR = PWR_CR_VOS_0;
		
		// HCLK = SYSCLK 
		RCC_HCLKConfig(RCC_SYSCLK_Div1); 
		// PCLK2 = HCLK 
		RCC_PCLK2Config(RCC_HCLK_Div1); 
		// PCLK1 = HCLK 
    	RCC_PCLK1Config(RCC_HCLK_Div1);
		 
		//PLLCLK = 16MHz *4/2 = 32 MHz 
		RCC_PLLConfig(RCC_PLLSource_HSE, RCC_PLLMul_4,RCC_PLLDiv_2);//RCC_PLLDiv_2
		//RCC_PLLConfig(RCC_PLLSource_HSI, RCC_PLLMul_4,RCC_PLLDiv_2);
		
		RCC_PLLCmd(ENABLE);// Enable PLL
		while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)// Wait till PLL is ready 
		{}
		
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//Select PLL as system clock source

		//RCC_HCLKConfig(RCC_SYSCLK_Div1);// HCLK = SYSCLK
		//RCC_PCLK2Config(RCC_HCLK_Div1);// PCLK2 = HCLK/4 
		//RCC_PCLK1Config(RCC_HCLK_Div1);//RCC_HCLK_Div1    PCLK1 = HCLK/1

		  /* Wait till PLL is used as system clock source */
	   // while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSE)
	   // {
	   // }

	}
	//SystemInit();

	/*RCC_ClocksTypeDef RCC_ClockFreq;
	
	RCC_ClockFreq.SYSCLK_Frequency=RCC_SYSCLKSource_HSE;
	RCC_ClockFreq.HCLK_Frequency=RCC_SYSCLK_Div1;
	RCC_ClockFreq.PCLK1_Frequency=RCC_HCLK_Div1;
	RCC_ClockFreq.PCLK2_Frequency=RCC_HCLK_Div2;

	RCC_GetClocksFreq(&RCC_ClockFreq);*/
	RCC_ClockSecuritySystemCmd(ENABLE);
	
}
int main(){
	SysTick_Config(SystemCoreClock / 2000);  // 0.5ms

	// NEED OF KEY AND LED
	RCC_ClockSecuritySystemCmd(ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	// Init LCD
	LCD_Cmd_InitFSMC();
	LCD_Cmd_Init();
	LCD_Cmd_InitBacklight();

	// Put some background
//	LCD_FillRectangle_RGB565(0, 0, 240, 320, C_RGB565(0x80, 0x80, 0x80));
	Painter_PutImage(res_img_res_lp_jpg,0,0,0xff);

//	Painter_SetupContextBitmask(240, 320, 0);
//	benchmark(2);

	// A circle to overlay long shadow
	LCD_FillCircle_RGB4444(120, 120, 100+18, 0x88fa);

	// now you can see it
	long_shadow();
	// an alternative shadow
//	LCD_FillCircle(120+1, 120+1, 100+2, 0x8886, 0);
//	LCD_FillCircle(120, 120, 100+2, 0x8886, 0);
//	LCD_FillCircle(120, 120, 100+1, 0x8886, 0);

	// Put my clock panel on screen
	LCD_FillCircle_RGB565(120, 120, 100, C_RGB565(0, 0, 0));
	LCD_FillCircle_RGB565(120, 120, 90, C_RGB565(0xff, 0xff, 0xff));

	// Clock frame need some shadow!
	Painter_SetupContextBitmask(210, 210, 0);
	u16 x, y, i;
	u32 t;
	for (x=0;x<200;x++) for (y=0;y<200;y++){
		t = (x-100)*(x-100)+(y-100)*(y-100);
		if ((t>=91*91)&&(t<100*100)) LCD_SetBitMask(_d_dl_ctx.bm, x, y, 210);
	}

	// Draw ticks and numbers
	const s8 TICK_XI[] = { 0,  42,  73,  85,  73,  42,   0, -42, -73, -85, -73, -42};
	const s8 TICK_YI[] = {-85, -73, -42,   0,  42,  73,  85,  73,  42,   0, -42, -73};
	const s8 TICK_XO[] = { 0,  44,  77,  90,  77,  45,   0, -44, -77, -90, -77, -45};
	const s8 TICK_YO[] = {-90, -77, -45,   0,  44,  77,  90,  77,  45,   0, -44, -77};
	const u16 *text[] = {res_string_0/*_FSTR_0*/,
			res_string_1/*_FSTR_1*/,
			res_string_2/*_FSTR_2*/,
			res_string_3/*_FSTR_3*/,
			res_string_4/*_FSTR_4*/,
			res_string_5/*_FSTR_5*/,
			res_string_6/*_FSTR_6*/,
			res_string_7/*_FSTR_7*/,
			res_string_8/*_FSTR_8*/,
			res_string_9/*_FSTR_9*/,
			res_string_11/*_FSTR_10*/,
			res_string_12/*_FSTR_11*/,
			res_string_13/*_FSTR_12*/
			};
	const u16 *col = res_string_14/*_FSTR_:*/;
	const u16 *space = res_string_15/*_FSTR_ */;
	u16 time[20];

	for (i=0;i<12;i++){
		Painter_LocateContextBitmask(100+TICK_XO[i], 100+TICK_YO[i]);
		Painter_DrawLine(120+TICK_XO[i], 120+TICK_YO[i], 120+TICK_XI[i], 120+TICK_YI[i], 0x000f, 5, PAINTER_DRAW_BM_HOLD);
		Painter_PutString(text[i?i:12], 14, 0x000f, 0
						 , 120+TICK_XI[i]-(TICK_XO[i]-TICK_XI[i])*2-5, 120+TICK_YI[i]-(TICK_YO[i]-TICK_YI[i])*2-8
						 , 20, 20, PAINTER_STR_SHADOW*2);
	}

	// Bravo!
	Painter_Fill_BitMaskShadow(30, 30, 230, 230, 0L, 10, 10, 210, 0x0008, 4, 4, 15);

	// LET IT RUN!
	u8 m, s; // so-called second and minute
	bitmask bm;
	_UNUSED(bm);
	s = 0; m = 0;
	Painter_SetupContextBitmask(200, 200, 0); // new context for new shadow
	Painter_LocateContextBitmask(100, 100); // start from center
	u8 shadow_on;
	u16 bg[200*60];
	// grab screen region, saving for later repaint
	LCD_SetWindow(20, 240, 200, 60);
	LCD_GetImage_RGB565(bg, 200*60);
	while (1){
		// PUSH BUTTON TO RUN REALLY FAST, WITHOUT SHADOW!
		shadow_on = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)?PAINTER_STR_SHADOW*2:0;
		LCD_FillCircle_RGB565(120, 120, 65, 0xffff); // Clear clock hands, sucks aha~
		// Dont forget bitmasks
		for (x=30;x<170;x++) for (y=30;y<170;y++){
			LCD_ResetBitMask(_d_dl_ctx.bm, x, y, 200);
		}
		// second hand
		u16 sh_x[] = {120, 120+(TICK_XI[_mod(s+11,12)]+3)/5,
					  120+TICK_XI[s]-(TICK_XO[s]-TICK_XI[s])*6,
					  120+(TICK_XI[_mod(s+1,12)]+3)/5
					 };
		u16 sh_y[] = {120, 120+(TICK_YI[_mod(s+11,12)]+3)/5,
					  120+TICK_YI[s]-(TICK_YO[s]-TICK_YI[s])*6,
					  120+(TICK_YI[_mod(s+1,12)]+3)/5
					 };
		// Center circle makes it cute~
		Painter_DrawCircle(120, 120, 4, 0x6a2f, 12, PAINTER_DRAW_BM_HOLD);
		Painter_DrawPoly(sh_x, sh_y, 4, 0x6a2f, 3, PAINTER_DRAW_BM_HOLD | PAINTER_DRAW_POLY_CLOSE);
//		Painter_DrawLine(120, 120
//					, 120+TICK_XI[s]-(TICK_XO[s]-TICK_XI[s])*6
//					, 120+TICK_YI[s]-(TICK_YO[s]-TICK_YI[s])*6,
//					0x6a2f, 5, PAINTER_DRAW_BM_HOLD);
//		Painter_DrawLine(120, 120
//					 , 120-(TICK_XI[s]+4)/8
//					 , 120-(TICK_YI[s]+4)/8,
//				0x6a2f, 5, PAINTER_DRAW_BM_HOLD);
		// GET MY SHADOW!
		if (shadow_on) Painter_Fill_BitMaskShadow(50, 50, 190, 190, 0L, 30, 30, 200, 0x0008, 4, 4, 12);
		for (x=30;x<170;x++) for (y=30;y<170;y++){
			LCD_ResetBitMask(_d_dl_ctx.bm, x, y, 200);
		}
		u32 mm = m % 12;
		// minute hand
		Painter_DrawLine(120, 120
						 , 120+TICK_XI[mm]-(TICK_XO[mm]-TICK_XI[mm])*10
						 , 120+TICK_YI[mm]-(TICK_YO[mm]-TICK_YI[mm])*10,
						 0xa22f, 5, PAINTER_DRAW_BM_HOLD);
		Painter_DrawLine(120, 120
						 , 120-(TICK_XI[mm]+4)/8
						 , 120-(TICK_YI[mm]+4)/8,
						 0xa22f, 5, PAINTER_DRAW_BM_HOLD);
		// Center circle makes it cute~
		Painter_DrawCircle(120, 120, 3, 0xa22f, 5, PAINTER_DRAW_BM_HOLD);
		// GET MY SHADOW!
		if (shadow_on) Painter_Fill_BitMaskShadow(50, 50, 190, 190, 0L, 30, 30, 200, 0x0008, 4, 4, 12);

		// show time in text
		u16 *p = time, *q;
		// need some space
		*p++ = space[0];
		mm = m?m:1;
		while (mm<10000) {
			*p++ = space[0];
			mm *= 10;
		}
		//put minute digits
		p = itoa(m, p);
		*p++ = col[0];
		//put second text directly
		q = (u16 *)(text)[s];
		while (*q) *p++ = *q++;
		*p++ = 0;

		// repaint the background
		LCD_SetWindow(20, 240, 200, 60);
		LCD_PutImage_RGB565(bg, 200*60);
		// GET MY TIME!
		Painter_PutString((const u16*)time, 48, 0xffff, 0xf5b9
						 , 20, 240, 200, 60, shadow_on  | PAINTER_STR_SFLUSH);

		put_fps(1);
		// PUSH A TO DASH!
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)) DUMMY;
		// TICK TOCK~
		s++;
		if (s == 12) {
			m++;
			s = 0;
		}
	}
	return 0;
}