/**
  ******************************************************************************
  * @file     SysTick_Delay.c
  * @author   SINOMCU-AE
  * @brief    SysTick delay ms,soft timer 10ms,100ms,user ms
  *    
  *          This file provides functions of delay_ms:
  *              SysTick_Ms(volatile uint32_t Cnt); 
  *              Start_10MS_SF_Timer();
  *              Stop_10MS_SF_Timer();
  *              SoftTimerStatus Get_State_10MS_SF_Timer();
  *              Start_100MS_SF_Timer();
  *              Stop_100MS_SF_Timer();
  *              SoftTimerStatus Get_State_100MS_SF_Timer();
  *              Start_User_MS_SF_Timer();
  *              Stop_User_MS_SF_Timer(void);
  *              SoftTimerStatus Get_State_User_MS_SF_Timer();
  * Needed call SysTickDelay_Callback function in ms32f0xx_it.c file by 
  * SysTick_Handler() function.
  * 
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "SysTick_Delay.h"

#define  DELAY_10MS_NUM      10                       // unit ms
#define  DELAY_100MS_NUM     100                      // unit ms
__IO uint16_t delay_user_ms  =500;                    // defalut user delay 500 ms

SoftTimerStatus DELAY_10MS_FLAG = SF_TIMER_STOP;      // defalut soft timer stop
SoftTimerStatus DELAY_100MS_FLAG = SF_TIMER_STOP;
SoftTimerStatus DELAY_USER_MS_FLAG = SF_TIMER_STOP;

static __IO uint8_t delay_10ms_count=0;
static __IO uint8_t delay_100ms_count=0;
static __IO uint16_t delay_user_ms_count=0;
static __IO uint32_t TimeDelayCnt;

/**
  * @brief  SysTick Initialization Function 1ms interrupt
  * @param  None
  * @retval None
  */
void SysTick_Init(void)
{
  // SystemFrequency / 1000, 1ms
  SysTick_Config( SystemCoreClock / 1000);  
  NVIC_SetPriority(SysTick_IRQn, 0x3);
}

/**
  * @brief  Decrement TimeDelayCnt,soft timer count
  * @param  None
  * @retval None
  * @note call by SysTick_Handler()
  */
void SysTickDelay_Callback(void)
{
  if(TimeDelayCnt != 0x00)
  { 
    TimeDelayCnt--;
  }

  if(DELAY_10MS_FLAG != SF_TIMER_STOP)
  {
    delay_10ms_count++;
    if(delay_10ms_count == DELAY_10MS_NUM )      // soft timer OV
    {
      delay_10ms_count =0;
      DELAY_10MS_FLAG =SF_TIMER_OV;
    }
  }

  if(DELAY_100MS_FLAG != SF_TIMER_STOP)
  {
    delay_100ms_count++;
    if(delay_100ms_count == DELAY_100MS_NUM )    // soft timer OV
    {
      delay_100ms_count =0;
      DELAY_100MS_FLAG =SF_TIMER_OV;
    }
  }

  if(DELAY_USER_MS_FLAG != SF_TIMER_STOP)
  {
    delay_user_ms_count++;
    if(delay_user_ms_count == delay_user_ms)     // soft timer OV
    {
      delay_user_ms_count =0;
      DELAY_USER_MS_FLAG =SF_TIMER_OV;
    }
  }
}

/**
  * @brief  Start soft timer 10ms,clean ov
  * @param  None
  * @retval None
  */
void Start_10MS_SF_Timer(void) {
  DELAY_10MS_FLAG = SF_TIMER_RUN;
}

/**
  * @brief  Stop soft timer 10ms
  * @param  None
  * @retval None
  */
void Stop_10MS_SF_Timer(void) {
  DELAY_10MS_FLAG = SF_TIMER_STOP;
}

/**
  * @brief  Get state timer 10ms
  * @param  None
  * @retval Status of timer 10ms
  */
SoftTimerStatus Get_State_10MS_SF_Timer(void) {
  return DELAY_10MS_FLAG;
}

/**
  * @brief  Start soft timer 100ms,clean ov
  * @param  None
  * @retval None
  */
void Start_100MS_SF_Timer(void) {
  DELAY_100MS_FLAG = SF_TIMER_RUN;
}

/**
  * @brief  Stop soft timer 100ms
  * @param  None
  * @retval None
  */
void Stop_100MS_SF_Timer(void) {
  DELAY_100MS_FLAG = SF_TIMER_STOP;
}

/**
  * @brief  Get state timer 100ms
  * @param  None
  * @retval Status of timer 100ms
  */
SoftTimerStatus Get_State_100MS_SF_Timer(void) {
  return DELAY_100MS_FLAG;
}

/**
  * @brief  Start soft timer user need ms,clean ov
  * @param  delay_ms
  * @retval None
  */
void Start_User_MS_SF_Timer(uint16_t delay_ms) {
  delay_user_ms =delay_ms;
  DELAY_USER_MS_FLAG = SF_TIMER_RUN;
}

/**
  * @brief  Stop soft timer user ms
  * @param  None
  * @retval None
  */
void Stop_User_MS_SF_Timer(void) {
  DELAY_USER_MS_FLAG = SF_TIMER_STOP;
}

/**
  * @brief  Get state user ms
  * @param  None
  * @retval Status of user ms
  */
SoftTimerStatus Get_State_User_MS_SF_Timer(void) {
  return DELAY_USER_MS_FLAG;
}

/**
  * @brief  delay ms with blocking mode
  * @param  How many ms delay
  *         This parameter can be uint32
  * @retval None
  */
void SysTick_Ms(volatile uint32_t Cnt) {
  TimeDelayCnt = Cnt;
  while(TimeDelayCnt != 0);
}

/******************************** END OF FILE *********************************/
