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

/*
    Copyright (c) 2024, DP Microelectronics Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#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_OscInitTypeStruct RCC_OscInitStruct;
  RCC_Unlock();

#if defined (DPM32M08x) || defined (DPM32M05x)
  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);
}
