//======================================================
// LED
//======================================================
// File Name : hw_config.c
// Function  : Hardware Configuration
//------------------------------------------------------
// Rev.01 2008.03.24 Takanaga Yamazaki
//------------------------------------------------------
// (C) Copyright 2008 Takanaga Yamazaki
//======================================================
// ---- License Information ----------------------------
// Anyone can FREELY use this code fully or partially
// under conditions shown below.
// 1. You should use this code under the GNU GPL.
// 2. You should remain this header text in your codes
//   including Copyright credit and License Information.
// 3. Your codes should inherit this license information.
//======================================================
// ---- Patent Notice ----------------------------------
// I have not cared whether this system (hw + sw) causes 
// infringement on the patent, copyright, trademark,
// or trade secret rights of others. You have all 
// responsibilities for determining if your designs
// and products infringe on the intellectual property
// rights of others, when you use technical information
// included in this system for your business.
//======================================================
// ---- Disclaimers ------------------------------------
// The function and reliability of this system are not 
// guaranteed. They may cause any damages to loss of
// properties, data, money, profits, life, or business.
// By adopting this sytem even partially, you assume
// all responsibility for its use.
//======================================================

#include "stm32f10x_it.h"
#include "hw_config.h"
#include "common.h"

//======================
// Global
//======================
UI16 gCCR4_Val;

//==========================================
// Initialize NVIC (Interrupt)
//==========================================
void Init_NVIC(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    #ifdef  VECT_TAB_RAM
    // Set the Vector Table base location at 0x20000000
    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
    #else  // VECT_TAB_FLASH
    // Set the Vector Table base location at 0x08000000
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    #endif

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    // SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default)
    SysTick_SetReload(9000);

    // Enable SysTick interrupt
    SysTick_ITConfig(ENABLE);

    // Enable the SysTick Counter
    SysTick_CounterCmd(SysTick_Counter_Enable);
}

//=================================
// Initialize System
//=================================
void Set_System(void)
{
    ErrorStatus HSEStartUpStatus;
    GPIO_InitTypeDef GPIO_InitStructure;

    // SYSCLK, HCLK, PCLK2 and PCLK1 configuration
    // RCC system reset(for debug purpose)
    RCC_DeInit();

    // Enable HSE
    RCC_HSEConfig(RCC_HSE_ON);

    // Wait till HSE is ready
    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    if(HSEStartUpStatus == SUCCESS)
    {
        // Enable Prefetch Buffer
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        // Flash 2 wait state
        FLASH_SetLatency(FLASH_Latency_2);
 
        // HCLK = SYSCLK
        RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
        // PCLK2 = HCLK
        RCC_PCLK2Config(RCC_HCLK_Div1); 

        // PCLK1 = HCLK/2
        RCC_PCLK1Config(RCC_HCLK_Div2);

        // ADCCLK = PCLK2/6
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);

        // PLLCLK = 8MHz * 9 = 72 MHz
        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)
        {
        }
      }
      // Enable GPIOC clock
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
      // Configure PC.06 as output push-pull
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOC, &GPIO_InitStructure);
}

//======================================================
// End of Program
//======================================================
