/**
 *******************************************************************************
 * @file    systick.c
 *
 * @brief   System initialization file.
 *
 * @author  LPX
 *
 * @version V1.0.0
 *
 * @date    2023-11-13
 *
 *******************************************************************************/
#include "systick.h"


/* Private Variables */
static __IO uint32_t uwTick;


/**
 *******************************************************************************
 * @brief   Get system tick value.
 * @param   None.
 * @retval  tick value.
 ******************************************************************************/
uint32_t GetTick(void)
{
  return uwTick;
}

/**
 *******************************************************************************
 * @brief   System tick interrupt service function.
 * @param   None.
 * @retval  tick value.
 ******************************************************************************/
void IncTick(void)
{
   uwTick++;
}

/**
 *******************************************************************************
 * @brief   Minimum delay (in milliseconds) based on variable incremented.
 * @param   None.
 * @retval  tick value.
 ******************************************************************************/
void DelayMs(uint32_t Delay)
{
  uint32_t tickStart = GetTick();
  while (GetTick() - tickStart < Delay)
    ;
}

/**
 *******************************************************************************
 * @brief   System Clock initialization.
 * @param   None.
 * @retval  None.
 ******************************************************************************/
void SystemClockInit(void)
{
  RCC_ClkInitTypeStruct RCC_ClkInitStruct;
  
  RCC_Unlock();
  
#if defined (DPM32M08x) || defined (DPM32M05x)
  RCC_OscInitTypeStruct RCC_OscInitStruct;
  RCC_OscStructInit(&RCC_OscInitStruct);
  RCC_OscInitStruct.RCC_PLL.PLLSource = RCC_PLL_SOURCE_HSI_DIV8;
  RCC_OscInitStruct.RCC_PLL.PLLMUL = RCC_PLL_MUL16;
  RCC_OscInitStruct.RCC_PLL.PLLState = RCC_PLL_ON;
  RCC_OscConfig(&RCC_OscInitStruct);
#endif
  
  RCC_ClockStructInit(&RCC_ClkInitStruct);
  RCC_ClkInitStruct.RCC_ClockType = (uint32_t)(RCC_CLOCK_TYPE_SYSCLK | 
                                                RCC_CLOCK_TYPE_HCLK | RCC_CLOCK_TYPE_PCLK);
  
#if defined (DPM32M08x) || defined (DPM32M05x)
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLK_SOURCE_PLL;
#else
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLK_SOURCE_HSI;
#endif

  RCC_ClkInitStruct.HCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.PCLKDivider = RCC_HCLK_DIV1;
  RCC_ClockConfig(&RCC_ClkInitStruct);

  RCC_Lock();
  
  FLASH_Unlock();
  FLASH_PrefetchBufferCmd(ENABLE);
  FLASH_Lock();
}


/**
 *******************************************************************************
 * @brief   System tick initialization.
 * @param   None.
 * @retval  None.
 ******************************************************************************/
void SystickInit(void)
{
  /* setup systick timer for 1000Hz interrupts */
  if (SysTick_Config(RCC_GetSYSCLKFrequency() / 1000U))
  {
    /* capture error */
    while (1)
    {
    }
  }
  /* configure the systick handler priority */
  NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
