/*****************************************************************************
*
*
*****************************************************************************/
#include "stm32f1xx_hal.h"
#include "uip_kernel_api.h"
#include "chip_uip_interrupt.h"
#include "hardware_init.h"

/* 系统 tick 的宏定义，即，每秒钟 tick 中断的次数 */
#define UIP_KERNEL_TICKS_PER_SEC       1000

unsigned char SystemTickIntHandlingData[10];

/*
*********************************************************************************************************
*
* Description : Initialize System Hardware.
*
*********************************************************************************************************
*/
void  Hardware_Init(void)
{                                      
    //STM32_OSC_Init();
    
    __HAL_RCC_AFIO_CLK_ENABLE();

    /* Configure Flash prefetch */
#if (PREFETCH_ENABLE != 0)
#if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
    defined(STM32F102x6) || defined(STM32F102xB) || \
    defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \
    defined(STM32F105xC) || defined(STM32F107xC)

    /* Prefetch buffer is not available on value line devices */
    __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif
#endif /* PREFETCH_ENABLE */

    /* Set Interrupt Group Priority */
    HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
    
    Chip_UIP_InterruptInit();		 
}

/*
*******************************************************************************************************
*
* Description: Initialize the system tick interrupt.
*
*******************************************************************************************************
*/
void Kernel_System_Tick_Init(void)
{	
    /* 初始化系统定时器 */   
    /* Setup the SysTick timer for 1ms. */
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 0xFF, 0);               


    /* 配置并注册对应系统定时器的中断处理程序 */
    /**************************************************************************
    Chip_UIP_InterruptRegisterHandler(Int_ID_SysTick_IRQHandler,
    		                      Chip_UIP_Kernel_Tick_ISR_Handler,
                                  &SystemTickIntHandlingData[0],
                                  Chip_UIP_Kernel_Tick_ISR_Handler_Callback);
    ***************************************************************************/
    /* 启动系统定时器 *//* Enable SysTick IRQ and SysTick Timer */
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;                    
                                                 
}

void STM32_OSC_Init(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.LSIState = RCC_LSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
}

/**********************************************************************/
/*                     End of File : hardware_init.c                  */
/**********************************************************************/
